Skip to content

Commit 544b437

Browse files
frsyukik0kubun
authored andcommitted
Add support for zip archive type for Windows and macOS
Binary releases at https://github.com/sqldef/sqldef use tar.gz for linux while zip for windows and darwin.
1 parent aca3f8b commit 544b437

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/sqldef.rb

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'stringio'
88
require 'uri'
99
require 'zlib'
10+
require 'zip'
1011
require_relative 'sqldef/version'
1112

1213
module Sqldef
@@ -24,6 +25,13 @@ module Sqldef
2425
]
2526
private_constant :COMMANDS
2627

28+
OS_ARCHIVE = {
29+
'linux' => 'tar.gz',
30+
'windows' => 'zip',
31+
'darwin' => 'zip',
32+
}
33+
private_constant :OS_ARCHIVE
34+
2735
@bin = Dir.pwd
2836

2937
class << self
@@ -79,16 +87,26 @@ def download(command)
7987
return path if File.executable?(path)
8088

8189
print("Downloading '#{command}' under '#{bin}'... ")
82-
resp = get(build_url(command), code: 302) # Latest
90+
url = build_url(command)
91+
resp = get(url, code: 302) # Latest
8392
resp = get(resp['location'], code: 302) # vX.Y.Z
8493
resp = get(resp['location'], code: 200) # Binary
8594

86-
gzip = Zlib::GzipReader.new(StringIO.new(resp.body))
87-
Gem::Package::TarReader.new(gzip) do |tar|
88-
unless file = tar.find { |f| f.full_name == command }
89-
raise "'#{command}' was not found in the archive"
95+
if url.end_with?('.zip')
96+
Zip::File.open_buffer(resp.body) do |zip|
97+
unless entry = zip.find_entry(command)
98+
raise "'#{command}' was not found in the archive"
99+
end
100+
File.binwrite(path, zip.read(entry))
101+
end
102+
else
103+
gzip = Zlib::GzipReader.new(StringIO.new(resp.body))
104+
Gem::Package::TarReader.new(gzip) do |tar|
105+
unless file = tar.find { |f| f.full_name == command }
106+
raise "'#{command}' was not found in the archive"
107+
end
108+
File.binwrite(path, file.read)
90109
end
91-
File.binwrite(path, file.read)
92110
end
93111

94112
FileUtils.chmod('+x', path)
@@ -109,8 +127,10 @@ def build_url(command)
109127
raise "Unexpected sqldef command: #{command}"
110128
end
111129
os = Etc.uname.fetch(:sysname).downcase
112-
arch = GOARCH.fetch(Etc.uname.fetch(:machine))
113-
"https://github.com/k0kubun/sqldef/releases/latest/download/#{command}_#{os}_#{arch}.tar.gz"
130+
archive = OS_ARCHIVE.fetch(os)
131+
arch = Etc.uname.fetch(:machine)
132+
goarch = GOARCH.fetch(arch, arch)
133+
"https://github.com/k0kubun/sqldef/releases/latest/download/#{command}_#{os}_#{goarch}.#{archive}"
114134
end
115135

116136
# TODO: Retry transient errors

sqldef.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
1717
spec.metadata['homepage_uri'] = spec.homepage
1818
spec.metadata['source_code_uri'] = spec.homepage
1919

20+
spec.add_runtime_dependency 'rubyzip'
21+
2022
# Specify which files should be added to the gem when it is released.
2123
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
2224
spec.files = Dir.chdir(File.expand_path(__dir__)) do

0 commit comments

Comments
 (0)