Module: Pro2

Defined in:
lib/pro2-runner.rb

Class Method Summary collapse

Class Method Details

.command_build(filter = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pro2-runner.rb', line 75

def command_build(filter = nil)
  javajava = parse_project!
  javajava = filter_javajava(javajava, filter) if filter
  javajava.each do |java|
    filename = java.fetch('filename')
    build_cmd = java.fetch('build')

    puts "Build #{filename} ..."
    puts %x(#{build_cmd})
    puts ""
  end
end

.command_execute(filter = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pro2-runner.rb', line 88

def command_execute(filter = nil)
  javajava = parse_project!
  javajava = filter_javajava(javajava, filter) if filter
  javajava.each do |java|
    filename = java.fetch('filename')
    run_cmd = java.fetch('run')

    puts "Execute #{filename} ..."
    puts %x(#{run_cmd})
    puts ""
  end
end

.command_init(prefix, num_file) ⇒ Object

Int -> Int -> (project.yaml)



67
68
69
70
71
72
73
# File 'lib/pro2-runner.rb', line 67

def command_init(prefix, num_file)
  project_file = 'project.yaml'

  javajava = initialize_project(prefix, num_file)
  file_write!(project_file, javajava.to_yaml)
  generate_empty_javajava(javajava)
end

.command_run(filter = nil) ⇒ Object



101
102
103
104
# File 'lib/pro2-runner.rb', line 101

def command_run(filter = nil)
  command_build(filter)
  command_execute(filter)
end

.file_write!(filename, content) ⇒ Object

filename
String -> content

String -> (File write)



61
62
63
64
# File 'lib/pro2-runner.rb', line 61

def file_write!(filename, content)
  raise "#{filename} already exist!" if File.exists? filename
  File.write(filename, content)
end

.filter_javajava(javajava, filter) ⇒ Object

javajava -> filter -> javajava



36
37
38
# File 'lib/pro2-runner.rb', line 36

def filter_javajava(javajava, filter)
  javajava.select {|java| java.fetch('filename') == filter}
end

.generate_empty_javajava(javajava) ⇒ Object

javajava -> (Java files)



53
54
55
56
57
58
# File 'lib/pro2-runner.rb', line 53

def generate_empty_javajava(javajava)
  javajava.each do |java|
    filename = java.fetch('filename')
    file_write!(filename, generate_java(filename))
  end
end

.generate_java(filename) ⇒ Object

String -> String



41
42
43
44
45
46
47
48
49
50
# File 'lib/pro2-runner.rb', line 41

def generate_java(filename)
  class_name = File.basename(filename, '.java')

  content =
%(public class #{class_name} {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
})
end

.initialize_project(prefix, num_file) ⇒ Object

prefix: X for ProbXY.java num_file: Y for ProbXY.java Int -> Int -> javajava



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pro2-runner.rb', line 14

def initialize_project(prefix, num_file)
  javajava = []
  1.upto(num_file) do |i|
    filename = "Prob#{prefix}#{i}"
    javajava.push({
      'filename' => "#{filename}.java",
      'build' => "javac #{filename}.java",
      'run' => "java #{filename}",
    })
  end
  javajava
end

.parse_project!Object

javajava



28
29
30
31
32
33
# File 'lib/pro2-runner.rb', line 28

def parse_project!
  project_file = 'project.yaml'
  raise "#{project_file} not found!! Run `pro2 init`" unless File.exists? project_file
  yaml = File.read(project_file)
  YAML.load yaml
end