Use a constant instead of a global variable in sync_default_gems.rb

This commit is contained in:
Benoit Daloze 2020-08-29 11:51:05 +02:00
parent 6d946665bd
commit 232d6c4077

View File

@ -3,7 +3,7 @@
require 'fileutils' require 'fileutils'
include FileUtils include FileUtils
$repositories = { REPOSITORIES = {
rubygems: 'rubygems/rubygems', rubygems: 'rubygems/rubygems',
bundler: 'rubygems/rubygems', bundler: 'rubygems/rubygems',
rdoc: 'ruby/rdoc', rdoc: 'ruby/rdoc',
@ -70,9 +70,9 @@ $repositories = {
} }
def sync_default_gems(gem) def sync_default_gems(gem)
puts "Sync #{$repositories[gem.to_sym]}" puts "Sync #{REPOSITORIES[gem.to_sym]}"
upstream = File.join("..", "..", $repositories[gem.to_sym]) upstream = File.join("..", "..", REPOSITORIES[gem.to_sym])
case gem case gem
when "rubygems" when "rubygems"
@ -292,11 +292,11 @@ IGNORE_FILE_PATTERN =
)\z/x )\z/x
def sync_default_gems_with_commits(gem, ranges, edit: nil) def sync_default_gems_with_commits(gem, ranges, edit: nil)
puts "Sync #{$repositories[gem.to_sym]} with commit history." puts "Sync #{REPOSITORIES[gem.to_sym]} with commit history."
IO.popen(%W"git remote") do |f| IO.popen(%W"git remote") do |f|
unless f.read.split.include?(gem) unless f.read.split.include?(gem)
`git remote add #{gem} git@github.com:#{$repositories[gem.to_sym]}.git` `git remote add #{gem} git@github.com:#{REPOSITORIES[gem.to_sym]}.git`
end end
end end
system(*%W"git fetch --no-tags #{gem}") system(*%W"git fetch --no-tags #{gem}")
@ -326,7 +326,7 @@ def sync_default_gems_with_commits(gem, ranges, edit: nil)
ENV["FILTER_BRANCH_SQUELCH_WARNING"] = "1" ENV["FILTER_BRANCH_SQUELCH_WARNING"] = "1"
commits.each do |sha, subject| commits.each do |sha, subject|
puts "Pick #{sha} from #{$repositories[gem.to_sym]}." puts "Pick #{sha} from #{REPOSITORIES[gem.to_sym]}."
skipped = false skipped = false
result = IO.popen(%W"git cherry-pick #{sha}", &:read) result = IO.popen(%W"git cherry-pick #{sha}", &:read)
@ -371,8 +371,8 @@ def sync_default_gems_with_commits(gem, ranges, edit: nil)
puts "Update commit message: #{sha}" puts "Update commit message: #{sha}"
prefix = "[#{($repositories[gem.to_sym])}]".gsub(/\//, '\/') prefix = "[#{(REPOSITORIES[gem.to_sym])}]".gsub(/\//, '\/')
suffix = "https://github.com/#{($repositories[gem.to_sym])}/commit/#{sha[0,10]}" suffix = "https://github.com/#{(REPOSITORIES[gem.to_sym])}/commit/#{sha[0,10]}"
`git filter-branch -f --msg-filter 'sed "1s/^/#{prefix} /" && echo && echo #{suffix}' -- HEAD~1..HEAD` `git filter-branch -f --msg-filter 'sed "1s/^/#{prefix} /" && echo && echo #{suffix}' -- HEAD~1..HEAD`
unless $?.success? unless $?.success?
puts "Failed to modify commit message of #{sha}" puts "Failed to modify commit message of #{sha}"
@ -408,7 +408,7 @@ end
def update_default_gems(gem) def update_default_gems(gem)
author, repository = $repositories[gem.to_sym].split('/') author, repository = REPOSITORIES[gem.to_sym].split('/')
puts "Update #{author}/#{repository}" puts "Update #{author}/#{repository}"
@ -439,14 +439,14 @@ when "up"
if ARGV[1] if ARGV[1]
update_default_gems(ARGV[1]) update_default_gems(ARGV[1])
else else
$repositories.keys.each{|gem| update_default_gems(gem.to_s)} REPOSITORIES.keys.each{|gem| update_default_gems(gem.to_s)}
end end
when "all" when "all"
$repositories.keys.each{|gem| sync_default_gems(gem.to_s)} REPOSITORIES.keys.each{|gem| sync_default_gems(gem.to_s)}
when "list" when "list"
ARGV.shift ARGV.shift
pattern = Regexp.new(ARGV.join('|')) pattern = Regexp.new(ARGV.join('|'))
$repositories.each_pair do |name, gem| REPOSITORIES.each_pair do |name, gem|
next unless pattern =~ name or pattern =~ gem next unless pattern =~ name or pattern =~ gem
printf "%-15s https://github.com/%s\n", name, gem printf "%-15s https://github.com/%s\n", name, gem
end end