How do I write an Albacore zip task that includes only certain folders and the folders themselves?

StackOverflow https://stackoverflow.com/questions/3151887

  •  01-10-2019
  •  | 
  •  

Question

I'm trying to zip up the artifacts of a rake build, using Albacore's ZipTask. The solution I'm building has three projects that have artifacts that need to be zipped up individually, but only the ASP.NET MVC project will be mentioned here. Here's the directory structure of the solution:

rakefile.rb
solution.sln
src/
    (other projects that are not relevant)
    website/
        (various folders I don't want included in the artifacts)
        bin/
        Content/
        Scripts/
        Views/
        Default.aspx
        Global.asax
        web.config

At first I wrote this task:

website_directory = File.join '.', 'src', 'website'
website_project_name = 'website'

zip :zip => [ :run_unit_tests, :less ] do |zip|
  zip.directories_to_zip = [ 'bin', 'Content', 'Scripts', 'Views' ].map{ |folder| File.join website_directory, folder }
  zip.additional_files = [ 'Default.aspx', 'favicon.ico', 'Global.asax', 'web.config'].map{ |file| File.join website_directory, file }
  zip.output_file = get_output_file_name
  zip.output_path = get_artifacts_output_path website_project_name
end

Problem is, the output of this task is a zip file containing the contents of those folders, not the folders themselves, which is obviously undesirable.

Next, I tried flipping the flatten_zip field to false (which is not a documented field but you can find it in the source). This produced a zip that contained the above folders, but at the bottom of the whole ./src/website/ folder hierarchy. I want the above folders at the root of the zip, so that's not working either.

So my next shot was this, using exclusions, which is also not documented:

zip :zip => [ :run_unit_tests, :less ] do |zip|
  zip.directories_to_zip website_directory
  zip.exclusions = [ /.git/, /.+\.cs/, /App_Data/, /Attributes/, /AutoMapper/, /Controllers/, /Diagrams/, /Extensions/, /Filters/, /Helpers/, /Models/, /obj/, /Properties/, /StructureMap/, /Templates/, /CompTracker.Web.csproj/, /Default.aspx.cs/, /Global.asax.cs/, /Publish.xml/, /pdb/ ]
  zip.output_file = get_output_file_name
  zip.output_path = get_artifacts_output_path website_project_name
end

This worked for me, but when I recently added /AutoMapper/ and /StructureMap/ to the exclusions array, it also caused AutoMapper.dll and StructureMap.dll to (of course) also be excluded from the bin folder.

How should I edit any of the above tasks to have only the folders and files I want at the root of my zip?

Was it helpful?

Solution

You can copy all the files and folders you need into a temp directory and then zip that up. In other words, set the property directories_to_zip to one folder which has all the correct files already copied in - where the copying code is what does all the filtering for you. I think that's the unspecified expected usage (and the way I use it). I'll just contrast the two ideas to make it clear.

Strategy A: Copy the folder structure

Copy what you need into a temp directory. This way, you set the 'directories_to_zip' property to one folder eg:

directories_to_zip = 'ziptemp/all.my.stuff.copied.here/'

Strategy B: Use a filter

(This is your current strategy)

Use the directories_to_zip property as a filter on an original directory (it is an array that takes multiple directories, so you couldn't be blamed for assuming it should work this way). You then use various properties to filter the files/folders you need.

Possible Resolution

To solve your issue, then, you could adopt Strategy A. Actually, unless there's any extraordinary issues with this (slow PC, amazingly low on disk space), I would have thought this would be a slightly more robust way of going about it because you can be sure that what you are zipping is exactly what you are copying, which can be verified by checking the ziptemp directory.

Incidentally, I can be sure Strategy A works because I use it (with Albacore) to zip files for our installers.

OTHER TIPS

I had the same problem using 'albacore', I wanted to zip a folder called 'tools' (not just the contents), store it in a folder called 'out' and name the zip 'tools.zip ... so instead I used this;

require 'zip/zipfilesystem'

Zip::ZipFile::open("out/tools.zip", Zip::ZipFile::CREATE) { |z|
    Dir['{tools}/**/*'].each { |f|
        z.add(f, f)
    }
}

I know its late but I hope this helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top