Module: IgniterLang::CLI

Defined in:
lib/igniter_lang/cli.rb

Constant Summary collapse

USAGE =
"Usage: igc compile SOURCE --out OUT.igapp " \
"[--compiler-profile-source PATH.json]"

Class Method Summary collapse

Class Method Details

.load_profile_source(path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/igniter_lang/cli.rb', line 62

def load_profile_source(path)
  raise ArgumentError, "compiler profile source path not found" unless path.exist?
  raise ArgumentError, "compiler profile source path must be a regular file" unless path.file?

  parsed = JSON.parse(path.read)
  raise ArgumentError, "compiler profile source JSON must be an object" unless parsed.is_a?(Hash)

  parsed
rescue Errno::EACCES
  raise ArgumentError, "compiler profile source path is not readable"
rescue JSON::ParserError
  raise ArgumentError, "compiler profile source file must contain valid JSON"
end

.parse_compile_args(argv) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/igniter_lang/cli.rb', line 36

def parse_compile_args(argv)
  source = argv.shift
  raise ArgumentError, USAGE unless source

  out_flag = argv.shift
  out = argv.shift
  raise ArgumentError, USAGE unless out_flag == "--out" && out

  profile_source_path = nil
  until argv.empty?
    flag = argv.shift
    case flag
    when "--compiler-profile-source"
      path = argv.shift
      raise ArgumentError, "--compiler-profile-source requires PATH.json" unless path
      raise ArgumentError, "unsupported argument for igc compile" if profile_source_path

      profile_source_path = Pathname.new(path)
    else
      raise ArgumentError, "unsupported argument for igc compile"
    end
  end

  [Pathname.new(source), Pathname.new(out), profile_source_path]
end

.run(argv) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/igniter_lang/cli.rb', line 15

def run(argv)
  command = argv.shift
  unless command == "compile"
    warn USAGE
    return false
  end

  source_path, out_path, profile_source_path = parse_compile_args(argv)
  compiler_profile_source = load_profile_source(profile_source_path) if profile_source_path
  orchestration = IgniterLang.compile(
    source_path: source_path,
    out_path: out_path,
    compiler_profile_source: compiler_profile_source
  )
  puts JSON.pretty_generate(CompilerResult.public_result(orchestration.fetch("result")))
  orchestration.fetch("status") == "ok"
rescue ArgumentError => e
  warn e.message
  false
end