Build with Magefile #104341

Merged
Sybren A. Stüvel merged 26 commits from magefile into main 2024-10-04 21:59:46 +02:00
4 changed files with 117 additions and 10 deletions
Showing only changes of commit a5957c4952 - Show all commits

View File

@ -213,6 +213,7 @@ update-version:
addon/flamenco/__init__.py \
addon/flamenco/manager \
addon/flamenco/manager_README.md \
magefiles/version.go \
web/app/src/manager-api \
web/project-website/data/flamenco.yaml
@echo 'git tag -a -m "Tagged version ${VERSION}" v${VERSION}'

View File

@ -0,0 +1,88 @@
package main
import (
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"path/filepath"
"github.com/rs/zerolog/log"
)
const mageFile = "magefiles/version.go"
// updateMagefiles changes the version number in the Mage files.
// Returns whether the file actually changed.
func updateMagefiles() bool {
logger := log.With().Str("filename", mageFile).Logger()
// Parse the mage file as AST.
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, mageFile, nil, parser.SkipObjectResolution|parser.ParseComments)
if err != nil {
logger.Fatal().Err(err).Msgf("could not update mage version file")
return false
}
// Perform replacements on the AST.
replacements := map[string]string{
"version": cliArgs.newVersion,
"releaseCycle": releaseCycle,
}
var (
lastIdent *ast.Ident // Last-seen identifier.
anyFieldChanged bool
)
ast.Inspect(astFile, func(node ast.Node) bool {
switch x := node.(type) {
case *ast.Ident:
lastIdent = x
case *ast.BasicLit:
replacement, ok := replacements[lastIdent.Name]
if ok {
newValue := fmt.Sprintf("%q", replacement)
if x.Value != newValue {
logger.Info().
Str("old", x.Value).
Str("new", newValue).
Msg("updating mage version file")
x.Value = newValue
anyFieldChanged = true
}
}
}
return true
})
// Open a temporary file for writing.
mageDir := filepath.Dir(mageFile)
writer, err := os.CreateTemp(mageDir, filepath.Base(mageFile)+"*.go")
if err != nil {
log.Fatal().Err(err).Msgf("cannot create file in %s", mageDir)
}
defer writer.Close()
// Write the altered AST to the temp file.
if err := format.Node(writer, fset, astFile); err != nil {
log.Fatal().Err(err).Msgf("cannot write updated version of %s to %s", mageFile, writer.Name())
}
// Close the file.
if err := writer.Close(); err != nil {
log.Fatal().Err(err).Msgf("cannot close %s", writer.Name())
}
// Overwrite the original mage file with the temp file.
if err := os.Rename(writer.Name(), mageFile); err != nil {
log.Fatal().Err(err).Msgf("cannot rename %s to %s", writer.Name(), mageFile)
}
return anyFieldChanged
}

View File

@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/mattn/go-colorable"
@ -13,13 +14,18 @@ import (
"github.com/rs/zerolog/log"
)
var cliArgs struct {
// Global variables used by the updateXXX() functions.
var (
cliArgs struct {
// Logging level flags.
quiet, debug, trace bool
newVersion string
updateMakefile bool
}
}
releaseCycle string
)
func main() {
parseCliArgs()
@ -29,11 +35,23 @@ func main() {
log.Info().Str("version", cliArgs.newVersion).Msg("updating Flamenco version")
switch {
case strings.Contains(cliArgs.newVersion, "alpha"), strings.Contains(cliArgs.newVersion, "dev"):
releaseCycle = "alpha"
case strings.Contains(cliArgs.newVersion, "beta"):
releaseCycle = "beta"
case strings.Contains(cliArgs.newVersion, "rc"):
releaseCycle = "rc"
default:
releaseCycle = "release"
}
var anyFileWasChanged bool
if cliArgs.updateMakefile {
anyFileWasChanged = anyFileWasChanged || updateMakefile()
anyFileWasChanged = updateMakefile() || anyFileWasChanged
}
anyFileWasChanged = anyFileWasChanged || updateAddon()
anyFileWasChanged = updateAddon() || anyFileWasChanged
anyFileWasChanged = updateMagefiles() || anyFileWasChanged
if !anyFileWasChanged {
log.Warn().Msg("nothing changed")

View File

@ -11,8 +11,8 @@ import (
// To update the version number in all the relevant places, update the VERSION
// variable below and run `mage update-version`.
const (
version = "3.3-beta0"
releaseCycle = "beta"
version = "3.6-alpha5"
releaseCycle = "alpha"
)
func gitHash() (string, error) {