Class: AtCoderFriends::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/at_coder_friends/cli.rb

Overview

command line interface

Constant Summary collapse

EXITING_OPTIONS =
i[version].freeze
OPTION_BANNER =
"Usage:\n  at_coder_friends setup        path/contest       # setup contest folder\n  at_coder_friends test-one     path/contest/src   # run 1st test case\n  at_coder_friends test-all     path/contest/src   # run all test cases\n  at_coder_friends submit       path/contest/src   # submit source code\n  at_coder_friends check-and-go path/contest/src   # submit source if all tests passed\n  at_coder_friends open-contest path/contest/src   # open contest page\nOptions:\n"
STATUS_SUCCESS =
0
STATUS_ERROR =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



24
25
26
# File 'lib/at_coder_friends/cli.rb', line 24

def ctx
  @ctx
end

Instance Method Details

#check_and_goObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/at_coder_friends/cli.rb', line 123

def check_and_go
  vf = ctx.verifier
  if ctx.sample_test_runner.test_all
    # submit automatically
    ctx.scraping_agent.submit
    vf.unverify
    open_submission_list
  else
    # enable manual submit
    vf.verify
  end
end

#exec_command(command, path, *args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/at_coder_friends/cli.rb', line 68

def exec_command(command, path, *args)
  @ctx = Context.new(@options, path)
  case command
  when 'setup'
    setup
  when 'test-one'
    test_one(*args)
  when 'test-all'
    test_all
  when 'submit'
    submit
  when 'check-and-go'
    check_and_go
  when 'judge-one'
    judge_one(*args)
  when 'judge-all'
    judge_all
  when 'open-contest'
    open_contest
  else
    raise ParamError, "unknown command: #{command}"
  end
  ctx.post_process
end

#handle_exiting_optionObject



61
62
63
64
65
66
# File 'lib/at_coder_friends/cli.rb', line 61

def handle_exiting_option
  return unless EXITING_OPTIONS.any? { |o| @options.key? o }

  puts AtCoderFriends::VERSION if @options[:version]
  exit STATUS_SUCCESS
end

#judge_allObject



140
141
142
# File 'lib/at_coder_friends/cli.rb', line 140

def judge_all
  ctx.judge_test_runner.judge_all
end

#judge_one(id = '') ⇒ Object



136
137
138
# File 'lib/at_coder_friends/cli.rb', line 136

def judge_one(id = '')
  ctx.judge_test_runner.judge_one(id)
end

#open_contestObject



144
145
146
# File 'lib/at_coder_friends/cli.rb', line 144

def open_contest
  Launchy.open(ctx.scraping_agent.contest_url)
end

#open_submission_listObject



148
149
150
151
152
# File 'lib/at_coder_friends/cli.rb', line 148

def open_submission_list
  url = ctx.scraping_agent.contest_url('submissions/me')
  puts "submission status : #{url}"
  Launchy.open(url)
end

#parse_options!(args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/at_coder_friends/cli.rb', line 44

def parse_options!(args)
  op = OptionParser.new do |opts|
    opts.banner = OPTION_BANNER
    opts.on('-v', '--version', 'Display version.') do
      @options[:version] = true
    end
    opts.on('-d', '--debug', 'Display debug info.') do
      @options[:debug] = true
    end
  end
  @usage = op.to_s
  @options = {}
  op.parse!(args)
rescue OptionParser::InvalidOption => e
  raise ParamError, e.message
end

#run(args = ARGV) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/at_coder_friends/cli.rb', line 26

def run(args = ARGV)
  parse_options!(args)
  handle_exiting_option
  raise ParamError, 'command or path is not specified.' if args.size < 2

  exec_command(*args)
  STATUS_SUCCESS
rescue AtCoderFriends::ParamError => e
  warn @usage
  warn "error: #{e.message}"
  STATUS_ERROR
rescue AtCoderFriends::AppError => e
  warn e.message
  STATUS_ERROR
rescue SystemExit => e
  e.status
end

#setupObject

Raises:



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/at_coder_friends/cli.rb', line 93

def setup
  path = ctx.path
  raise AppError, "#{path} is not empty." \
    if Dir.exist?(path) && !Dir["#{path}/*"].empty?

  ctx.scraping_agent.fetch_all do |pbm|
    Parser::Main.process(pbm)
    ctx.generator.process(pbm)
    ctx.emitter.emit(pbm)
  end
end

#submitObject

Raises:



114
115
116
117
118
119
120
121
# File 'lib/at_coder_friends/cli.rb', line 114

def submit
  vf = ctx.verifier
  raise AppError, "#{vf.file} has not been tested." unless vf.verified?

  ctx.scraping_agent.submit
  vf.unverify
  open_submission_list
end

#test_allObject



109
110
111
112
# File 'lib/at_coder_friends/cli.rb', line 109

def test_all
  ctx.sample_test_runner.test_all
  ctx.verifier.verify
end

#test_one(id = '001') ⇒ Object



105
106
107
# File 'lib/at_coder_friends/cli.rb', line 105

def test_one(id = '001')
  ctx.sample_test_runner.test_one(id)
end