diff --git a/lib/pdk/cli/exec.rb b/lib/pdk/cli/exec.rb index e97438898..b4c68d1cc 100644 --- a/lib/pdk/cli/exec.rb +++ b/lib/pdk/cli/exec.rb @@ -5,6 +5,7 @@ require 'tty-which' require 'pdk/util' +require 'pdk/util/git' module PDK module CLI @@ -21,37 +22,6 @@ def self.ensure_bin_present!(bin_path, bin_name) raise PDK::CLI::FatalError, message unless TTY::Which.exist?(bin_path) end - def self.git_bindir - @git_dir ||= File.join('private', 'git', Gem.win_platform? ? 'cmd' : 'bin') - end - - def self.git_paths - @paths ||= begin - paths = [File.join(PDK::Util.pdk_package_basedir, git_bindir)] - - if Gem.win_platform? - paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'bin') - paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'libexec', 'git-core') - paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'usr', 'bin') - end - - paths - end - end - - def self.git_bin - git_bin = Gem.win_platform? ? 'git.exe' : 'git' - vendored_bin_path = File.join(git_bindir, git_bin) - - try_vendored_bin(vendored_bin_path, git_bin) - end - - def self.git(*args) - ensure_bin_present!(git_bin, 'git') - - execute(git_bin, *args) - end - def self.bundle(*args) ensure_bin_present!(bundle_bin, 'bundler') @@ -169,7 +139,7 @@ def execute! File.join(@process.environment['GEM_PATH'], 'bin'), package_binpath, ENV['PATH'], - PDK::Util.package_install? ? PDK::CLI::Exec.git_paths : nil, + PDK::Util.package_install? ? PDK::Util::Git.git_paths : nil, ].compact.flatten.join(File::PATH_SEPARATOR) mod_root = PDK::Util.module_root diff --git a/lib/pdk/module/templatedir.rb b/lib/pdk/module/templatedir.rb index decca1cb3..b5bf8ac2a 100644 --- a/lib/pdk/module/templatedir.rb +++ b/lib/pdk/module/templatedir.rb @@ -1,6 +1,6 @@ require 'yaml' require 'pdk/util' -require 'pdk/cli/exec' +require 'pdk/util/git' require 'pdk/cli/errors' require 'pdk/template_file' @@ -48,7 +48,7 @@ def initialize(path_or_url, module_metadata = {}) # use. temp_dir = PDK::Util.make_tmpdir_name('pdk-module-template') - clone_result = PDK::CLI::Exec.git('clone', path_or_url, temp_dir) + clone_result = PDK::Util::Git.git('clone', path_or_url, temp_dir) unless clone_result[:exit_code].zero? PDK.logger.error clone_result[:stdout] PDK.logger.error clone_result[:stderr] @@ -85,7 +85,7 @@ def initialize(path_or_url, module_metadata = {}) def metadata return {} unless @repo - ref_result = PDK::CLI::Exec.git('--git-dir', File.join(@path, '.git'), 'describe', '--all', '--long', '--always') + ref_result = PDK::Util::Git.git('--git-dir', File.join(@path, '.git'), 'describe', '--all', '--long', '--always') if ref_result[:exit_code].zero? { 'template-url' => @repo, 'template-ref' => ref_result[:stdout].strip } else diff --git a/lib/pdk/util/git.rb b/lib/pdk/util/git.rb new file mode 100644 index 000000000..84061815b --- /dev/null +++ b/lib/pdk/util/git.rb @@ -0,0 +1,36 @@ +module PDK + module Util + module Git + def self.git_bindir + @git_dir ||= File.join('private', 'git', Gem.win_platform? ? 'cmd' : 'bin') + end + + def self.git_paths + @paths ||= begin + paths = [File.join(PDK::Util.pdk_package_basedir, git_bindir)] + + if Gem.win_platform? + paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'bin') + paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'mingw64', 'libexec', 'git-core') + paths << File.join(PDK::Util.pdk_package_basedir, 'private', 'git', 'usr', 'bin') + end + + paths + end + end + + def self.git_bin + git_bin = Gem.win_platform? ? 'git.exe' : 'git' + vendored_bin_path = File.join(git_bindir, git_bin) + + PDK::CLI::Exec.try_vendored_bin(vendored_bin_path, git_bin) + end + + def self.git(*args) + PDK::CLI::Exec.ensure_bin_present!(git_bin, 'git') + + PDK::CLI::Exec.execute(git_bin, *args) + end + end + end +end diff --git a/lib/pdk/util/version.rb b/lib/pdk/util/version.rb index 0c897c361..78b2ef461 100644 --- a/lib/pdk/util/version.rb +++ b/lib/pdk/util/version.rb @@ -1,5 +1,6 @@ require 'pdk/version' require 'pdk/cli/exec' +require 'pdk/util/git' module PDK module Util @@ -27,7 +28,7 @@ def self.git_ref return nil unless File.directory?(source_git_dir) - ref_result = PDK::CLI::Exec.git('--git-dir', source_git_dir, 'describe', '--all', '--long') + ref_result = PDK::Util::Git.git('--git-dir', source_git_dir, 'describe', '--all', '--long') return ref_result[:stdout].strip if ref_result[:exit_code].zero? end diff --git a/spec/unit/pdk/util/version_spec.rb b/spec/unit/pdk/util/version_spec.rb index b6b954b6a..06b30f0dd 100644 --- a/spec/unit/pdk/util/version_spec.rb +++ b/spec/unit/pdk/util/version_spec.rb @@ -17,7 +17,7 @@ allow(result).to receive(:[]).with(:stdout).and_return('git_hash') allow(result).to receive(:[]).with(:exit_code).and_return(0) - allow(PDK::CLI::Exec).to receive(:git).with('--git-dir', %r{.git\Z}, 'describe', '--all', '--long').and_return(result) + allow(PDK::Util::Git).to receive(:git).with('--git-dir', %r{.git\Z}, 'describe', '--all', '--long').and_return(result) end describe '#git_ref' do