feat: Add justfiles module

This commit is contained in:
lorduskordus 2024-06-26 06:13:41 +02:00
parent 4634ac7330
commit 499f8d2ddb
3 changed files with 131 additions and 0 deletions

View file

@ -0,0 +1,37 @@
# `justfiles`
The `justfiles` module allows for easy `.just` files importing. It can be useful for example to separate DE specific justfiles when building multiple images.
## What it does
1. The module checks if the `config/justfiles/` folder is present.
* If it's not there, it fails.
2. The module finds all `.just` files inside of the `config/justfiles/` folder or starting from the relative path specified under `include`.
* If no `.just` files are found, it fails.
* The structure of the `config/justfiles/` folder does not matter, folders/files can be placed in there however desired, the module will find all `.just` files.
* Optionally, the `.just` files can be validated.
3. The module copies over the files/folders containing `.just` files to `/usr/share/bluebuild/justfiles/`.
* The folder structure of the copy destination remains the same as in the config folder.
4. The module generates import lines and appends them to the `/usr/share/ublue-os/just/60-custom.just` file.
* The module does not overwrite the destination file. New lines are added to an existing file.
* If the generated import lines are already present, the module fails to avoid duplications.
## How to use it
Place all your `.just` files or folders with `.just` files inside the `config/justfiles/` folder. If that folder doesn't exist, create it.
Without specifying `include`, the module will assume you want to import everything. Otherwise, specify your files/folders under `include`.
If you also want to validate your justfiles, set `validate: true`. The validation can be very unforgiving and is turned off by default.
* The validation command usually prints huge number of lines. To avoid cluttering up the logs, the module will only tell you which files did not pass the validation. You can then use the command `just --fmt --check --unstable --justfile <DESTINATION FILE>` to troubleshoot them.

View file

@ -0,0 +1,83 @@
#!/usr/bin/env bash
set -euo pipefail
get_yaml_array CONFIG_SELECTION '.include[]' "$1"
VALIDATE="$(echo "$1" | yq -I=0 ".validate")"
IMPORT_FILE="/usr/share/ublue-os/just/60-custom.just"
CONFIG_FOLDER="${CONFIG_DIRECTORY}/justfiles"
DEST_FOLDER="/usr/share/bluebuild/justfiles"
# Abort if justfiles folder is not present
if [ ! -d "${CONFIG_FOLDER}" ]; then
echo "Error: The config folder '${CONFIG_FOLDER}' was not found."
exit 1
fi
# Include all files in the folder if none specified
if [[ ${#CONFIG_SELECTION[@]} == 0 ]]; then
CONFIG_SELECTION=($(ls "${CONFIG_FOLDER}"))
fi
for SELECTED in "${CONFIG_SELECTION[@]}"; do
echo "----------------------------------------------------------"
echo "------------ Adding folder/file '${SELECTED}' ------------"
echo "----------------------------------------------------------"
# Find all justfiles, starting from 'SELECTED' and get their paths
JUSTFILES=($(find "${CONFIG_FOLDER}/${SELECTED}" -type f -name "*.just" | sed "s|${CONFIG_FOLDER}/||g"))
# Abort if no justfiles found at 'SELECTED'
if [[ ${#JUSTFILES[@]} == 0 ]]; then
echo "Error: No justfiles were found in '${CONFIG_FOLDER}/${SELECTED}'."
exit 1
fi
# Validate all found justfiles if set to do so
if [ "${VALIDATE}" == "true" ]; then
echo "-- Validating justfiles"
VALIDATION_FAILED=0
for JUSTFILE in "${JUSTFILES[@]}"; do
if ! /usr/bin/just --fmt --check --unstable --justfile "${CONFIG_FOLDER}/${JUSTFILE}" &> /dev/null; then
echo "The justfile '${JUSTFILE}' FAILED validation."
VALIDATION_FAILED=1
fi
done
# Exit if any justfiles are not valid
if [ ${VALIDATION_FAILED} -eq 1 ]; then
echo "Error: Some justfiles didn't pass validation."
exit 1
else
echo "All justfiles passed validation."
fi
fi
# Copy 'SELECTED' to destination folder
echo "-- Copying folders/files"
mkdir -p "${DEST_FOLDER}/$(dirname ${SELECTED})"
cp -rfT "${CONFIG_FOLDER}/${SELECTED}" "${DEST_FOLDER}/${SELECTED}"
echo "Copied '${CONFIG_FOLDER}/${SELECTED}' to '${DEST_FOLDER}/${SELECTED}'."
# Generate import lines for all found justfiles
echo "-- Adding import lines"
for JUSTFILE in "${JUSTFILES[@]}"; do
# Create an import line
IMPORT_LINE="import \"${DEST_FOLDER}/${JUSTFILE}\""
# Abort if import line already exists, else append it to import file
if grep -wq "${IMPORT_LINE}" "${IMPORT_FILE}"; then
echo "Error: Duplicated import line: '${IMPORT_LINE}' found."
exit 1
else
echo "${IMPORT_LINE}" | tee -a "${IMPORT_FILE}"
fi
done
done

View file

@ -0,0 +1,11 @@
name: justfiles
shortdesc: The justfiles module allows for easy justfiles importing.
readme: https://raw.githubusercontent.com/blue-build/modules/main/modules/justfiles/README.md
example: |
type: justfiles
validate: true
include:
- common
- gnome/monitors.just
- shared/flatpak/fix-theming.just
- justfile1.just