Module: Prremote::RuntimeManager

Defined in:
lib/prremote/runtime_manager.rb

Constant Summary collapse

BOARDS =
%w[pico picow esp32].freeze

Class Method Summary collapse

Class Method Details

.artifact_filename(version, board) ⇒ Object

Pico boards ship as UF2 (copied to the BOOTSEL drive); ESP32 ships as a single merged .bin (bootloader + partition table + app) flashed at 0x0 with esptool.



12
13
14
15
# File 'lib/prremote/runtime_manager.rb', line 12

def self.artifact_filename(version, board)
  ext = board == 'esp32' ? 'bin' : 'uf2'
  "prremote-#{board}-runtime-#{version}.#{ext}"
end

.cache_dirObject



21
22
23
# File 'lib/prremote/runtime_manager.rb', line 21

def self.cache_dir
  File.join(Dir.home, '.prremote', 'runtime')
end

.cached_path(version, board) ⇒ Object



25
26
27
# File 'lib/prremote/runtime_manager.rb', line 25

def self.cached_path(version, board)
  File.join(cache_dir, artifact_filename(version, board))
end

.download(url, dest, redirects = 5) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/prremote/runtime_manager.rb', line 41

def self.download(url, dest, redirects = 5)
  raise 'Too many redirects' if redirects.zero?

  uri = URI.parse(url)
  response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(Net::HTTP::Get.new(uri))
  end

  case response
  when Net::HTTPRedirection
    download(response['Location'], dest, redirects - 1)
  when Net::HTTPSuccess
    File.binwrite(dest, response.body)
  else
    FileUtils.rm_f(dest)
    raise "Download failed: #{response.code} #{response.message}"
  end
end

.fetch(version, board) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prremote/runtime_manager.rb', line 29

def self.fetch(version, board)
  path = cached_path(version, board)
  return path if File.exist?(path)

  FileUtils.mkdir_p(cache_dir)
  $stderr.print "Downloading #{artifact_filename(version, board)}..."
  $stderr.flush
  download(release_url(version, board), path)
  warn ' done.'
  path
end

.release_url(version, board) ⇒ Object



17
18
19
# File 'lib/prremote/runtime_manager.rb', line 17

def self.release_url(version, board)
  "https://github.com/lumbermill/prremote/releases/download/runtime-#{version}/#{artifact_filename(version, board)}"
end