Class: Stove::Packager
Constant Summary collapse
- ACCEPTABLE_FILES =
[ 'README.*', 'CHANGELOG.*', 'metadata.json', 'attributes/*.rb', 'definitions/*.rb', 'files/**/*', 'libraries/*.rb', 'providers/*.rb', 'recipes/*.rb', 'resources/*.rb', 'templates/**/*', ].freeze
- ACCEPTABLE_FILES_LIST =
ACCEPTABLE_FILES.join(',').freeze
- TMP_FILES =
[ /^(?:.*[\\\/])?\.[^\\\/]+\.sw[p-z]$/, /~$/, ].freeze
Instance Attribute Summary collapse
-
#cookbook ⇒ Object
readonly
The cookbook to package.
Instance Method Summary collapse
-
#gzip(io) ⇒ IO
GZip the given IO object (like a File or StringIO).
-
#initialize(cookbook) ⇒ Packager
constructor
Create a new packager instance.
-
#packaging_slip ⇒ Hash<String, String>
A map from physical file path to tarball file path.
-
#tar(root, slip) ⇒ StringIO
Create a tar file from the given root and packaging slip.
- #tarball ⇒ Object
Constructor Details
#initialize(cookbook) ⇒ Packager
Create a new packager instance.
40 41 42 |
# File 'lib/stove/packager.rb', line 40 def initialize(cookbook) @cookbook = cookbook end |
Instance Attribute Details
#cookbook ⇒ Object (readonly)
The cookbook to package.
34 35 36 |
# File 'lib/stove/packager.rb', line 34 def cookbook @cookbook end |
Instance Method Details
#gzip(io) ⇒ IO
GZip the given IO object (like a File or StringIO).
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/stove/packager.rb', line 134 def gzip(io) gz = StringIO.new('') z = Zlib::GzipWriter.new(gz) z.write(io.string) z.close # z was closed to write the gzip footer, so # now we need a new StringIO StringIO.new(gz.string) end |
#packaging_slip ⇒ Hash<String, String>
A map from physical file path to tarball file path
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/stove/packager.rb', line 56 def packaging_slip root = File.(cookbook.path) path = File.join(root, "{#{ACCEPTABLE_FILES_LIST}}") Dir.glob(path, File::FNM_DOTMATCH) .reject { |path| %w(. ..).include?(File.basename(path)) } .reject { |path| TMP_FILES.any? { |regex| path.match(regex) } } .map { |path| [path, path.sub(/^#{Regexp.escape(root)}/, cookbook.name)] } .reduce({}) do |map, (cookbook_file, tarball_file)| map[cookbook_file] = tarball_file map end end |
#tar(root, slip) ⇒ StringIO
Create a tar file from the given root and packaging slip
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/stove/packager.rb', line 105 def tar(root, slip) io = StringIO.new('') Gem::Package::TarWriter.new(io) do |tar| slip.each do |original_file, tarball_file| mode = File.stat(original_file).mode if File.directory?(original_file) tar.mkdir(tarball_file, mode) else tar.add_file(tarball_file, mode) do |tf| File.open(original_file, 'rb') { |f| tf.write(f.read) } end end end end io.rewind io end |
#tarball ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/stove/packager.rb', line 70 def tarball # Generate the metadata.json on the fly = File.join(cookbook.path, 'metadata.json') File.open(, 'wb') do |file| file.write(cookbook..to_json) end io = tar(File.dirname(cookbook.path), packaging_slip) tgz = gzip(io) tempfile = Tempfile.new([cookbook.name, '.tar.gz']) while buffer = tgz.read(1024) tempfile.write(buffer) end tempfile.rewind tempfile ensure if defined?() File.delete() end end |