flamenco/cmd/update-version/magefiles.go
Sybren A. Stüvel 5f37bcb629 Build with Magefile (#104341)
Convert most of the code in `Makefile` to [Magefile](https://magefile.org/):

This makes it possible to build Flamenco without `make` (and the POSIX environment/commands it expect) by running:

```bash
$ go run mage.go webappInstallDeps  # Only on the first build.
$ go run mage.go build
```

More efficient builds are possible with other commands, and some release-related commands still require `make`. At least the barrier to entry should be considerably lower (compared to having to install Make + Cygwin/MSYS2 on Windows).

Fix: #102633

This does not port the building of release packages, so it doesn't address #102671.

### Main Targets

| Target   | Description                                                                     |
|----------|---------------------------------------------------------------------------------|
| build    | Build Flamenco Manager and Flamenco Worker, including the webapp and the add-on |
| check    | Run unit tests, check for vulnerabilities, and run the linter                   |
| clean    | Remove executables and other build output                                       |
| generate | Generate code (OpenAPI and test mocks)                                          |

### All Targets

Get these via `go run mage.go -l`:

```
Targets:
  build                           Flamenco Manager and Flamenco Worker, including the webapp and the add-on
  check                           Run unit tests, check for vulnerabilities, and run the linter
  clean                           Remove executables and other build output
  flamencoManager                 Build Flamenco Manager with the webapp and add-on ZIP embedded
  flamencoManagerWithoutWebapp    Only build the Flamenco Manager executable, do not rebuild the webapp
  flamencoWorker                  Build the Flamenco Worker executable
  generate                        code (OpenAPI and test mocks)
  generateGo                      Generate Go code for Flamenco Manager and Worker
  generateJS                      Generate JavaScript code for the webapp
  generatePy                      Generate Python code for the add-on
  govulncheck                     Check for known vulnerabilities.
  staticcheck                     Analyse the source code.
  test                            Run unit tests
  version                         Show which version information would be embedded in executables
  vet                             Run `go vet`
  webappInstallDeps               Use Yarn to install the webapp's NodeJS dependencies
  webappStatic                    Build the webapp as static files that can be served
```

Co-authored-by: Mateus Abelli <mateusabelli@gmail.com>
Reviewed-on: #104341
2024-10-04 21:59:44 +02:00

89 lines
2.1 KiB
Go

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
}