Rakefiles: allow `zip()` to archive only a subset of files
...within a directory.
--- a/rakelib/extensions.rb Thu Aug 24 01:13:48 2017 +0100
+++ b/rakelib/extensions.rb Thu Aug 24 09:25:49 2017 +0100
@@ -244,6 +244,9 @@
# has the same name unless explicitily specified by `archive:` option.
# If `remove: true` option is set to true, the `directory` to archive is
# removed after adding the archive.
+ #
+ # If `include` is given (i.e., not `nil`), then only files within
+ # `directory` are added to the archive.
#
# The type of the archive is automatically derived from `archive` name (if
# provided) or defaults to `.tar.bz2` on UNIXes and to `.zip` on Windows.
@@ -262,21 +265,25 @@
#
# zip '/tmp/build_dir', archive: 'smalltalkx', remove: true
#
- def zip(directory, remove: false, archive: nil, sha256: true)
+ def zip(directory, remove: false, archive: nil, sha256: true, include: nil)
archive = directory unless archive
if !(archive.end_with? '.zip') && !(archive.end_with? '.tar.gz') && !(archive.end_with? '.tar.bz2')
archive = "#{directory}#{win32? ? '.zip' : '.tar.bz2'}"
+ end
+ archive = File.expand_path(archive)
+ source = [ "\"#{File.basename(directory)}\"" ]
+ unless include.nil?
+ source = include.collect { | each | "\"#{File.join(File.basename(directory), each)}\"" }
end
-
- archive = File.expand_path(archive)
+
chdir File.dirname(directory) do
case
when (archive.end_with? '.zip')
- sh "zip -q -r #{remove ? '-T -m' : ''} \"#{archive}\" \"#{File.basename(directory)}\""
+ sh "zip -q -r #{remove ? '-T -m' : ''} \"#{archive}\" #{source.join(' ')}"
when (archive.end_with? '.tar.bz2')
- sh "tar cjf \"#{archive}\" #{remove ? '--remove-files' : ''} \"#{File.basename(directory)}\""
+ sh "tar cjf \"#{archive}\" #{remove ? '--remove-files' : ''} #{source.join(' ')}"
when (archive.end_with? '.tar.gz')
- sh "tar czf \"#{archive}\" #{remove ? '--remove-files' : ''} \"#{File.basename(directory)}\""
+ sh "tar czf \"#{archive}\" #{remove ? '--remove-files' : ''} #{source.join(' ')}"
else
raise Exception.new("Unknown archive type: #{File.basename(archive)}")
end
--- a/tests/tests_extensions.rb Thu Aug 24 01:13:48 2017 +0100
+++ b/tests/tests_extensions.rb Thu Aug 24 09:25:49 2017 +0100
@@ -10,8 +10,8 @@
assert which('boguscommand').nil?
end
- # A helper for `test_zip()`
- def test_zip_fmt(fmt)
+ # A helper for `test_zip_01()`
+ def test_zip_01_fmt(fmt)
Dir.mktmpdir do | tmp |
chdir File.join(File.dirname(__FILE__), '..') do
archive = File.join(tmp, "rakelib#{fmt}")
@@ -20,14 +20,15 @@
unzip archive
assert File.exist? File.join(tmp, 'rakelib')
assert File.exist? File.join(tmp, 'rakelib', 'extensions.rb')
+ assert File.exist? File.join(tmp, 'rakelib', 'support.rb')
end
end
end
def test_zip_01()
- test_zip_fmt('.zip') if which 'zip'
- test_zip_fmt('.tar.bz2') if which 'tar' and which 'bzip2'
- test_zip_fmt('.tar.gz') if which 'tar' and which 'gzip'
+ test_zip_01_fmt('.zip') if which 'zip'
+ test_zip_01_fmt('.tar.bz2') if which 'tar' and which 'bzip2'
+ test_zip_01_fmt('.tar.gz') if which 'tar' and which 'gzip'
end
def test_zip_02()
@@ -41,6 +42,27 @@
end
end
end
+
+ # A helper for `test_zip_03()`
+ def test_zip_03_fmt(fmt)
+ Dir.mktmpdir do | tmp |
+ chdir File.join(File.dirname(__FILE__), '..') do
+ archive = File.join(tmp, "rakelib#{fmt}")
+ zip "rakelib", archive: archive, include: [ 'extensions.rb' ]
+ assert File.exist? archive
+ unzip archive
+ assert File.exist? File.join(tmp, 'rakelib')
+ assert File.exist? File.join(tmp, 'rakelib', 'extensions.rb')
+ assert (not File.exist? File.join(tmp, 'rakelib', 'support.rb'))
+ end
+ end
+ end
+
+ def test_zip_03()
+ test_zip_03_fmt('.zip') if which 'zip'
+ test_zip_03_fmt('.tar.bz2') if which 'tar' and which 'bzip2'
+ test_zip_03_fmt('.tar.gz') if which 'tar' and which 'gzip'
+ end
end
if __FILE__ == $0