Class: Rpremote::DfuCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/rpremote/dfu_compiler.rb,
sig/rpremote.rbs

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.compile(source, target:, destination: nil, output: $stdout, runner: nil) ⇒ String

Parameters:

  • source (String)
  • target: (Target)
  • destination: (String, nil) (defaults to: nil)
  • output: (IO) (defaults to: $stdout)
  • runner: (Object) (defaults to: nil)

Returns:

  • (String)

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rpremote/dfu_compiler.rb', line 9

def self.compile(source, target:, destination: nil, output: $stdout, runner: nil)
  raise Error, "DFU bytecode compilation currently supports only picoruby" unless target.language == "picoruby"
  raise ArgumentError, "DFU source must have a .rb extension" unless File.extname(source).downcase == ".rb"

  source = File.expand_path(source)
  raise ArgumentError, "DFU source file not found: #{source}" unless File.file?(source)

  compiler = find_compiler(target.source_dir)
  destination ||= source.sub(/\.rb\z/i, ".mrb")
  destination = File.expand_path(destination)
  FileUtils.mkdir_p(File.dirname(destination))
  success = (runner || method(:run)).call(compiler, "-o", destination, source)
  raise Error, "PicoRuby compiler failed: #{compiler}" unless success

  rite_version = rite_version(File.binread(destination, 8))
  output.puts("compiled #{rite_version} app: #{source} -> #{destination}")
  destination
end