Module: Prremote::RuntimeManager

Defined in:
lib/prremote/runtime_manager.rb

Constant Summary collapse

REPO =
'lumbermill/prremote'.freeze
BOARD =
'picow'.freeze

Class Method Summary collapse

Class Method Details

.cache_dirObject



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

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

.cached_path(version) ⇒ Object



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

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

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



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

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) ⇒ Object



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

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

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

.release_url(version) ⇒ Object



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

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

.uf2_filename(version) ⇒ Object



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

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