feat(monorel/release): write+commit .goreleaser.yaml only when missing

Previously `monorel release` always overwrote .goreleaser.yaml, discarding
any manual customisations the user had made.

New behaviour:
- Missing → write the monorel-generated config and commit it (same as init)
- Exists, stock {{ .ProjectName }} in a monorepo subdir → warn and ask the
  user to update it manually before re-running
- Exists, looks fine → print "leaving unchanged" and continue
This commit is contained in:
AJ ONeal 2026-02-28 23:28:42 -07:00
parent bcd70c3843
commit 1e766daa14
No known key found for this signature in database

View File

@ -1008,25 +1008,35 @@ func processModule(group *moduleGroup, relPath string) {
"--pretty=format:- %h %s", "--", ".") "--pretty=format:- %h %s", "--", ".")
} }
// Write .goreleaser.yaml next to go.mod. // Write .goreleaser.yaml if it does not yet exist, then commit it.
// Warn if an existing file uses {{ .ProjectName }} (stock goreleaser config) // If it already exists, leave it unchanged — updates require manual review.
// and the module is a monorepo subdirectory (go.mod not adjacent to .git/).
yamlContent := goreleaserYAML(projectName, bins) yamlContent := goreleaserYAML(projectName, bins)
yamlPath := filepath.Join(modRoot, ".goreleaser.yaml") yamlPath := filepath.Join(modRoot, ".goreleaser.yaml")
if existing, err := os.ReadFile(yamlPath); err == nil { if existing, err := os.ReadFile(yamlPath); err == nil {
// File exists — leave it for manual review.
hasProjectName := strings.Contains(string(existing), "{{ .ProjectName }}") || hasProjectName := strings.Contains(string(existing), "{{ .ProjectName }}") ||
strings.Contains(string(existing), "{{.ProjectName}}") strings.Contains(string(existing), "{{.ProjectName}}")
gitInfo, gitErr := os.Stat(filepath.Join(modRoot, ".git")) gitInfo, gitErr := os.Stat(filepath.Join(modRoot, ".git"))
atGitRoot := gitErr == nil && gitInfo.IsDir() atGitRoot := gitErr == nil && gitInfo.IsDir()
if hasProjectName && !atGitRoot { if hasProjectName && !atGitRoot {
fmt.Fprintf(os.Stderr, "warning: %s: contains {{ .ProjectName }} but module is a monorepo subdirectory;\n", yamlPath) fmt.Fprintf(os.Stderr, "warning: %s uses {{ .ProjectName }} in a monorepo subdirectory;\n", yamlPath)
fmt.Fprintf(os.Stderr, " replacing stock goreleaser config with monorel-generated config.\n") fmt.Fprintf(os.Stderr, " update it manually and re-run 'monorel release'\n")
} else {
fmt.Fprintf(os.Stderr, "note: %s exists; leaving unchanged\n", yamlPath)
}
} else {
// File is missing — write it and commit it so goreleaser has a config.
if err := os.WriteFile(yamlPath, []byte(yamlContent), 0o644); err != nil {
fatalf("writing %s: %v", yamlPath, err)
}
fmt.Fprintf(os.Stderr, "wrote %s\n", yamlPath)
mustRunIn(modRoot, "git", "add", ".goreleaser.yaml")
if status := runIn(modRoot, "git", "status", "--porcelain", "--", ".goreleaser.yaml"); status != "" {
commitMsg := "chore(release): add .goreleaser.yaml for " + projectName
mustRunIn(modRoot, "git", "commit", "-m", commitMsg)
fmt.Fprintf(os.Stderr, "committed: %s\n", commitMsg)
} }
} }
if err := os.WriteFile(yamlPath, []byte(yamlContent), 0o644); err != nil {
fatalf("writing %s: %v", yamlPath, err)
}
fmt.Fprintf(os.Stderr, "wrote %s\n", yamlPath)
headSHA := mustRunIn(modRoot, "git", "rev-parse", "HEAD") headSHA := mustRunIn(modRoot, "git", "rev-parse", "HEAD")
printModuleScript(relPath, projectName, bins, printModuleScript(relPath, projectName, bins,