Class: Lightpanda::Binary

Inherits:
Object
  • Object
show all
Defined in:
lib/lightpanda/binary.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

GITHUB_RELEASE_URL =
"https://github.com/lightpanda-io/browser/releases/download/nightly"
PLATFORMS =
{
  %w[x86_64 linux] => "lightpanda-x86_64-linux",
  %w[aarch64 darwin] => "lightpanda-aarch64-macos",
  %w[arm64 darwin] => "lightpanda-aarch64-macos"
}.freeze

Class Method Summary collapse

Class Method Details

.default_binary_pathObject



96
97
98
99
100
# File 'lib/lightpanda/binary.rb', line 96

def default_binary_path
  cache_dir = ENV.fetch("XDG_CACHE_HOME") { File.expand_path("~/.cache") }

  File.join(cache_dir, "lightpanda", "lightpanda")
end

.downloadObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lightpanda/binary.rb', line 76

def download
  binary_name = platform_binary
  url = "#{GITHUB_RELEASE_URL}/#{binary_name}"
  destination = default_binary_path

  FileUtils.mkdir_p(File.dirname(destination))

  download_file(url, destination)
  FileUtils.chmod(0o755, destination)

  destination
end

.fetch(url) ⇒ Object

Raises:



51
52
53
54
55
56
# File 'lib/lightpanda/binary.rb', line 51

def fetch(url)
  result = run("fetch", "--dump", url)
  raise BinaryError, result.stderr unless result.success?

  result.stdout
end

.findObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lightpanda/binary.rb', line 63

def find
  env_path = ENV.fetch("LIGHTPANDA_PATH", nil)
  return env_path if env_path && File.executable?(env_path)

  path_binary = find_in_path
  return path_binary if path_binary

  default_path = default_binary_path
  return default_path if File.executable?(default_path)

  nil
end

.find_or_downloadObject



38
39
40
# File 'lib/lightpanda/binary.rb', line 38

def find_or_download
  find || download
end

.pathObject



34
35
36
# File 'lib/lightpanda/binary.rb', line 34

def path
  find_or_download
end

.platform_binaryObject



89
90
91
92
93
94
# File 'lib/lightpanda/binary.rb', line 89

def platform_binary
  arch = normalize_arch(RbConfig::CONFIG["host_cpu"])
  os = normalize_os(RbConfig::CONFIG["host_os"])

  PLATFORMS[[arch, os]] || raise(UnsupportedPlatformError, "Unsupported platform: #{arch}-#{os}")
end

.run(*args) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/lightpanda/binary.rb', line 42

def run(*args)
  binary = find_or_download
  stdout, stderr, status = Open3.capture3(binary, *args)

  Result.new(stdout: stdout, stderr: stderr, status: status)
rescue Errno::ENOENT
  raise BinaryNotFoundError, "Lightpanda binary not found"
end

.versionObject



58
59
60
61
# File 'lib/lightpanda/binary.rb', line 58

def version
  result = run("version")
  result.output.strip
end