Little extensions to easily manage file versions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Little extensions to easily manage file versions

Stephane Ducasse-3
Hi

I really like (when I save little data files) to have a simple versioning.
For example for my game collection I have
Games.1.ston
Games.2.ston
...
and I like that this is managed for me by the system.

I harvested this functionality from squeak long time ago.
I rewrote it fast and dirty in Pharo. I find this two methods super
handy and they provide cheap versioning for little files.



lastNameFor: baseFileName extension: extension
   "Assumes a file name includes a version number encoded as '.'
followed by digits
   preceding the file extension, e.g., games.22.ston
   Answer the file name with the largest number.
   If a version number is not found, raises an error"

   "FileSystem workingDirectory lastNameFor: 'games' extension: 'ston'"

    | files |
    files := self childrenMatching: baseFileName , '.*.' , extension.
    files ifEmpty: [ ^ self error: 'No file with number pattern' ].
    ^ (files asSortedCollection: [ :a :b | a basename < b basename ]) last

nextNameFor: baseFileName extension: extension
   "Assumes a file name includes a version number encoded as '.'
followed by digits
   preceding the file extension, e.g., games.22.ston
   Increment the version number (of the largest one) and answer the
new file name, e.g., games23.ston
   If a version number is not found, set the version to 1 and answer a
new file name"

    "FileSystem workingDirectory nextNameFor: 'games' extension: 'ston'"

    | files splits version |
    files := self childrenMatching: baseFileName , '.*.' , extension.
    files ifEmpty: [ ^ baseFileName , '.1.' , extension ].
    splits := files
         collect: [ :filename | filename basename splitOn: $. ]
         thenSelect: [ :split | (split at: 1) = baseFileName and: [
split size = 3 ] ].
     splits := splits asSortedCollection: [ :a :b | (a at: 2) < (b at: 2) ].
     version := splits isEmpty
        ifTrue: [ 1 ]
        ifFalse: [ (splits last at: 2) asNumber + 1 ].
     ^ baseFileName , '.' , version asString , '.' , extension

I think that these to methods can be a valuable addition to
fileReference (the methods can be better implemented).

Let me know what you think.

https://pharo.fogbugz.com/f/cases/20324/Handy-file-automatic-numbering

Stef

Reply | Threaded
Open this post in threaded view
|

Re: Little extensions to easily manage file versions

Denis Kudriashov
Do you know that there is #nextVersion method in FileReference?

2017-08-18 22:04 GMT+02:00 Stephane Ducasse <[hidden email]>:
Hi

I really like (when I save little data files) to have a simple versioning.
For example for my game collection I have
Games.1.ston
Games.2.ston
...
and I like that this is managed for me by the system.

I harvested this functionality from squeak long time ago.
I rewrote it fast and dirty in Pharo. I find this two methods super
handy and they provide cheap versioning for little files.



lastNameFor: baseFileName extension: extension
   "Assumes a file name includes a version number encoded as '.'
followed by digits
   preceding the file extension, e.g., games.22.ston
   Answer the file name with the largest number.
   If a version number is not found, raises an error"

   "FileSystem workingDirectory lastNameFor: 'games' extension: 'ston'"

    | files |
    files := self childrenMatching: baseFileName , '.*.' , extension.
    files ifEmpty: [ ^ self error: 'No file with number pattern' ].
    ^ (files asSortedCollection: [ :a :b | a basename < b basename ]) last

nextNameFor: baseFileName extension: extension
   "Assumes a file name includes a version number encoded as '.'
followed by digits
   preceding the file extension, e.g., games.22.ston
   Increment the version number (of the largest one) and answer the
new file name, e.g., games23.ston
   If a version number is not found, set the version to 1 and answer a
new file name"

    "FileSystem workingDirectory nextNameFor: 'games' extension: 'ston'"

    | files splits version |
    files := self childrenMatching: baseFileName , '.*.' , extension.
    files ifEmpty: [ ^ baseFileName , '.1.' , extension ].
    splits := files
         collect: [ :filename | filename basename splitOn: $. ]
         thenSelect: [ :split | (split at: 1) = baseFileName and: [
split size = 3 ] ].
     splits := splits asSortedCollection: [ :a :b | (a at: 2) < (b at: 2) ].
     version := splits isEmpty
        ifTrue: [ 1 ]
        ifFalse: [ (splits last at: 2) asNumber + 1 ].
     ^ baseFileName , '.' , version asString , '.' , extension

I think that these to methods can be a valuable addition to
fileReference (the methods can be better implemented).

Let me know what you think.

https://pharo.fogbugz.com/f/cases/20324/Handy-file-automatic-numbering

Stef


Reply | Threaded
Open this post in threaded view
|

Re: Little extensions to easily manage file versions

Stephane Ducasse-3
No I will have a look :)
But I also need latestVersion because you need the two: to always load
the latest and save latest + 1
Stef

On Fri, Aug 18, 2017 at 11:38 PM, Denis Kudriashov <[hidden email]> wrote:

> Do you know that there is #nextVersion method in FileReference?
>
> 2017-08-18 22:04 GMT+02:00 Stephane Ducasse <[hidden email]>:
>>
>> Hi
>>
>> I really like (when I save little data files) to have a simple versioning.
>> For example for my game collection I have
>> Games.1.ston
>> Games.2.ston
>> ...
>> and I like that this is managed for me by the system.
>>
>> I harvested this functionality from squeak long time ago.
>> I rewrote it fast and dirty in Pharo. I find this two methods super
>> handy and they provide cheap versioning for little files.
>>
>>
>>
>> lastNameFor: baseFileName extension: extension
>>    "Assumes a file name includes a version number encoded as '.'
>> followed by digits
>>    preceding the file extension, e.g., games.22.ston
>>    Answer the file name with the largest number.
>>    If a version number is not found, raises an error"
>>
>>    "FileSystem workingDirectory lastNameFor: 'games' extension: 'ston'"
>>
>>     | files |
>>     files := self childrenMatching: baseFileName , '.*.' , extension.
>>     files ifEmpty: [ ^ self error: 'No file with number pattern' ].
>>     ^ (files asSortedCollection: [ :a :b | a basename < b basename ]) last
>>
>> nextNameFor: baseFileName extension: extension
>>    "Assumes a file name includes a version number encoded as '.'
>> followed by digits
>>    preceding the file extension, e.g., games.22.ston
>>    Increment the version number (of the largest one) and answer the
>> new file name, e.g., games23.ston
>>    If a version number is not found, set the version to 1 and answer a
>> new file name"
>>
>>     "FileSystem workingDirectory nextNameFor: 'games' extension: 'ston'"
>>
>>     | files splits version |
>>     files := self childrenMatching: baseFileName , '.*.' , extension.
>>     files ifEmpty: [ ^ baseFileName , '.1.' , extension ].
>>     splits := files
>>          collect: [ :filename | filename basename splitOn: $. ]
>>          thenSelect: [ :split | (split at: 1) = baseFileName and: [
>> split size = 3 ] ].
>>      splits := splits asSortedCollection: [ :a :b | (a at: 2) < (b at: 2)
>> ].
>>      version := splits isEmpty
>>         ifTrue: [ 1 ]
>>         ifFalse: [ (splits last at: 2) asNumber + 1 ].
>>      ^ baseFileName , '.' , version asString , '.' , extension
>>
>> I think that these to methods can be a valuable addition to
>> fileReference (the methods can be better implemented).
>>
>> Let me know what you think.
>>
>> https://pharo.fogbugz.com/f/cases/20324/Handy-file-automatic-numbering
>>
>> Stef
>>
>