Crucible – In Depth
In the previous post I briefly highlighted my side project. This article will do a little more in depth.
Getting started
To get started and use it go download the binary and save it somewhere in your PATH
in your favourite shell. Then start up your terminal with the shell in question. Navigate to a new path, in our case ~/projects/crucible-example
. Within that directory run crucible init
. The script will ask two questions. What will be the name of the project and what will the name of the main file be. Project will be Tutorial and the main file will be run.sh.
Then your directory will look a bit like this:
~/projects/crucible-example
|
-- .crucible
-- .mould
-- run.sh
The contents of .crucible is :
working_dir=~/projects/crucible-example
shared_dir=~/.local/share/crucible/4e800f0bd0ef69a050bbcae2e0f10c58d058920ce6a686441cfc07011ad7dd16
version=0.2.6
location=v0.2.6
project_name=Tutorial
main_file=run.sh
author=StealthyCoder
created=1595939715
The shared_dir
is a place that will follow the XDG
conventions if they are set in place and it will house the modules that will be downloaded (required) later on. This will be sectioned off and versioned so you can work on multiple projects without worrying about clashes in versions.
Then .mould will contain the logic of the require function.
Simple example
In our case let us add some logic to the run.sh
file. If you open it up you see the following:
#!/usr/bin/env bash
source .mould
This is the start. We shall add the following code to it making it look as follows:
#!/usr/bin/env bash
source .mould
require arrays/arrays
require logging/logging
arrays.transform_into_array "a"
arrays.add_all a 1 2 3 4
function multiply_by_2 {
local result
result="$(( $1 * 2))"
echo "$result"
}
arrays.map a multiply_by_2
function simple_log {
logging.info "The value is: $1"
}
arrays.foreach a simple_log
Explanation of the code
require
The two require lines get the modules downloaded to where your shared location is in the .crucible file.
arrays.transform_into_array
This line will create a global variable with the name you give it and make it be an array.
arrays.add_all
This line will add to the newly created array all the values after the array name. The first position is the name of the array and there are checks and validators that do in fact check if the variable is an array or not.
arrays.map
The function declaration above it is just there to have a function that will get an input and return that multiplied by 2. The arrays.map
takes an array and then the name of a function to be applied to each element. It will check if the arguments are indeed an array and a function. The result of the function will be stored at that position in the array. In our case all numbers will be doubled.
arrays.foreach
The function declaration above it is just there to have a simple logging function that states "The value is: "
with the value given to it. This does not change the array in place however.
Getting a tarball
So this script is nice but it is not distributional, yet. Running crucible pour
will get us a tarball with everything correctly in it. The name of the tarball is the name of the project. In this case Tutorial.tar.gz
.
Within are two files: .lib
and run.sh
. The .lib
file contains all the functions needed and the run.sh
just has the require
lines removed and the source .mould
turned into source .lib
.
Improvements
The improvements I want to make on the pour
command is that it applies so called tree shaking. Meaning it will only contain what is actually called in from within the script for example, but also make it so it does not have the functions double declared as is the case now.