Skip to content

Commit

Permalink
Support per project replacements (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniscruff authored Jan 15, 2024
1 parent 494d043 commit b1da370
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/Added-20240115-130635.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Added
body: Replacements can now be configured per project in a monorepo setup
time: 2024-01-15T13:06:35.399119346-08:00
custom:
Issue: "591"
12 changes: 8 additions & 4 deletions cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error {
// If we have projects, merge all of them.
if len(cfg.Projects) > 0 {
for _, pc := range cfg.Projects {
err = m.mergeProject(cfg, pc.Key, pc.ChangelogPath)
err = m.mergeProject(cfg, pc.Key, pc.ChangelogPath, pc.Replacements)
if err != nil {
return err
}
Expand All @@ -89,10 +89,14 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error {
return nil
}

return m.mergeProject(cfg, "", cfg.ChangelogPath)
return m.mergeProject(cfg, "", cfg.ChangelogPath, cfg.Replacements)
}

func (m *Merge) mergeProject(cfg *core.Config, project, changelogPath string) error {
func (m *Merge) mergeProject(
cfg *core.Config,
project, changelogPath string,
replacements []core.Replacement,
) error {
var writer io.Writer
if m.DryRun {
writer = m.Command.OutOrStdout()
Expand Down Expand Up @@ -188,7 +192,7 @@ func (m *Merge) mergeProject(cfg *core.Config, project, changelogPath string) er
Metadata: version.Metadata(),
}

for _, rep := range cfg.Replacements {
for _, rep := range replacements {
err = rep.Execute(m.ReadFile, m.WriteFile, replaceData)
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions cmd/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,20 @@ func TestMergeVersionsSuccessfullyWithProject(t *testing.T) {
Label: "A thing",
Key: "a",
ChangelogPath: "a/thing/CHANGELOG.md",
Replacements: []core.Replacement{
{
Path: "a/VERSION",
Find: "version",
Replace: "{{.Version}}",
},
},
},
}
then.WithTempDirConfig(t, cfg)

then.WriteFile(t, []byte("first version\n"), cfg.ChangesDir, "a", "v0.1.0.md")
then.WriteFile(t, []byte("second version\n"), cfg.ChangesDir, "a", "v0.2.0.md")
then.WriteFile(t, []byte("version\n"), "a", "VERSION")
then.Nil(t, os.MkdirAll(filepath.Join("a", "thing"), core.CreateDirMode))

cmd := NewMerge(
Expand All @@ -102,6 +110,7 @@ func TestMergeVersionsSuccessfullyWithProject(t *testing.T) {
first version
`
then.FileContents(t, changeContents, "a", "thing", "CHANGELOG.md")
then.FileContents(t, "v0.2.0\n", "a", "VERSION")
}

func TestMergeVersionsErrorMissingProjectDir(t *testing.T) {
Expand Down
10 changes: 9 additions & 1 deletion core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,18 @@ type ProjectConfig struct {
// example: yaml
// key: frontend
Key string `yaml:"key"`
//ChangelogPath is the path to the changelog for this project.
// ChangelogPath is the path to the changelog for this project.
// example: yaml
// changelog: src/frontend/CHANGELOG.md
ChangelogPath string `yaml:"changelog"`
// Replacements to run when merging a changelog for our project.
// example: yaml
// # nodejs package.json replacement
// replacements:
// - path: ui/package.json
// find: ' "version": ".*",'
// replace: ' "version": "{{.VersionNoPrefix}}",'
Replacements []Replacement `yaml:"replacements"`
}

// Config handles configuration for a project.
Expand Down

0 comments on commit b1da370

Please sign in to comment.