class NewlineHw::Shell::Setup
Generate a series of language AGNOISIC commands to download and organize a students homework.
Constants
- BRANCH_NAME
Attributes
config[RW]
newline_submission_id[RW]
url[RW]
Public Class Methods
new(id_or_url, config)
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 13 def initialize(id_or_url, config) @config = config @newline_submission_id = Integer(id_or_url) @url = submission_info["url"] rescue ArgumentError @config = config @url = id_or_url end
Public Instance Methods
branch?()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 58 def branch? %r{\/\/github.com}.match(url) && %r{\/tree\/([^\\]+)}.match(url) end
clean_dir()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 66 def clean_dir FileUtils.rm_rf(File.join(homework_path, dir_name)) end
cloneable?()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 26 def cloneable? pr? || gist? || branch? || github_project_link? || git? || false end
cmd()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 78 def cmd setup clean_dir cmds = [] cmds << "cd #{homework_path}" cmds << "git clone #{git_url} #{dir_name}" cmds << "cd #{dir_name}" cmds << fetch_and_checkout_pr if pr? cmds << fetch_and_checkout_branch if branch? cmds << "git checkout -b #{BRANCH_NAME} #{sha}" if sha cmds << "echo #{Shellwords.escape JSON.pretty_generate submission_info} > .newline_submission_meta.json" if newline_submission_id cmds.join(" && ") end
dir_name()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 62 def dir_name git_url.split(%r{[\/\.]})[-3..-2].join("-") end
gist?()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 50 def gist? /gist\.github\.com/.match(url) end
git?()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 46 def git? url.starts_with?("git") || url.ends_with?(".git") end
git_url()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 36 def git_url return infer_git_url_from_pr if pr? final_url = url.split("/tree/").first "#{final_url}#{'.git' unless final_url.end_with?('.git')}" end
github_project_link?()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 42 def github_project_link? url.starts_with?("https://github.com") && URI(url).path.split("/").reject(&:empty?).size == 2 end
homework_path()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 70 def homework_path File.expand_path(config.homework_dir) end
pr?()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 54 def pr? %r{\/\/github.com}.match(url) && %r{\/pull\/\d+}.match(url) end
setup()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 74 def setup FileUtils.mkdir_p homework_path end
sha()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 30 def sha return submission_info["sha"] if @newline_submission_id matches = /\b[0-9a-f]{40}\b/.match(url) matches.to_s if matches && !gist? end
submission_info()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 22 def submission_info @_submission_info ||= query_submission_info end
submitted_branch_name()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 96 def submitted_branch_name branch?[1] end
Private Instance Methods
fetch_and_checkout_branch()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 100 def fetch_and_checkout_branch "git fetch origin #{submitted_branch_name}:#{BRANCH_NAME} && git checkout #{BRANCH_NAME}" end
fetch_and_checkout_pr()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 92 def fetch_and_checkout_pr "git fetch origin pull/#{pr_id}/head:#{BRANCH_NAME} && git checkout #{BRANCH_NAME}" end
infer_git_url_from_pr()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 104 def infer_git_url_from_pr url.split("/pull/").first + ".git" end
pr_id()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 108 def pr_id url.split("/pull/").last.to_i end
query_submission_info()
click to toggle source
# File lib/newline_hw/shell/setup.rb, line 112 def query_submission_info NewlineHw::Api.new.get("assignment_submissions/#{@newline_submission_id}") end