class CloneGitFile::Cloner

Public Class Methods

new(file, options = {}) click to toggle source
# File lib/clone_git_file.rb, line 6
def initialize(file, options = {})
  @file = file.gsub("%20", " ")
  @options = options
end

Public Instance Methods

open_file() click to toggle source
# File lib/clone_git_file.rb, line 11
def open_file
  if Dir.exists?(File.expand_path(local_repo_path))
    update_repo
  else
    clone_repo
  end

  if @options[:open_in_editor] ||
      @options[:output_run_command_to_terminal]
    launch_editor
  else
    print_clone_location
  end
end

Private Instance Methods

clone_repo() click to toggle source
# File lib/clone_git_file.rb, line 63
def clone_repo
  commands = ""
  commands << %(git clone #{parsed_data.repo_url} "#{local_repo_path}")

  branch_name = parsed_data.branch_name
  if branch_name && branch_name != ""
    commands << %(\ncd "#{local_repo_path}")
    commands << "\ngit checkout #{parsed_data.branch_name}"
  end

  commands = make_commands_silent(commands) if @options[:silent]

  system(commands)
end
launch_editor() click to toggle source
# File lib/clone_git_file.rb, line 36
def launch_editor
  commands = ""
  file_path = "#{local_repo_path}"
  file_path << "/#{parsed_data.file_relative_path}" if parsed_data.file_relative_path

  # change into the directory so that relative file loads will work
  if File.directory?(file_path)
    commands << %(cd "#{file_path}")
  else
    commands << %(cd "#{File.dirname(file_path)}")
  end
  commands << %(\n#{ENV["EDITOR"]} "#{file_path}")

  if @options[:output_run_command_to_terminal]
    puts(commands)
  else
    system(commands)
  end
end
local_repo_path() click to toggle source
# File lib/clone_git_file.rb, line 32
def local_repo_path
  @local_repo_path ||= File.expand_path("#{ENV["TARGET_DIRECTORY"]}/#{parsed_data.github_username}/#{parsed_data.repo_name}")
end
make_commands_silent(commands) click to toggle source
# File lib/clone_git_file.rb, line 90
def make_commands_silent(commands)
  cmds = commands.split("\n")

  cmds.map { |c| "#{c} &> /dev/null" }.join("\n")
end
parsed_data() click to toggle source
# File lib/clone_git_file.rb, line 28
def parsed_data
  @parsed_data ||= GithubRepoParser.new(@file).parse
end
print_clone_location() click to toggle source
update_repo() click to toggle source
# File lib/clone_git_file.rb, line 78
def update_repo
  commands = ""

  commands << %(cd "#{local_repo_path}")
  commands << "\ngit reset HEAD --hard"
  commands << "\ngit pull"
  commands << "\ngit checkout #{parsed_data.branch_name}" if parsed_data.branch_name
  commands = make_commands_silent(commands) if @options[:silent]

  system(commands)
end