class GitHelper::Setup

Public Instance Methods

execute() click to toggle source
# File lib/git_helper/setup.rb, line 5
def execute
  execute_config_file
  execute_plugins
end

Private Instance Methods

ask_question(prompt, secret: false) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/git_helper/setup.rb, line 79
        def ask_question(prompt, secret: false)
  highline.ask(prompt, { required: true, secret: secret })
end
config_file() click to toggle source

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize

# File lib/git_helper/setup.rb, line 104
        def config_file
  git_config_reader.git_config_file_path
end
config_file_exists?() click to toggle source
# File lib/git_helper/setup.rb, line 47
        def config_file_exists?
  File.exist?(config_file)
end
create_or_update_config_file() click to toggle source
# File lib/git_helper/setup.rb, line 40
        def create_or_update_config_file
  contents = generate_file_contents
  puts "Creating or updating your #{config_file} file..."
  File.open(config_file, 'w') { |file| file.puts contents }
  puts "\nDone!\n\n"
end
create_or_update_plugin_files() click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/git_helper/setup.rb, line 85
        def create_or_update_plugin_files
  plugins_dir = "#{Dir.pwd.scan(%r{\A/\w*/\w*/}).first}.git_helper/plugins"
  plugins_url = 'https://api.github.com/repos/emmahsax/git_helper/contents/plugins'
  header = 'Accept: application/vnd.github.v3.raw'
  token = git_config_reader.github_token
  user = git_config_reader.github_user

  Dir.mkdir(plugins_dir) unless File.exist?(plugins_dir)

  all_plugins = JSON.parse(`curl -s -u #{user}:#{token} -H "#{header}" -L "#{plugins_url}"`)

  all_plugins.each do |plugin|
    plugin_content = `curl -s -u #{user}:#{token} -H "#{header}" -L "#{plugins_url}/#{plugin['name']}"`
    File.open("#{plugins_dir}/#{plugin['name']}", 'w') { |file| file.puts plugin_content }
  end
end
execute_config_file() click to toggle source

rubocop:disable Style/ConditionalAssignment

# File lib/git_helper/setup.rb, line 11
        def execute_config_file
  if config_file_exists?
    answer = highline.ask_yes_no(
      "It looks like the #{config_file} file already exists. Do you wish to replace it? (y/n)",
      { required: true }
    )
  else
    answer = true
  end

  create_or_update_config_file if answer
end
execute_plugins() click to toggle source

rubocop:enable Style/ConditionalAssignment

# File lib/git_helper/setup.rb, line 25
        def execute_plugins
  answer = highline.ask_yes_no(
    'Do you wish to set up the Git Helper plugins? (y/n) (This process will ' \
    'attempt to use your GitHub personal access token to authenticate)',
    { required: true }
  )

  return unless answer

  create_or_update_plugin_files
  puts "\nNow add this line to your ~/.bash_profile:\n" \
       '  export PATH=/path/to/computer/home/.git_helper/plugins:$PATH'
  puts "\nDone!"
end
generate_file_contents() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/git_helper/setup.rb, line 52
        def generate_file_contents
  file_contents = ''.dup

  if highline.ask_yes_no('Do you wish to set up GitHub credentials? (y/n)', { required: true })
    file_contents << ":github_user:  #{ask_question('GitHub username?')}\n"
    file_contents << ':github_token: ' \
      "#{ask_question(
        'GitHub personal access token? (Navigate to https://github.com/settings/tokens ' \
        'to create a new personal access token)',
        secret: true
      )}\n"
  end

  if highline.ask_yes_no('Do you wish to set up GitLab credentials? (y/n)', { required: true })
    file_contents << ":gitlab_user:  #{ask_question('GitLab username?')}\n"
    file_contents << ':gitlab_token: ' \
      "#{ask_question(
        'GitLab personal access token? (Navigate to https://gitlab.com/-/profile/personal_access_tokens' \
        ' to create a new personal access token)',
        secret: true
      )}\n"
  end

  file_contents.strip
end
git_config_reader() click to toggle source
# File lib/git_helper/setup.rb, line 108
        def git_config_reader
  @git_config_reader ||= GitHelper::GitConfigReader.new
end
highline() click to toggle source
# File lib/git_helper/setup.rb, line 112
        def highline
  @highline ||= HighlineWrapper.new
end