Skip to content

Commit

Permalink
merge all projects if projects are being used (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniscruff authored Oct 22, 2023
1 parent 18607a1 commit 6f8dc52
Showing 3 changed files with 32 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/Changed-20231021-140739.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Changed
body: Merge will merge all projects versions if projects are enabled.
time: 2023-10-21T14:07:39.098558299-07:00
custom:
Issue: "552"
60 changes: 27 additions & 33 deletions cmd/merge.go
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@ type Merge struct {
// cli args
DryRun bool
UnreleasedHeader string
Project string

// dependencies
ReadFile shared.ReadFiler
@@ -66,39 +65,34 @@ Note that a newline is added between each version file.`,
"",
"Include unreleased changes with this value as the header",
)
cmd.Flags().StringVarP(
&m.Project,
"project", "j",
"",
"(Preview) Specify which project we are merging",
)

m.Command = cmd

return m
}

//nolint:gocyclo
func (m *Merge) Run(cmd *cobra.Command, args []string) error {
config, err := core.LoadConfig(m.ReadFile)
cfg, err := core.LoadConfig(m.ReadFile)
if err != nil {
return err
}

changelogPath := config.ChangelogPath

if len(config.Projects) > 0 {
var pc *core.ProjectConfig

pc, err = config.Project(m.Project)
if err != nil {
return err
// 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)
if err != nil {
return err
}
}

m.Project = pc.Key
changelogPath = pc.ChangelogPath
return nil
}

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

func (m *Merge) mergeProject(cfg *core.Config, project, changelogPath string) error {
var writer io.Writer
if m.DryRun {
writer = m.Command.OutOrStdout()
@@ -111,25 +105,25 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error {
writer = changeFile
}

allVersions, err := core.GetAllVersions(m.ReadDir, config, false, m.Project)
allVersions, err := core.GetAllVersions(m.ReadDir, cfg, false, project)
if err != nil {
return err
}

if config.HeaderPath != "" {
err = core.AppendFile(m.OpenFile, writer, filepath.Join(config.ChangesDir, config.HeaderPath))
if cfg.HeaderPath != "" {
err = core.AppendFile(m.OpenFile, writer, filepath.Join(cfg.ChangesDir, cfg.HeaderPath))
if err != nil {
return err
}

_ = core.WriteNewlines(writer, config.Newlines.AfterChangelogHeader)
_ = core.WriteNewlines(writer, cfg.Newlines.AfterChangelogHeader)
}

if m.UnreleasedHeader != "" {
var unrelErr error

allChanges, unrelErr := core.GetChanges(
config,
cfg,
nil,
m.ReadDir,
m.ReadFile,
@@ -141,14 +135,14 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error {

// Make sure we have any changes before writing the unreleased content.
if len(allChanges) > 0 {
_ = core.WriteNewlines(writer, config.Newlines.BeforeChangelogVersion)
_ = core.WriteNewlines(writer, config.Newlines.BeforeVersion)
_ = core.WriteNewlines(writer, cfg.Newlines.BeforeChangelogVersion)
_ = core.WriteNewlines(writer, cfg.Newlines.BeforeVersion)
_, _ = writer.Write([]byte(m.UnreleasedHeader))
_ = core.WriteNewlines(writer, config.Newlines.AfterVersion)
_ = core.WriteNewlines(writer, cfg.Newlines.AfterVersion)

// create a fake batch to write the changes
b := &Batch{
config: config,
config: cfg,
writer: writer,
TemplateCache: m.TemplateCache,
}
@@ -159,20 +153,20 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error {
}

_ = core.WriteNewlines(b.writer, b.config.Newlines.EndOfVersion)
_ = core.WriteNewlines(writer, config.Newlines.AfterChangelogVersion)
_ = core.WriteNewlines(writer, cfg.Newlines.AfterChangelogVersion)
}
}

for _, version := range allVersions {
_ = core.WriteNewlines(writer, config.Newlines.BeforeChangelogVersion)
versionPath := filepath.Join(config.ChangesDir, m.Project, version.Original()+"."+config.VersionExt)
_ = core.WriteNewlines(writer, cfg.Newlines.BeforeChangelogVersion)
versionPath := filepath.Join(cfg.ChangesDir, project, version.Original()+"."+cfg.VersionExt)

err = core.AppendFile(m.OpenFile, writer, versionPath)
if err != nil {
return err
}

_ = core.WriteNewlines(writer, config.Newlines.AfterChangelogVersion)
_ = core.WriteNewlines(writer, cfg.Newlines.AfterChangelogVersion)
}

if len(allVersions) == 0 {
@@ -194,7 +188,7 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error {
Metadata: version.Metadata(),
}

for _, rep := range config.Replacements {
for _, rep := range cfg.Replacements {
err = rep.Execute(m.ReadFile, m.WriteFile, replaceData)
if err != nil {
return err
2 changes: 0 additions & 2 deletions cmd/merge_test.go
Original file line number Diff line number Diff line change
@@ -94,7 +94,6 @@ func TestMergeVersionsSuccessfullyWithProject(t *testing.T) {
os.Create,
core.NewTemplateCache(),
)
cmd.Project = "a"

err := cmd.Run(cmd.Command, nil)
then.Nil(t, err)
@@ -126,7 +125,6 @@ func TestMergeVersionsErrorInvalidProject(t *testing.T) {
os.Create,
core.NewTemplateCache(),
)
cmd.Project = "not_found"

err := cmd.Run(cmd.Command, nil)
then.NotNil(t, err)

0 comments on commit 6f8dc52

Please sign in to comment.