Class: CGen::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/cgen/compiler.rb

Instance Method Summary collapse

Constructor Details

#initialize(tex_out_pth) ⇒ Compiler

Returns a new instance of Compiler.



3
4
5
6
7
# File 'lib/cgen/compiler.rb', line 3

def initialize(tex_out_pth)
  @tex_out_pth = tex_out_pth
  @generator_regex = /^generate\s*\(\s*(?<generator>[^,()]+)\s*,\s*(?<param>[^,()]+)\s*\)\s*$/i
  @line_regex = /(?<re>\<=(?:(?<ctnt>(?>[^<>=]+)|\g<re>)+)\=\>)/
end

Instance Method Details

#compile(data, template_pth, lang) ⇒ Object



32
33
34
35
36
37
38
39
40
41
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
# File 'lib/cgen/compiler.rb', line 32

def compile(data, template_pth, lang)
  puts '>> Compiling the language    '.cyan + ":#{lang}".light_black
  puts '   against the template      '.cyan + template_pth.to_s.light_black
  puts '   into the output directory '.cyan + @tex_out_pth.to_s.light_black

  Dir.glob(template_pth.join('**').join('*')) do |file_pth|
    if File.file?(file_pth)
      rel_file_pth = file_pth.to_s.gsub(/#{template_pth}[\/]?/, '')
      out_file_pth = @tex_out_pth.join(lang.to_s).join(rel_file_pth)

      if File.extname(file_pth) == '.tex'
        out_lines = []

        File.open(file_pth, 'r').each do |line|
          out_lines << line.gsub(@line_regex) do |_|
            req_str = $2.strip.dup

            md = @generator_regex.match(req_str)
            if md
              # Generate command
              handle_generate(md[:generator].to_sym, md[:param], data, lang)
            else
              # Macro substitution
              handle_generate(:macro_substitution, req_str, data, lang)
            end
          end
        end

        FileUtils.mkdir_p(out_file_pth.dirname)
        File.open(out_file_pth, 'w') do |tex_out_file|
          tex_out_file.write(out_lines.join(''))
        end
      else
        FileUtils.mkdir_p(out_file_pth.dirname)
        FileUtils.cp_r(file_pth, out_file_pth)
      end
    end
  end
end

#handle_generate(generator, param, data, lang) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cgen/compiler.rb', line 72

def handle_generate(generator, param, data, lang)
  generators = {
      # Generic
      cvitem: CGen::Generator::CvItem,
      cventry: CGen::Generator::CvEntry,
      cvitemwithcomment: CGen::Generator::CvItemWithComment,
      cvdoubleitem: CGen::Generator::CvDoubleItem,
      cvlistitem: CGen::Generator::CvListItem,
      cvlistdoubleitem: CGen::Generator::CvListDoubleItem,
      cvcolumn: CGen::Generator::CvColumn,
      list: CGen::Generator::List,
      # Specific
      work_experience: CGen::Generator::WorkExperience,
      education: CGen::Generator::Education,
      self_assessment: CGen::Generator::SelfAssessment,
      # Macro
      macro_substitution: CGen::Generator::MacroSubstitution
  }

  generators.has_key?(generator) ?
      generators[generator].new(param, data, lang).generate :
      raise("Invalid generator: #{generator}. Expected one of: #{generators}")
end

#validate_deps(deps_file_pth) ⇒ Object



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

def validate_deps(deps_file_pth)
  puts '>> Ensuring that the dependencies are satisfied'

  # ==> Load the data from the YAML file
  data = {}
  File.open(deps_file_pth.to_s, 'r') { |deps_file| data.merge!(YAML::load(deps_file)) }

  # ==> Validate commands are available on the system
  data.has_key?('cmds') && data['cmds'].respond_to?(:all?) && data['cmds'].all? do |cmd|
    CGen::Util::ShellCommand.exist?(cmd) ? true : puts(">> Command #{cmd} not found".red)
  end

  # ==> Validate that the required fonts are available
  data.has_key?('pkgs') && data['pkgs'].respond_to?(:all?) && data['pkgs'].all? do |pkg|
    `tlmgr list --only-installed | grep "i #{pkg}:"`.strip.length > 0 ? true : puts(">> Package #{pkg} not found".red)
  end

  # Outputs the manual dependencies
  data.has_key?('manual_deps') && data['manual_deps'].respond_to?(:each) do |manual_dep|
    puts '>> Please ensure that the manual dependency is installed:'.yellow + manual_dep.to_s.light_black
  end
end