diff --git a/lib/rake/packagetask.rb b/lib/rake/packagetask.rb index 72fef4d5e..aeff81c29 100644 --- a/lib/rake/packagetask.rb +++ b/lib/rake/packagetask.rb @@ -79,6 +79,9 @@ class PackageTask < TaskLib # Zip command for zipped archives. The default is 'zip'. attr_accessor :zip_command + # True if parent directory should be omited (default is false) + attr_accessor :without_parent_dir + # Create a Package Task with the given name and version. Use +:noversion+ # as the version to build a package without a version or to provide a # fully-versioned package name. @@ -102,6 +105,7 @@ def init(name, version) @need_zip = false @tar_command = "tar" @zip_command = "zip" + @without_parent_dir = false end # Create the tasks defined by this task library. @@ -132,7 +136,8 @@ def define task package: ["#{package_dir}/#{file}"] file "#{package_dir}/#{file}" => [package_dir_path] + package_files do - chdir(package_dir) { sh @tar_command, "#{flag}cvf", file, package_name } + chdir(working_dir) { sh @tar_command, "#{flag}cvf", file, target_dir } + mv "#{package_dir_path}/#{target_dir}", package_dir if without_parent_dir end end end @@ -141,7 +146,8 @@ def define task package: ["#{package_dir}/#{zip_file}"] file "#{package_dir}/#{zip_file}" => [package_dir_path] + package_files do - chdir(package_dir) { sh @zip_command, "-r", zip_file, package_name } + chdir(working_dir) { sh @zip_command, "-r", zip_file, target_dir } + mv "#{package_dir_path}/#{zip_file}", package_dir if without_parent_dir end end @@ -202,6 +208,15 @@ def tar_xz_file def zip_file "#{package_name}.zip" end + + def working_dir + without_parent_dir ? package_dir_path : package_dir + end + + # target directory relative to working_dir + def target_dir + without_parent_dir ? "." : package_name + end end end diff --git a/test/test_rake_package_task.rb b/test/test_rake_package_task.rb index d3886f8ca..25a1baa95 100644 --- a/test/test_rake_package_task.rb +++ b/test/test_rake_package_task.rb @@ -78,4 +78,16 @@ def test_package_name_noversion assert_equal "a", pkg.package_name end + def test_without_parent_dir + pkg = Rake::PackageTask.new("foo", :noversion) + + assert_equal "pkg", pkg.working_dir + assert_equal "foo", pkg.target_dir + + pkg.without_parent_dir = true + + assert_equal "pkg/foo", pkg.working_dir + assert_equal ".", pkg.target_dir + end + end