|
| 1 | +require 'json' |
| 2 | +require 'yaml' |
| 3 | +require 'fileutils' |
| 4 | + |
| 5 | +module ChangesetLib |
| 6 | + CHANGESET_DIR = '.changesets' |
| 7 | + RELEASE_META_FILE = '_current_release.json' |
| 8 | + VERSION_FILE = 'turnkey_client/lib/turnkey_client/version.rb' |
| 9 | + CONFIG_FILE = 'turnkey_client_inputs/config.json' |
| 10 | + CHANGELOG_FILE = 'CHANGELOG.md' |
| 11 | + CHANGELOG_HEADER = '# Changelog' |
| 12 | + |
| 13 | + ChangesetEntry = Struct.new(:path, :title, :date, :bump, :note, keyword_init: true) |
| 14 | + |
| 15 | + def slugify(str) |
| 16 | + slug = str.downcase |
| 17 | + .gsub(/[^a-z0-9\s_-]/, '') |
| 18 | + .gsub(/[\s_-]+/, '-') |
| 19 | + .gsub(/\A-+|-+\z/, '') |
| 20 | + slug.empty? ? 'changeset' : slug |
| 21 | + end |
| 22 | + |
| 23 | + def format_timestamp(time) |
| 24 | + time.strftime('%Y%m%d-%H%M%S') |
| 25 | + end |
| 26 | + |
| 27 | + def date_only(time) |
| 28 | + time.strftime('%Y-%m-%d') |
| 29 | + end |
| 30 | + |
| 31 | + def today_date |
| 32 | + date_only(Time.now) |
| 33 | + end |
| 34 | + |
| 35 | + def escape_yaml_string(str) |
| 36 | + str.gsub('\\', '\\\\\\\\').gsub('"', '\\"') |
| 37 | + end |
| 38 | + |
| 39 | + def parse_version(version_str) |
| 40 | + base = version_str.strip.split(/[-+]/, 2).first |
| 41 | + parts = base.split('.') |
| 42 | + unless parts.length == 3 && parts.all? { |p| p.match?(/\A\d+\z/) } |
| 43 | + raise "Invalid version format: '#{version_str}', expected X.Y.Z" |
| 44 | + end |
| 45 | + parts.map(&:to_i) |
| 46 | + end |
| 47 | + |
| 48 | + def next_version(current, bump) |
| 49 | + major, minor, patch = parse_version(current) |
| 50 | + case bump |
| 51 | + when 'major' |
| 52 | + "#{major + 1}.0.0" |
| 53 | + when 'minor' |
| 54 | + "#{major}.#{minor + 1}.0" |
| 55 | + when 'patch' |
| 56 | + "#{major}.#{minor}.#{patch + 1}" |
| 57 | + else |
| 58 | + raise "Unknown bump type: '#{bump}'" |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def bump_level(bump) |
| 63 | + case bump |
| 64 | + when 'patch' then 1 |
| 65 | + when 'minor' then 2 |
| 66 | + when 'major' then 3 |
| 67 | + else raise "Unknown bump type: '#{bump}'" |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def max_bump(bumps) |
| 72 | + bumps.max_by { |b| bump_level(b) } |
| 73 | + end |
| 74 | + |
| 75 | + def read_current_version |
| 76 | + unless File.exist?(VERSION_FILE) |
| 77 | + raise "Cannot read version: #{VERSION_FILE} does not exist" |
| 78 | + end |
| 79 | + content = File.read(VERSION_FILE) |
| 80 | + match = content.match(/VERSION\s*=\s*['"]([^'"]+)['"]/) |
| 81 | + unless match |
| 82 | + raise "Cannot find VERSION constant in #{VERSION_FILE}" |
| 83 | + end |
| 84 | + match[1] |
| 85 | + end |
| 86 | + |
| 87 | + def write_version_rb(new_version) |
| 88 | + content = File.read(VERSION_FILE) |
| 89 | + updated = content.sub(/VERSION\s*=\s*['"][^'"]+['"]/, "VERSION = '#{new_version}'") |
| 90 | + File.write(VERSION_FILE, updated) |
| 91 | + end |
| 92 | + |
| 93 | + def write_config_json(new_version) |
| 94 | + content = File.read(CONFIG_FILE) |
| 95 | + data = JSON.parse(content) |
| 96 | + data['gemVersion'] = new_version |
| 97 | + File.write(CONFIG_FILE, JSON.pretty_generate(data) + "\n") |
| 98 | + end |
| 99 | + |
| 100 | + def load_changesets |
| 101 | + dir = CHANGESET_DIR |
| 102 | + return [] unless Dir.exist?(dir) |
| 103 | + |
| 104 | + Dir.glob(File.join(dir, '*.md')) |
| 105 | + .reject { |f| File.basename(f).start_with?('_') } |
| 106 | + .sort |
| 107 | + .map { |f| parse_changeset_file(f) } |
| 108 | + .compact |
| 109 | + end |
| 110 | + |
| 111 | + def parse_changeset_file(path) |
| 112 | + raw = File.read(path).strip |
| 113 | + unless raw.start_with?('---') |
| 114 | + warn "warning: #{path} does not start with frontmatter delimiter, skipping" |
| 115 | + return nil |
| 116 | + end |
| 117 | + |
| 118 | + parts = raw.split(/^---\s*$/m) |
| 119 | + if parts.length < 3 |
| 120 | + warn "warning: #{path} has malformed frontmatter, skipping" |
| 121 | + return nil |
| 122 | + end |
| 123 | + |
| 124 | + frontmatter = YAML.safe_load(parts[1]) |
| 125 | + body = parts[2..].join('---').strip |
| 126 | + body = '_No additional notes._' if body.empty? |
| 127 | + |
| 128 | + ChangesetEntry.new( |
| 129 | + path: path, |
| 130 | + title: frontmatter['title'], |
| 131 | + date: frontmatter['date'] || today_date, |
| 132 | + bump: frontmatter['bump'] || 'patch', |
| 133 | + note: body |
| 134 | + ) |
| 135 | + rescue => e |
| 136 | + warn "warning: failed to parse changeset #{path}: #{e.message}" |
| 137 | + nil |
| 138 | + end |
| 139 | + |
| 140 | + def read_release_meta |
| 141 | + meta_path = File.join(CHANGESET_DIR, RELEASE_META_FILE) |
| 142 | + unless File.exist?(meta_path) |
| 143 | + raise "No release metadata found at #{meta_path}. Run `make version` first." |
| 144 | + end |
| 145 | + JSON.parse(File.read(meta_path)) |
| 146 | + end |
| 147 | + |
| 148 | + def write_release_meta(meta) |
| 149 | + FileUtils.mkdir_p(CHANGESET_DIR) |
| 150 | + meta_path = File.join(CHANGESET_DIR, RELEASE_META_FILE) |
| 151 | + File.write(meta_path, JSON.pretty_generate(meta) + "\n") |
| 152 | + end |
| 153 | + |
| 154 | + def delete_processed_changesets(meta) |
| 155 | + (meta['changes'] || []).each do |change| |
| 156 | + path = change['changesetPath'] |
| 157 | + if path && File.exist?(path) |
| 158 | + File.delete(path) |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + meta_path = File.join(CHANGESET_DIR, RELEASE_META_FILE) |
| 163 | + File.delete(meta_path) if File.exist?(meta_path) |
| 164 | + end |
| 165 | +end |
0 commit comments