flamenco/magefiles/build.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: https://projects.blender.org/studio/flamenco/pulls/104341
2024-10-04 21:59:44 +02:00

135 lines
2.9 KiB
Go

//go:build mage
package main
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"fmt"
"path/filepath"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
)
const (
goPkg = "projects.blender.org/studio/flamenco"
)
var (
// The directory that will contain the built webapp files, and some other
// files that will be served as static files by the Flamenco Manager web
// server.
webStatic = filepath.Join("web", "static")
)
// Build Flamenco Manager and Flamenco Worker, including the webapp and the add-on
func Build() {
mg.Deps(FlamencoManager, FlamencoWorker)
}
// Build Flamenco Manager with the webapp and add-on ZIP embedded
func FlamencoManager() error {
mg.Deps(WebappStatic)
mg.Deps(flamencoManager)
return nil
}
// Only build the Flamenco Manager executable, do not rebuild the webapp
func FlamencoManagerWithoutWebapp() error {
mg.Deps(flamencoManager)
return nil
}
func flamencoManager() error {
return build("./cmd/flamenco-manager")
}
// Build the Flamenco Worker executable
func FlamencoWorker() error {
return build("./cmd/flamenco-worker")
}
// Build the webapp as static files that can be served
func WebappStatic() error {
runInstall, err := target.Dir("web/app/node_modules")
if err != nil {
return err
}
if runInstall {
mg.SerialDeps(WebappInstallDeps)
}
if err := cleanWebappStatic(); err != nil {
return err
}
env := map[string]string{
"MSYS2_ARG_CONV_EXCL": "*",
}
// When changing the base URL, also update the line
// e.GET("/app/*", echo.WrapHandler(webAppHandler))
// in `cmd/flamenco-manager/main.go`
err = sh.RunWithV(env,
"yarn",
"--cwd", "web/app",
"build",
"--outDir", "../static",
"--base=/app/",
"--logLevel", "warn",
// For debugging you can add:
// "--minify", "false",
)
if err != nil {
return err
}
fmt.Printf("Web app has been installed into %s\n", webStatic)
// Build the add-on ZIP as it's part of the static web files.
zipPath := filepath.Join(webStatic, "flamenco3-addon.zip")
return packAddon(zipPath)
}
// Use Yarn to install the webapp's NodeJS dependencies
func WebappInstallDeps() error {
env := map[string]string{
"MSYS2_ARG_CONV_EXCL": "*",
}
return sh.RunWithV(env,
"yarn",
"--cwd", "web/app",
"install",
)
}
func build(exePackage string) error {
flags, err := buildFlags()
if err != nil {
return err
}
args := []string{"build", "-v"}
args = append(args, flags...)
args = append(args, exePackage)
return sh.RunV(mg.GoCmd(), args...)
}
func buildFlags() ([]string, error) {
hash, err := gitHash()
if err != nil {
return nil, err
}
ldflags := "" +
fmt.Sprintf(" -X %s/internal/appinfo.ApplicationVersion=%s", goPkg, version) +
fmt.Sprintf(" -X %s/internal/appinfo.ApplicationGitHash=%s", goPkg, hash) +
fmt.Sprintf(" -X %s/internal/appinfo.ReleaseCycle=%s", goPkg, releaseCycle)
flags := []string{
"-ldflags=" + ldflags,
}
return flags, nil
}