Blade is a task runner designed to be easy, small, highly powerful, and with built in Bash completion and documentation. It is portable and easy to install, only a single binary.
- Easy install - one binary
- Automatic generated documentation
- Automatic bash completion for defined tasks
- Command line parameters are passed to the task
- Create custom help messages for tasks with comments
- Create custom bash completion for build targets
- Call any program as it were a function with
shmodule - Built in file watcher
- Easy and expressive as tasks are defined in Lua
- Blade
- Features
- Contents
- Install
- Bash Completion
- Getting Started
- Targets
- Setup and teardown
- Shell Module
- Blade API
- Plugins
- Lua
- Build from Source
- Cross Compile
Pre built binaries can be downloaded at https://github.com/otm/blade/releases/latest
Download the binary and copy it in your path.
If you prefer to build from source please read the section: Build from Soruce
The -generate-bash-conf option outputs the bash completion configuration to stdout. Either manually copy it or you can for instance use tee:
blade -generate-bash-conf | sudo tee /etc/bash_completion.d/blade
Note: The location of the bash completion configuration might differ depending on distribution and platform
Note: zsh can also run bash completion commands.
Create a Bladefile file in the current directory, the easiest way is to use the blade command.
blade -initThis will create a minimal Bladefile with one target called demo. Tasks in blade are called targets. To execute the target demo target run:
blade demoThe demo showcases some important features:
- Documentation of targets. Access documentation by running
bladewith no arguments. - Receive command line arguments
- Execute shell commands
Defining new blade targets is done by adding functions to the target table.
Example:
functiontarget.build()
-- build target codeendExample: arguments
functiontarget.install(devDeps)
-- install target code-- example setting default valuesdevDeps=devDepsor"true"endExample: variable arguments
functiontarget.install(...)
-- If the ... notation is used arguments are assigned to the arg variable-- arg.n is special and returns the number of elements in arg-- To test: blade install -i --dev /var/logprint("Number of inputs: ", arg.n)
forindex, valueinipairs(arg) doprint(index, "=", value)
endendThe only built in target is help. It will print an automatically generated help message. It is possible to target help messages, see blade.help
bladehelpIf not defining a target when running blade the help target will be executed. This can be overridden by setting blade.default.
Example:
-- set the default target to `test`blade.default=target.test-- run a custom function for the default targetfunctionblade.default()
-- default target codeendIt is possible to run setup and teardown code that is run before and after the blade target. Both setup and teardown receive a target argument with the name of the current target to be run. If no target has been defined at the command line target will be an empty string. Returning false in the setup or teardown will abort the target execution.
Example:
functionblade.setup(target)
-- setup codeendExample:
functionblade.teardown(target)
-- teardown codeendsh is a interface to call any program as it were a function. Programs are executed asynchronously to enable streaming of data in pipes. Therefor it is necessary to manually wait on programs.
localsh=require("sh")
sh.echo("hello", "world"):print()Output:
hello world
For commands with exotic names or names which are reserved words call sh directly.
sh(./script-in-my-directory)Commands that take multiple arguments needs to be invoked with separate strings for each arguments. That is, sh.tar("xzf", "test.tar") will work; however, sh.tar("xzf test.tar") will not.
By default all commands are executed in the background.
-- non blockingsh.sleep(3)
print("prints immediately")
-- blocksh.sleep(3):success()
print("...3 seconds later")
-- utilizing asyncsleep=sh.sleep(3)
print("prints immediately")
sleep.success()
print("...3 seconds later")print() prints the command's combined output.
-- print output of commandsh.echo("hello world"):print()Note:print() has to be called before any method that waits. For instance: ok(), success(), or exitcode(),
All these three methods takes an optional filename argument. If the filename is omitted the function returns the output of the command.
If filename is given the output will be written to the file and returned.
-- print output of commandoutput=sh.echo("hello world"):combinedOutput("/tmp/output")
print(output)The example above will print hello world and it will write it to /tmp/output
Bash like piping is done by calling methods on the previous commands.
sh.du("-sb"):sort("-rn"):print()There are several ways to wait for a command.
Waits for the command to finish and aborts execution if the command returns a non zero exit code. Example: sh.ls():ok()
Returns true if the exit code of the command is zero, false otherwise. Example: sh.ls():success()
To access the exit code of a command call the method exitcode(). Example:
sh.ls():exitcode()
A small set of convince functions are provided, attached to a lua table called blade.
Prints a pretty printed status message to the terminal, normaly used for printing execution status.
Example:
localsh=require("sh")
blade.printStatus("true", true)
blade.printStatus("false", false)
blade.printStatus("0", 0)
blade.printStatus("1", 1)
blade.printStatus("nil")
blade.printStatus("true (shell)", sh.date():success())
blade.printStatus("false (shell)", sh("false"):success())
-- outputs:-- true [ ok ]-- false [fail]-- 0 [ ok ]-- 1 [fail]-- nil [udef]-- true (shell) [ ok ]-- false (shell) [fail]blade.help associates a message with a target.
Example:
functiontarget.build()
-- build target codeendblade.help(target.build, "<dev|prod>")blade.compgen associates an opts string or a function that will be executed by blade if bash completion is set up. The function signature is:
function(compWords, compCWord)
- compWords: a table containing the arguments on the command line
- compCWord: a int pointing to the cursor position (zero indexed)
Note on cursor position: (cursor denoted by "|")
- blade target | ==> compWords = { target }, compCWord = 1
- blade target opt1| ==> compWords = { target, opt1 }, compCWord = 1
- balde target opt1 | ==> compWords = { target, opt1 }, compCWord = 2
Example:
functiontarget.build()
-- build target codeend-- bind a static stringblade.compgen(target.build, "dev prod")
-- bind a functionblade.compgen(target.build, function(compWords, compCWord)
ifcompCWord==1thenreturn"dev prod"endreturn""end)blade have a built-in simple file watcher.
- callback - function(file, op): function for processing file events
- dir - string: the directory to watch
- recursive - bool: watch sub directories recursively
- filter - string: files matching regexp will be sent processed
- exclude - {string, ...}: a table of strings of directories to exclude
Note: Several watch statements can be specified in one target
functioncmd.watch()
blade.plugin.watch{callback=onFileEvent, dir="."}
endfunctiononFileEvent(file, op)
print("File: " ..file..", Operation: " ..op)
endThis section contains some Lua tips for new users
- Define strings:
"str",'str'or[[str]] - Read environment variables:
os.getenv("HOME") - if-else:
if <statement> then <code> elseif <statement> then <code> else <code> end - named function variables:
fn{key=name, ...}equivalent:arg = {key=name, ...}; fn(arg) - reading files in directory:
forfileinio.popen("ls -1 *.go"):lines() do--use fileendSplitting strings can be done in many ways in Lua but they are all quite cumbersome. To aid this there is a non standard Lua function for splitting strings in blade
Example:
out="first\nsecond"forlineinout:split("\n") doprint("i", line)
endout:split("\n", function(line)
print("cb", line)
end)To build from source you need a working Go installation, see https://golang.org/doc/install
go get github.com/otm/blade
go install github.com/otm/blade
Pre built binaries can be downloaded at https://github.com/otm/blade/releases/latest
Getting blade to all your favorite platforms. Cross compiling can easily be done with gox. See https://github.com/mitchellh/gox for information about the tool. To setup and cross compile you can run.
blade goxSetup
blade build