Class: Rpremote::Builder

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

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(root: Dir.pwd, runner: nil, mrbgems_class: Mrbgems, source_patcher: nil) ⇒ Builder

Returns a new instance of Builder.

Parameters:

  • root: (String) (defaults to: Dir.pwd)
  • runner: (Object) (defaults to: nil)
  • mrbgems_class: (Object) (defaults to: Mrbgems)
  • source_patcher: (Object) (defaults to: nil)


13
14
15
16
17
18
# File 'lib/rpremote/builder.rb', line 13

def initialize(root: Dir.pwd, runner: nil, mrbgems_class: Mrbgems, source_patcher: nil)
  @root = File.expand_path(root)
  @runner = runner || method(:run_command)
  @mrbgems_class = mrbgems_class
  @source_patcher = source_patcher || method(:patch_source)
end

Instance Method Details

#build(output: $stdout, error: $stderr, **options) ⇒ Object

Raises:



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
# File 'lib/rpremote/builder.rb', line 20

def build(output: $stdout, error: $stderr, **options)
  target = Target.new(**options.slice(:language, :language_version, :board, :cache_dir, :firmware))
  script = File.expand_path("../../tasks/firmware_build.rb", __dir__)
  raise Error, "rpremote installation has no firmware build script: #{script}" unless File.file?(script)

  Language.validate!(target.language)

  source_dir = source_for(target)
  source_patcher.call(source_dir, target.language_version)
  environment = {
    "RPREMOTE_LANGUAGE" => target.language,
    "RPREMOTE_LANGUAGE_VERSION" => target.language_version,
    "RPREMOTE_BOARD" => target.board,
    "RPREMOTE_ROOT" => root,
    "PICORUBY_DIR" => source_dir
  }
  build_options = options.merge(
    language: target.language,
    language_version: target.language_version,
    board: target.board
  )
  add_mrbgems_environment!(environment, build_options, source_dir, output)
  environment["RPREMOTE_FIRMWARE"] = target.firmware_path(root: root)
  success = runner.call(
    environment,
    RbConfig.ruby,
    script,
    chdir: root,
    out: output,
    err: error
  )
  raise Error, "custom firmware build failed" unless success
end

#clean(output: $stdout) ⇒ void

This method returns an undefined value.

Parameters:

  • output: (IO) (defaults to: $stdout)


54
55
56
57
58
59
60
61
62
63
# File 'lib/rpremote/builder.rb', line 54

def clean(output: $stdout)
  directory = File.join(root, "build")
  unless File.directory?(directory)
    output.puts("no build files: #{directory}")
    return
  end

  FileUtils.rm_rf(directory)
  output.puts("removed build files: #{directory}")
end