Skip to content

Commit 7eff2ab

Browse files
authored
Merge pull request #310 from tonytonyjan/without_parent_dir
Make `PackageTask` be able to omit parent directory while packing files
2 parents 3e0c46b + 382e804 commit 7eff2ab

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/rake/packagetask.rb

+17-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ class PackageTask < TaskLib
7979
# Zip command for zipped archives. The default is 'zip'.
8080
attr_accessor :zip_command
8181

82+
# True if parent directory should be omited (default is false)
83+
attr_accessor :without_parent_dir
84+
8285
# Create a Package Task with the given name and version. Use +:noversion+
8386
# as the version to build a package without a version or to provide a
8487
# fully-versioned package name.
@@ -102,6 +105,7 @@ def init(name, version)
102105
@need_zip = false
103106
@tar_command = "tar"
104107
@zip_command = "zip"
108+
@without_parent_dir = false
105109
end
106110

107111
# Create the tasks defined by this task library.
@@ -132,7 +136,8 @@ def define
132136
task package: ["#{package_dir}/#{file}"]
133137
file "#{package_dir}/#{file}" =>
134138
[package_dir_path] + package_files do
135-
chdir(package_dir) { sh @tar_command, "#{flag}cvf", file, package_name }
139+
chdir(working_dir) { sh @tar_command, "#{flag}cvf", file, target_dir }
140+
mv "#{package_dir_path}/#{target_dir}", package_dir if without_parent_dir
136141
end
137142
end
138143
end
@@ -141,7 +146,8 @@ def define
141146
task package: ["#{package_dir}/#{zip_file}"]
142147
file "#{package_dir}/#{zip_file}" =>
143148
[package_dir_path] + package_files do
144-
chdir(package_dir) { sh @zip_command, "-r", zip_file, package_name }
149+
chdir(working_dir) { sh @zip_command, "-r", zip_file, target_dir }
150+
mv "#{package_dir_path}/#{zip_file}", package_dir if without_parent_dir
145151
end
146152
end
147153

@@ -202,6 +208,15 @@ def tar_xz_file
202208
def zip_file
203209
"#{package_name}.zip"
204210
end
211+
212+
def working_dir
213+
without_parent_dir ? package_dir_path : package_dir
214+
end
215+
216+
# target directory relative to working_dir
217+
def target_dir
218+
without_parent_dir ? "." : package_name
219+
end
205220
end
206221

207222
end

test/test_rake_package_task.rb

+12
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,16 @@ def test_package_name_noversion
7878
assert_equal "a", pkg.package_name
7979
end
8080

81+
def test_without_parent_dir
82+
pkg = Rake::PackageTask.new("foo", :noversion)
83+
84+
assert_equal "pkg", pkg.working_dir
85+
assert_equal "foo", pkg.target_dir
86+
87+
pkg.without_parent_dir = true
88+
89+
assert_equal "pkg/foo", pkg.working_dir
90+
assert_equal ".", pkg.target_dir
91+
end
92+
8193
end

0 commit comments

Comments
 (0)