Fwd: [Pharo-dev] Add download+uncompress behavior to Metacello?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Fwd: [Pharo-dev] Add download+uncompress behavior to Metacello?

hernanmd

---------- Forwarded message ----------
From: Hernán Morales Durand <[hidden email]>
Date: 2016-04-21 1:05 GMT-03:00
Subject: Re: [Pharo-dev] Add download+uncompress behavior to Metacello?
To: Pharo Development List <[hidden email]>


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





--
You received this message because you are subscribed to the Google Groups "Metacello" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...