Class: QuestionCompiler::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/question_compiler/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(args, dev = false) ⇒ Command

Returns a new instance of Command.



10
11
12
13
# File 'lib/question_compiler/command.rb', line 10

def initialize(args, dev = false)
  @args = args
  @dev = dev
end

Instance Method Details

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/question_compiler/command.rb', line 15

def run
  opts = Slop.parse(@args) do |o|
    o.bool '-z', '--zip', 'Builds zip files for all questions and contexts. Invoke from root folder.'
    o.array '-c', '--compile', 'Builds the given questions Invoke from root folder. Usage: qc -c user1/qst1 user1/qst2 user2/qst3'
    o.bool '-s', '--server', 'Starts a development webserver. Invoke from context or question folder.'
    o.integer '-p', '--port', 'Webserver port.', default: 3000
    o.on '-v', '--version', 'Prints the gem version.'
    o.bool '-h', '--help', 'Prints this message.'
  end
  
  if opts.help?
    puts opts
  elsif opts.version?
    puts QuestionCompiler::VERSION
  elsif opts.compile?
    log = Logger.new(STDOUT)
    log.level = Logger::DEBUG if @dev
    opts[:compile].each do |question_path|
      username, _ = question_path.split('/')
      FileUtils.mkdir_p File.join('compiled', question_path)
      FileUtils.cp_r File.join('questions', question_path), File.join('compiled', username)
      
      QuestionCompiler::Compiler.new(log).compile(
        File.read(File.join('questions', question_path, 'question.html')),
        QuestionCompiler::LocalContextReader.new('contexts'),
        QuestionCompiler::LocalQuestionWriter.new('compiled', question_path)
      )
    end
  elsif opts.zip?
    QuestionCompiler::Zipper.new.zip_all Dir.getwd
  elsif opts.server?
    QuestionCompiler::Server.start opts[:port]
  else
    puts opts
  end
end