3

I would like to create a package that contains a file but renames it inside the package.

For example:

  Rake::PackageTask.new("rake", "1.2.3") do |p|
    p.package_files.include("aa.rb")
  end

I would like aa.rb to be named bb.rb inside the package.

How can I do this elegantly?

viebel
  • 19,372
  • 10
  • 49
  • 83

1 Answers1

4

Looking at the PackageTask source, it seems you could define a new task (say rename_files) that depends on on the p.package_dir_path task defined by Rake::PackageTask. In rename_files task you can rename the file link(s) which package_dir_path task made in package_dir. Then you add your new rename_files task as a dependency for each of the "#{package_dir}/#{[tar|zip|etc]_file}" task targets you care about.

With these dependencies, the order of operations should become:

  1. set up package_dir with links to source files from package_files
  2. rename links with your injected dependency
  3. execute archive creation command on package_dir

If this isn't clear enough to get you there, I'll try and post some actual code later.

[LATER] Ok, some code. I made a sample project which looks like this:

$ find .
.
./lib
./lib/aa.rb
./lib/foo.rb
./Rakefile

And in the Rakefile, I define a package task as:

require 'rake/packagetask'

Rake::PackageTask.new('test', '1.2.3') do |p|

  p.need_tar = true
  p.package_files.include('lib/**/*')

  task :rename_files => [ p.package_dir_path ] do
    fn     = File.join( p.package_dir_path, 'lib', 'aa.rb' )
    fn_new = File.join( p.package_dir_path, 'lib', 'bb.rb' )
    File.rename( fn, fn_new )
  end

  [
    [p.need_tar, p.tgz_file, "z"],
    [p.need_tar_gz, p.tar_gz_file, "z"],
    [p.need_tar_bz2, p.tar_bz2_file, "j"],
    [p.need_zip, p.zip_file, ""]
  ].each do |(need, file, flag)|
    task "#{p.package_dir}/#{file}" => [ :rename_files ]
  end

end

The logic here is what I explained above. Running it, you can see that the hard link made in the package dir is renamed from "aa.rb" to "bb.rb", then we tar the directory and viola!

$ rake package
(in /Users/dbenhur/p)
mkdir -p pkg
mkdir -p pkg/test-1.2.3/lib
rm -f pkg/test-1.2.3/lib/aa.rb
ln lib/aa.rb pkg/test-1.2.3/lib/aa.rb
rm -f pkg/test-1.2.3/lib/foo.rb
ln lib/foo.rb pkg/test-1.2.3/lib/foo.rb
cd pkg
tar zcvf test-1.2.3.tgz test-1.2.3
a test-1.2.3
a test-1.2.3/lib
a test-1.2.3/lib/bb.rb
a test-1.2.3/lib/foo.rb
cd -

Here's the tar manifest with "bb.rb" instead of "aa.rb":

$ tar tf pkg/test-1.2.3.tgz 
test-1.2.3/
test-1.2.3/lib/
test-1.2.3/lib/bb.rb
test-1.2.3/lib/foo.rb
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
dbenhur
  • 20,008
  • 4
  • 48
  • 45
  • This solution doesn't work if you don't specify a version: `Rake::PackageTask.new('test', :noversion)`. This is because the block is executed before the `define` task, so `@version` in the `RakeTask` is the symbol instead of being set to `nil`. Still trying to figure out a way around this... – John Engelman May 22 '14 at 15:15