Skip to content

Commit f8b3aa9

Browse files
committed
packages: add helper script to update Gemfile
For LTS channel: $ ruby update-gemfile.rb --lts For standard channel: $ ruby update-gemfile.rb Signed-off-by: Kentaro Hayashi <[email protected]>
1 parent 0fa6197 commit f8b3aa9

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

fluent-package/update-gemfile.rb

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#
2+
# Utility to update fluent-package-builder/fluent-package/Gemfile
3+
#
4+
5+
require 'optparse'
6+
require 'open-uri'
7+
require 'json'
8+
require 'fileutils'
9+
begin
10+
require 'term/ansicolor'
11+
rescue LoadError
12+
puts "Prerequisite:\n\tgem install term-ansicolor"
13+
exit 1
14+
end
15+
16+
options = {
17+
lts: false,
18+
replace: true,
19+
gemfile: "Gemfile"
20+
}
21+
22+
opt = OptionParser.new
23+
opt.on("--lts", "LTS") {|v| options[:lts] = true }
24+
opt.on("--[no-]replace", "Specify whether Gemfile should be replaced or not") {|v| options[:replace] = v }
25+
opt.on("--gemfile", "Specify path of Gemfile") {|v| options[:gemfile] = v }
26+
paths = opt.parse!(ARGV)
27+
28+
unless paths.empty?
29+
gemfile_path = paths.select do |path|
30+
path.end_with?("Gemfile")
31+
end
32+
options[:gemfile] = gemfile_path
33+
end
34+
35+
class GemfileUpdateChecker
36+
def initialize(options)
37+
@options = options
38+
@gemfile_path = options[:gemfile]
39+
@new_gemfile_path = @gemfile_path + ".new"
40+
end
41+
42+
def fetch_latest_version(gem, version)
43+
base_version = version.split(".")[0..-2].join(".")
44+
latest_version = version
45+
message = ""
46+
URI.open("https://rubygems.org/api/v1/gems/#{gem}.json") do |request|
47+
fetched_version = JSON.parse(request.read)["version"]
48+
if @options[:lts]
49+
if Gem::Version.new(fetched_version) < Gem::Version.new("#{base_version.succ}")
50+
latest_version = fetched_version
51+
else
52+
message = Term::ANSIColor.yellow { Term::ANSIColor.on_black { "WARN: skip #{gem} #{fetched_version} for LTS" }}
53+
end
54+
else
55+
latest_version = fetched_version
56+
end
57+
end
58+
[latest_version, message]
59+
end
60+
61+
def show_latest_version(gem, version, latest_version, appendix = "")
62+
if version != latest_version
63+
puts "GEM: #{gem} #{version} => #{Term::ANSIColor.magenta { Term::ANSIColor.on_black{ latest_version }}} #{appendix}"
64+
else
65+
puts "GEM: #{gem} #{latest_version} #{appendix}"
66+
end
67+
end
68+
69+
def generate_updated_gemfile
70+
contents = ""
71+
File.open(@gemfile_path, "r") do |gemfile|
72+
gemfile.readlines.each do |line|
73+
if line.start_with?("gem")
74+
if line =~ /^gem "(.+)", "(.+)"(.*)/
75+
plugin=$1
76+
version=$2
77+
rest=$3
78+
latest_version, warning = fetch_latest_version(plugin, version)
79+
show_latest_version(plugin, version, latest_version, warning)
80+
contents << "gem \"#{plugin}\", \"#{latest_version}\"#{rest}\n"
81+
elsif line =~ /^gem "(.+)", '(.+)'/
82+
plugin=$1
83+
version=$2
84+
latest_version, warning = fetch_latest_version(plugin, version)
85+
show_latest_version(plugin, version, latest_version, warning)
86+
contents << "gem \"#{plugin}\", \"#{latest_version}\"\n"
87+
else
88+
contents << line
89+
end
90+
else
91+
contents << line
92+
end
93+
end
94+
end
95+
contents
96+
end
97+
98+
def run
99+
File.open(@new_gemfile_path, "w+") do |gemfile|
100+
contents = generate_updated_gemfile
101+
gemfile.puts(contents)
102+
puts "#{@new_gemfile_path} was generated successfully"
103+
end
104+
if @options[:replace]
105+
FileUtils.cp(@new_gemfile_path, @gemfile_path)
106+
puts "#{@gemfile_path} was replaced with #{@new_gemfile_path} successfully"
107+
else
108+
puts "#{@gemfile_path} was not replaced with #{@new_gemfile_path}"
109+
end
110+
end
111+
end
112+
113+
checker = GemfileUpdateChecker.new(options)
114+
checker.run
115+

0 commit comments

Comments
 (0)