Skip to content

Commit

Permalink
build: add persevere check to setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Oct 19, 2024
1 parent 604dad8 commit ad8cb1c
Show file tree
Hide file tree
Showing 20 changed files with 571 additions and 509 deletions.
4 changes: 2 additions & 2 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ tasks:
- poetry run isort {{.PYTHON}}
- poetry run ruff format {{.PYTHON}}
- poetry run mdformat --wrap 79 --number {{.MD}}
- poetry run shfmt --write {{.SH}}
- poetry run shfmt --write --simplify {{.SH}}

lint:
desc: Lint the code using different linters.
Expand Down Expand Up @@ -115,7 +115,7 @@ tasks:
poetry run openfisca test {{.PY_TEST}} \
--country-package=openfisca_country_template \
--extensions=openfisca_extension_template
- bashunit --parallel {{.SH_TEST}}
- bashunit --parallel --simple {{.SH_TEST}}

check-build:
desc: Check if the build is ok.
Expand Down
170 changes: 0 additions & 170 deletions first-time-setup.sh

This file was deleted.

54 changes: 33 additions & 21 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,19 @@ set -o nounset
# Prevent errors in a pipeline from being masked.
set -o pipefail

# Define a cleanup function to be called on script exit or interruption.
trap cleanup SIGINT SIGTERM ERR EXIT
# Define a cleanup functions to be called on script exit or interruption.
trap 'unexpected_error $? $LINENO' ERR
trap interruped SIGINT
trap cleanup EXIT

# Define constants for directory and file paths.
declare -r ROOT_DIR=.
declare -r TEMP_FILE="$ROOT_DIR/temp.sh"
declare -r SOURCE_DIR="$ROOT_DIR/src/first_time_setup"
declare -r TEMPLATE_FILE="$SOURCE_DIR/_template.sh"
declare -r TEMPLATE_FILE="$SOURCE_DIR/main.sh"

# Define a function to print a message when a task starts.
function init() {
echo "$(tput setaf 5)[⚙]$(tput sgr0) $1"
}
# Define a function to print a message when a task passes.
function pass() {
echo "$(tput setaf 6)[λ]$(tput sgr0) $1"
}

# Define a function to print a message when a task finishes.
function info() {
echo "$(tput setaf 2)[✓]$(tput sgr0) $1"
}
# Source the colours script.
source "$SOURCE_DIR/_colours.sh"

# Main function to drive the script.
function main() {
Expand All @@ -57,16 +48,22 @@ function main() {
# Function to build the output file.
function build() {
local out=$1
echo -e "$(init "Building '$out'")"
echo -e "$(colours::task "Building '$out'")"
# Create a temporary file.
create_temp_file
# Add each script file in the source directory to the temporary file.
echo -e "$(colours::task "Adding files...")"
for file in "$SOURCE_DIR"/*.sh; do add_file_to_temp "$file"; done
echo -e "$(colours::done "Files added...")"
# Apply the template to the output file.
echo -e "$(colours::task "Applying template...")"
apply_template "$out"
echo -e "$(colours::done "Template applied...")"
# Set the version in the output file.
echo -e "$(colours::task "Setting version...")"
set_version "$out"
echo -e "$(info "Building complete :)")"
echo -e "$(colours::done "Version set...")"
echo -e "$(colours::done "Building complete :)")"
}

# Function to create the initial temporary file.
Expand Down Expand Up @@ -94,7 +91,7 @@ function create_temp_file() {
function add_file_to_temp() {
local file=$1
if [ "$file" != "$TEMPLATE_FILE" ]; then
echo -e "$(pass "Adding $file...")"
echo -e "$(colours::pass "Adding $file...")"
# Append the content of the file, starting from the 8th line, to the
# temporary file.
tail -n +8 "$file" >>"$TEMP_FILE"
Expand All @@ -120,13 +117,28 @@ function set_version() {
local version
# Get the version from poetry.
version=$(poetry version --short)
echo -e "$(pass "Setting version to $version...")"
echo -e "$(colours::info "Setting version to $version...")"
# Replace 'X.X.X' with the actual version in the output file.
sed -i '' "s/X\.X\.X/$version/g" "$out"
}

# Function to clean up temporary files.
# Error handling.
function unexpected_error() {
trap - ERR
echo -e "$(colours::fail "Error $1 on line $2.")"
}

# Error handling.
function interrupted() {
trap - ERR
echo ""
echo -e "$(colours::warn "Interrupted.")"
}

# Function to clean up the temporary file.
function cleanup() {
trap - EXIT
echo -e "$(colours::info "Cleaning up.")"
rm -f "$TEMP_FILE"
}

Expand Down
24 changes: 18 additions & 6 deletions src/first_time_setup/_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,34 @@ fi

# Check if the script should run in non-interactive mode.
function checks::non_interactive() {
[ -z "$JURISDICTION_NAME" ] && echo false
[ -z "$REPOSITORY_URL" ] && echo false
[[ -z $JURISDICTION_NAME ]] && echo false && return
[[ -z $REPOSITORY_URL ]] && echo false && return
echo true
}

# Check if the script is running in a CI environment.
function checks::we_are_in_ci() {
[ -z "$CI" ] && echo false
[ "$CI" == "0" ] && echo false
[ "$CI" == "false" ] && echo false
[[ -z $CI ]] && echo false && return
[[ $CI == "0" ]] && echo false && return
[[ $CI == "false" ]] && echo false && return
echo true
}

# Check if the repository exists.
function checks::repo_exists() {
local result
result=$(git rev-parse --is-inside-work-tree &>/dev/null && echo $? || echo $?)
[ "$result" -eq 0 ] && echo true || echo false
[[ $result -eq 0 ]] && echo true && return || echo false
}

function checks::persevere() {
local we_are_in_ci
local repo_exists
we_are_in_ci=$(checks::we_are_in_ci)
repo_exists=$(checks::repo_exists)
if ! $we_are_in_ci && $repo_exists; then
echo false && return
else
echo true
fi
}
45 changes: 34 additions & 11 deletions src/first_time_setup/_colours.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@ if ! (return 0 2>/dev/null); then
exit 1
fi

# Set the colours for the script.
declare -r GREEN="\033[0;32m"
declare -r PURPLE="\033[1;35m"
declare -r YELLOW="\033[0;33m"
declare -r BLUE="\033[1;34m"

# Export the colours.
export GREEN
export PURPLE
export YELLOW
export BLUE
# Define a function to print a message when a task starts.
function colours::task() {
echo "$(tput setaf 5)[⚙]$(tput sgr0) $1"
}

# Define a function to print a message when a asking for input.
function colours::user() {
echo "$(tput setaf 4)[❯]$(tput sgr0) $1"
}

# Define a function to print a message when a task passes.
function colours::pass() {
echo "$(tput setaf 6)[λ]$(tput sgr0) $1"
}

# Define a function to print a message with a warning.
function colours::warn() {
echo "$(tput setaf 3)[!]$(tput sgr0) $1"
}

# Define a function to print a message when a task fails.
function colours::fail() {
echo "$(tput setaf 1)[x]$(tput sgr0) $1"
}

# Define a function to print a message when a task finishes.
function colours::done() {
echo "$(tput setaf 2)[✓]$(tput sgr0) $1"
}

# Define a function to print an info message (for example, an interruption).
function colours::info() {
echo "$(tput setaf 7)[i]$(tput sgr0) $1"
}
4 changes: 1 addition & 3 deletions src/first_time_setup/_envvars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ declare -r REPOSITORY_URL=${REPOSITORY_URL:-}
declare -r CI=${CI:-}

# Export the variables.
export JURISDICTION_NAME
export REPOSITORY_URL
export CI
export JURISDICTION_NAME REPOSITORY_URL CI
Loading

0 comments on commit ad8cb1c

Please sign in to comment.