Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PackageTask be able to omit parent directory while packing files #310

Merged
merged 1 commit into from
Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/rake/packagetask.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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