class LearnBase

Constants

GITHUB_ORG
JAVASCRIPT_LAB_TEMPLATE
REACT_LAB_TEMPLATE
README_TEMPLATE
RUBY_LAB_TEMPLATE

Public Class Methods

new(filepath) click to toggle source
# File lib/learn-tool/learn-base.rb, line 9
def initialize(filepath)
  @filepath = filepath
  @old_repo_name = ''
  @new_repo_name = ''
end

Public Instance Methods

cd_into_and(filepath, command) click to toggle source
# File lib/learn-tool/learn-base.rb, line 22
def cd_into_and(filepath, command)
  cmd = "cd #{filepath} && #{command}"
  `#{cmd}`
end
create_new_repo() click to toggle source
# File lib/learn-tool/learn-base.rb, line 52
def create_new_repo
  # 'cd' doesn't work the way it would in the shell, must be used before every command
  puts 'Cloning old repository'
  git_clone
  # puts "Renaming old directory with new name: #{new_repo_name}"
  # rename_repo
  puts ''
  puts 'Creating new remote learn-co-curriculum repository'
  git_create_and_set_new_origin
  puts ''
  puts 'Setting new git remote based on SSH settings'
  git_set_remote
  puts ''
  puts 'Pushing all old-remote branches to new remote'
  git_push
end
end_message() click to toggle source
# File lib/learn-tool/learn-base.rb, line 69
def end_message
  puts ''
  puts 'To access local folder, change directory into ' + @new_repo_name + '/'
  puts "Repository available at #{GITHUB_ORG}" + @new_repo_name
end
get_new_name() click to toggle source
# File lib/learn-tool/learn-base.rb, line 45
def get_new_name
  puts 'Note: You must have write access to the learn-co-curriculum org on GitHub to use this tool'
  until name_new_repo do
    puts 'Careful - rate limiting can occur'
  end
end
name_new_repo() click to toggle source
# File lib/learn-tool/learn-base.rb, line 27
def name_new_repo
  puts 'What is the name of the repository you would like to create?'
  new_name = gets.strip.gsub(/\s+/, '-').downcase

  if name_length_is_good(new_name)
    if repo_is_available(new_name)
      @new_repo_name = new_name
    else
      puts 'A repository with that name already exists. Please try again.'
      return false
    end
  else
    puts 'Repository names must be shorter than 100 characters'
    return false
  end
  @new_repo_name
end
repo_is_available(repo_name) click to toggle source
# File lib/learn-tool/learn-base.rb, line 15
def repo_is_available(repo_name)
  url = GITHUB_ORG + repo_name
  encoded_url = URI.encode(url).slice(0, url.length)
  check_existing = Faraday.get URI.parse(encoded_url)
  check_existing.body.include? '"Not Found"'
end

Private Instance Methods

check_ssh_config() click to toggle source
# File lib/learn-tool/learn-base.rb, line 106
def check_ssh_config
  result = Open3.capture2e('ssh -T git@github.com').first
  result.include?("You've successfully authenticated")
end
git_clone() click to toggle source
# File lib/learn-tool/learn-base.rb, line 77
def git_clone
  cmd = "git clone https://github.com/learn-co-curriculum/#{@old_repo_name} #{@new_repo_name}"
  puts cmd
  `#{cmd}`
end
git_create_and_set_new_origin() click to toggle source
# File lib/learn-tool/learn-base.rb, line 83
def git_create_and_set_new_origin
  # Creates repo **and** assigns new remote to 'origin' shortname
  cd_into_and(@filepath + "/#{@new_repo_name}", "hub create learn-co-curriculum/#{@new_repo_name}")
end
git_push() click to toggle source
# File lib/learn-tool/learn-base.rb, line 93
def git_push
  # Copy `master`, attempt to copy `solution`, but if it's not there, no complaints
  cmds = [
    %q|git push origin 'refs/remotes/origin/master:refs/heads/master' > /dev/null 2>&1|,
    %q|git push origin 'refs/remotes/origin/solution:refs/heads/solution' > /dev/null 2>&1|
  ]
  cmds.each { |cmd| cd_into_and(@filepath + "/#{@new_repo_name}", cmd) }
end
git_set_remote() click to toggle source
# File lib/learn-tool/learn-base.rb, line 88
def git_set_remote
  remote = check_ssh_config ? "git@github.com:learn-co-curriculum/#{@new_repo_name}.git" : "https://github.com/learn-co-curriculum/#{new_repo_name}"
  cd_into_and(@filepath + "/#{@new_repo_name}", "git remote set-url origin #{remote}")
end
name_length_is_good(name) click to toggle source
# File lib/learn-tool/learn-base.rb, line 102
def name_length_is_good(name)
  name.length < 100
end