summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjtimberman <joshua@opscode.com>2012-08-06 14:30:34 -0600
committerjtimberman <joshua@opscode.com>2012-08-06 14:30:34 -0600
commitb91020183720f158f47f9da9a7eadec713f9d3fc (patch)
treedfabbdca1e4b61059ddf433cf0d371d0def39c13
parentc080dbddb914ed967d17b1bcc1d673598ea8dcbf (diff)
parentb41a4c42acf43d84f7a35bc2e5523c62e5be8323 (diff)
downloadmixlib-shellout-b91020183720f158f47f9da9a7eadec713f9d3fc.tar.gz
Merge branch 'COOK-1476'
-rw-r--r--providers/zipfile.rb38
-rw-r--r--resources/zipfile.rb2
2 files changed, 39 insertions, 1 deletions
diff --git a/providers/zipfile.rb b/providers/zipfile.rb
index 808bb86..c34abce 100644
--- a/providers/zipfile.rb
+++ b/providers/zipfile.rb
@@ -22,6 +22,8 @@
include Windows::Helper
+require 'find'
+
action :unzip do
ensure_rubyzip_gem_installed
Chef::Log.debug("unzip #{@new_resource.source} => #{@new_resource.path} (overwrite=#{@new_resource.overwrite})")
@@ -39,6 +41,42 @@ action :unzip do
@new_resource.updated_by_last_action(true)
end
+action :zip do
+ ensure_rubyzip_gem_installed
+ # sanitize paths for windows.
+ @new_resource.source.downcase.gsub!(::File::SEPARATOR, ::File::ALT_SEPARATOR)
+ @new_resource.path.downcase.gsub!(::File::SEPARATOR, ::File::ALT_SEPARATOR)
+ Chef::Log.debug("zip #{@new_resource.source} => #{@new_resource.path} (overwrite=#{@new_resource.overwrite})")
+
+ if @new_resource.overwrite == false && ::File.exists?(@new_resource.path)
+ Chef::Log.info("file #{@new_resource.path} already exists and overwrite is set to false, exiting")
+ else
+ # delete the archive if it already exists, because we are recreating it.
+ if ::File.exists?(@new_resource.path)
+ ::File.unlink(@new_resource.path)
+ end
+ # only supporting compression of a single directory (recursively).
+ if ::File.directory?(@new_resource.source)
+ z = Zip::ZipFile.new(@new_resource.path, true)
+ unless @new_resource.source =~ /::File::ALT_SEPARATOR$/
+ @new_resource.source << ::File::ALT_SEPARATOR
+ end
+ Find.find(@new_resource.source) do |f|
+ f.downcase.gsub!(::File::SEPARATOR, ::File::ALT_SEPARATOR)
+ # don't add root directory to the zipfile.
+ next if f == @new_resource.source
+ # strip the root directory from the filename before adding it to the zipfile.
+ zip_fname = f.sub(@new_resource.source, '')
+ Chef::Log.debug("adding #{zip_fname} to archive, sourcefile is: #{f}")
+ z.add(zip_fname, f)
+ end
+ z.close
+ else
+ Chef::Log.info("Single directory must be specified for compression, and #{@new_resource.source} does not meet that criteria.")
+ end
+ end
+end
+
private
def ensure_rubyzip_gem_installed
begin
diff --git a/resources/zipfile.rb b/resources/zipfile.rb
index a4aa135..8dad4a5 100644
--- a/resources/zipfile.rb
+++ b/resources/zipfile.rb
@@ -20,7 +20,7 @@
# limitations under the License.
#
-actions :unzip # TODO , :zip
+actions :unzip, :zip
attribute :path, :kind_of => String, :name_attribute => true
attribute :source, :kind_of => String