2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# This module contains various utility methods as module methods.
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
module Gem::Util
|
|
|
|
##
|
|
|
|
# Zlib::GzipReader wrapper that unzips +data+.
|
|
|
|
|
|
|
|
def self.gunzip(data)
|
|
|
|
require 'zlib'
|
2015-07-01 17:50:14 -04:00
|
|
|
require 'stringio'
|
|
|
|
data = StringIO.new(data, 'r')
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2020-04-05 09:31:10 -04:00
|
|
|
gzip_reader = begin
|
|
|
|
Zlib::GzipReader.new(data)
|
|
|
|
rescue Zlib::GzipFile::Error => e
|
|
|
|
raise e.class, e.inspect, e.backtrace
|
|
|
|
end
|
|
|
|
|
|
|
|
unzipped = gzip_reader.read
|
2018-05-30 09:01:35 -04:00
|
|
|
unzipped.force_encoding Encoding::BINARY
|
2013-11-18 19:34:13 -05:00
|
|
|
unzipped
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Zlib::GzipWriter wrapper that zips +data+.
|
|
|
|
|
|
|
|
def self.gzip(data)
|
|
|
|
require 'zlib'
|
2015-07-01 17:50:14 -04:00
|
|
|
require 'stringio'
|
2016-02-01 07:43:26 -05:00
|
|
|
zipped = StringIO.new(String.new, 'w')
|
2018-05-30 09:01:35 -04:00
|
|
|
zipped.set_encoding Encoding::BINARY
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2019-02-14 07:59:03 -05:00
|
|
|
Zlib::GzipWriter.wrap zipped do |io|
|
|
|
|
io.write data
|
|
|
|
end
|
2013-11-18 19:34:13 -05:00
|
|
|
|
|
|
|
zipped.string
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# A Zlib::Inflate#inflate wrapper
|
|
|
|
|
|
|
|
def self.inflate(data)
|
|
|
|
require 'zlib'
|
|
|
|
Zlib::Inflate.inflate data
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2019-06-01 05:45:11 -04:00
|
|
|
# This calls IO.popen and reads the result
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.popen(*command)
|
2013-11-21 18:27:30 -05:00
|
|
|
IO.popen command, &:read
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# Invokes system, but silences all output.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.silent_system(*command)
|
2018-05-30 09:01:35 -04:00
|
|
|
opt = {:out => IO::NULL, :err => [:child, :out]}
|
2014-07-25 03:55:55 -04:00
|
|
|
if Hash === command.last
|
|
|
|
opt.update(command.last)
|
|
|
|
cmds = command[0...-1]
|
|
|
|
else
|
|
|
|
cmds = command.dup
|
|
|
|
end
|
2019-06-01 05:45:11 -04:00
|
|
|
system(*(cmds << opt))
|
2013-11-25 14:14:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Enumerates the parents of +directory+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.traverse_parents(directory, &block)
|
2013-11-25 14:14:49 -05:00
|
|
|
return enum_for __method__, directory unless block_given?
|
|
|
|
|
|
|
|
here = File.expand_path directory
|
2017-10-07 21:32:18 -04:00
|
|
|
loop do
|
2018-02-05 21:58:35 -05:00
|
|
|
Dir.chdir here, &block rescue Errno::EACCES
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
new_here = File.expand_path('..', here)
|
|
|
|
return if new_here == here # toplevel
|
|
|
|
here = new_here
|
2013-11-25 14:14:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
##
|
|
|
|
# Globs for files matching +pattern+ inside of +directory+,
|
|
|
|
# returning absolute paths to the matching files.
|
|
|
|
|
|
|
|
def self.glob_files_in_dir(glob, base_path)
|
|
|
|
if RUBY_VERSION >= "2.5"
|
2018-12-22 19:20:49 -05:00
|
|
|
Dir.glob(glob, base: base_path).map! {|f| File.expand_path(f, base_path) }
|
2018-08-27 06:05:04 -04:00
|
|
|
else
|
|
|
|
Dir.glob(File.expand_path(glob, base_path))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-01 05:45:11 -04:00
|
|
|
##
|
|
|
|
# Corrects +path+ (usually returned by `URI.parse().path` on Windows), that
|
|
|
|
# comes with a leading slash.
|
|
|
|
|
|
|
|
def self.correct_for_windows_path(path)
|
|
|
|
if path[0].chr == '/' && path[1].chr =~ /[a-z]/i && path[2].chr == ':'
|
|
|
|
path[1..-1]
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
end
|