Skip to content

Commit aad0b60

Browse files
kenhysWatson1978
andauthored
Add gemfile:update task to bump version in Gemfile (#950)
rake gemfile:update updates fluent-package/Gemfile. --------- Signed-off-by: Kentaro Hayashi <[email protected]> Co-authored-by: Shizuo Fujita <[email protected]>
1 parent 5a1d589 commit aad0b60

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

fluent-package/Rakefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,22 @@ class UpdateLockfileTask
366366
end
367367
end
368368
end
369+
370+
namespace :gemfile do
371+
desc "Update Gemfile"
372+
task :update do
373+
require_relative '../lib/update-gems'
374+
cd gemfile_dir do
375+
options = {
376+
lts: !!ENV['LTS'],
377+
replace: true,
378+
gemfile: 'Gemfile'
379+
}
380+
checker = GemfileUpdateChecker.new(options)
381+
checker.run
382+
end
383+
end
384+
end
369385
end
370386
end
371387

lib/update-gems.rb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 'pastel'
11+
rescue LoadError
12+
puts "Prerequisite:\n\tgem install pastel"
13+
exit 1
14+
end
15+
16+
options = {
17+
lts: false,
18+
replace: true,
19+
gemfile: "Gemfile"
20+
}
21+
22+
class GemfileUpdateChecker
23+
def initialize(options)
24+
@options = options
25+
@gemfile_path = options[:gemfile]
26+
@new_gemfile_path = @gemfile_path + ".new"
27+
@pastel = Pastel.new
28+
@freeze_in_lts = %w(oj)
29+
end
30+
31+
def fetch_latest_version(gem, version)
32+
base_version = version.split(".")[0..-2].join(".")
33+
latest_version = version
34+
message = ""
35+
URI.open("https://rubygems.org/api/v1/gems/#{gem}.json") do |request|
36+
fetched_version = JSON.parse(request.read)["version"]
37+
if @options[:lts]
38+
if Gem::Version.new(fetched_version) < Gem::Version.new("#{base_version.succ}")
39+
if @freeze_in_lts.include?(gem)
40+
message = @pastel.red.on_black ( "WARN: skip even though teeny update #{gem} #{fetched_version} for LTS" )
41+
else
42+
latest_version = fetched_version
43+
end
44+
else
45+
message = @pastel.yellow.on_black ( "WARN: skip #{gem} #{fetched_version} for LTS" )
46+
end
47+
else
48+
latest_version = fetched_version
49+
end
50+
end
51+
[latest_version, message]
52+
end
53+
54+
def show_latest_version(gem, version, latest_version, appendix = "")
55+
if version != latest_version
56+
puts "GEM: #{gem} #{version} => #{@pastel.magenta.on_black( latest_version )} #{appendix}"
57+
else
58+
puts "GEM: #{gem} #{latest_version} #{appendix}"
59+
end
60+
end
61+
62+
def generate_updated_gemfile
63+
contents = ""
64+
File.open(@gemfile_path, "r") do |gemfile|
65+
gemfile.readlines.each do |line|
66+
if line.start_with?("gem")
67+
if line =~ /^gem "(.+)", "(.+)"(.*)/
68+
plugin=$1
69+
version=$2
70+
rest=$3
71+
latest_version, warning = fetch_latest_version(plugin, version)
72+
show_latest_version(plugin, version, latest_version, warning)
73+
contents << "gem \"#{plugin}\", \"#{latest_version}\"#{rest}\n"
74+
elsif line =~ /^gem "(.+)", '(.+)'/
75+
plugin=$1
76+
version=$2
77+
latest_version, warning = fetch_latest_version(plugin, version)
78+
show_latest_version(plugin, version, latest_version, warning)
79+
contents << "gem \"#{plugin}\", \"#{latest_version}\"\n"
80+
else
81+
contents << line
82+
end
83+
else
84+
contents << line
85+
end
86+
end
87+
end
88+
contents
89+
end
90+
91+
def run
92+
File.open(@new_gemfile_path, "w+") do |gemfile|
93+
contents = generate_updated_gemfile
94+
gemfile.puts(contents)
95+
puts "#{@new_gemfile_path} was generated successfully"
96+
end
97+
if @options[:replace]
98+
FileUtils.cp(@new_gemfile_path, @gemfile_path)
99+
puts "#{@gemfile_path} was replaced with #{@new_gemfile_path} successfully"
100+
else
101+
puts "#{@gemfile_path} was not replaced with #{@new_gemfile_path}"
102+
end
103+
end
104+
end

0 commit comments

Comments
 (0)