sbbs

package module
v0.0.0-...-80d6bce Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 14 Imported by: 8

README

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

Constants

const DefaultBenchTargetName = "bench"

const DefaultFmtTargetName = "fmt"

const DefaultGenerateTargetName = "generate"

const DefaultTestTargetName = "test"

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

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

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

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

func RegisterGoEnumTargets()

Registers one target:

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

func RegisterGoMarkDocTargets

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

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:

  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

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

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 RmDir

func RmDir(path string) error

A utility function that removes the supplied directory.

func RmFile

func RmFile(path string) error

A utility function that removes the supplied file or empty directory.

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 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 supplied, the given target will be expected to format the code. A
    // diff will then be run to make sure that the commited code is properly
    // formated.
    FmtTarget string
    // When supplied, the given target will be expected to test the code to make
    // sure the commited code passes all unit tests.
    TestTarget string
    // When supplied, the given target will be expected to generate the code
    // required for the project. A diff will then be run to make sure that the
    // commited code is properly formated.
    GenerateTarget string
    // 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

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!

Documentation

Overview

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

Index

Constants

View Source
const DefaultBenchTargetName = "bench"
View Source
const DefaultFmtTargetName = "fmt"
View Source
const DefaultGenerateTargetName = "generate"
View Source
const DefaultTestTargetName = "test"

Variables

View Source
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.")
)

Functions

func AllGoTargets

func AllGoTargets() *goTargets

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 NewGoTargets

func NewGoTargets() *goTargets

func Open

func Open(name string) (*os.File, error)

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

func RegisterBsBuildTarget

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

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

func RegisterGoEnumTargets()

Registers one target:

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

func RegisterGoMarkDocTargets

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

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:

  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

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

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 RmDir

func RmDir(path string) error

A utility function that removes the supplied directory.

func RmFile

func RmFile(path string) error

A utility function that removes the supplied file or empty directory.

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.

Types

type MergegateTargets

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 supplied, the given target will be expected to format the code. A
	// diff will then be run to make sure that the commited code is properly
	// formated.
	FmtTarget string
	// When supplied, the given target will be expected to test the code to make
	// sure the commited code passes all unit tests.
	TestTarget string
	// When supplied, the given target will be expected to generate the code
	// required for the project. A diff will then be run to make sure that the
	// commited code is properly formated.
	GenerateTarget string
	// 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
}

Defines all possible stages that can run in a mergegate target.

type StageFunc

type StageFunc func(ctxt context.Context, cmdLineArgs ...string) error

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.

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

type TargetFunc func(cmdLineArgs ...string)

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL