Class: NewlineHw::Shell::Setup
- Inherits:
-
Object
- Object
- NewlineHw::Shell::Setup
- Defined in:
- lib/newline_hw/shell/setup.rb
Overview
Generate a series of language AGNOISIC commands to download and organize a students homework.
Constant Summary collapse
- BRANCH_NAME =
"submitted_assignment".freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#newline_submission_id ⇒ Object
Returns the value of attribute newline_submission_id.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #branch? ⇒ Boolean
- #clean_dir ⇒ Object
- #cloneable? ⇒ Boolean
- #cmd ⇒ Object
- #dir_name ⇒ Object
- #gist? ⇒ Boolean
- #git? ⇒ Boolean
- #git_url ⇒ Object
- #github_project_link? ⇒ Boolean
- #homework_path ⇒ Object
-
#initialize(id_or_url, config) ⇒ Setup
constructor
A new instance of Setup.
- #pr? ⇒ Boolean
- #setup ⇒ Object
- #sha ⇒ Object
- #submission_info ⇒ Object
- #submitted_branch_name ⇒ Object
Constructor Details
#initialize(id_or_url, config) ⇒ Setup
Returns a new instance of Setup.
13 14 15 16 17 18 19 20 |
# 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 |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
12 13 14 |
# File 'lib/newline_hw/shell/setup.rb', line 12 def config @config end |
#newline_submission_id ⇒ Object
Returns the value of attribute newline_submission_id.
12 13 14 |
# File 'lib/newline_hw/shell/setup.rb', line 12 def newline_submission_id @newline_submission_id end |
#url ⇒ Object
Returns the value of attribute url.
12 13 14 |
# File 'lib/newline_hw/shell/setup.rb', line 12 def url @url end |
Instance Method Details
#branch? ⇒ Boolean
58 59 60 |
# File 'lib/newline_hw/shell/setup.rb', line 58 def branch? %r{\/\/github.com}.match(url) && %r{\/tree\/([^\\]+)}.match(url) end |
#clean_dir ⇒ Object
66 67 68 |
# File 'lib/newline_hw/shell/setup.rb', line 66 def clean_dir FileUtils.rm_rf(File.join(homework_path, dir_name)) end |
#cloneable? ⇒ Boolean
26 27 28 |
# File 'lib/newline_hw/shell/setup.rb', line 26 def cloneable? pr? || gist? || branch? || github_project_link? || git? || false end |
#cmd ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# 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 ⇒ Object
62 63 64 |
# File 'lib/newline_hw/shell/setup.rb', line 62 def dir_name git_url.split(%r{[\/\.]})[-3..-2].join("-") end |
#gist? ⇒ Boolean
50 51 52 |
# File 'lib/newline_hw/shell/setup.rb', line 50 def gist? /gist\.github\.com/.match(url) end |
#git? ⇒ Boolean
46 47 48 |
# File 'lib/newline_hw/shell/setup.rb', line 46 def git? url.starts_with?("git") || url.ends_with?(".git") end |
#git_url ⇒ Object
36 37 38 39 40 |
# 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? ⇒ Boolean
42 43 44 |
# 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 ⇒ Object
70 71 72 |
# File 'lib/newline_hw/shell/setup.rb', line 70 def homework_path File.(config.homework_dir) end |
#pr? ⇒ Boolean
54 55 56 |
# File 'lib/newline_hw/shell/setup.rb', line 54 def pr? %r{\/\/github.com}.match(url) && %r{\/pull\/\d+}.match(url) end |
#setup ⇒ Object
74 75 76 |
# File 'lib/newline_hw/shell/setup.rb', line 74 def setup FileUtils.mkdir_p homework_path end |
#sha ⇒ Object
30 31 32 33 34 |
# 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 ⇒ Object
22 23 24 |
# File 'lib/newline_hw/shell/setup.rb', line 22 def submission_info @_submission_info ||= query_submission_info end |
#submitted_branch_name ⇒ Object
96 97 98 |
# File 'lib/newline_hw/shell/setup.rb', line 96 def submitted_branch_name branch?[1] end |