class ChefConfig::PackageTask

def chef_root_path

def chef_root_path
  module_name == "Chef" ? root_path : File.dirname(root_path)
end

def class_or_module

def class_or_module
  generate_version_class ? "class" : "module"
end

def component_full_paths

def component_full_paths
  component_paths.map { |path| File.expand_path(path, root_path) }
end

def define

def define
  raise "Need to provide package root and module name" if root_path.nil? || module_name.nil?
  desc "Build Gems of component dependencies"
  task :package_components do
    component_full_paths.each do |component_path|
      Dir.chdir(component_path) do
        sh "rake package"
      end
    end
  end
  task :package => :package_components
  desc "Build and install component dependencies"
  task :install_components => :package_components do
    component_full_paths.each do |component_path|
      Dir.chdir(component_path) do
        sh "rake install"
      end
    end
  end
  task :install => :install_components
  desc "Clean up builds of component dependencies"
  task :clobber_component_packages do
    component_full_paths.each do |component_path|
      Dir.chdir(component_path) do
        sh "rake clobber_package"
      end
    end
  end
  task :clobber_package => :clobber_component_packages
  desc "Update the version number for component dependencies"
  task :update_components_versions do
    component_full_paths.each do |component_path|
      Dir.chdir(component_path) do
        sh "rake version"
      end
    end
  end
  namespace :version do
    desc 'Regenerate lib/#{@module_path}/version.rb from VERSION file'
    task :update => :update_components_versions do
      update_version_rb
      update_gemfile_lock
    end
    task :bump => %w{version:bump_patch version:update}
    task :show do
      puts version
    end
    # Add 1 to the current patch version in the VERSION file, and write it back out.
    task :bump_patch do
      current_version = version
      new_version = current_version.sub(/^(\d+\.\d+\.)(\d+)/) { "#{$1}#{$2.to_i + 1}" }
      puts "Updating version in #{version_rb_path} from #{current_version.chomp} to #{new_version.chomp}"
      IO.write(version_file_path, new_version)
    end
    task :bump_minor do
      current_version = version
      new_version = current_version.sub(/^(\d+)\.(\d+)\.(\d+)/) { "#{$1}.#{$2.to_i + 1}.0" }
      puts "Updating version in #{version_rb_path} from #{current_version.chomp} to #{new_version.chomp}"
      IO.write(version_file_path, new_version)
    end
    task :bump_major do
      current_version = version
      new_version = current_version.sub(/^(\d+)\.(\d+\.\d+)/) { "#{$1.to_i + 1}.0.0" }
      puts "Updating version in #{version_rb_path} from #{current_version.chomp} to #{new_version.chomp}"
      IO.write(version_file_path, new_version)
    end
    def update_version_rb # rubocop:disable Lint/NestedMethodDefinition
      puts "Updating #{version_rb_path} to include version #{version} ..."
      contents = <<-VERSION_RB
pyright:: Copyright 2010-2016, Chef Software, Inc.
cense:: Apache License, Version 2.0
censed under the Apache License, Version 2.0 (the "License");
u may not use this file except in compliance with the License.
u may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
less required by applicable law or agreed to in writing, software
stributed under the License is distributed on an "AS IS" BASIS,
THOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e the License for the specific language governing permissions and
mitations under the License.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TE: This file is generated by running `rake version` in the top level of
is repo. Do not edit this manually. Edit the VERSION file and run the rake
sk instead.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ass_or_module} #{module_name}
module_name.upcase}_ROOT = File.expand_path("../..", __FILE__)
RSION = "#{version}"
TE: the Chef::Version class is defined in version_class.rb
TE: DO NOT Use the Chef::Version class on #{module_name}::VERSIONs.  The
    Chef::Version class is for _cookbooks_ only, and cannot handle
    pre-release versions like "10.14.0.rc.2".  Please use Rubygem's
    Gem::Version class instead.
      VERSION_RB
      IO.write(version_rb_path, contents)
    end
    def update_gemfile_lock # rubocop:disable Lint/NestedMethodDefinition
      if File.exist?(gemfile_lock_path)
        puts "Updating #{gemfile_lock_path} to include version #{version} ..."
        contents = IO.read(gemfile_lock_path)
        contents.gsub!(/^\s*(chef|chef-config)\s*\((= )?\S+\)\s*$/) do |line|
          line.gsub(/\((= )?\d+(\.\d+)+/) { "(#{$1}#{version}" }
        end
        IO.write(gemfile_lock_path, contents)
      end
    end
  end
  task :version => "version:update"
  gemspec_platform_to_install = ""
  Dir[File.expand_path("*.gemspec", root_path)].reverse_each do |gemspec_path|
    gemspec = eval(IO.read(gemspec_path))
    Gem::PackageTask.new(gemspec) do |task|
      task.package_dir = full_package_dir
    end
    gemspec_platform_to_install = "-#{gemspec.platform}" if gemspec.platform != Gem::Platform::RUBY && Gem::Platform.match(gemspec.platform)
  end
  desc "Build and install a #{module_path} gem"
  task :install => [:package] do
    with_clean_env do
      full_module_path = File.join(full_package_dir, module_path)
      sh %{gem install #{full_module_path}-#{version}#{gemspec_platform_to_install}.gem --no-rdoc --no-ri}
    end
  end
  task :uninstall do
    sh %{gem uninstall #{module_path} -x -v #{version} }
  end
  desc "Build it, tag it and ship it"
  task :ship => [:clobber_package, :gem] do
    sh("git tag #{version}")
    sh("git push #{git_remote} --tags")
    Dir[File.expand_path("*.gem", full_package_dir)].reverse_each do |built_gem|
      sh("gem push #{built_gem}")
    end
  end
end

def full_package_dir

def full_package_dir
  File.expand_path(package_dir, root_path)
end

def gemfile_lock_path

def gemfile_lock_path
  File.join(root_path, "Gemfile.lock")
end

def init(root_path, module_name, gem_name)

def init(root_path, module_name, gem_name)
  @root_path = root_path
  @module_name = module_name
  @gem_name = gem_name
  @component_paths = []
  @module_path = nil
  @package_dir = "pkg"
  @git_remote = "origin"
  @generate_version_class = false
end

def initialize(root_path = nil, module_name = nil, gem_name = nil)

def initialize(root_path = nil, module_name = nil, gem_name = nil)
  init(root_path, module_name, gem_name)
  yield self if block_given?
  define unless root_path.nil? || module_name.nil?
end

def module_path

def module_path
  @module_path || module_name.downcase
end

def update_gemfile_lock # rubocop:disable Lint/NestedMethodDefinition

rubocop:disable Lint/NestedMethodDefinition
def update_gemfile_lock # rubocop:disable Lint/NestedMethodDefinition
  if File.exist?(gemfile_lock_path)
    puts "Updating #{gemfile_lock_path} to include version #{version} ..."
    contents = IO.read(gemfile_lock_path)
    contents.gsub!(/^\s*(chef|chef-config)\s*\((= )?\S+\)\s*$/) do |line|
      line.gsub(/\((= )?\d+(\.\d+)+/) { "(#{$1}#{version}" }
    end
    IO.write(gemfile_lock_path, contents)
  end
end

def update_version_rb # rubocop:disable Lint/NestedMethodDefinition

rubocop:disable Lint/NestedMethodDefinition
def update_version_rb # rubocop:disable Lint/NestedMethodDefinition
  puts "Updating #{version_rb_path} to include version #{version} ..."
  contents = <<-VERSION_RB
ght:: Copyright 2010-2016, Chef Software, Inc.
e:: Apache License, Version 2.0
ed under the Apache License, Version 2.0 (the "License");
y not use this file except in compliance with the License.
y obtain a copy of the License at
tp://www.apache.org/licenses/LICENSE-2.0
 required by applicable law or agreed to in writing, software
buted under the License is distributed on an "AS IS" BASIS,
T WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e License for the specific language governing permissions and
tions under the License.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
This file is generated by running `rake version` in the top level of
epo. Do not edit this manually. Edit the VERSION file and run the rake
nstead.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
or_module} #{module_name}
le_name.upcase}_ROOT = File.expand_path("../..", __FILE__)
N = "#{version}"
the Chef::Version class is defined in version_class.rb
DO NOT Use the Chef::Version class on #{module_name}::VERSIONs.  The
Chef::Version class is for _cookbooks_ only, and cannot handle
pre-release versions like "10.14.0.rc.2".  Please use Rubygem's
Gem::Version class instead.
  VERSION_RB
  IO.write(version_rb_path, contents)
end

def version

def version
  IO.read(version_file_path).strip
end

def version_file_path

def version_file_path
  File.join(chef_root_path, "VERSION")
end

def version_rb_path

def version_rb_path
  File.expand_path("lib/#{module_path}/version.rb", root_path)
end

def with_clean_env(&block)

def with_clean_env(&block)
  if defined?(Bundler)
    Bundler.with_clean_env(&block)
  else
    yield
  end
end