Class: CommandLine::SubCommands::SubmitCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/git/contest/command_line/sub_commands/submit_command.rb

Instance Attribute Summary

Attributes inherited from Command

#args, #input_stream, #opt_parser, #options, #terminal, #tokens

Instance Method Summary collapse

Methods inherited from Command

#init

Constructor Details

#initialize(new_args, new_input_stream = STDIN) ⇒ SubmitCommand

Returns a new instance of SubmitCommand.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/git/contest/command_line/sub_commands/submit_command.rb', line 15

def initialize(new_args, new_input_stream = STDIN)
  super

  # load sites
  $config = get_config() || {}
  $sites  = {}
  if $config.has_key? 'sites'
    $sites = $config["sites"]
  end

  # load drivers
  Contest::Driver::Utils.load_plugins

  $drivers = {}
  load_drivers
end

Instance Method Details

#define_optionsObject



32
33
34
35
36
37
# File 'lib/git/contest/command_line/sub_commands/submit_command.rb', line 32

def define_options
  opt_parser.on "-h", "--help", "help" do
    usage
    exit 0
  end
end

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/git/contest/command_line/sub_commands/submit_command.rb', line 42

def run

  # check options
  sub_commands = $sites.keys

  # detect site
  unless has_next_token?
    usage
    exit 0
  end

  site = next_token.strip

  unless $sites.has_key?(site)
    if site != ""
      puts "site not found"
    else
      usage
    end
    exit 0
  end

  # detect driver
  driver_name = $sites[site]["driver"]

  unless $drivers.has_key?(driver_name)
    puts "driver not found"
    exit
  end

  #
  # Submit Start
  #
  driver = $drivers[driver_name].new(args)

  $submit_info = {}

  # set events
  driver.on(
    'start',
    Proc.new do
      puts "@start: submit"
    end
  )

  driver.on(
    'before_login',
    Proc.new do
      puts "@submit: logging in..."
    end
  )

  driver.on(
    'after_login',
    Proc.new do
      puts "@submit: login ok"
    end
  )

  driver.on(
    'before_submit',
    Proc.new do |submit_info|
      $submit_info = submit_info
      puts "@submit: doing..."
    end
  )

  driver.on(
    'after_submit',
    Proc.new do
      puts "@submit: done"
    end
  )

  driver.on(
    'before_wait',
    Proc.new do
      print "@result: waiting..."
    end
  )

  driver.on(
    'retry',
    Proc.new do
      print "."
    end
  )

  driver.on(
    'after_wait',
    Proc.new do |submission_info|
      puts ""
      next unless submission_info.is_a?(Hash)
      puts ""
      puts "@result: Submission Result"
      puts "  %s: %s" % ["submission id", "#{submission_info[:submission_id]}"]
      puts "  %s: %s" % ["status", "#{submission_info[:status]}"]
      puts ""
      if Git.contest_is_initialized
        puts "@commit"
        Git.do "add #{get_git_add_target($config["submit_rules"]["add"] || ".")}"
        Git.do "commit --allow-empty -m \"#{submission_info[:result]}\""
      end
    end
  )

  driver.on(
    'finish',
    Proc.new do
      puts "@finish"
    end
  )

  # global config
  $config["submit_rules"] ||= {}
  $config["file"] ||= {}

  # set config
  driver.config = $sites[site]
  driver.config.merge! $config

  # parse driver options
  driver.options = driver.get_opts()

  result = driver.submit()

end

#set_default_optionsObject



39
40
# File 'lib/git/contest/command_line/sub_commands/submit_command.rb', line 39

def set_default_options
end