Sha256: c8da1b1c1cc531bd6451ef95c5cf663a8d245c13ef68b78f360636c3ff38e41e

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module Berkshelf
  class CookbookSource
    # @author Jamie Winsor <jamie@vialstudios.com>
    class GitLocation
      include Location

      attr_accessor :uri
      attr_accessor :branch

      alias_method :ref, :branch
      alias_method :tag, :branch

      # @param [#to_s] name
      # @param [DepSelector::VersionConstraint] version_constraint
      # @param [Hash] options
      def initialize(name, version_constraint, options)
        @name = name
        @version_constraint = version_constraint
        @uri = options[:git]
        @branch = options[:branch] || options[:ref] || options[:tag]

        Git.validate_uri!(@uri)
      end

      # @param [#to_s] destination
      #
      # @return [Berkshelf::CachedCookbook]
      def download(destination)
        tmp_clone = Dir.mktmpdir
        ::Berkshelf::Git.clone(uri, tmp_clone)
        ::Berkshelf::Git.checkout(tmp_clone, branch) if branch
        unless branch
          self.branch = ::Berkshelf::Git.rev_parse(tmp_clone)
        end

        unless File.chef_cookbook?(tmp_clone)
          msg = "Cookbook '#{name}' not found at git: #{uri}" 
          msg << " with branch '#{branch}'" if branch
          raise CookbookNotFound, msg
        end

        cb_path = File.join(destination, "#{self.name}-#{Git.rev_parse(tmp_clone)}")
        FileUtils.mv(tmp_clone, cb_path, force: true)
                
        cached = CachedCookbook.from_store_path(cb_path)
        validate_cached(cached)

        set_downloaded_status(true)
        cached
      end

      def to_s
        s = "git: '#{uri}'"
        s << " with branch: '#{branch}'" if branch
        s
      end

      private

        def git
          @git ||= Berkshelf::Git.new(uri)
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
berkshelf-0.3.7 lib/berkshelf/cookbook_source/git_location.rb