Class: HttpMimic::Downloader
- Inherits:
-
Object
- Object
- HttpMimic::Downloader
show all
- Defined in:
- lib/http_mimic/downloader.rb
Defined Under Namespace
Classes: DownloadError, UnsupportedPlatformError
Constant Summary
collapse
- DEFAULT_GITHUB_REPO =
'lexiforest/curl-impersonate'
- DEFAULT_VERSION =
'v2.1.1'
Class Method Summary
collapse
Class Method Details
.available_binaries(install_dir: nil) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/http_mimic/downloader.rb', line 74
def available_binaries(install_dir: nil)
target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
return [] unless Dir.exist?(target_dir)
Dir.glob(File.join(target_dir, '*')).select do |file|
File.file?(file) && File.executable?(file)
end.map { |f| File.basename(f) }
end
|
.binary_path(name, install_dir: nil) ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/http_mimic/downloader.rb', line 66
def binary_path(name, install_dir: nil)
target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
candidate = File.join(target_dir, binary_name_for_platform(name))
return candidate if File.file?(candidate) && File.executable?(candidate)
nil
end
|
.download!(version: nil, install_dir: nil, repo: nil, force: false) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/http_mimic/downloader.rb', line 20
def download!(version: nil, install_dir: nil, repo: nil, force: false)
target_version = normalize_version(version || HttpMimic.configuration.driver_version || DEFAULT_VERSION)
target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
target_repo = repo || HttpMimic.configuration.github_repo || DEFAULT_GITHUB_REPO
FileUtils.mkdir_p(target_dir)
if !force && installed?(version: target_version, install_dir: target_dir)
log_info("curl-impersonate #{target_version} is already installed in #{target_dir}")
return target_dir
end
slug = platform_slug
asset_name = "curl-impersonate-#{target_version}.#{slug}.tar.gz"
download_url = "https://github.com/#{target_repo}/releases/download/#{target_version}/#{asset_name}"
log_info("Downloading curl-impersonate (#{target_version}) [#{asset_name}]...")
archive_data = fetch_binary(download_url)
log_info("Extracting archive to #{target_dir}...")
(archive_data, target_dir)
File.write(version_file_path(target_dir), target_version)
log_info("curl-impersonate #{target_version} installation complete!")
target_dir
end
|
.installed?(version: nil, install_dir: nil) ⇒ Boolean
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/http_mimic/downloader.rb', line 51
def installed?(version: nil, install_dir: nil)
target_dir = File.expand_path(install_dir || HttpMimic.configuration.install_dir)
core_binary = File.join(target_dir, binary_name_for_platform('curl-impersonate'))
return false unless File.file?(core_binary) && File.executable?(core_binary)
if version
target_version = normalize_version(version)
v_file = version_file_path(target_dir)
return false unless File.file?(v_file)
return File.read(v_file).strip == target_version
end
true
end
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/http_mimic/downloader.rb', line 83
def platform_slug
os = host_os
cpu = host_cpu
case os
when :macos
case cpu
when :arm64 then 'arm64-macos'
when :x86_64 then 'x86_64-macos'
else
raise UnsupportedPlatformError, "Unsupported macOS CPU architecture: #{cpu}"
end
when :linux
libc = linux_libc
case cpu
when :x86_64
libc == :musl ? 'x86_64-linux-musl' : 'x86_64-linux-gnu'
when :aarch64, :arm64
libc == :musl ? 'aarch64-linux-musl' : 'aarch64-linux-gnu'
when :i386, :i686
'i386-linux-gnu'
when :arm
'arm-linux-gnueabihf'
when :riscv64
'riscv64-linux-gnu'
when :loongarch64
'loongarch64-linux-gnu'
else
raise UnsupportedPlatformError, "Unsupported Linux CPU architecture: #{cpu}"
end
when :windows
case cpu
when :x86_64 then 'x86_64-win32'
when :arm64 then 'arm64-win32'
when :i386, :i686 then 'i686-win32'
else
raise UnsupportedPlatformError, "Unsupported Windows CPU architecture: #{cpu}"
end
when :freebsd
case cpu
when :x86_64 then 'x86_64-freebsd'
when :arm64, :aarch64 then 'aarch64-freebsd'
else
raise UnsupportedPlatformError, "Unsupported FreeBSD CPU architecture: #{cpu}"
end
else
raise UnsupportedPlatformError, "Unsupported operating system: #{RbConfig::CONFIG['host_os']}"
end
end
|