From 2b1151abef4a616049efdda2fb7bf1b551fb9d48 Mon Sep 17 00:00:00 2001 From: Jian Weihang Date: Fri, 19 Apr 2019 18:14:30 +0900 Subject: [PATCH] feat: add `without_parent_dir` to `PackageTask` --- lib/rake/packagetask.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) 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