Class: Rpremote::Config
- Inherits:
-
Object
- Object
- Rpremote::Config
- Defined in:
- lib/rpremote/config.rb,
sig/rpremote.rbs
Defined Under Namespace
Constant Summary collapse
- DEFAULT_PATH =
"config/setting.json"- OPTION_TYPES =
{ port: String, mount: String, cache: String, language_version: String, board: String, firmware: String, baud: Integer, timeout: Numeric, language: String, mrbgems: [String, FalseClass].freeze }.freeze
- COMMAND_OPTIONS =
{ "setup" => %i[cache language language_version], "build" => %i[cache language language_version board firmware mrbgems], "dfu" => %i[cache language language_version port baud timeout], "flash" => %i[cache language language_version board firmware mount port timeout], "run" => %i[port baud timeout language], "exec" => %i[port baud timeout language], "reset" => %i[port baud timeout], "monitor" => %i[port baud timeout], "repl" => %i[port baud timeout], "fs" => %i[port baud timeout], "config" => OPTION_TYPES.keys, "ports" => [], "mrbgems" => [] }.freeze
Class Method Summary collapse
- .extract_option!(args) ⇒ [String, bool]
- .load(command, filename: DEFAULT_PATH, required: false, cwd: Dir.pwd) ⇒ Object
- .load_command(command, filename:, required:) ⇒ Hash[Symbol, untyped]
- .setup(filename: DEFAULT_PATH, cwd: Dir.pwd) ⇒ Result
Class Method Details
.extract_option!(args) ⇒ [String, bool]
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rpremote/config.rb', line 41 def self.extract_option!(args) filename = nil index = 0 while index < args.length argument = args[index] break if argument == "--" value, consumed = config_argument(args, index) unless consumed index += 1 next end raise Error, "--config specified more than once" if filename raise Error, "--config requires a value" if value.nil? || value.empty? filename = value args.slice!(index, consumed) end [filename || DEFAULT_PATH, !filename.nil?] end |
.load(command, filename: DEFAULT_PATH, required: false, cwd: Dir.pwd) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rpremote/config.rb', line 69 def self.load(command, filename: DEFAULT_PATH, required: false, cwd: Dir.pwd) path = File.(filename, cwd) text = File.read(path) values = parse(text, path) validate!(values) values.slice(*COMMAND_OPTIONS.fetch(command, [])) rescue Errno::ENOENT raise Error, "config file does not exist: #{path}" if required {} end |
.load_command(command, filename:, required:) ⇒ Hash[Symbol, untyped]
62 63 64 65 66 67 |
# File 'lib/rpremote/config.rb', line 62 def self.load_command(command, filename:, required:) return {} if command.nil? || %w[help --help -h --version -V].include?(command) return {} unless COMMAND_OPTIONS.key?(command) load(command, filename: filename, required: required && command != "setup") end |
.setup(filename: DEFAULT_PATH, cwd: Dir.pwd) ⇒ Result
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rpremote/config.rb', line 81 def self.setup(filename: DEFAULT_PATH, cwd: Dir.pwd) path = File.(filename, cwd) FileUtils.mkdir_p(File.dirname(path)) File.open(path, "wx") { |file| file.write("{}\n") } Result.new(path: path, created: true) rescue Errno::EEXIST Result.new(path: path, created: false) rescue SystemCallError => e raise Error, "cannot create config file #{path}: #{e.}" end |