Skip to content

Commit

Permalink
sync_default_gems: Replace the external URIs to docs with rdoc-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Oct 12, 2022
1 parent c67e496 commit df58844
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tool/sync_default_gems.rb
Expand Up @@ -84,6 +84,30 @@ def pipe_readlines(args, rs: "\0", chomp: true)
end
end

def replace_rdoc_ref(file)
src = File.binread(file)
src.gsub!(%r[\[\Khttps://docs\.ruby-lang\.org/en/master/(([A-Z]\w+(?:/[A-Z]\w+)*)|\w+_rdoc)\.html(\#\S+)?(?=\])]) do
name, mod, label = $1, $2, $3
mod &&= mod.gsub('/', '::')
if label && (m = label.match(/\A\#(?:method-([ci])|(?:(?:class|module)-#{mod}-)?label)-([-+\w]+)\z/))
scope, label = m[1], m[2]
scope = scope ? scope.tr('ci', '.#') : '@'
end
"rdoc-ref:#{mod || name.chomp("_rdoc") + ".rdoc"}#{scope}#{label}"
end or return false
File.rename(file, file + "~")
File.binwrite(file, src)
return true
end

def replace_rdoc_ref_all
result = pipe_readlines(%W"git status porcelain -z -- *.c *.rb *.rdoc")
result.map! {|line| line[/\A.M (.*)/, 1]}
result.compact!
result = pipe_readlines(%W"git grep -z -l -F [https://docs.ruby-lang.org/en/master/ --" + result)
result.inject(false) {|changed, file| changed | replace_rdoc_ref(file)}
end

# We usually don't use this. Please consider using #sync_default_gems_with_commits instead.
def sync_default_gems(gem)
repo = REPOSITORIES[gem.to_sym]
Expand Down Expand Up @@ -382,6 +406,7 @@ def sync_default_gems(gem)
else
sync_lib gem, upstream
end
replace_rdoc_ref_all
end

IGNORE_FILE_PATTERN =
Expand Down Expand Up @@ -508,6 +533,10 @@ def sync_default_gems_with_commits(gem, ranges, edit: nil)
next
end

if replace_rdoc_ref_all
`git commit --amend --no-edit`
end

puts "Update commit message: #{sha}"

IO.popen(%W[git filter-branch -f --msg-filter #{[filter, repo, sha].join(' ')} -- HEAD~1..HEAD], &:read)
Expand Down

0 comments on commit df58844

Please sign in to comment.