zb
zb copied to clipboard
an opinionated repo based tool for linting, testing and building go source
zb — an opinionated repo based tool for linting, testing and building go source
███████╗██████╗ ██████╗ ██████╗ ███████╗███████╗ ██╗████████╗ ╚══███╔╝██╔══██╗ ██╔══██╗██╔═══██╗██╔════╝██╔════╝ ██║╚══██╔══╝ ███╔╝ ██████╔╝ ██║ ██║██║ ██║█████╗ ███████╗ ██║ ██║ ███╔╝ ██╔══██╗ ██║ ██║██║ ██║██╔══╝ ╚════██║ ██║ ██║ ███████╗██████╔╝ ██████╔╝╚██████╔╝███████╗███████║ ██║ ██║ ╚══════╝╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝╚══════╝ ╚═╝ ╚═╝
Benefits
- Faster builds (by defaulting to
go installexcept formainpackages, and by running concurrentgo installcommands when the dependency tree allows) - Faster testing (by caching test results and not retesting except when necessary)
- Faster linting (by caching lint results from
gometalinter) - Did I mention fast!
- Automatically runs
go generateif its dependency calculation determines it's required - Operates on all packages in a repository (by default) with intelligent support for vendored packages
- Can complement other build tools like
make - Does not interfere with tools like
govendororgb
Installation
Simply run go get jrubin.io/zb
Rationale
Many go repositories have multiple packages. It is often necessary to build/install, test, lint, etc. across all packages within the repository. Go "ellipsis" wildcards (...) can be used to select all subdirectories of a given repository, but don't exclude vendored packages which makes running tests and linting complicated. Some operations should be aware of vendored packages (e.g. build/install), while others should ignore them (e.g. lint). Yet others need to be aware of changes in vendored packages, but should not operate directly on them (e.g. test). Traditional build tools, like make, can be used to supplement the go command to work only on the intended packages. Building dependency lists for make targets, which are required, for example, to dynamically identify modified .go files and what would need to be rebuilt as a result, is at the very least complicated (consider .go files modified outside the repo) and at best slow.
zb fixes all of this
Packages → Repositories
zb is aware of the directory it is called from. If any of its commands is called without a package argument, it will use the current working directory.
zb can also be passed packages just like the go command. Both package names (e.g. fmt, jrubin.io/zb) and relative package names (e.g. ./jrubin.io/zb) are supported, as are ellipsis (...).
zb will identify the repository associated with each package by locating the directory and walking up the directory tree to find the repository directory (containing .git [only git is supported at present]). It will then execute the command for all packages in each repository it identified.
go generate
go generate is great, but sometimes it needs to be executed before a build. Forgetting to execute go generate can be a major problem if, for example, new values were added to a stringer.
zb does its own dependency calculation and can identify go generate dependencies provided an additional annotation is also present.
The following formats are available to define dependencies of go generate
zb:generate formats
-
//zb:generate glob glob...Causesgo generateto be executed on the go file with the annotation if the go file itself is newer than the files expanded from the globs. This is useful with commands like stringer://go:generate stringer -type=YourType //zb:generate yourtype_string.go -
//zb:generate -patsubst %pattern %replacement glob glob...Works likemake's patsubst. Causesgo generateto be executed on the go file with the annotation if any of the files expanded from the globs is newer than any of the filenames generated through the pattern substitution.//go:generate make proto //zb:generate -patsubst %.proto %.pb.go *.protoThis command first matches all files matching
*.protoand then performs the substitution by extracting the part of each of those file names before.proto(identified with the%in%.proto) and taking that extracted pattern and inserting it into the%part of%.pb.go.So if there were files
message.protoandtypes.proto,go generatewould be executed if either of those files were newer thanmessage.pb.goortypes.pb.go(including if the.pb.gofiles did not yet exist). -
//zb:generate -target file glob glob...Basically a simplified-patsubst. Causesgo generateto be executed if any of the files expanded from the globs is newer thanfileCan also be written as//zb:generate -patsubst % file glob glob...
Commands
install
Initially, zb install appears to do the same things as go install (just for all packages in the repositories). In fact, zb install just calls go install under the hood and supports all of its flags. There are a few differences though.
go generatemay be called before building the package according to the//go:generateand//zb:generateannotationsmainpackages (commands) are built with extra linker flags that causemain.gitCommitandmain.buildDatevariables to be set if they exist. Seezb/main.goas an example of how to utilize this.- Executes
go installfor each stale package it finds and will execute concurrentgo installprocesses when the dependency tree allows. Concurrency can be limited with$GOMAXPROCS. - If any of the non-vendored
.gofiles in the repository containTODOorFIXMEthese lines will be emitted to the console as warnings (unless the global-nflag is enabled).
build
zb build differs from go build in that non main packages will be installed with go install. Commands, however, will not be installed (to $GOPATH/bin), they are built with the binary being placed in the root of the repository tree. If there is already a directory in the root of the repository with the same name as the command, the command will be placed in that directory instead.
Otherwise, zb build is identical to zb install.
lint
Delegates functionality to gometalinter but with more useful defaults and caching of results.
- The
--concurrency, -jflag is dynamically calculated to be1less than half the number of CPU cores (but at least1) [gometalinterdefault is16] --testsis enabled by default--deadlineis set to30s[gometalinterdefault is5s]--enable-gcis enabled by defaultalighcheck,dupl,gocycloandstructcheckare disabled by defaulterrcheck,gofmt,goimportsandunusedare enabled (in addition to all other default enabled checkers) by default
The -n flag can be used to hide golint warnings about missing comments.
Since dependency calculation can sometimes add a non-trivial amount of time to the zb lint command, go generate will not be executed.
Files matching certain suffixes will be excluded from the results. This list can be modified with the --ignore-suffix flag. By default files with the following suffixes will be excluded:
.pb.go.pb.gw.go_string.gobindata.gobindata_assetfs.gostatic.go
All other gometalinter flags will be honored as defined.
test
Delegates functionality to go test but caches the results (like gt).
Honors all other flags just like go test except those intended to be passed directly to the test binary.
Use the -f flag to treat the test results as uncached, forcing the tests to be executed (and cached) again.
To see which tests would be executed (because their results are not-cached or the -f flag was provided), use the -l flag.
Since dependency calculation can sometimes add a non-trivial amount of time to the zb test command, go generate will not be executed.
complete
zb has full support for shell autocompletion in both bash and zsh.
Simply execute eval "$(zb complete)" (or put in your init files) to enable.
clean
Removes the executables produced by zb build
commands
Lists the absolute paths where each of the commands (from main packages) will be placed with zb build
list
Similar to go list (and takes the same flags) but will list all of the packages in each of the repositories. Use the --vendor flag to exclude vendored packages.
help
zb contains a built-in, comprehensive help system. Running zb by itself (or with the -h or --help flags) will list the commands and global flags. zb help <command>, zb <command> -h and zb <command> --help will show contextual help for the given command.
Global Flags
--log-level, -l, $LOG_LEVEL
Defaults to info. Available levels are:
errorwarninfodebug
--no-warn-todo-fixme, -n, $NO_WARN_TODO_FIXME
Do not warn when finding WARN or FIXME in .go files
--cache, $CACHE
Modify the base directory used for storing results of commands that cache their results (test and lint).
Defaults to $HOME/Library/Caches/zb on mac and $HOME/.cache/zb elsewhere.
--package, -p
Causes zb to execute only on the explicitly listed packages and not on all packages in their repositories.