Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PDK-673) Moving git commands into a util class #347

Merged
merged 1 commit into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 2 additions & 32 deletions lib/pdk/cli/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'tty-which'

require 'pdk/util'
require 'pdk/util/git'

module PDK
module CLI
Expand All @@ -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')

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/pdk/module/templatedir.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions lib/pdk/util/git.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/pdk/util/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'pdk/version'
require 'pdk/cli/exec'
require 'pdk/util/git'

module PDK
module Util
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/util/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down