Class: Capybara::Lightpanda::Binary
- Inherits:
-
Object
- Object
- Capybara::Lightpanda::Binary
- Defined in:
- lib/capybara/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
- .default_binary_path ⇒ Object
- .download ⇒ Object
-
.ensure_nightly(max_age: 86_400) ⇒ Object
Always return the nightly binary, downloading if missing or stale.
- .exec ⇒ Object
- .fetch(url) ⇒ Object
- .find ⇒ Object
- .find_or_download ⇒ Object
- .path ⇒ Object
- .platform_binary ⇒ Object
- .run ⇒ Object
- .version ⇒ Object
Class Method Details
.default_binary_path ⇒ Object
108 109 110 111 112 |
# File 'lib/capybara/lightpanda/binary.rb', line 108 def default_binary_path cache_dir = ENV.fetch("XDG_CACHE_HOME") { File.("~/.cache") } File.join(cache_dir, "lightpanda", "lightpanda") end |
.download ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/capybara/lightpanda/binary.rb', line 88 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 |
.ensure_nightly(max_age: 86_400) ⇒ Object
Always return the nightly binary, downloading if missing or stale. Skips PATH lookup so the system binary is never used.
45 46 47 48 49 |
# File 'lib/capybara/lightpanda/binary.rb', line 45 def ensure_nightly(max_age: 86_400) path = default_binary_path download if !File.executable?(path) || (Time.now - File.mtime(path)) > max_age path end |
.exec ⇒ Object
59 60 61 |
# File 'lib/capybara/lightpanda/binary.rb', line 59 def exec(*) Kernel.exec(path, *) end |
.fetch(url) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/capybara/lightpanda/binary.rb', line 63 def fetch(url) result = run("fetch", "--dump", url) raise BinaryError, result.stderr unless result.success? result.stdout end |
.find ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/capybara/lightpanda/binary.rb', line 75 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_download ⇒ Object
39 40 41 |
# File 'lib/capybara/lightpanda/binary.rb', line 39 def find_or_download find || download end |
.path ⇒ Object
35 36 37 |
# File 'lib/capybara/lightpanda/binary.rb', line 35 def path @path ||= find_or_download end |
.platform_binary ⇒ Object
101 102 103 104 105 106 |
# File 'lib/capybara/lightpanda/binary.rb', line 101 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 ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/capybara/lightpanda/binary.rb', line 51 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 |
.version ⇒ Object
70 71 72 73 |
# File 'lib/capybara/lightpanda/binary.rb', line 70 def version result = run("version") result.output.strip end |