Class: NewlineHw::Shell::Setup

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/newline_hw/shell/setup.rb', line 12

def config
  @config
end

#newline_submission_idObject

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

#urlObject

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

Returns:

  • (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_dirObject



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

Returns:

  • (Boolean)


26
27
28
# File 'lib/newline_hw/shell/setup.rb', line 26

def cloneable?
  pr? || gist? || branch? || github_project_link? || git? || false
end

#cmdObject



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_nameObject



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

Returns:

  • (Boolean)


50
51
52
# File 'lib/newline_hw/shell/setup.rb', line 50

def gist?
  /gist\.github\.com/.match(url)
end

#git?Boolean

Returns:

  • (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_urlObject



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

Returns:

  • (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_pathObject



70
71
72
# File 'lib/newline_hw/shell/setup.rb', line 70

def homework_path
  File.expand_path(config.homework_dir)
end

#pr?Boolean

Returns:

  • (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

#setupObject



74
75
76
# File 'lib/newline_hw/shell/setup.rb', line 74

def setup
  FileUtils.mkdir_p homework_path
end

#shaObject



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_infoObject



22
23
24
# File 'lib/newline_hw/shell/setup.rb', line 22

def submission_info
  @_submission_info ||= query_submission_info
end

#submitted_branch_nameObject



96
97
98
# File 'lib/newline_hw/shell/setup.rb', line 96

def 
  branch?[1]
end