-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathbump-dependency-upper-bound
executable file
·56 lines (42 loc) · 1.39 KB
/
bump-dependency-upper-bound
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
#!/usr/bin/env ruby
require 'semantic_puppet'
require 'json'
def normalize_name(name)
name.tr('-', '/')
end
def bump_dependency(filename, module_name, upper_bound)
metadata = JSON.load(File.read(filename))
raise Exception.new("Unable to find dependencies") unless metadata['dependencies'].is_a?(Array)
dependency = metadata['dependencies'].detect { |dep| normalize_name(dep['name']) == module_name }
raise Exception.new("Dependency #{module_name} not found") unless dependency
old = dependency['version_requirement']
requirement = SemanticPuppet::VersionRange.parse(old)
return [old, old] if requirement.end == upper_bound
new = ">= #{requirement.begin} < #{upper_bound}"
dependency['version_requirement'] = new
File.open(filename, 'w') do
|file| file.write(JSON.pretty_generate(metadata) + "\n")
end
[old, new]
end
def main
if ARGV.length < 3
puts "Usage: #{$PROGRAM_NAME} dependency upper_bound metadata_path [metadata_path]"
exit 1
end
module_name, upper_bound, *paths = ARGV
module_name = normalize_name(module_name)
paths.each do |path|
begin
old, new = bump_dependency(path, module_name, upper_bound)
if old != new
puts "Updated #{path}: '#{old}' to '#{new}'"
else
puts "#{path} already matches #{upper_bound}"
end
rescue Exception => e
puts "Failed to update #{path}: #{e}"
end
end
end
main