Add download+uncompress behavior to Metacello?

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

Add download+uncompress behavior to Metacello?

hernanmd

Is there a chance to get this methods (or any better versions of them) added to Metacello?

>>extractZip: fileRef in: dst
    " If fileRef is a ZIP archive, uncompress its contents in dst and delete it "

    | zipArchive |
    (fileRef fullName endsWith: '.zip')
        ifFalse: [ ^ self ].
    zipArchive := ZipArchive new.
    [ zipArchive
        readFrom: fileRef fullName;
        extractAllTo: dst ]
        ensure: [ zipArchive close ].
    fileRef delete


>>extractGZip: fileRef in: dst
    " If fileRef is a ZIP archive, uncompress its contents in dst and delete it "

    | unzipped |
    (fileRef fullName endsWith: '.gz')
        ifFalse: [ ^ self ].
    unzipped := GZipReadStream unzip: fileRef basename to: dst.
    (Smalltalk hasClassNamed: #OSProcess)
        ifTrue: [
            (Smalltalk at: #OSProcess) thisOSProcess waitForCommandOutput: 'tar xvf ' , unzipped asFileReference fullName.
            fileRef delete.
            unzipped asFileReference delete ]

>>uncompress: fileRef
    " Private - Uncompress fileRef to the current working directory "

    | dst |
    dst := FileSystem workingDirectory.
    self extractZip: fileRef in: dst.
    self extractGZip: fileRef in: dst.

>>downloadResourcesFrom: urlString
    " Private - Download files from urlString "

    ^ [ ZnEasy get: urlString ]
        on: ZnEntityTooLarge
        do: [ : ex | ex resume  ].


I use them to download external resources for my projects as follows:

First I add a #preLoadDoIt: send in my baseline:

    spec for: #'common' do: [
        spec blessing: #'baseline'.
        spec preLoadDoIt: #preLoad.
         ....

>>preLoad

    | url fileRef response |

    self logCr: '====== Downloading my resource files...'.
    url := self platformFilesUrl asZnUrl.
    fileRef := FileSystem disk workingDirectory / url pathSegments last.
    (response := self downloadResourcesFrom: url) isSuccess
        ifTrue: [
            self logCr: '====== Writing resource file...'.
            fileRef writeStreamDo: [ : stream | stream nextPutAll: response contents ] ]
        ifFalse: [ self error: 'Cannot download data files' ].
    self logCr: '====== Uncompressing files...'.
    self uncompress: fileRef.

Hernán

Reply | Threaded
Open this post in threaded view
|

Re: Add download+uncompress behavior to Metacello?

stepharo
packaged them first :)

Stef

Le 19/4/16 15:14, Hernán Morales Durand a écrit :

>
> Is there a chance to get this methods (or any better versions of them)
> added to Metacello?
>
> >>extractZip: fileRef in: dst
>     " If fileRef is a ZIP archive, uncompress its contents in dst and
> delete it "
>
>     | zipArchive |
>     (fileRef fullName endsWith: '.zip')
>         ifFalse: [ ^ self ].
>     zipArchive := ZipArchive new.
>     [ zipArchive
>         readFrom: fileRef fullName;
>         extractAllTo: dst ]
>         ensure: [ zipArchive close ].
>     fileRef delete
>
>
> >>extractGZip: fileRef in: dst
>     " If fileRef is a ZIP archive, uncompress its contents in dst and
> delete it "
>
>     | unzipped |
>     (fileRef fullName endsWith: '.gz')
>         ifFalse: [ ^ self ].
>     unzipped := GZipReadStream unzip: fileRef basename to: dst.
>     (Smalltalk hasClassNamed: #OSProcess)
>         ifTrue: [
>             (Smalltalk at: #OSProcess) thisOSProcess
> waitForCommandOutput: 'tar xvf ' , unzipped asFileReference fullName.
>             fileRef delete.
>             unzipped asFileReference delete ]
>
> >>uncompress: fileRef
>     " Private - Uncompress fileRef to the current working directory "
>
>     | dst |
>     dst := FileSystem workingDirectory.
>     self extractZip: fileRef in: dst.
>     self extractGZip: fileRef in: dst.
>
> >>downloadResourcesFrom: urlString
>     " Private - Download files from urlString "
>
>     ^ [ ZnEasy get: urlString ]
>         on: ZnEntityTooLarge
>         do: [ : ex | ex resume  ].
>
>
> I use them to download external resources for my projects as follows:
>
> First I add a #preLoadDoIt: send in my baseline:
>
>     spec for: #'common' do: [
>         spec blessing: #'baseline'.
>         spec preLoadDoIt: #preLoad.
>          ....
>
> >>preLoad
>
>     | url fileRef response |
>
>     self logCr: '====== Downloading my resource files...'.
>     url := self platformFilesUrl asZnUrl.
>     fileRef := FileSystem disk workingDirectory / url pathSegments last.
>     (response := self downloadResourcesFrom: url) isSuccess
>         ifTrue: [
>             self logCr: '====== Writing resource file...'.
>             fileRef writeStreamDo: [ : stream | stream nextPutAll:
> response contents ] ]
>         ifFalse: [ self error: 'Cannot download data files' ].
>     self logCr: '====== Uncompressing files...'.
>     self uncompress: fileRef.
>
> Hernán
>


Reply | Threaded
Open this post in threaded view
|

Re: Add download+uncompress behavior to Metacello?

hernanmd
Hi,

I checked Metacello configurations but I don't know where to add such methods.
One option is to add them to Object but I don't like to add methods there.
I guess Metacello uses "template methods" somewhere but this download+uncompress feature is not something used by all projects.

Where do you recommend to add them?

Hernán




2016-04-19 10:47 GMT-03:00 stepharo <[hidden email]>:
packaged them first :)

Stef

Le 19/4/16 15:14, Hernán Morales Durand a écrit :


Is there a chance to get this methods (or any better versions of them) added to Metacello?

>>extractZip: fileRef in: dst
    " If fileRef is a ZIP archive, uncompress its contents in dst and delete it "

    | zipArchive |
    (fileRef fullName endsWith: '.zip')
        ifFalse: [ ^ self ].
    zipArchive := ZipArchive new.
    [ zipArchive
        readFrom: fileRef fullName;
        extractAllTo: dst ]
        ensure: [ zipArchive close ].
    fileRef delete


>>extractGZip: fileRef in: dst
    " If fileRef is a ZIP archive, uncompress its contents in dst and delete it "

    | unzipped |
    (fileRef fullName endsWith: '.gz')
        ifFalse: [ ^ self ].
    unzipped := GZipReadStream unzip: fileRef basename to: dst.
    (Smalltalk hasClassNamed: #OSProcess)
        ifTrue: [
            (Smalltalk at: #OSProcess) thisOSProcess waitForCommandOutput: 'tar xvf ' , unzipped asFileReference fullName.
            fileRef delete.
            unzipped asFileReference delete ]

>>uncompress: fileRef
    " Private - Uncompress fileRef to the current working directory "

    | dst |
    dst := FileSystem workingDirectory.
    self extractZip: fileRef in: dst.
    self extractGZip: fileRef in: dst.

>>downloadResourcesFrom: urlString
    " Private - Download files from urlString "

    ^ [ ZnEasy get: urlString ]
        on: ZnEntityTooLarge
        do: [ : ex | ex resume  ].


I use them to download external resources for my projects as follows:

First I add a #preLoadDoIt: send in my baseline:

    spec for: #'common' do: [
        spec blessing: #'baseline'.
        spec preLoadDoIt: #preLoad.
         ....

>>preLoad

    | url fileRef response |

    self logCr: '====== Downloading my resource files...'.
    url := self platformFilesUrl asZnUrl.
    fileRef := FileSystem disk workingDirectory / url pathSegments last.
    (response := self downloadResourcesFrom: url) isSuccess
        ifTrue: [
            self logCr: '====== Writing resource file...'.
            fileRef writeStreamDo: [ : stream | stream nextPutAll: response contents ] ]
        ifFalse: [ self error: 'Cannot download data files' ].
    self logCr: '====== Uncompressing files...'.
    self uncompress: fileRef.

Hernán