class Kitchen::Generator::Init

A project initialization generator, to help prepare a cookbook project for testing with Kitchen.

@author Fletcher Nichol <fnichol@nichol.ca>

Public Instance Methods

init() click to toggle source

Invoke the command.

# File lib/kitchen/generator/init.rb, line 56
def init
  self.class.source_root(Kitchen.source_root.join("templates", "init"))

  create_kitchen_yaml
  create_chefignore
  prepare_rakefile
  prepare_thorfile
  create_test_dir
  prepare_gitignore
  prepare_gemfile
  display_bundle_message
end

Private Instance Methods

add_gem_to_gemfile() click to toggle source

Appends entries to a Gemfile.

@api private

# File lib/kitchen/generator/init.rb, line 215
def add_gem_to_gemfile
  if not_in_file?("Gemfile", /gem ('|")test-kitchen('|")/)
    append_to_file("Gemfile", %{gem "test-kitchen"\n})
    @display_bundle_msg = true
  end
end
append_to_gitignore(line) click to toggle source

Appends a line to the .gitignore file.

@api private

# File lib/kitchen/generator/init.rb, line 185
def append_to_gitignore(line)
  create_file(".gitignore") unless File.exist?(File.join(destination_root, ".gitignore"))

  if IO.readlines(File.join(destination_root, ".gitignore")).grep(/^#{line}/).empty?
    append_to_file(".gitignore", "#{line}\n")
  end
end
create_chefignore() click to toggle source

Creates the `chefignore` file.

@api private

# File lib/kitchen/generator/init.rb, line 90
def create_chefignore
  template("chefignore.erb", "chefignore")
end
create_gemfile_if_missing() click to toggle source

Creates a Gemfile if missing

@api private

# File lib/kitchen/generator/init.rb, line 206
def create_gemfile_if_missing
  unless File.exist?(File.join(destination_root, "Gemfile"))
    create_file("Gemfile", %{source "https://rubygems.org"\n\n})
  end
end
create_kitchen_yaml() click to toggle source

Creates the `.kitchen.yml` file.

@api private

# File lib/kitchen/generator/init.rb, line 74
def create_kitchen_yaml
  cookbook_name = if File.exist?(File.expand_path("metadata.rb"))
                    MetadataChopper.extract("metadata.rb").first
                  end
  run_list = cookbook_name ? "recipe[#{cookbook_name}::default]" : nil
  driver_plugin = Array(options[:driver]).first || "dummy"

  template("kitchen.yml.erb", "kitchen.yml",
    driver_plugin: driver_plugin.sub(/^kitchen-/, ""),
    provisioner: options[:provisioner],
    run_list: Array(run_list))
end
create_test_dir() click to toggle source

Create the default test directory

@api private

# File lib/kitchen/generator/init.rb, line 168
def create_test_dir
  empty_directory "test/integration/default" if init_test_dir?
end
display_bundle_message() click to toggle source

Displays a bundle warning message to the user.

@api private

# File lib/kitchen/generator/init.rb, line 225
def display_bundle_message
  if @display_bundle_msg
    say "You must run `bundle install' to fetch any new gems.", :red
  end
end
init_gemfile?() click to toggle source

@return [true,false] whether or not a Gemfile needs to be initialized @api private

# File lib/kitchen/generator/init.rb, line 96
def init_gemfile?
  File.exist?(File.join(destination_root, "Gemfile")) ||
    options[:create_gemfile]
end
init_git?() click to toggle source

@return [true,false] whether or not a `.gitignore` file needs to be

initialized

@api private

# File lib/kitchen/generator/init.rb, line 125
def init_git?
  File.directory?(File.join(destination_root, ".git"))
end
init_rakefile?() click to toggle source

@return [true,false] whether or not a Rakefile needs to be initialized @api private

# File lib/kitchen/generator/init.rb, line 103
def init_rakefile?
  File.exist?(File.join(destination_root, "Rakefile")) &&
    not_in_file?("Rakefile", %r{require 'kitchen/rake_tasks'})
end
init_test_dir?() click to toggle source

@return [true,false] whether or not a test directory needs to be

initialized

@api private

# File lib/kitchen/generator/init.rb, line 118
def init_test_dir?
  Util.list_directory("test/integration/").select { |d| File.directory?(d) }.empty?
end
init_thorfile?() click to toggle source

@return [true,false] whether or not a Thorfile needs to be initialized @api private

# File lib/kitchen/generator/init.rb, line 110
def init_thorfile?
  File.exist?(File.join(destination_root, "Thorfile")) &&
    not_in_file?("Thorfile", %r{require 'kitchen/thor_tasks'})
end
not_in_file?(filename, regexp) click to toggle source

Determines whether or not a pattern is found in a file.

@param filename [String] filename to read @param regexp [Regexp] a regular expression @return [true,false] whether or not a pattern is found in a file @api private

# File lib/kitchen/generator/init.rb, line 237
def not_in_file?(filename, regexp)
  IO.readlines(File.join(destination_root, filename)).grep(regexp).empty?
end
prepare_gemfile() click to toggle source

Prepares a Gemfile.

@api private

# File lib/kitchen/generator/init.rb, line 196
def prepare_gemfile
  return unless init_gemfile?

  create_gemfile_if_missing
  add_gem_to_gemfile
end
prepare_gitignore() click to toggle source

Prepares the .gitignore file

@api private

# File lib/kitchen/generator/init.rb, line 175
def prepare_gitignore
  return unless init_git?

  append_to_gitignore(".kitchen/")
  append_to_gitignore(".kitchen.local.yml")
end
prepare_rakefile() click to toggle source

Prepares a Rakefile.

@api private

# File lib/kitchen/generator/init.rb, line 132
      def prepare_rakefile
        return unless init_rakefile?

        rakedoc = <<-RAKE.gsub(/^ {10}/, "")

          begin
            require_relative '../rake_tasks'
            Kitchen::RakeTasks.new
          rescue LoadError
            puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI']
          end
        RAKE
        append_to_file(File.join(destination_root, "Rakefile"), rakedoc)
      end
prepare_thorfile() click to toggle source

Prepares a Thorfile.

@api private

# File lib/kitchen/generator/init.rb, line 150
      def prepare_thorfile
        return unless init_thorfile?

        thordoc = <<-THOR.gsub(/^ {10}/, "")

          begin
            require_relative '../thor_tasks'
            Kitchen::ThorTasks.new
          rescue LoadError
            puts '>>>>> Kitchen gem not loaded, omitting tasks' unless ENV['CI']
          end
        THOR
        append_to_file(File.join(destination_root, "Thorfile"), thordoc)
      end
unbundlerize() { || ... } click to toggle source

Save off any Bundler/Ruby-related environment variables so that the yielded block can run “bundler-free” (and restore at the end).

@api private

# File lib/kitchen/generator/init.rb, line 245
def unbundlerize
  keys = ENV.keys.select { |key| key =~ /^BUNDLER?_/ } + %w{RUBYOPT}

  keys.each { |key| ENV["__#{key}"] = ENV[key]; ENV.delete(key) }
  yield
  keys.each { |key| ENV[key] = ENV.delete("__#{key}") }
end