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 =
{
  ["x86_64", "linux"] => "lightpanda-x86_64-linux",
  ["aarch64", "darwin"] => "lightpanda-aarch64-macos",
  ["arm64", "darwin"] => "lightpanda-aarch64-macos",
}.freeze

Class Method Summary collapse

Class Method Details

.default_binary_pathObject



99
100
101
102
103
# File 'lib/lightpanda/binary.rb', line 99

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

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

.downloadObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lightpanda/binary.rb', line 79

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

.execObject



50
51
52
# File 'lib/lightpanda/binary.rb', line 50

def exec(*)
  Kernel.exec(path, *)
end

.fetch(url) ⇒ Object

Raises:



54
55
56
57
58
59
# File 'lib/lightpanda/binary.rb', line 54

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

  result.stdout
end

.findObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lightpanda/binary.rb', line 66

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
  Lightpanda.configuration.binary_path ||= find_or_download
end

.platform_binaryObject



92
93
94
95
96
97
# File 'lib/lightpanda/binary.rb', line 92

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

.runObject



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

def run(*)
  stdout, stderr, status = Open3.capture3(path, *)

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

.versionObject



61
62
63
64
# File 'lib/lightpanda/binary.rb', line 61

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