import "github.com/barbell-math/smoothbrain-bs"
A very simple build system written in 100% golang to avoid the need to have cmake as a dependency.
- Variables
- func Cd(dir string) error
- func CreateFile(name string) (*os.File, error)
- func GitRevParse(ctxt context.Context) (string, error)
- func LogErr(fmt string, args ...any)
- func LogInfo(fmt string, args ...any)
- func LogPanic(fmt string, args ...any)
- func LogQuietInfo(fmt string, args ...any)
- func LogSuccess(fmt string, args ...any)
- func LogWarn(fmt string, args ...any)
- func Main(progName string)
- func Mkdir(path string) error
- func Open(name string) (*os.File, error)
- func RegisterBsBuildTarget()
- func RegisterCommonGoCmdTargets(g GoTargets)
- func RegisterGoEnumTargets()
- func RegisterGoMarkDocTargets()
- func RegisterMergegateTarget(a MergegateTargets)
- func RegisterSqlcTargets(pathInRepo string)
- func RegisterTarget(ctxt context.Context, name string, stages ...StageFunc)
- func RegisterUpdateDepsTarget()
- func Run(ctxt context.Context, pipe io.Writer, prog string, args ...string) error
- func RunCwd(ctxt context.Context, pipe io.Writer, cwd string, prog string, args ...string) error
- func RunCwdStdout(ctxt context.Context, cwd string, prog string, args ...string) error
- func RunStdout(ctxt context.Context, prog string, args ...string) error
- func RunTarget(ctxt context.Context, target string, cmdLineArgs ...string)
- func TmpEnvVarSet(name string, val string) (reset func() error, err error)
- func Touch(name string) error
- type GoTargets
- type MergegateTargets
- type StageFunc
- type TargetFunc
var (
// An error that a stage can return to stop the target it is part of from
// further execution. This is intended to be used when other error
// information has been printed to the console.
StopErr = errors.New("Generic stop error. See log above for error details.")
)
func Cd
func Cd(dir string) error
A utility function that changes the programs current working directory and logs the old and new current working directories.
func CreateFile
func CreateFile(name string) (*os.File, error)
A utility function that creates a file and logs the file's path.
func GitRevParse
func GitRevParse(ctxt context.Context) (string, error)
A helpful utility function that runs `git rev-parse --show-toplevel` and returns the stdout. This is often useful when attempting to change the current working directory to a repositories root directory.
func LogErr
func LogErr(fmt string, args ...any)
Logs errors in red.
func LogInfo
func LogInfo(fmt string, args ...any)
Logs info in cyan.
func LogPanic
func LogPanic(fmt string, args ...any)
Logs errors in bold red and exits.
func LogQuietInfo
func LogQuietInfo(fmt string, args ...any)
Logs quiet info in gray.
func LogSuccess
func LogSuccess(fmt string, args ...any)
Logs successes in green.
func LogWarn
func LogWarn(fmt string, args ...any)
Logs warnings in yellow.
func Main
func Main(progName string)
The main function that runs the build system. This is intended to be called by the `main` function of any code that uses this library.
func Mkdir
func Mkdir(path string) error
A utility function that creates the supplied directory as well as all necessary parent directories.
func Open
func Open(name string) (*os.File, error)
A utility function that opens a file and logs the file's path.
func RegisterBsBuildTarget()
Registers a target that rebuilds the build system. This is often useful when changes are made to the build system of a project.
func RegisterCommonGoCmdTargets(g GoTargets)
Registers some common go cmds as targets. See the MergegateTargets struct for details about the available targets that can be added.
func RegisterGoEnumTargets()
Registers one target:
- The first target will run install go-enum in ~/go/bin
func RegisterGoMarkDocTargets()
Registers two targets:
- The first target will run gomarkdoc, embeding the results in README.md
- The second target will install gomarkdoc using go intstall
func RegisterMergegateTarget(a MergegateTargets)
Registers a mergegate target that will perform the actions that are defined by the MergegateTargets struct. See the MergegateTargets struct for details about the available stages the mergegate target can run.
func RegisterSqlcTargets
func RegisterSqlcTargets(pathInRepo string)
Registers two targets:
- The first target will run sqlc generate in the provided path, relative to the repo root dir.
- The second target will install sqlc using go intstall
func RegisterTarget
func RegisterTarget(ctxt context.Context, name string, stages ...StageFunc)
Registers a new build target to the build system. When run, the new target will sequentially run all provided stages, stopping if an error is encountered.
func RegisterUpdateDepsTarget()
Registers a target that updates all dependences. Dependencies that are in the `barbell-math` repo will always be pinned at latest and all other dependencies will be updated to the latest version.
func Run
func Run(ctxt context.Context, pipe io.Writer, prog string, args ...string) error
Runs the program with the specified `args` using the supplied context in the current working directory. The supplied pipe will be used to capture Stdout. Stderr will always be printed to the console.
func RunCwd
func RunCwd(ctxt context.Context, pipe io.Writer, cwd string, prog string, args ...string) error
Runs the program with the specified `args` using the supplied context. The supplied pipe will be used to capture Stdout. Stderr will always be printed to the console.
func RunCwdStdout
func RunCwdStdout(ctxt context.Context, cwd string, prog string, args ...string) error
Runs the program with the specified `args` using the supplied context. All output of the program will be printed to stdout. Equivalent to calling Run and providing os.Stdout for the `pipe` argument.
func RunStdout
func RunStdout(ctxt context.Context, prog string, args ...string) error
Runs the program with the specified `args` using the supplied context in the current working directory. All output of the program will be printed to stdout. Equivalent to calling Run and providing os.Stdout for the `pipe` argument.
func RunTarget
func RunTarget(ctxt context.Context, target string, cmdLineArgs ...string)
Runs the supplied target, given that the supplied target is present in the build systems target list. Execution of all further targets/stages will stop if running the supplied target fails.
func TmpEnvVarSet
func TmpEnvVarSet(name string, val string) (reset func() error, err error)
A utility function that changes the supplied env variable to the supplied value, returning a closure that can be used to set the env variable back to it's original value. If the supplied env variable did not exist before calling this function then the returned closure will remove the env variable instead of reseting it to it's original value.
func Touch
func Touch(name string) error
A utility function that creates but does not open a file and logs the file's path.
type GoTargets
Defines the available targets that can be added by RegisterCommonGoCmdTargets.
type GoTargets struct {
// When true a target will be added that runs `go test ./...`
GenericTestTarget bool
// When true a target will be added that runs `go test -bench=. ./...`
GenericBenchTarget bool
// When true a target will be added that runs `go fmt ./...`
GenericFmtTarget bool
// When true a target will be added that runs `go generate ./...`
GenericGenerateTarget bool
}
type MergegateTargets
Defines all possible stages that can run in a mergegate target.
type MergegateTargets struct {
// When true a stage will update all deps and run a diff to make sure that
// the commited code is using all of the up to date dependencies.
CheckDepsUpdated bool
// When true a stage will install gomarkdoc, update the readme using the
// `gomarkdocReadme` target, and run a diff to make sure that the committed
// readme is up to date.
CheckReadmeGomarkdoc bool
// When true a stage will run go fmt and then run a diff to make sure that
// the commited code is properly formated.
CheckFmt bool
// When true a stage will run all unit tests in the repo to make sure that
// the commited code passes all unit tests.
CheckUnitTests bool
// When true a stage will run go generate and make sure that the generated
// code matches what is commited to the repo.
CheckGeneratedCode bool
// Any stages that should be run prior to all other mergegate stages as
// defined by the other flags in this struct. Useful for installing
// dependencies that the other stages might rely upon.
PreStages []StageFunc
// Any stages that should be run after all other mergegate stages as defined
// by the other flags in this struct. Useful for adding additional mergegate
// checks.
PostStages []StageFunc
}
type StageFunc
The function that will be executed to perform an operation for a given target. The supplied context is meant to be used to control the runtime of the stage operation.
type StageFunc func(ctxt context.Context, cmdLineArgs ...string) error
func CdToRepoRoot
func CdToRepoRoot() StageFunc
Changes the current working directory to the repositories root directory if the current working directory is inside a repo. Results in an error if the current working directory is not inside a repo.
func GitDiffStage
func GitDiffStage(errMessage string, targetToRun string) StageFunc
Runs git diff on the current directory and if any output is returned prints the given error message, the diff result, and suggests a target to run to fix the issue if `targetToRun` is not an empty string. An error will be returned if the diff returns any a non-empty result.
func Stage
func Stage(name string, op func(ctxt context.Context, cmdLineArgs ...string) error) StageFunc
Creates a stage that can be added to a build target. Stages define the operations that will take place when a build target is executing. The supplied context can be modified and passed to Run functions to deterministically control how long various operations take. This prevents builds from hanging forever.
func TargetAsStage
func TargetAsStage(target string) StageFunc
Runs the supplied target as though it were a stage, given that the supplied target is preset in the build systems target list. Execution of all further targets/stages will stop if running the supplied target fails.
type TargetFunc
The function that will be executed when a target is run. This function will be given all of the leftover cmd line arguments that were supplied after the target. Parsing of these arguments is up to the logic defined be the targets stages.
type TargetFunc func(cmdLineArgs ...string)
Generated by gomarkdoc
For examples of using this build system refer to the following repositories:
To build the build system the first time:
go build -o ./bs/bs ./bs
The build system can then be used as usual:
./bs/bs --help
./bs/bs buildbs # builds the build system!