Module: Prremote::RuntimeManager

Defined in:
lib/prremote/runtime_manager.rb

Constant Summary collapse

BOARDS =
%w[pico picow].freeze

Class Method Summary collapse

Class Method Details

.cache_dirObject



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

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

.cached_path(version, board) ⇒ Object



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

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

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/prremote/runtime_manager.rb', line 37

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



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/prremote/runtime_manager.rb', line 25

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

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

.release_url(version, board) ⇒ Object



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

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

.uf2_filename(version, board) ⇒ Object



9
10
11
# File 'lib/prremote/runtime_manager.rb', line 9

def self.uf2_filename(version, board)
  "prremote-#{board}-runtime-#{version}.uf2"
end