summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2022-11-27 20:14:26 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2022-11-29 18:12:43 +0900
commited4b5c4f21d50609d1e895449f50462c293b2a9d (patch)
tree898b640481f5c39a848d453ad80ebe8bd49d33b7
parent8e3ac264df55ed7903f4f9519b353ca182eea81f (diff)
downloadruby-ed4b5c4f21d50609d1e895449f50462c293b2a9d.tar.gz
Extract outdate-bundled-gems.rb
-rw-r--r--common.mk30
-rwxr-xr-xtool/outdate-bundled-gems.rb134
2 files changed, 135 insertions, 29 deletions
diff --git a/common.mk b/common.mk
index 95b6052f21..8b2805d3dd 100644
--- a/common.mk
+++ b/common.mk
@@ -1406,35 +1406,7 @@ extract-gems$(gnumake:yes=-sequential): PHONY
gems/bundled_gems
outdate-bundled-gems: PHONY
- $(Q) $(BASERUBY) \
- -rfileutils \
- -e "srcdir = ARGV.shift" \
- -e "FU = /\A-\w*n/ =~ ENV['MFLAGS'] ? FileUtils::DryRun : FileUtils::Verbose" \
- -e "Dir.glob(%[#{srcdir}/.bundle/gems/*/]) {|dir|" \
- -e "gem = %[#{srcdir}/gems/#{File.basename(dir)}.gem]" \
- -e "FU.rm_rf(dir) unless File.exist?(gem)" \
- -e "}" \
- -e "Dir.glob(%[#{srcdir}/.bundle/specifications/*.gemspec]) {|spec|" \
- -e "gem = %[#{srcdir}/gems/#{File.basename(spec, '.gemspec')}.gem]" \
- -e "FU.rm_f(spec) unless File.exist?(gem)" \
- -e "}" \
- -e "Dir.glob('.bundle/specifications/*.gemspec') {|spec|" \
- -e "dir = %[#{srcdir}/.bundle/gems/#{File.basename(spec, '.gemspec')}]" \
- -e "FU.rm_f(spec) unless File.directory?(dir)" \
- -e "}" \
- -e "Dir.glob('.bundle/gems/*/') {|dir|" \
- -e "spec = %[.bundle/specifications/#{File.basename(dir)}.gemspec]" \
- -e "FU.rm_rf(dir) unless File.exist?(spec)" \
- -e "}" \
- -e "Dir.glob('.bundle/extensions/*/*/*/') {|dir|" \
- -e "spec = %[.bundle/specifications/#{File.basename(dir)}.gemspec]" \
- -e "unless File.exist?(spec)" \
- -e "FU.rm_rf(dir)" \
- -e "FU.rmdir(File.dirname(dir), parents: true) rescue nil" \
- -e "end" \
- -e "}" \
- -e "# $(MAKE)" \
- "$(srcdir)"
+ $(Q) $(BASERUBY) $(tooldir)/$@.rb --make="$(MAKE)" --mflags="$(MFLAGS)" "$(srcdir)"
update-bundled_gems: PHONY
$(Q) $(RUNRUBY) -rrubygems \
diff --git a/tool/outdate-bundled-gems.rb b/tool/outdate-bundled-gems.rb
new file mode 100755
index 0000000000..9dd19a71a1
--- /dev/null
+++ b/tool/outdate-bundled-gems.rb
@@ -0,0 +1,134 @@
+#!/usr/bin/ruby
+require 'fileutils'
+require 'rubygems'
+
+fu = FileUtils::Verbose
+until ARGV.empty?
+ case ARGV.first
+ when '--'
+ ARGV.shift
+ break
+ when '-n', '--dryrun'
+ fu = FileUtils::DryRun
+ when /\A--make=/
+ # just to run when `make -n`
+ when /\A--mflags=(.*)/
+ fu = FileUtils::DryRun if /\A-\S*n/ =~ $1
+ when /\A--basedir=(.*)/m
+ dir = $1
+ when /\A-/
+ raise "#{$0}: unknown option: #{ARGV.first}"
+ else
+ break
+ end
+ ARGV.shift
+end
+
+class Removal
+ def initialize(base = nil)
+ @base = (File.join(base, "/") if base)
+ @remove = {}
+ end
+
+ def prefixed(name)
+ @base ? File.join(@base, name) : name
+ end
+
+ def stripped(name)
+ if @base && name.start_with?(@base)
+ name[@base.size..-1]
+ else
+ name
+ end
+ end
+
+ def slash(name)
+ name.sub(%r[[^/]\K\z], '/')
+ end
+
+ def exist?(name)
+ !@remove.fetch(name) {|k| @remove[k] = !File.exist?(prefixed(name))}
+ end
+ def directory?(name)
+ !@remove.fetch(slash(name)) {|k| @remove[k] = !File.directory?(prefixed(name))}
+ end
+
+ def unlink(name)
+ @remove[stripped(name)] = :rm_f
+ end
+ def rmdir(name)
+ @remove[slash(stripped(name))] = :rm_rf
+ end
+
+ def glob(pattern, *rest)
+ Dir.glob(prefixed(pattern), *rest) {|n|
+ yield stripped(n)
+ }
+ end
+
+ def each_file
+ @remove.each {|k, v| yield prefixed(k) if v == :rm_f}
+ end
+
+ def each_directory
+ @remove.each {|k, v| yield prefixed(k) if v == :rm_rf}
+ end
+end
+
+srcdir = Removal.new(ARGV.shift)
+curdir = Removal.new
+
+srcdir.glob(".bundle/gems/*/") do |dir|
+ unless srcdir.exist?("gems/#{File.basename(dir)}.gem")
+ srcdir.rmdir(dir)
+ end
+end
+
+srcdir.glob(".bundle/specifications/*.gemspec") do |spec|
+ unless srcdir.directory?(".bundle/gems/#{File.basename(spec, '.gemspec')}/")
+ srcdir.unlink(spec)
+ end
+end
+
+curdir.glob(".bundle/specifications/*.gemspec") do |spec|
+ unless srcdir.directory?(".bundle/gems/#{File.basename(spec, '.gemspec')}")
+ curdir.unlink(spec)
+ end
+end
+
+curdir.glob(".bundle/gems/*/") do |dir|
+ unless curdir.exist?(".bundle/specifications/#{File.basename(dir)}.gemspec")
+ curdir.rmdir(dir)
+ end
+end
+
+platform = Gem::Platform.local.to_s
+curdir.glob(".bundle/{extensions,.timestamp}/*/") do |dir|
+ unless File.basename(dir) == platform
+ curdir.rmdir(dir)
+ end
+end
+
+version = RbConfig::CONFIG['ruby_version']
+curdir.glob(".bundle/{extensions,.timestamp}/#{platform}/*/") do |dir|
+ unless File.basename(dir) == version
+ curdir.rmdir(dir)
+ end
+end
+
+curdir.glob(".bundle/extensions/#{platform}/#{version}/*/") do |dir|
+ unless curdir.exist?(".bundle/specifications/#{File.basename(dir)}.gemspec")
+ curdir.rmdir(dir)
+ end
+end
+
+curdir.glob(".bundle/.timestamp/#{platform}/#{version}/.*.time") do |stamp|
+ unless curdir.directory?(File.join(".bundle", stamp[%r[/\.([^/]+)\.time\z], 1].gsub('.-.', '/')))
+ curdir.unlink(stamp)
+ end
+end
+
+srcdir.each_file {|f| fu.rm_f(f)}
+srcdir.each_directory {|d| fu.rm_rf(d)}
+curdir.each_file {|f| fu.rm_f(f)}
+curdir.each_directory {|d| fu.rm_rf(d)}