# class Chef::Knife::CookbookSiteInstall

### Attributes

cookbook_name[R]
vendor_path[R]

### Public Instance Methods

clear_existing_files(cookbook_path) click to toggle source

    # File lib/chef/knife/cookbook_site_install.rb, line 150
    def clear_existing_files(cookbook_path)
      ui.info("Removing pre-existing version.")
      FileUtils.rmtree(cookbook_path) if File.directory?(cookbook_path)
    end

convert_path(upstream_file) click to toggle source

    # File lib/chef/knife/cookbook_site_install.rb, line 155
    def convert_path(upstream_file)
      if ENV['MSYSTEM'] == 'MINGW32'
        return upstream_file.sub(/^([[:alpha:]]):/, '/\1')
            else
        return Shellwords.escape upstream_file
      end
    end

download_cookbook_to(download_path) click to toggle source

    # File lib/chef/knife/cookbook_site_install.rb, line 137
    def download_cookbook_to(download_path)
      downloader = Chef::Knife::CookbookSiteDownload.new
      downloader.config[:file] = download_path
      downloader.name_args = name_args
      downloader.run
      downloader
    end

extract_cookbook(upstream_file, version) click to toggle source

    # File lib/chef/knife/cookbook_site_install.rb, line 145
    def extract_cookbook(upstream_file, version)
      ui.info("Uncompressing #{@cookbook_name} version #{version}.")
      shell_out!("tar zxvf #{convert_path upstream_file}", :cwd => @install_path)
    end

parse_name_args!() click to toggle source

    # File lib/chef/knife/cookbook_site_install.rb, line 124
    def parse_name_args!
      if name_args.empty?
        ui.error("Please specify a cookbook to download and install.")
        exit 1
      elsif name_args.size >= 2
        unless name_args.last.match(/^(\d+)(\.\d+){1,2}$/) and name_args.size == 2
          ui.error("Installing multiple cookbooks at once is not supported.")
          exit 1
        end
      end
      name_args.first
    end

run() click to toggle source

    # File lib/chef/knife/cookbook_site_install.rb, line 65
    def run
      extend Chef::Mixin::ShellOut

      if config[:cookbook_path]
        Chef::Config[:cookbook_path] = config[:cookbook_path]
      else
        config[:cookbook_path] = Chef::Config[:cookbook_path]
      end

      @cookbook_name = parse_name_args!
      # Check to ensure we have a valid source of cookbooks before continuing
      #
      @install_path = File.expand_path(Array(config[:cookbook_path]).first)
      ui.info "Installing #@cookbook_name to #{@install_path}"

      @repo = CookbookSCMRepo.new(@install_path, ui, config)
      #cookbook_path = File.join(vendor_path, name_args[0])
      upstream_file = File.join(@install_path, "#{@cookbook_name}.tar.gz")

      @repo.sanity_check
      unless config[:use_current_branch]
        @repo.reset_to_default_state
        @repo.prepare_to_import(@cookbook_name)
      end

      downloader = download_cookbook_to(upstream_file)
      clear_existing_files(File.join(@install_path, @cookbook_name))
      extract_cookbook(upstream_file, downloader.version)

      # TODO: it'd be better to store these outside the cookbook repo and
      # keep them around, e.g., in ~/Library/Caches on OS X.
      ui.info("removing downloaded tarball")
      File.unlink(upstream_file)

      if @repo.finalize_updates_to(@cookbook_name, downloader.version)
        unless config[:use_current_branch]
          @repo.reset_to_default_state
        end
        @repo.merge_updates_from(@cookbook_name, downloader.version)
      else
        unless config[:use_current_branch]
          @repo.reset_to_default_state
        end
      end

      unless config[:no_deps]
        md = Chef::Cookbook::Metadata.new
        md.from_file(File.join(@install_path, @cookbook_name, "metadata.rb"))
        md.dependencies.each do |cookbook, version_list|
          # Doesn't do versions.. yet
          nv = self.class.new
          nv.config = config
          nv.name_args = [ cookbook ]
          nv.run
        end
      end
    end