joshuar-go-hass-agent/build/magefiles/build.go

93 lines
2.0 KiB
Go

// Copyright (c) 2024 Joshua Rich <joshua.rich@gmail.com>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package main
import (
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type Build mg.Namespace
var ErrBuildFailed = errors.New("build failed")
// Full runs all prep steps and then builds the binary.
func (Build) Full() error {
slog.Info("Starting full build.")
// Make everything nice, neat, and proper
mg.Deps(Preps.Tidy)
mg.Deps(Preps.Format)
mg.Deps(Preps.Generate)
// Record all licenses in a registry
mg.Deps(Checks.Licenses)
return buildProject()
}
// Fast just builds the binary and does not run any prep steps. It will fail if
// the prep steps have not run.
func (Build) Fast() error {
return buildProject()
}
func (b Build) CI() error {
if !isCI() {
return ErrNotCI
}
mg.SerialDeps(Preps.Deps)
mg.SerialDeps(b.Full)
return nil
}
//nolint:mnd
func buildProject() error {
if err := os.RemoveAll(distPath); err != nil {
return fmt.Errorf("could not clean dist directory: %w", err)
}
if err := os.Mkdir(distPath, 0o755); err != nil {
return fmt.Errorf("could not create dist directory: %w", err)
}
envMap, err := generateEnv()
if err != nil {
return errors.Join(ErrBuildFailed, err)
}
ldflags, err := getFlags()
if err != nil {
return errors.Join(ErrBuildFailed, err)
}
var output string
// Generate the output binary name depending on the arch.
if strings.Contains(envMap["GOARCH"], "arm") {
output = filepath.Join(distPath, "/go-hass-agent-"+envMap["GOARCH"]+envMap["GOARM"])
} else {
output = filepath.Join(distPath, "/go-hass-agent-"+envMap["GOARCH"])
}
slog.Info("Running go build...", "output", output, "ldflags", ldflags)
if err := sh.RunWithV(envMap, "go", "build", "-ldflags="+ldflags, "-o", output); err != nil {
return fmt.Errorf("failed to build project: %w", err)
}
return nil
}