A recipe based file generator.
Snip requires predefined templates or recipes to work. All files within
~/.config/snip
will be scanned and added as available templates and recipes
that snip can use.
Templates are simply regular files that can contain variables escaped by placing them within pair curly brackets, for example:
Hello {{ name }}
These files can then be generated by interactively providing the required variables or programatically by using recipes.
Recipes are script files that can do anything for example perform system
commands, collect user input and anything else that can be done within scripts.
When the recipe is a shell script (such as zsh
, bash
, etc.) some
convenience functions are provided for easy template handling and user interaction.
A full list of predefined commands is provided bellow.
Snip can be invoked directly and will present a list of available recipes:
$ snip
Snip can receive various arguments depending on the recipe used, for example the React scripts provided with the package can accept a relative or absolute path for component generation:
$ snip src/ExampleComponent
When the recipe is defined as a shell script the following predefined commands can be used:
resolve FILENAME
Resolve the full path of FILENAME by searching in the following directories:
- the working directory
- inside of
~/.config/snip
The full file path is returned or the command will exit with exit code 1.
variables TEMPLATE
Collect all variables from a template and return a JSON object with the variable names as keys and empty strings as values.
data=$(variables 'react/component/component.tsx')
In this example data
might be:
{
"foo": "bar"
}
merge VARS1 VARS2
Merge two JSON objects and return the resulting JSON object. If a key exists in both objects the latter one will take precedence.
merged=$(merge $data1 $data2)
set-value VARS value
Set a value for a given key in a JSON object. If the key did not exist it will be added. The original data will not be altered, a modified copy is returned instead.
copy=$(set-value "$data" "name" "Hello")
get-value VARS value
Get the value of a given key from a JSON object.
value=$(get-value $data key)
edit VARS
Edit the data interactively in $EDITOR
.
data=$(edit $data)
render TEMPLATE
Render the given template by printing it to stdout with the substituted variables. The output should usually be redirected to the output file.
render 'template.js' $data > $output.js
Templates are files that can contain variables escaped by double curly braces
for example {{ variable }}
.
An example of a template is:
// hello.js
console.log("hello {{ name }}");
Recipes can be any executable file that aims to programatically generate files and possibly interact with the user, read templates or execute other system commands.
Typically shell scripts are used and some commands are provided to deal with common tasks such as substituting variables in a template. A full list of commands can be found in the above section.
In order to use predefined recipes from this repository simply find the
files/recipes you want to use and copy them to ~/.config/snip
.
For example:
$ cp -r recipes/react ~/.config/snip
The following dependencies must be installed:
Simply symlink the snip.zsh
script to a location in your path.
For example:
$ sudo ln -s snip.zsh /usr/local/bin/snip
Snip will utilize the $EDITOR
environmental variable for interactive file editing, in order to use a specific editor
simply set the variable accordingly.
shiro [email protected]
MIT