In rake, how to create a package containing a file but renaming it inside the package

I would like to create a package containing the file, but renaming 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 it to be aa.rb

named bb.rb

inside the package.

How can I do this elegantly?

+3


source to share


1 answer


By looking at the PackageTask source you can define a new task (say rename_files

) that depends on the p.package_dir_path

task defined by Rake :: PackageTask. In the task, rename_files

you can rename the link to the file (s) that is package_dir_path

set in package_dir

. Then you add a new task rename_files

as a dependency for every target "#{package_dir}/#{[tar|zip|etc]_file}"

you care about.

With these dependencies, the order of operations should be as follows:

  • configure package_dir

    with links to source files frompackage_files

  • rename links with your nested dependency
  • execute the command to create an archive on package_dir

If it's not clear enough to get you there, I'll try to post some actual code later.

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

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

      



And in the Rakefile, I define the 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. By running it, you will see that the hardlink created in the dir directory is renamed from "aa.rb" to "bb.rb", then we deactivate 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 tar appears 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

      

+4


source







All Articles