class MacSetup::GitRepoInstaller

Attributes

install_path[R]
repo[R]
status[R]
tracking_key[R]

Public Class Methods

install_repo(repo, install_path, track: nil, status: nil) click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 18
def self.install_repo(repo, install_path, track: nil, status: nil)
  new(repo, install_path, tracking_key: track, status: status).install_or_update
end
new(repo, install_path, tracking_key: nil, status: nil) click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 22
def initialize(repo, install_path, tracking_key: nil, status: nil)
  @repo         = repo
  @install_path = install_path
  @tracking_key = tracking_key
  @status       = status
end
run(config, _status) click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 7
def self.run(config, _status)
  repos = config.git_repos
  return if repos.none?

  MacSetup.log "Installing Git Repos..."

  repos.each do |repo, install_path|
    new(repo, File.expand_path(install_path)).install_or_update
  end
end

Public Instance Methods

install_or_update() click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 29
def install_or_update
  if Dir.exist?(install_path)
    MacSetup.log("#{repo} Already Installed. Updating") { update }
  else
    MacSetup.log("Installing #{repo}") { install }
  end
end

Private Instance Methods

can_update?() click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 62
def can_update?
  Shell.result("git status --porcelain").empty?
end
clone_repo() click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 58
def clone_repo
  Shell.run(%(git clone --recursive #{repo_url} "#{install_path}"))
end
in_install_path() { || ... } click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 80
def in_install_path
  Dir.chdir(install_path) { yield }
end
install() click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 39
def install
  clone_repo
  track_install
end
repo_url() click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 84
def repo_url
  repo =~ %r{^[^/]+/[^/]+$} ? "https://github.com/#{repo}.git" : repo
end
track_install() click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 66
def track_install
  return unless tracking_key

  in_install_path do
    status.git_changes(tracking_key, Shell.result("git ls-files").split("\n"))
  end
end
track_update() click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 74
def track_update
  return unless tracking_key

  status.git_changes(tracking_key, Shell.result("git diff --name-only origin").split("\n"))
end
update() click to toggle source
# File lib/mac_setup/git_repo_installer.rb, line 44
def update
  in_install_path do
    unless can_update?
      MacSetup.log "Can't update. Unstaged changes in #{install_path}"
      MacSetup.log Shell.result("git status --porcelain")
      return
    end

    Shell.run("git fetch")
    track_update
    Shell.run("git merge origin && git submodule update --init --recursive")
  end
end