Class: MilkTea::BindgenCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/bindings/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, out:, err:, help_printer:) ⇒ BindgenCLI

Returns a new instance of BindgenCLI.



9
10
11
12
13
14
# File 'lib/milk_tea/bindings/cli.rb', line 9

def initialize(argv, out:, err:, help_printer:)
  @argv = argv.dup
  @out = out
  @err = err
  @help_printer = help_printer
end

Class Method Details

.start(argv = ARGV, out:, err:, help_printer:) ⇒ Object



5
6
7
# File 'lib/milk_tea/bindings/cli.rb', line 5

def self.start(argv = ARGV, out:, err:, help_printer:)
  new(argv, out:, err:, help_printer:).start
end

Instance Method Details

#startObject



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
51
52
53
# File 'lib/milk_tea/bindings/cli.rb', line 16

def start
  module_name = @argv.shift
  header_path = @argv.shift
  unless module_name && header_path
    @err.puts("missing module name or header path")
    print_help
    return 1
  end

  require_relative "../bindings"

  options = parse_options
  return 1 unless options

  output_path = options.delete(:output_path)
  nullable_report_path = options.delete(:nullable_report_path)
  if nullable_report_path
    require "json"
    result = Bindgen.generate_with_report(module_name:, header_path:, **options)
    source = result.fetch(:source)
    report_path = File.expand_path(nullable_report_path)
    FileUtils.mkdir_p(File.dirname(report_path))
    File.write(report_path, JSON.pretty_generate(result.fetch(:nullable_policy_report)))
  else
    source = Bindgen.generate(module_name:, header_path:, **options)
  end

  if output_path
    FileUtils.mkdir_p(File.dirname(File.expand_path(output_path)))
    File.write(output_path, source)
    @out.puts("generated #{header_path} -> #{output_path}")
    @out.puts("nullable report #{header_path} -> #{report_path}") if nullable_report_path
  else
    @out.write(source)
    @err.puts("wrote nullable report #{report_path}") if nullable_report_path
  end
  0
end