Class: Rpremote::Mrbgems

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

Defined Under Namespace

Classes: Definition, DefinitionError, Dependency, Error, LockError, Overlay

Constant Summary collapse

DEFAULT_PATH =

Returns:

  • (String)
"Mrbgems"
DEFAULT_LOCK_PATH =

Returns:

  • (String)
"Mrbgems.lock"
LOCK_VERSION =

Returns:

  • (Integer)
1
GITHUB_PATTERN =
%r{\A[\w.-]+/[\w.-]+\z}
COMMIT_PATTERN =
/\A[0-9a-f]{40,64}\z/i
VMS =
%i[mruby mrubyc].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: DEFAULT_PATH, lock_path: nil, cwd: Dir.pwd, resolver: nil) ⇒ Mrbgems

Returns a new instance of Mrbgems.



26
27
28
29
30
# File 'lib/rpremote/mrbgems.rb', line 26

def initialize(path: DEFAULT_PATH, lock_path: nil, cwd: Dir.pwd, resolver: nil)
  @path = File.expand_path(path, cwd)
  @lock_path = File.expand_path(lock_path || DEFAULT_LOCK_PATH, File.dirname(@path))
  @resolver = resolver || method(:resolve_github)
end

Instance Attribute Details

#lock_pathString (readonly)

Returns the value of attribute lock_path.

Returns:

  • (String)


24
25
26
# File 'lib/rpremote/mrbgems.rb', line 24

def lock_path
  @lock_path
end

#pathString (readonly)

Returns the value of attribute path.

Returns:

  • (String)


24
25
26
# File 'lib/rpremote/mrbgems.rb', line 24

def path
  @path
end

Instance Method Details

#checkArray[Dependency]

Returns:



45
46
47
48
# File 'lib/rpremote/mrbgems.rb', line 45

def check
  dependencies.each { |dependency| validate_local!(dependency) if dependency.type == :path }
  dependencies
end

#dependenciesArray[Dependency]

Returns:



36
37
38
# File 'lib/rpremote/mrbgems.rb', line 36

def dependencies
  @dependencies ||= load_definition
end

#exist?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rpremote/mrbgems.rb', line 32

def exist?
  File.file?(path)
end

#generate_overlay(base_config:, target:, directory:, update: false) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/rpremote/mrbgems.rb', line 71

def generate_overlay(base_config:, target:, directory:, update: false)
  lock_data = lock(update: update)
  fingerprint = build_fingerprint(base_config, lock_data)
  output_dir = File.join(File.expand_path(directory), fingerprint)
  output_path = File.join(output_dir, "build_config.rb")
  FileUtils.mkdir_p(output_dir)
  write_file(output_path, overlay_source(base_config, target, lock_data.fetch("gems")))
  Overlay.new(path: output_path, fingerprint: fingerprint)
end

#lock(update: false) ⇒ Hash[String, untyped]

Parameters:

  • update: (Boolean) (defaults to: false)

Returns:

  • (Hash[String, untyped])


50
51
52
53
54
55
56
57
# File 'lib/rpremote/mrbgems.rb', line 50

def lock(update: false)
  check
  previous = update ? nil : read_lock(required: false)
  entries = dependencies.map { |dependency| lock_entry(dependency, previous) }
  contents = { "version" => LOCK_VERSION, "vm" => vm&.to_s, "gems" => entries }.compact
  write_json(lock_path, contents)
  contents
end

#read_lock(required: true) ⇒ Hash[String, untyped]?

Parameters:

  • required: (Boolean) (defaults to: true)

Returns:

  • (Hash[String, untyped], nil)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rpremote/mrbgems.rb', line 59

def read_lock(required: true)
  contents = JSON.parse(File.read(lock_path))
  validate_lock!(contents)
  contents
rescue Errno::ENOENT
  raise LockError, "mrbgems lock file does not exist: #{lock_path}" if required

  nil
rescue JSON::ParserError => e
  raise LockError, "invalid mrbgems lock file #{lock_path}: #{e.message}"
end

#vmSymbol?

Returns:

  • (Symbol, nil)


40
41
42
43
# File 'lib/rpremote/mrbgems.rb', line 40

def vm
  dependencies
  @definition.vm_name
end