Class: Task

Inherits:
Object
  • Object
show all
Defined in:
lib/atcoder_tools/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contest, name) ⇒ Task

contest引数はcontestクラス



10
11
12
13
14
15
# File 'lib/atcoder_tools/task.rb', line 10

def initialize(contest, name)
  @contest = contest
  @name = name
  @tests = []
  @test_outputs = []
end

Instance Attribute Details

#contestObject (readonly)

Returns the value of attribute contest.



7
8
9
# File 'lib/atcoder_tools/task.rb', line 7

def contest
  @contest
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/atcoder_tools/task.rb', line 7

def name
  @name
end

Instance Method Details

#codeObject



146
147
148
# File 'lib/atcoder_tools/task.rb', line 146

def code
  File.read(task_file_path)
end

#commandObject



97
98
99
100
101
102
103
104
# File 'lib/atcoder_tools/task.rb', line 97

def command
  case contest.language
  when 'ruby'
    "ruby #{task_file_path}"
  when 'c++(gcc)'
    "#{compile_file_path}"
  end
end

#compileObject



89
90
91
92
93
94
95
# File 'lib/atcoder_tools/task.rb', line 89

def compile
  case contest.language
  when 'c++(gcc)'
    p 'hoge'
    system("c++ #{task_file_path} -o #{@contest.name}/#{@name}")
  end
end

#compile_file_pathObject



32
33
34
# File 'lib/atcoder_tools/task.rb', line 32

def compile_file_path
  "#{@contest.name}/#{@name}"
end

#create!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/atcoder_tools/task.rb', line 51

def create!
  html = URI.open(url) do |f|
    charset = f.charset
    f.read
  end

  doc = Nokogiri::HTML.parse(html, nil, 'utf8')
  samples = doc.css('.lang-ja > .part > section > pre').map { |e| e.children.text }
  inputs, outputs = samples.partition.with_index { |_sample, i| i.even? }

  erb = ERB.new(File.read(template_task_file_path))
  File.write(task_file_path, erb.result(binding))
end

#read_meta_dataObject



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
# File 'lib/atcoder_tools/task.rb', line 106

def 
  meta_flag = false
  test_no = nil
  tests = []
  mode = nil
  File.foreach(task_file_path) do |line|
    line.chomp!
    if line == "# METADATA::START" || line == "// METADATA::START"
      meta_flag = true
      next
    end
    if line == "# METADATA::FINISH" || line == "// METADATA::FINISH"
      meta_flag = false
      next
    end
    unless meta_flag
      next
    end
    if line =~ /(\#|\/\/)\stest-/
      test_no = line.scan(/(\#|\/\/)\stest-(\d+)/)[0][1].to_i
      tests.push({ no: test_no })
    end
    if line =~ /(\#|\/\/)\sinput:/
      test = tests.find{|test| test[:no] == test_no}
      input_str = line.scan(/(\#|\/\/)\sinput\:\s\"(.*)\"/)[0][1]
      inputs = input_str.chomp.split('\\n')
      test.merge!({ inputs: inputs })
    end
    if line =~ /(\#|\/\/)\soutput:/
      test = tests.find{|test| test[:no] == test_no}
      test.merge!({ output: line.scan(/(\#|\/\/)\soutput\:\s\"(.*)\"/)[0][1] })
    end
    if line =~ /(\#|\/\/)\sMODE:/
      mode = line.scan(/(\#|\/\/)\sMODE:\s(.+)/)[0][1]
    end
  end
  @tests = tests
  @mode = mode
end

#runObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/atcoder_tools/task.rb', line 65

def run
  
  puts("#{@contest.name}/#{@name} is running in #{@mode} MODE")
  update_spec
  if @mode == "DEBUG"
    compile
    io = IO.popen(command, "w+")
    @tests[0][:inputs].each do |input|
      io.puts(input)
    end
    io.close_write
    puts io.readlines
  end
  if @mode == "RUN"
    compile
    system("echo put your inputs")
    system(command)
  end
  if @mode == "TEST"
    compile
    system("bundle exec rspec #{test_file_path}")
  end
end

#task_file_pathObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/atcoder_tools/task.rb', line 21

def task_file_path
  extension = nil
  case @contest.language
  when 'ruby'
    extension = 'rb'
  when 'c++(gcc)'
    extension = 'cpp'
  end
  "#{@contest.name}/#{@name}.#{extension}"
end

#template_task_file_pathObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/atcoder_tools/task.rb', line 40

def template_task_file_path
  extension = nil
  case @contest.language
  when 'ruby'
    extension = 'rb'
  when 'c++(gcc)'
    extension = 'cpp'
  end
  File.dirname(__dir__)+"/atcoder_tools/sources/task.#{extension}.erb"
end

#test_file_pathObject



36
37
38
# File 'lib/atcoder_tools/task.rb', line 36

def test_file_path
  ".atcoder/#{@contest.name}/#{@name}/spec.rb"
end

#update_specObject



150
151
152
153
154
# File 'lib/atcoder_tools/task.rb', line 150

def update_spec
  FileUtils.mkdir_p ".atcoder/#{@contest.name}/#{@name}"
  erb = ERB.new(File.read(File.dirname(__dir__)+"/atcoder_tools/sources/spec.rb.erb"))
  File.write(test_file_path, erb.result(binding))
end

#urlObject



17
18
19
# File 'lib/atcoder_tools/task.rb', line 17

def url
  "https://atcoder.jp/contests/#{@contest.name}/tasks/#{@contest.name}_#{@name}"
end