Last active
December 29, 2015 06:18
-
-
Save calorie/7627566 to your computer and use it in GitHub Desktop.
boxen : update Puppetfile script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'json' | |
READ_START_SYMBOL = '# ---auto update---' | |
READ_END_SYMBOL = '# ---/auto update---' | |
def notify(name, from, to) | |
puts "update #{name} from #{from} to #{to}" | |
end | |
def fetch(pkg) | |
uri = "https://api.github.com/repos/boxen/puppet-#{pkg}/tags" | |
result = open(uri).read | |
hash = JSON.parse(result) | |
hash.first['name'] | |
end | |
def update(line) | |
if line.chomp.empty? || line.chars.first == '#' || !(line =~ /github/) | |
return line | |
end | |
line.match(/^github \"(\w+)\",\s*\"(\d+\.\d+\.\d+)\"/) | |
name, version = $1, $2 | |
new_version = fetch(name) | |
return line unless new_version | |
notify(name, version, new_version) if version != new_version | |
line.gsub(version, new_version) | |
end | |
def read(file) | |
file = './Puppetfile' if ARGV.empty? | |
flg = false | |
lines = File.readlines(file) | |
lines.map do |l| | |
flg = flg || l.chomp =~ /#{READ_START_SYMBOL}/ | |
flg = flg && !(l.chomp =~ /#{READ_END_SYMBOL}/) | |
flg ? update(l) : l | |
end | |
end | |
def write(lines) | |
File.open('./Puppetfile', 'w') do |f| | |
lines.each do |l| | |
f.write(l) | |
end | |
end | |
end | |
write read(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment