Skip to content

Commit f8e02f5

Browse files
add support for autoupdating the snippets/includes
1 parent 6faa1b8 commit f8e02f5

File tree

1 file changed

+76
-12
lines changed

1 file changed

+76
-12
lines changed

internal/changelog/renderer.go

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (r Renderer) Render() error {
186186
if template == "markdown-index" {
187187
return path.Join(r.dest, r.changelog.Version, "index.md")
188188
} else if template == "markdown-breaking" {
189-
return path.Join(r.dest, r.changelog.Version, "breaking.md")
189+
return path.Join(r.dest, r.changelog.Version, "breaking-changes.md")
190190
} else if template == "markdown-deprecations" {
191191
return path.Join(r.dest, r.changelog.Version, "deprecations.md")
192192
} else {
@@ -208,19 +208,35 @@ func (r Renderer) Template() ([]byte, error) {
208208
var err error
209209

210210
if embeddedFileName, ok := assets.GetEmbeddedTemplates()[r.templ]; ok {
211-
if r.templ == "markdown-index" {
212-
data, err = assets.MarkdownIndexTemplate.ReadFile(embeddedFileName)
213-
} else if r.templ == "markdown-breaking" {
214-
data, err = assets.MarkdownBreakingTemplate.ReadFile(embeddedFileName)
215-
} else if r.templ == "markdown-deprecations" {
216-
data, err = assets.MarkdownDeprecationsTemplate.ReadFile(embeddedFileName)
217-
} else if r.templ == "asciidoc-embedded" {
218-
data, err = assets.AsciidocTemplate.ReadFile(embeddedFileName)
211+
var readFunc func(string) ([]byte, error)
212+
switch r.templ {
213+
case "markdown-index":
214+
readFunc = assets.MarkdownIndexTemplate.ReadFile
215+
case "markdown-breaking":
216+
readFunc = assets.MarkdownBreakingTemplate.ReadFile
217+
case "markdown-deprecations":
218+
readFunc = assets.MarkdownDeprecationsTemplate.ReadFile
219+
case "asciidoc-embedded":
220+
readFunc = assets.AsciidocTemplate.ReadFile
219221
}
220-
if err != nil {
221-
return []byte{}, fmt.Errorf("cannot read embedded template: %s %w", embeddedFileName, err)
222+
if readFunc != nil {
223+
data, err = readFunc(embeddedFileName)
224+
if err != nil {
225+
return nil, fmt.Errorf("cannot read embedded template: %s %w", embeddedFileName, err)
226+
}
227+
// If using the snippet/include model, update the includes
228+
if strings.Contains(r.dest, "release-notes/_snippets") {
229+
switch r.templ {
230+
case "markdown-index":
231+
addInclude(r.fs, r.changelog.Version, r.dest, "index")
232+
case "markdown-breaking":
233+
addInclude(r.fs, r.changelog.Version, r.dest, "breaking-changes")
234+
case "markdown-deprecations":
235+
addInclude(r.fs, r.changelog.Version, r.dest, "deprecations")
236+
}
237+
}
238+
return data, nil
222239
}
223-
return data, nil
224240
}
225241

226242
data, err = afero.ReadFile(r.fs, r.templ)
@@ -321,3 +337,51 @@ func buildTitleByComponents(entries []Entry) string {
321337
return match
322338
}
323339
}
340+
341+
func addInclude(fs afero.Fs, version string, dest string, templ string) {
342+
// Get minor version
343+
re := regexp.MustCompile(`^\d+\.\d+`)
344+
matches := re.FindStringSubmatch(version)
345+
if len(matches) == 0 {
346+
fmt.Printf("Could not get minor version from: %v\n", version)
347+
return
348+
}
349+
minorVersion := matches[0]
350+
351+
// Get include directory
352+
includeDirRe := regexp.MustCompile(`/release-notes/.+$`)
353+
includeDirMatches := includeDirRe.FindStringSubmatch(dest)
354+
if len(includeDirMatches) == 0 {
355+
fmt.Printf("Could not derive include directory from: %v\n", dest)
356+
return
357+
}
358+
includeDir := includeDirMatches[0]
359+
360+
// Get the snippet file listing all patches for the minor
361+
minorFilePath := fmt.Sprintf("%s/%s/%s.md", dest, templ, minorVersion)
362+
minorFileContent, err := afero.ReadFile(fs, minorFilePath)
363+
// If no file exists for the specified minor:
364+
// * Create the file
365+
// * Include it in the snippet file listing all minors
366+
if err != nil {
367+
// Create the file
368+
afero.WriteFile(fs, minorFilePath, nil, changelogFilePerm)
369+
fmt.Printf("Created new include file: %s\n", minorFilePath)
370+
// Include it in the snippet file listing all minors
371+
templateTypeFilePath := fmt.Sprintf("%s/%s.md", dest, templ)
372+
templateTypeFileContent, err := afero.ReadFile(fs, templateTypeFilePath)
373+
if err != nil {
374+
fmt.Printf("Could not get file: %s\n", templateTypeFilePath)
375+
return
376+
}
377+
// Add the new minor version to the top of the existing content
378+
newMinorVersionInclude := fmt.Sprintf(":::{include} %s/%s/%s.md\n:::", includeDir, templ, minorVersion)
379+
newContent := fmt.Sprintf("%s\n\n%s", newMinorVersionInclude, templateTypeFileContent)
380+
afero.WriteFile(fs, templateTypeFilePath, []byte(newContent), changelogFilePerm)
381+
}
382+
383+
// Add new patch version to top of the existing content
384+
newPatchVersionInclude := fmt.Sprintf(":::{include} %s/%s/%s.md\n:::", includeDir, version, templ)
385+
newContent := fmt.Sprintf("%s\n\n%s", newPatchVersionInclude, minorFileContent)
386+
afero.WriteFile(fs, minorFilePath, []byte(newContent), changelogFilePerm)
387+
}

0 commit comments

Comments
 (0)