Sha256: 1277640772f3e7d75c91b28469b85fdf33d8d463817523d027f396602753bd40

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

require 'archive/tar/minitar'
require 'fileutils'
require 'tmpdir'
require 'tempfile'
require 'zlib'

module Stove
  class Packager
    ACCEPTABLE_FILES = [
      'README.*',
      'CHANGELOG.*',
      'metadata.{json,rb}',
      'attributes',
      'definitions',
      'files',
      'libraries',
      'providers',
      'recipes',
      'resources',
      'templates',
    ]

    # The cookbook to package.
    #
    # @erturn [Stove::Cookbook]
    attr_reader :cookbook

    # Create a new packager instance.
    #
    # @param [Stove::Cookbook]
    #   the cookbook to package
    def initialize(cookbook)
      @cookbook = cookbook
    end

    # The list of files that should actually be uploaded.
    #
    # @return [Array]
    #   the array of file paths
    def cookbook_files
      Dir.glob("#{File.expand_path(cookbook.path)}/{#{ACCEPTABLE_FILES.join(',')}}")
    end

    # The path to the tar.gz package in the temporary directory.
    #
    # @return [String]
    def package_path
      pack!
    end

    private
      def pack!
        destination = Tempfile.new(cookbook.name).path

        # Sandbox
        sandbox = Dir.mktmpdir
        FileUtils.mkdir_p(sandbox)

        # Containing folder
        container = File.join(sandbox, cookbook.name)
        FileUtils.mkdir_p(container)

        # Copy filles
        FileUtils.cp_r(cookbook_files, container)

        # Generate metadata
        File.open(File.join(container, 'metadata.json'), 'w') do |f|
          f.write(cookbook.metadata.to_json)
        end

        Dir.chdir(sandbox) do |dir|
          # This is super fucking annoying. The community site should really
          # be better at reading tarballs
          relative_path = container.gsub(sandbox + '/', '') + '/'
          tgz = Zlib::GzipWriter.new(File.open(destination, 'wb'))
          Archive::Tar::Minitar.pack(relative_path, tgz)
        end

        return destination
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stove-1.1.2 lib/stove/packager.rb
stove-1.1.0 lib/stove/packager.rb
stove-1.0.1 lib/stove/packager.rb
stove-1.0.0 lib/stove/packager.rb