Skip to content

barbell-math/smoothbrain-bs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sbbs

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.

Index

Variables

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(name string) (*os.File, error)

A utility function that creates a file and logs the file's path.

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(fmt string, args ...any)

Logs errors in bold red and exits.

func LogQuietInfo(fmt string, args ...any)

Logs quiet info in gray.

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:

  1. The first target will run install go-enum in ~/go/bin

func RegisterGoMarkDocTargets()

Registers two targets:

  1. The first target will run gomarkdoc, embeding the results in README.md
  2. 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(pathInRepo string)

Registers two targets:

  1. The first target will run sqlc generate in the provided path, relative to the repo root dir.
  2. The second target will install sqlc using go intstall

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(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(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(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(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.

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
}

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
}

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() 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(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(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.

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

Examples

For examples of using this build system refer to the following repositories:

  1. smoothbrain-errs
  2. smoothbrain-test
  3. smoothbrain-hashmap
  4. smoothbrain-argparse

Helpful Developer Cmds

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!

About

A very simple build system written in 100% golang to avoid the need to have cmake as a dependency.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages