Skip to content

Commit

Permalink
Merge pull request #310 from tonytonyjan/without_parent_dir
Browse files Browse the repository at this point in the history
Make `PackageTask` be able to omit parent directory while packing files
  • Loading branch information
hsbt committed Aug 17, 2019
2 parents 3e0c46b + 382e804 commit 7eff2ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/rake/packagetask.rb
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions test/test_rake_package_task.rb
Expand Up @@ -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

0 comments on commit 7eff2ab

Please sign in to comment.