-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupdate_versions.rb
72 lines (63 loc) · 2.1 KB
/
update_versions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Script for updating Gemfile in our Jekyll site
# Usage: ruby update_versions.rb
# Copyright (C) 2020 Diego Asterio de Zaballa
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
require 'net/http'
require 'json'
require 'uri'
require 'tempfile'
require 'fileutils'
name_vers = Struct.new(:name, :version)
# Regex for finding gems
GEM_RE = /^(?<space>\s*)gem\s+[\"\'](?<name>(?:\w|[-_\/])+)[\"\']\s*(?:,\s*[\"\']((?:<=|>=|~>)\s*)?(?<version>(?:.|\d)+)[\"\'])?$/
def replacement_gem(line, versions)
ret = nil
# Checks if the line is a gem
m = line.match GEM_RE
if m
name = m[:name]
indent = m[:space]
# If inside versions recommended by github change the line
if versions[name]
ret = "#{indent}gem \"#{name}\", \"#{versions[name]}\""
end
end
ret
end
if __FILE__ == $0
uri = URI('https://pages.github.com/versions.json')
res = Net::HTTP.get_response(uri)
# Ends execution if resource is not found
return unless res.class.body_permitted? and !res.body.nil?
msg = res.body
versions = JSON.parse(msg)
path = "./Gemfile"
temp_file = Tempfile.new("Gemfile")
begin
File.open(path,mode="r") do |file|
file.each_line do |line|
repl = replacement_gem(line, versions)
# if exists a replacement replace it
if repl
temp_file.puts repl
else
temp_file.puts line
end
end
end
temp_file.close
FileUtils.mv(temp_file, path)
ensure
temp_file.close
temp_file.unlink
end
end