Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage the timestamp for revision.h #6653

Merged
merged 6 commits into from Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions common.mk
Expand Up @@ -1228,13 +1228,8 @@ $(BUILTIN_RB_INCS): $(top_srcdir)/tool/mk_builtin_loader.rb

$(srcdir)/revision.h: $(REVISION_H)

revision.$(HAVE_BASERUBY:no=tmp)::
$(Q) $(NULLCMD) > $@
revision.$(HAVE_BASERUBY:yes=tmp):: $(srcdir)/version.h $(tooldir)/file2lastrev.rb $(REVISION_FORCE)
$(Q) $(BASERUBY) $(tooldir)/file2lastrev.rb -q --revision.h --srcdir="$(srcdir)" > $@

$(REVISION_H): revision.tmp
-$(Q)$(IFCHANGE) "--timestamp=$@" "$(srcdir)/revision.h" revision.tmp
$(REVISION_H):
$(Q) $(BASERUBY) $(tooldir)/file2lastrev.rb -q --revision.h --srcdir="$(srcdir)" --output=revision.h --timestamp=$@

$(srcdir)/ext/ripper/ripper.c: $(srcdir)/ext/ripper/tools/preproc.rb $(srcdir)/parse.y $(srcdir)/defs/id.def $(srcdir)/ext/ripper/depend
$(ECHO) generating $@
Expand Down
35 changes: 19 additions & 16 deletions tool/file2lastrev.rb
Expand Up @@ -8,20 +8,22 @@
# this file run with BASERUBY, which may be older than 1.9, so no
# require_relative
require File.expand_path('../lib/vcs', __FILE__)
require File.expand_path('../lib/output', __FILE__)

Program = $0

@output = nil
def self.output=(output)
if @output and @output != output
@format = nil
def self.format=(format)
if @format and @format != format
raise "you can specify only one of --changed, --revision.h and --doxygen"
end
@output = output
@format = format
end
@suppress_not_found = false
@limit = 20
@output = Output.new

format = '%Y-%m-%dT%H:%M:%S%z'
time_format = '%Y-%m-%dT%H:%M:%S%z'
vcs = nil
OptionParser.new {|opts|
opts.banner << " paths..."
Expand All @@ -33,24 +35,25 @@ def self.output=(output)
srcdir = path
end
opts.on("--changed", "changed rev") do
self.output = :changed
self.format = :changed
end
opts.on("--revision.h", "RUBY_REVISION macro") do
self.output = :revision_h
self.format = :revision_h
end
opts.on("--doxygen", "Doxygen format") do
self.output = :doxygen
self.format = :doxygen
end
opts.on("--modified[=FORMAT]", "modified time") do |fmt|
self.output = :modified
format = fmt if fmt
self.format = :modified
time_format = fmt if fmt
end
opts.on("--limit=NUM", "limit branch name length (#@limit)", Integer) do |n|
@limit = n
end
opts.on("-q", "--suppress_not_found") do
@suppress_not_found = true
end
@output.def_options(opts)
opts.order! rescue abort "#{File.basename(Program)}: #{$!}\n#{opts}"
begin
vcs = VCS.detect(srcdir || ".", vcs_options, opts.new)
Expand All @@ -61,32 +64,32 @@ def self.output=(output)
end
}

output =
case @output
formatter =
case @format
when :changed, nil
Proc.new {|last, changed|
changed
}
when :revision_h
Proc.new {|last, changed, modified, branch, title|
vcs.revision_header(last, modified, modified, branch, title, limit: @limit)
vcs.revision_header(last, modified, modified, branch, title, limit: @limit).join("\n")
}
when :doxygen
Proc.new {|last, changed|
"r#{changed}/r#{last}"
}
when :modified
Proc.new {|last, changed, modified|
modified.strftime(format)
modified.strftime(time_format)
}
else
raise "unknown output format `#{@output}'"
raise "unknown output format `#{@format}'"
end

ok = true
(ARGV.empty? ? [nil] : ARGV).each do |arg|
begin
puts output[*vcs.get_revisions(arg)]
@output.write(formatter[*vcs.get_revisions(arg)]+"\n")
rescue => e
warn "#{File.basename(Program)}: #{e.message}"
ok = false
Expand Down
41 changes: 8 additions & 33 deletions tool/generic_erb.rb
Expand Up @@ -5,31 +5,23 @@

require 'erb'
require 'optparse'
require_relative 'lib/vpath'
require_relative 'lib/colorize'
require_relative 'lib/output'

vpath = VPath.new
timestamp = nil
output = nil
ifchange = nil
out = Output.new
source = false
color = nil
templates = []

ARGV.options do |o|
o.on('-t', '--timestamp[=PATH]') {|v| timestamp = v || true}
o.on('-i', '--input=PATH') {|v| template << v}
o.on('-o', '--output=PATH') {|v| output = v}
o.on('-c', '--[no-]if-change') {|v| ifchange = v}
o.on('-x', '--source') {source = true}
o.on('--color') {color = true}
vpath.def_options(o)
out.def_options(o)
o.order!(ARGV)
templates << (ARGV.shift or abort o.to_s) if templates.empty?
end
color = Colorize.new(color)
unchanged = color.pass("unchanged")
updated = color.fail("updated")

# Used in prelude.c.tmpl and unicode_norm_gen.tmpl
output = out.path
vpath = out.vpath

result = templates.map do |template|
if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
Expand All @@ -41,21 +33,4 @@
source ? erb.src : proc{erb.result(binding)}.call
end
result = result.size == 1 ? result[0] : result.join("")
if output
if ifchange and (vpath.open(output, "rb") {|f| f.read} rescue nil) == result
puts "#{output} #{unchanged}"
else
open(output, "wb") {|f| f.print result}
puts "#{output} #{updated}"
end
if timestamp
if timestamp == true
dir, base = File.split(output)
timestamp = File.join(dir, ".time." + base)
end
File.open(timestamp, 'a') {}
File.utime(nil, nil, timestamp)
end
else
print result
end
out.write(result)
2 changes: 1 addition & 1 deletion tool/lib/colorize.rb
Expand Up @@ -7,7 +7,7 @@ class Colorize
def initialize(color = nil, opts = ((_, color = color, nil)[0] if Hash === color))
@colors = @reset = nil
@color = (opts[:color] if opts)
if color or (color == nil && STDOUT.tty?)
if color or (color == nil && STDOUT.tty? && (ENV["NO_COLOR"] || "").empty?)
if (%w[smso so].any? {|attr| /\A\e\[.*m\z/ =~ IO.popen("tput #{attr}", "r", :err => IO::NULL, &:read)} rescue nil)
@beg = "\e["
colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:\n]*)/)] : {}
Expand Down
47 changes: 47 additions & 0 deletions tool/lib/output.rb
@@ -0,0 +1,47 @@
require_relative 'vpath'
require_relative 'colorize'

class Output
attr_reader :path, :vpath

def initialize
@path = @timestamp = @ifchange = @color = nil
@vpath = VPath.new
end

def def_options(opt)
opt.on('-o', '--output=PATH') {|v| @path = v}
opt.on('-t', '--timestamp[=PATH]') {|v| @timestamp = v || true}
opt.on('-c', '--[no-]if-change') {|v| @ifchange = v}
opt.on('--color') {@color = true}
@vpath.def_options(opt)
end

def write(data)
unless @path
$stdout.print data
return true
end
color = Colorize.new(@color)
unchanged = color.pass("unchanged")
updated = color.fail("updated")

if @ifchange and (@vpath.read(@path, "rb") == data rescue false)
puts "#{@path} #{unchanged}"
written = false
else
File.binwrite(@path, data)
puts "#{@path} #{updated}"
written = true
end
if timestamp = @timestamp
if timestamp == true
dir, base = File.split(@path)
timestamp = File.join(dir, ".time." + base)
end
File.binwrite(timestamp, '')
File.utime(nil, nil, timestamp)
end
written
end
end
2 changes: 1 addition & 1 deletion tool/lib/vpath.rb
Expand Up @@ -56,7 +56,7 @@ def def_options(opt)
opt.on("-I", "--srcdir=DIR", "add a directory to search path") {|dir|
@additional << dir
}
opt.on("-L", "--vpath=PATH LIST", "add directories to search path") {|dirs|
opt.on("-L", "--vpath=PATH-LIST", "add directories to search path") {|dirs|
@additional << [dirs]
}
opt.on("--path-separator=SEP", /\A(?:\W\z|\.(\W).+)/, "separator for vpath") {|sep, vsep|
Expand Down