|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +var manifestVersionRe = regexp.MustCompile(`(Version:\s*)"(\d+\.\d+\.\d+)"`) |
| 14 | +var docVersionRowRe = regexp.MustCompile("(?m)^\\|\\s*Version\\s*\\|\\s*`([^`]+)`\\s*\\|\\s*$") |
| 15 | + |
| 16 | +func runGenBump(args []string) int { |
| 17 | + fs := flag.NewFlagSet("gen-bump", flag.ContinueOnError) |
| 18 | + all := fs.Bool("all", false, "bump all generators") |
| 19 | + name := fs.String("name", "", "bump a specific generator by name (comma-separated for multiple)") |
| 20 | + bump := fs.String("bump", "patch", "bump type: patch, minor, or major") |
| 21 | + set := fs.String("set", "", "set an explicit version instead of bumping (e.g. 1.2.3)") |
| 22 | + if err := fs.Parse(args); err != nil { |
| 23 | + return 2 |
| 24 | + } |
| 25 | + |
| 26 | + if !*all && *name == "" { |
| 27 | + fmt.Fprintln(os.Stderr, "dot gen-bump: --all or --name <name> is required") |
| 28 | + fs.Usage() |
| 29 | + return 2 |
| 30 | + } |
| 31 | + if *all && *name != "" { |
| 32 | + fmt.Fprintln(os.Stderr, "dot gen-bump: --all and --name are mutually exclusive") |
| 33 | + return 2 |
| 34 | + } |
| 35 | + if *set != "" && !isValidVersion(*set) { |
| 36 | + fmt.Fprintf(os.Stderr, "dot gen-bump: --set %q is not a valid semver (expected MAJOR.MINOR.PATCH)\n", *set) |
| 37 | + return 2 |
| 38 | + } |
| 39 | + |
| 40 | + var names []string |
| 41 | + if *name != "" { |
| 42 | + for _, n := range strings.Split(*name, ",") { |
| 43 | + n = strings.TrimSpace(n) |
| 44 | + if n != "" { |
| 45 | + names = append(names, n) |
| 46 | + } |
| 47 | + } |
| 48 | + } else { |
| 49 | + found, err := listGeneratorDirs("generators") |
| 50 | + if err != nil { |
| 51 | + fmt.Fprintln(os.Stderr, "dot gen-bump:", err) |
| 52 | + return 1 |
| 53 | + } |
| 54 | + names = found |
| 55 | + } |
| 56 | + |
| 57 | + if len(names) == 0 { |
| 58 | + fmt.Fprintln(os.Stderr, "dot gen-bump: no generators found") |
| 59 | + return 1 |
| 60 | + } |
| 61 | + |
| 62 | + label := *bump |
| 63 | + if *set != "" { |
| 64 | + label = *set |
| 65 | + } |
| 66 | + PrintHeading(fmt.Sprintf("gen-bump (%s)", label)) |
| 67 | + |
| 68 | + anyFailed := false |
| 69 | + for _, n := range names { |
| 70 | + var err error |
| 71 | + if *set != "" { |
| 72 | + err = setGenVersion(n, *set) |
| 73 | + } else { |
| 74 | + err = bumpGenVersion(n, *bump) |
| 75 | + } |
| 76 | + if err != nil { |
| 77 | + fmt.Fprintf(os.Stderr, " error: %s: %v\n", n, err) |
| 78 | + anyFailed = true |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if anyFailed { |
| 83 | + return 1 |
| 84 | + } |
| 85 | + return 0 |
| 86 | +} |
| 87 | + |
| 88 | +func listGeneratorDirs(root string) ([]string, error) { |
| 89 | + entries, err := os.ReadDir(root) |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("read %s: %w", root, err) |
| 92 | + } |
| 93 | + var names []string |
| 94 | + for _, e := range entries { |
| 95 | + if !e.IsDir() { |
| 96 | + continue |
| 97 | + } |
| 98 | + if _, err := os.Stat(filepath.Join(root, e.Name(), "manifest.go")); err == nil { |
| 99 | + names = append(names, e.Name()) |
| 100 | + } |
| 101 | + } |
| 102 | + return names, nil |
| 103 | +} |
| 104 | + |
| 105 | +func bumpGenVersion(name, bumpType string) error { |
| 106 | + manifestPath := filepath.Join("generators", name, "manifest.go") |
| 107 | + content, err := os.ReadFile(manifestPath) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("read %s: %w", manifestPath, err) |
| 110 | + } |
| 111 | + |
| 112 | + var oldVer, newVer string |
| 113 | + var bumpErr error |
| 114 | + updated := manifestVersionRe.ReplaceAllStringFunc(string(content), func(match string) string { |
| 115 | + if bumpErr != nil { |
| 116 | + return match |
| 117 | + } |
| 118 | + sub := manifestVersionRe.FindStringSubmatch(match) |
| 119 | + if sub == nil { |
| 120 | + return match |
| 121 | + } |
| 122 | + oldVer = sub[2] |
| 123 | + var bumped string |
| 124 | + switch bumpType { |
| 125 | + case "major": |
| 126 | + bumped, bumpErr = bumpMajorVer(sub[2]) |
| 127 | + case "minor": |
| 128 | + bumped, bumpErr = bumpMinorVer(sub[2]) |
| 129 | + default: |
| 130 | + bumped, bumpErr = bumpPatchVer(sub[2]) |
| 131 | + } |
| 132 | + if bumpErr != nil { |
| 133 | + return match |
| 134 | + } |
| 135 | + newVer = bumped |
| 136 | + return sub[1] + `"` + bumped + `"` |
| 137 | + }) |
| 138 | + if bumpErr != nil { |
| 139 | + return bumpErr |
| 140 | + } |
| 141 | + if newVer == "" { |
| 142 | + return fmt.Errorf("version field not found in %s", manifestPath) |
| 143 | + } |
| 144 | + fmt.Printf(" %-40s %s → %s\n", name, oldVer, newVer) |
| 145 | + if err := os.WriteFile(manifestPath, []byte(updated), 0644); err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + syncGenDocVersion(name, newVer) |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func setGenVersion(name, version string) error { |
| 153 | + manifestPath := filepath.Join("generators", name, "manifest.go") |
| 154 | + content, err := os.ReadFile(manifestPath) |
| 155 | + if err != nil { |
| 156 | + return fmt.Errorf("read %s: %w", manifestPath, err) |
| 157 | + } |
| 158 | + |
| 159 | + var oldVer string |
| 160 | + updated := manifestVersionRe.ReplaceAllStringFunc(string(content), func(match string) string { |
| 161 | + sub := manifestVersionRe.FindStringSubmatch(match) |
| 162 | + if sub == nil { |
| 163 | + return match |
| 164 | + } |
| 165 | + oldVer = sub[2] |
| 166 | + return sub[1] + `"` + version + `"` |
| 167 | + }) |
| 168 | + if oldVer == "" { |
| 169 | + return fmt.Errorf("version field not found in %s", manifestPath) |
| 170 | + } |
| 171 | + fmt.Printf(" %-40s %s → %s\n", name, oldVer, version) |
| 172 | + if err := os.WriteFile(manifestPath, []byte(updated), 0644); err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + syncGenDocVersion(name, version) |
| 176 | + return nil |
| 177 | +} |
| 178 | + |
| 179 | +// syncGenDocVersion updates the Version row in the generator's doc page if it exists. |
| 180 | +func syncGenDocVersion(name, version string) { |
| 181 | + docPath := filepath.Join("docs", "contributor", "generators", name+".md") |
| 182 | + content, err := os.ReadFile(docPath) |
| 183 | + if err != nil { |
| 184 | + return |
| 185 | + } |
| 186 | + updated := docVersionRowRe.ReplaceAllString(string(content), "| Version | `"+version+"` |") |
| 187 | + if updated == string(content) { |
| 188 | + return |
| 189 | + } |
| 190 | + _ = os.WriteFile(docPath, []byte(updated), 0644) |
| 191 | +} |
| 192 | + |
| 193 | +func isValidVersion(v string) bool { |
| 194 | + parts := strings.SplitN(v, ".", 3) |
| 195 | + if len(parts) != 3 { |
| 196 | + return false |
| 197 | + } |
| 198 | + for _, p := range parts { |
| 199 | + if _, err := strconv.Atoi(p); err != nil { |
| 200 | + return false |
| 201 | + } |
| 202 | + } |
| 203 | + return true |
| 204 | +} |
| 205 | + |
| 206 | +func bumpMajorVer(v string) (string, error) { |
| 207 | + parts := strings.SplitN(v, ".", 3) |
| 208 | + if len(parts) != 3 { |
| 209 | + return "", fmt.Errorf("unexpected version format %q", v) |
| 210 | + } |
| 211 | + major, err := strconv.Atoi(parts[0]) |
| 212 | + if err != nil { |
| 213 | + return "", fmt.Errorf("non-numeric major in %q: %w", v, err) |
| 214 | + } |
| 215 | + return strconv.Itoa(major+1) + ".0.0", nil |
| 216 | +} |
| 217 | + |
| 218 | +func bumpMinorVer(v string) (string, error) { |
| 219 | + parts := strings.SplitN(v, ".", 3) |
| 220 | + if len(parts) != 3 { |
| 221 | + return "", fmt.Errorf("unexpected version format %q", v) |
| 222 | + } |
| 223 | + minor, err := strconv.Atoi(parts[1]) |
| 224 | + if err != nil { |
| 225 | + return "", fmt.Errorf("non-numeric minor in %q: %w", v, err) |
| 226 | + } |
| 227 | + return parts[0] + "." + strconv.Itoa(minor+1) + ".0", nil |
| 228 | +} |
| 229 | + |
| 230 | +func bumpPatchVer(v string) (string, error) { |
| 231 | + parts := strings.SplitN(v, ".", 3) |
| 232 | + if len(parts) != 3 { |
| 233 | + return "", fmt.Errorf("unexpected version format %q", v) |
| 234 | + } |
| 235 | + patch, err := strconv.Atoi(parts[2]) |
| 236 | + if err != nil { |
| 237 | + return "", fmt.Errorf("non-numeric patch in %q: %w", v, err) |
| 238 | + } |
| 239 | + return parts[0] + "." + parts[1] + "." + strconv.Itoa(patch+1), nil |
| 240 | +} |
0 commit comments