Skip to content

Commit

Permalink
Added support for specifying a non-default changelog file
Browse files Browse the repository at this point in the history
Also added support for resolving files relative to `.sfreleaser` location.
  • Loading branch information
maoueh committed May 18, 2023
1 parent 9020199 commit 4f6c321
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v0.4.2

* Added support for resolving files relative to `.sfreleaser` location.

* Added support for specifying a non-default changelog file.

## v0.4.1

* Added checks that `docker` CLI exists and also that `docker info` works properly.
Expand Down
7 changes: 3 additions & 4 deletions cmd/sfreleaser/bashism.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ func (i *commandInfo) String() string {
return strings.Join(append(append(i.env, i.command), i.args...), " ")
}

func run(inputs ...string) (output string, err error) {
var info *commandInfo
output, info, err = maybeRun(inputs...)
func run(inputs ...string) (output string) {
output, info, err := maybeRun(inputs...)
cli.NoError(err, "Command %q failed", info)

return output, nil
return output
}

// runSilent is like [run] but do not print the command output but do print
Expand Down
18 changes: 18 additions & 0 deletions cmd/sfreleaser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/spf13/pflag"
"github.com/streamingfast/cli"
Expand Down Expand Up @@ -83,3 +84,20 @@ func verifyCommandRunSuccesfully(command string, onErrorText string) {
cli.Exit(1)
}
}

func findSfreleaserDir(workingDirectory string) string {
current := workingDirectory
volumeName := filepath.VolumeName(current)

for {
if current == volumeName {
return ""
}

if _, err := os.Stat(filepath.Join(current, ".sfreleaser")); err == nil {
return current
}

current = filepath.Dir(current)
}
}
24 changes: 19 additions & 5 deletions cmd/sfreleaser/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ import (
)

type GlobalModel struct {
Project string
Binary string
Language Language
Variant Variant
Root string
Project string
Binary string
Language Language
Variant Variant
Root string
ConfigRoot string

WorkingDirectory string
}

func (g *GlobalModel) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("project", g.Project)
encoder.AddString("binary", g.Binary)
encoder.AddString("language", g.Language.String())
encoder.AddString("variant", g.Variant.String())
encoder.AddString("config_root", g.ConfigRoot)
encoder.AddString("working_directory", g.WorkingDirectory)

return nil
}

Expand All @@ -50,9 +54,19 @@ func mustGetGlobal(cmd *cobra.Command) *GlobalModel {
global.Binary = global.Project
}

global.ConfigRoot = findSfreleaserDir(global.WorkingDirectory)

return global
}

func (g *GlobalModel) ResolveFile(in string) string {
if filepath.IsAbs(in) {
return in
}

return filepath.Join(g.ConfigRoot, in)
}

func (g *GlobalModel) ensureValidForRelease() {
var errors []string
if g.Language == LanguageUnset {
Expand Down
5 changes: 4 additions & 1 deletion cmd/sfreleaser/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var ReleaseCmd = Command(release,
`),
Flags(func(flags *pflag.FlagSet) {
flags.Bool("allow-dirty", false, "Perform release step even if Git is not clean, tries to configured used tool(s) to also allow dirty Git state")
flags.String("changelog-path", "CHANGELOG.md", "Path where to find the changelog file used to extract the release notes")
flags.StringArray("pre-build-hooks", nil, "Set of pre build hooks to run before run the actual building steps")
flags.String("upload-substreams-spkg", "", "If provided, add this Substreams package file to the release, if manifest is a 'substreams.yaml' file, the package is first built")
flags.Bool("publish-now", false, "By default, publish the release to GitHub in draft mode, if the flag is used, the release is published as latest")
Expand Down Expand Up @@ -94,6 +95,7 @@ func release(cmd *cobra.Command, args []string) error {
}

allowDirty := sflags.MustGetBool(cmd, "allow-dirty")
changelogPath := global.ResolveFile(sflags.MustGetString(cmd, "changelog-path"))
goreleaserDockerImage := sflags.MustGetString(cmd, "goreleaser-docker-image")
publishNow := sflags.MustGetBool(cmd, "publish-now")
preBuildHooks := sflags.MustGetStringArray(cmd, "pre-build-hooks")
Expand All @@ -104,6 +106,7 @@ func release(cmd *cobra.Command, args []string) error {
zlog.Debug("starting 'sfreleaser release'",
zap.Inline(global),
zap.Bool("allow_dirty", allowDirty),
zap.String("changelog_path", changelogPath),
zap.String("goreleaser_docker_image", goreleaserDockerImage),
zap.Bool("publish_now", publishNow),
zap.Strings("pre_build_hooks", preBuildHooks),
Expand Down Expand Up @@ -135,7 +138,7 @@ func release(cmd *cobra.Command, args []string) error {
cli.NoError(os.MkdirAll("build", os.ModePerm), "Unable to create build directory")

configureGitHubTokenEnvFile("build/.env.release")
cli.WriteFile("build/.release_notes.md", readReleaseNotes())
cli.WriteFile("build/.release_notes.md", readReleaseNotes(changelogPath))

// By doing this after creating the build directory and release notes, we ensure
// that those are ignored, the user will need to ignore them to process (or --allow-dirty).
Expand Down
3 changes: 1 addition & 2 deletions cmd/sfreleaser/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

var headerRegex = regexp.MustCompile(`^##([^#])`)

func readReleaseNotes() string {
changelogFile := "./CHANGELOG.md"
func readReleaseNotes(changelogFile string) string {
if !cli.FileExists(changelogFile) {
return ""
}
Expand Down

0 comments on commit 4f6c321

Please sign in to comment.