Skip to content

Commit

Permalink
feat: add support for running hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
janosmiko committed Nov 22, 2024
1 parent 5bd00e2 commit 04f839b
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ dump_build_version() {
}

main() {
run_hooks "pre-build"

check_requirements

command_before_build
Expand All @@ -229,6 +231,8 @@ main() {
dump_build_version

command_after_build

run_hooks "post-build"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,14 @@ check_command() {
error "Error: $1 is required but not installed."
fi
}

run_hooks() {
local hook="${1:-}"
if [[ -n "${hook}" ]] && [[ -d "$(app_path)/hooks/${hook}.d" ]]; then
for file in "$(app_path)"/hooks/"${hook}.d"/*.sh; do
log "Running ${file} for ${hook}"
# shellcheck disable=SC1090
source "${file}"
done
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,19 @@ function test_app_path() {
assert_equals "/app" "$(app_path)"
}

function version_gt() {
function test_version_gt() {
assert_true "$(version_gt '2.4.4' '2.3.99')"
assert_false "$(version_gt '2.3.99' '2.4.4')"
assert_false "$(version_gt '2.4.4' '2.4.4')"
assert_true "$(version_gt '2.4' '2.3.99')"
assert_false "$(version_gt '2.3.99' '2.4')"
assert_false "$(version_gt '2.4' '2.4')"
}

function test_run_hooks() {
local APP_PATH="./test-data/app"
mkdir -p "${APP_PATH}/hooks/test.d"
printf "#!/bin/bash\necho 'test-123'" >"${APP_PATH}/hooks/test.d/01-test.sh"
assert_contains "test-123" "$(run_hooks 'test')"
rm -fr "./test-data"
}
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,6 @@ magento_publish_config() {
}

main() {
check_requirements

LOCKFILE="$(shared_config_path)/.deploy.lock"
readonly LOCKFILE

Expand All @@ -604,6 +602,10 @@ main() {

lock_acquire "${LOCKFILE}"

run_hooks "pre-install"

check_requirements

conditional_sleep
command_before_install
bootstrap_check
Expand All @@ -630,6 +632,8 @@ main() {
magento_publish_config

command_after_install

run_hooks "post-install"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fi
create_symlink() {
mkdir -p "$(app_path)/app/etc"
ln -sf "$(shared_config_path)/app/etc/env.php" "$(app_path)/app/etc/env.php"

run_hooks "post-start"
}

main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ main() {
trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR

lock_deploy

run_hooks "pre-stop"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ dump_build_version() {
}

main() {
run_hooks "pre-build"

check_requirements

command_before_build
Expand All @@ -229,6 +231,8 @@ main() {
dump_build_version

command_after_build

run_hooks "post-build"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,14 @@ check_command() {
error "Error: $1 is required but not installed."
fi
}

run_hooks() {
local hook="${1:-}"
if [[ -n "${hook}" ]] && [[ -d "$(app_path)/hooks/${hook}.d" ]]; then
for file in "$(app_path)"/hooks/"${hook}.d"/*.sh; do
log "Running ${file} for ${hook}"
# shellcheck disable=SC1090
source "${file}"
done
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,19 @@ function test_app_path() {
assert_equals "/app" "$(app_path)"
}

function version_gt() {
function test_version_gt() {
assert_true "$(version_gt '2.4.4' '2.3.99')"
assert_false "$(version_gt '2.3.99' '2.4.4')"
assert_false "$(version_gt '2.4.4' '2.4.4')"
assert_true "$(version_gt '2.4' '2.3.99')"
assert_false "$(version_gt '2.3.99' '2.4')"
assert_false "$(version_gt '2.4' '2.4')"
}

function test_run_hooks() {
local APP_PATH="./test-data/app"
mkdir -p "${APP_PATH}/hooks/test.d"
printf "#!/bin/bash\necho 'test-123'" >"${APP_PATH}/hooks/test.d/01-test.sh"
assert_contains "test-123" "$(run_hooks 'test')"
rm -fr "./test-data"
}
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,6 @@ magento_publish_config() {
}

main() {
check_requirements

LOCKFILE="$(shared_config_path)/.deploy.lock"
readonly LOCKFILE

Expand All @@ -604,6 +602,10 @@ main() {

lock_acquire "${LOCKFILE}"

run_hooks "pre-install"

check_requirements

conditional_sleep
command_before_install
bootstrap_check
Expand All @@ -630,6 +632,8 @@ main() {
magento_publish_config

command_after_install

run_hooks "post-install"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fi
create_symlink() {
mkdir -p "$(app_path)/app/etc"
ln -sf "$(shared_config_path)/app/etc/env.php" "$(app_path)/app/etc/env.php"

run_hooks "post-start"
}

main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ main() {
trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR

lock_deploy

run_hooks "pre-stop"
}

(return 0 2>/dev/null) && sourced=1
Expand Down

0 comments on commit 04f839b

Please sign in to comment.