Skip to content

Commit

Permalink
(PDK-673) Moving git commands into a util class
Browse files Browse the repository at this point in the history
  • Loading branch information
Helen Campbell committed Nov 9, 2017
1 parent acc5897 commit 7fbe233
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
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

0 comments on commit 7fbe233

Please sign in to comment.