1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

extlibs.rb: Extract ExtLibs#process

For the case using this script as a library.
- `ExtLibs#process` reads and processes an extlibs file.
- `ExtLibs#process_under` processes all extlibs files under the
  given directory.
- `Extlibs.run` parses `ARGV` and lets an instance process the
  directories.
This commit is contained in:
Nobuyoshi Nakada 2022-03-18 22:11:49 +09:00
parent 9112cf4ae7
commit 247f8ecfa4
Notes: git 2022-03-24 13:16:40 +09:00

View file

@ -22,7 +22,9 @@ class Vars < Hash
end
class ExtLibs
def initialize
def initialize(mode = :all, cache_dir: nil)
@mode = mode
@cache_dir = cache_dir
@colorize = Colorize.new
end
@ -160,7 +162,78 @@ class ExtLibs
extracted
end
def run(argv)
def process(list)
mode = @mode
cache_dir = @cache_dir
after_extract = (mode == :all or mode == :patch)
success = true
if $VERBOSE
$stdout.puts "downloading for #{list}"
$stdout.flush
end
vars = Vars.new
extracted = false
dest = File.dirname(list)
url = chksums = nil
IO.foreach(list) do |line|
line.sub!(/\s*#.*/, '')
if /^(\w+)\s*=\s*(.*)/ =~ line
vars[$1] = vars.expand($2)
next
end
if chksums
chksums.concat(line.split)
elsif /^\t/ =~ line
if extracted and after_extract
patch, *args = line.split.map {|s| vars.expand(s)}
do_patch(dest, patch, args)
end
next
elsif /^!\s*(?:chdir:\s*([^|\s]+)\|\s*)?(.*)/ =~ line
if extracted and after_extract
command = vars.expand($2.strip)
chdir = $1 and chdir = vars.expand(chdir)
do_exec(command, chdir, dest)
end
next
elsif /->/ =~ line
if extracted and after_extract
link, file = $`.strip, $'.strip
do_link(vars.expand(link), vars.expand(file), dest)
end
next
else
url, *chksums = line.split(' ')
end
if chksums.last == '\\'
chksums.pop
next
end
unless url
chksums = nil
next
end
url = vars.expand(url)
begin
extracted = do_command(mode, dest, url, cache_dir, chksums)
rescue => e
warn e.full_message
success = false
end
url = chksums = nil
end
success
end
def process_under(dir)
success = true
Dir.glob("#{dir}/**/extlibs") do |list|
success &= process(list)
end
success
end
def self.run(argv)
cache_dir = nil
mode = :all
until argv.empty?
@ -190,71 +263,10 @@ class ExtLibs
argv.shift
end
success = true
argv.each do |dir|
Dir.glob("#{dir}/**/extlibs") do |list|
if $VERBOSE
$stdout.puts "downloading for #{list}"
$stdout.flush
end
vars = Vars.new
extracted = false
dest = File.dirname(list)
url = chksums = nil
IO.foreach(list) do |line|
line.sub!(/\s*#.*/, '')
if /^(\w+)\s*=\s*(.*)/ =~ line
vars[$1] = vars.expand($2)
next
end
if chksums
chksums.concat(line.split)
elsif /^\t/ =~ line
if extracted and (mode == :all or mode == :patch)
patch, *args = line.split.map {|s| vars.expand(s)}
do_patch(dest, patch, args)
end
next
elsif /^!\s*(?:chdir:\s*([^|\s]+)\|\s*)?(.*)/ =~ line
if extracted and (mode == :all or mode == :patch)
command = vars.expand($2.strip)
chdir = $1 and chdir = vars.expand(chdir)
do_exec(command, chdir, dest)
end
next
elsif /->/ =~ line
if extracted and (mode == :all or mode == :patch)
link, file = $`.strip, $'.strip
do_link(vars.expand(link), vars.expand(file), dest)
end
next
else
url, *chksums = line.split(' ')
end
if chksums.last == '\\'
chksums.pop
next
end
unless url
chksums = nil
next
end
url = vars.expand(url)
begin
extracted = do_command(mode, dest, url, cache_dir, chksums)
rescue => e
warn e.full_message
success = false
end
url = chksums = nil
end
end
extlibs = new(mode, cache_dir: cache_dir)
argv.inject(true) do |success, dir|
success & extlibs.process_under(dir)
end
success
end
def self.run(argv)
self.new.run(argv)
end
end