Module: CVGen::Exec

Defined in:
lib/cvgen/exec.rb

Class Method Summary collapse

Class Method Details

.callObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cvgen/exec.rb', line 8

def self.call
  config = parse_command_line_args
  watch = config.delete('watch')

  input_file, output_file = ARGV[0], ARGV[1]

  create_output(input_file, output_file, config)

  if watch
    trap(:INT) { exit }

    require 'listen'
    Listen.to('.', :only => Regexp.new(input_file)) do |modified, added, removed|
      #assume the only possibility is modification of source file
      create_output(input_file, output_file, config)
    end.start

    puts 'Listening for changes, Ctrl-C to exit'
    sleep
  end
end

.create_output(input_file, output_file, config) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cvgen/exec.rb', line 30

def self.create_output(input_file, output_file, config)
  input = File.read(input_file)
  merge_options(config, YAML.load_file('config.yml')) if File.exist? 'config.yml'

  if input.start_with? "---\n"
    input.slice!(0..3)
    front, input = input.split /^(?:\.{3}|-{3})\s*$/, 2

    front = YAML.load(front)

    merge_options(config, front)
  end

  pdf_data = CVGen.generate input, config
  IO.binwrite(output_file, pdf_data)
end

.merge_options(into, from) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/cvgen/exec.rb', line 47

def self.merge_options(into, from)
    if into[FEATURES_OPTION] && from[FEATURES_OPTION]
      into[FEATURES_OPTION].concat(from.delete(FEATURES_OPTION))
    end

    into.merge!(from)
end

.parse_command_line_argsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cvgen/exec.rb', line 55

def self.parse_command_line_args
  config = {}

  OptionParser.new do |opts|
    opts.on("-e", "--enable FEATURES", Array, "List of features to enable") do |features|
      config[FEATURES_OPTION] = features
    end

    opts.on("-w", "--watch", "Watch for changes and automatically regenerate pdf") do
      config['watch'] = true
    end

    opts.on("-d", "--debug", "Print result of Erb preprocessing and exit") do
      config['debug_erb'] = true
    end
  end.parse!

  config
end