fchastanet / bash-compiler

Go tool allowing to aggregate bash files into one allowing bash script reusability.
https://fchastanet.github.io/bash-compiler/
MIT License
0 stars 0 forks source link
bash bash-tools-framework compiler golang gtpl reusability

Bash Compiler

GoTemplate

TIP: Checkout related projects of this suite

GitHub release (latest SemVer) GitHubLicense CI/CD ProjectStatus DeepSource DeepSource AverageTimeToResolveAnIssue PercentageOfIssuesStillOpen

1. Excerpt

This tool allows to detect all the framework functions used inside a given sh file. The framework functions matches the pattern Namespace::functionName (we can have several namespaces separated by the characters ::). These framework functions will be injected inside a compiled file. The process is recursive so that every framework functions used by imported framework functions will be imported as well (of course only once).

2. Documentation

2.1. Go Libraries used

2.2. Template system

template system doc 1

There is the choice between Go template/text or template/html libraries. I chosen template/text to avoid some escaping that are not needed in bash.

Go template/text or template/html don't provide any execution context to the filters (FuncMap).

I'm not using Template.ParseGlob because I have to call it twice to include files of root directory and sub directories with 2 glob patterns. But a bug in text/template makes the template be initialized again after each calls to ParseGlob function. So I compute manually list of templates in internal/render/render.go NewTemplate function.

I simulated a context by pushing the context to the render function. So the data associated to the template has the following structure:

type Context struct {
 Template *template.Template
 Name     string
 RootData any
 Data     any
}

Then each filter has to be called with the right context. The special filter include allows to include a sub template overriding context Data.

Template filter functions, internal/render/functions/index.go includes:

2.3. Compiler

see Compile command.

3. Development

3.1. Pre-commit hook

This repository uses pre-commit software to ensure every commits respects a set of rules specified by the .pre-commit-config.yaml file. It supposes pre-commit software is installed in your environment.

You also have to execute the following command to enable it:

pre-commit install --hook-type pre-commit --hook-type pre-push

Now each time you commit or push, some linters/compilation tools are launched automatically

3.1.1. @embed

Allows to embed files, directories or a framework function. The following syntax can be used:

Syntax: # @embed "srcFile" AS "targetFile"

Syntax: # @embed "srcDir" AS "targetDir"

if @embed annotation is provided, the file/dir provided will be added inside the resulting bin file as a tar gz file(base64 encoded) and automatically extracted when executed.

The compiler's embed annotation offers the ability to embed files or directories. annotationEmbed allows to:

3.2. Build/run/clean

Formatting is managed exclusively by pre-commit hooks.

3.2.1. Build

build/build-docker.sh
build/build-local.sh

3.2.2. Tests

build/test.sh

3.2.3. Coverage

build/coverage.sh

3.2.4. run the binary

build/run.sh

3.2.5. Clean

build/clean.sh

4. Commands

Compile bin file

go run ./cmd/bash-compiler examples/configReference/shellcheckLint.yaml \
  --root-dir /home/wsl/fchastanet/bash-dev-env/vendor/bash-tools-framework \
  -t examples/generated -k -d

for debugging purpose, manually Transform and validate yaml file using cue

cue export \
  -l input: examples/generated/shellcheckLint-merged.yaml \
  internal/model/binFile.cue --out yaml \
  -e output >examples/generated/shellcheckLint-cue-transformed.yaml

5. KCL

https://www.kcl-lang.io/docs/user_docs/getting-started/install

cd internal/model/kcl
kcl -D configFile=testsKcl/example.yaml

6. Alternatives