|
| 1 | +require 'json' |
| 2 | + |
| 3 | +FIRST_TAG = '202501080'.freeze # First tag to include when regenerating |
| 4 | +IGNORED_PLATFORMS = %w[aix solaris redhatfips windowsfips el-6 osx sles-11 windows-2012r2 ppc64le armhf i386].freeze |
| 5 | +INCLUDED_PROJECTS = %w[agent-runtime openbolt-runtime].freeze |
| 6 | + |
| 7 | +def platforms |
| 8 | + # First item will be a `- Platforms` header. Also exclude |
| 9 | + # any platforms we don't actually build for or have problems parsing. |
| 10 | + `bundle exec vanagon list -l`.split("\n")[1..] |
| 11 | + .reject { |p| p =~ /(#{IGNORED_PLATFORMS.join('|')})/ } |
| 12 | +end |
| 13 | + |
| 14 | +def projects |
| 15 | + # First item will be a `- Projects` header. Also exclude |
| 16 | + # any projects prefixed with '_' as these are not real projects and |
| 17 | + # are shared between projects. Also ignore any projects we don't care |
| 18 | + # about. |
| 19 | + `bundle exec vanagon list -r`.split("\n")[1..] |
| 20 | + .reject { |p| p.start_with?('_') || p !~ /^(#{INCLUDED_PROJECTS.join('|')})/ } |
| 21 | +end |
| 22 | + |
| 23 | +# Sometimes the version is a git ref so extract |
| 24 | +# actual version numbers. Fall back to 0 if nothing |
| 25 | +# usable is found so everything is comparable. |
| 26 | +def parse_version(ver) |
| 27 | + Gem::Version.new(ver.to_s[/\d+(?:\.\d+)+/] || 0) |
| 28 | +end |
| 29 | + |
| 30 | +def component_info |
| 31 | + # Build component list with version/ref for each project |
| 32 | + puts `pwd` |
| 33 | + project_data = {} |
| 34 | + projects.each do |project| |
| 35 | + puts "Processing project #{project}" |
| 36 | + project_data[project] = {} |
| 37 | + |
| 38 | + platforms.each do |platform| |
| 39 | + puts " #{platform}" |
| 40 | + platform_data = JSON.parse(`bundle exec vanagon inspect #{project} #{platform}`) |
| 41 | + project_data[project][platform] = platform_data.map { |h| [h['name'], h['version'] || h.dig('options', 'ref')] }.to_h |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + component_data = project_data.values |
| 46 | + .flat_map(&:values).flatten # [{comp1 => ver1}, {comp2 => ver2}, ...] |
| 47 | + .flat_map(&:to_a) # [[comp1, ver1], [comp2, ver2], ...] |
| 48 | + .group_by(&:first) # { comp1 => [[comp1, ver1], [comp1, ver2], ...], ... } |
| 49 | + .transform_values do |pairs| # { comp1 => verN, ... } |
| 50 | + pairs.max_by { |_, ver| parse_version(ver) }.last |
| 51 | + end |
| 52 | + |
| 53 | + { 'components' => component_data, 'projects' => project_data } |
| 54 | +end |
| 55 | + |
| 56 | +# We use `vanagon inspect` to get components instead of parsing the |
| 57 | +# files directly, so that we can populate a list of components per |
| 58 | +# platform, as they can differ between platforms. |
| 59 | +namespace :vox do |
| 60 | + desc 'Update component_info.json file with current component versions' |
| 61 | + task :update_component_info, [:tag] do |_, args| |
| 62 | + abort 'You must provide the tag that will be used for this release.' if args[:tag].nil? || args[:tag].empty? |
| 63 | + |
| 64 | + File.write('component_info.json', '{}') unless File.exist?('component_info.json') |
| 65 | + data = JSON.parse(File.read('component_info.json')) |
| 66 | + |
| 67 | + data[args[:tag]] = component_info |
| 68 | + |
| 69 | + # Put the new data on top |
| 70 | + data = data.to_a.rotate(-1).to_h |
| 71 | + |
| 72 | + File.write('component_info.json', JSON.pretty_generate(data)) |
| 73 | + puts "Updated component_info.json with data for tag #{args[:tag]}" |
| 74 | + end |
| 75 | + |
| 76 | + desc 'Regenerate component_info.json with all tags' |
| 77 | + task :regenerate_component_info do |
| 78 | + # Clone a copy of the repo to avoid messing up the current working dir |
| 79 | + `git clone https://github.com/openvoxproject/puppet-runtime puppet-runtime-tmp` |
| 80 | + begin |
| 81 | + all_data = {} |
| 82 | + Dir.chdir('puppet-runtime-tmp') do |
| 83 | + `git fetch origin --tags --prune --prune-tags` |
| 84 | + `git tag --sort=creatordate | awk '$0=="#{FIRST_TAG}"{seen=1; next} seen'`.split("\n").each do |tag| |
| 85 | + puts "Checking out tag #{tag}..." |
| 86 | + `git checkout #{tag}` |
| 87 | + Bundler.with_unbundled_env do |
| 88 | + `BUNDLER_GEMFILE=./Gemfile bundle install --path .bundle` |
| 89 | + all_data[tag] = component_info |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + # Reverse order to latest is on top |
| 94 | + all_data = all_data.to_a.reverse.to_h |
| 95 | + File.write('component_info.json', JSON.pretty_generate(all_data)) |
| 96 | + puts 'Regenerated component_info.json with data for all tags.' |
| 97 | + ensure |
| 98 | + FileUtils.rm_rf('puppet-runtime-tmp') |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments