#!/usr/bin/env bash # # cobtisan - the Artisan of the mainframe. # # On the host it forwards commands into the Docker container. # Inside the container it does the actual work. # set -euo pipefail cd "$(dirname "$0")" G=$'\033[32m'; Y=$'\033[33m'; R=$'\033[31m'; B=$'\033[1m'; D=$'\033[2m'; N=$'\033[0m' # ---------------------------------------------------------------- host side if [ -z "${LARAVEL_COBOL_CONTAINER:-}" ]; then case "${1:-list}" in serve|up) exec docker compose up --build ;; down) exec docker compose down ;; benchmark) bench=(docker compose -f benchmarks/docker-compose.yml) status=0 "${bench[@]}" up --build --attach bench --abort-on-container-exit --exit-code-from bench || status=$? "${bench[@]}" down exit $status ;; *) if [ -n "$(docker compose ps -q --status running app 2>/dev/null)" ]; then exec docker compose exec app bash ./cobtisan "$@" else exec docker compose run --rm --no-deps app bash ./cobtisan "$@" fi ;; esac fi # ----------------------------------------------------------- container side export COB_LIBRARY_PATH=/app/build COPYBOOKS=(-I framework/copybooks -I app/Models) info() { printf "\n ${B}${G} INFO ${N} %s\n\n" "$*"; } fail() { printf "\n ${B}${R} ABEND ${N} %s\n\n" "$*"; exit 12; } dotenv() { # dotenv KEY -> value, the process environment wins local v="${!1:-}" if [ -z "$v" ] && [ -f .env ]; then v="$(grep -E "^$1=" .env | head -1 | cut -d= -f2- | sed 's/^"//; s/"$//')" fi printf '%s' "$v" } program_id() { grep -m1 -oE 'PROGRAM-ID\. *[A-Z0-9-]+' "$1" | sed -E 's/PROGRAM-ID\. *//' } dots() { # dots LABEL STATUS local label="$1" status="$2" width=64 local n=$(( width - ${#label} )) [ $n -lt 3 ] && n=3 printf " %s ${D}%s${N} %s\n" "$label" "$(printf '%*s' "$n" '' | tr ' ' '.')" "$status" } sources() { find app framework routes database -name '*.cbl' | sort } cmd_build() { mkdir -p build local newest_cpy newest_cpy="$(find framework/copybooks app/Models -name '*.cpy' -printf '%T@\n' | sort -n | tail -1)" local compiled=0 failed=0 info "Compiling. Please hold for the next available batch window." for src in $(sources) public/INDEX.cbl; do local pid out if [ "$src" = public/INDEX.cbl ]; then out=build/index else pid="$(program_id "$src")" out="build/${pid}.so" fi if [ -f "$out" ] && [ "$out" -nt "$src" ] \ && [ "$(stat -c %Y "$out")" -ge "${newest_cpy%.*}" ]; then continue fi local flags=(-m) [ "$src" = public/INDEX.cbl ] && flags=(-x) if log="$(cobc "${flags[@]}" "${COPYBOOKS[@]}" -o "$out" "$src" 2>&1)"; then dots "$src" "${G}DONE${N}" compiled=$((compiled + 1)) else dots "$src" "${R}FAIL${N}" printf '%s\n' "$log" | sed 's/^/ /' failed=$((failed + 1)) fi done echo if [ $failed -gt 0 ]; then fail "IGYSC0000-S $failed program(s) failed to compile. RC=12" fi if [ $compiled -eq 0 ]; then printf " ${D}Nothing to compile. Everything is already legacy.${N}\n\n" else printf " ${G}IGYSC0000-I %d program(s) compiled. RC=0${N}\n\n" "$compiled" fi } db_dir() { local d; d="$(dotenv DB_DATABASE)"; printf '%s' "${d:-database/data}"; } cmd_migrate() { local seed="${1:-}" dir log ran=0 dir="$(db_dir)"; mkdir -p "$dir"; log="$dir/migrations.log"; touch "$log" for src in $(find database/migrations -name '*.cbl' | sort); do local pid; pid="$(program_id "$src")" grep -qx "$pid" "$log" && continue [ $ran -eq 0 ] && info "Running migrations." local t0=$(date +%s%N) cobcrun "$pid" || fail "Migration $pid failed." echo "$pid" >> "$log" dots "$pid" "${G}$(( ($(date +%s%N) - t0) / 1000000 ))ms DONE${N}" ran=$((ran + 1)) done if [ $ran -eq 0 ]; then info "Nothing to migrate." else echo [ "$seed" = "--seed" ] && cmd_seed fi return 0 } cmd_fresh() { local dir; dir="$(db_dir)" info "Dropping all datasets. IEFBR14 says hello." rm -f "$dir"/*.dat* "$dir/migrations.log" cmd_migrate "${1:-}" } cmd_seed() { info "Seeding database." cobcrun DATABASE-SEEDER || fail "Seeder failed." dots "DATABASE-SEEDER" "${G}DONE${N}" echo } cmd_key() { [ -f .env ] || cp .env.example .env local key="cobol:$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n' | tr a-f A-F)" if grep -q '^APP_KEY=' .env; then sed -i "s/^APP_KEY=.*/APP_KEY=$key/" .env else echo "APP_KEY=$key" >> .env fi info "Application key set successfully." } cmd_make() { # cmd_make controller|middleware NAME local kind="$1" name="${2:-}" dir stub [ -n "$name" ] || fail "Usage: cobtisan make:$kind NAME" name="$(printf '%s' "$name" | tr a-z A-Z)" case "$kind" in controller) dir=app/Http/Controllers; stub=stubs/controller.stub [[ "$name" == *-CONTROLLER ]] || name="$name-CONTROLLER" ;; middleware) dir=app/Http/Middleware; stub=stubs/middleware.stub ;; esac local file="$dir/$name.cbl" [ -e "$file" ] && fail "$kind [$file] already exists." sed "s/{{ NAME }}/$name/g" "$stub" > "$file" info "$(printf '%s' "${kind^}") [$file] created successfully. ($(wc -l < "$file") lines. You're welcome.)" } cmd_lint() { local bad=0 while IFS= read -r f; do while IFS=: read -r line len; do printf " ${R}IGYDS0001-W${N} %s:%s is %s columns wide. Nature intended 72.\n" "$f" "$line" "$len" bad=$((bad + 1)) done < <(awk '{ n = length($0); if (n > 72) print NR ":" n }' "$f") done < <(find app framework routes database public -name '*.cbl' -o -name '*.cpy' | sort) if [ $bad -gt 0 ]; then fail "$bad line(s) beyond column 72." fi info "All lines fit in 72 columns. As nature intended." } cmd_test() { export DB_DATABASE=storage/framework/testing cmd_build >/dev/null cmd_fresh --seed >/dev/null set +e +o pipefail source tests/TestCase.sh for f in tests/Feature/*.sh; do source "$f" done test_summary } cmd_about() { echo printf " ${B}Environment${N}\n" dots "Application Name" "$(dotenv APP_NAME)" dots "Environment" "$(dotenv APP_ENV)" dots "Compiler" "$(cobc --version | head -1)" dots "Web Server" "$(lighttpd -v 2>&1 | head -1 | cut -d' ' -f1)" dots "Database" "$(dotenv DB_CONNECTION) ($(db_dir))" dots "Mainframe" "Not detected. Pretending." dots "Understanding" "Optional" echo } cmd_list() { cat < [arguments] ${Y}Available commands:${N} ${G}serve${N} Start the development server (Docker, on the host) ${G}down${N} Stop it ${G}about${N} Display basic information about your application ${G}benchmark${N} Race against Laravel (PHP) on a \$5 VPS budget (on the host) ${G}build${N} Compile all COBOL programs into build/ ${G}inspire${N} Display an inspiring quote ${G}key:generate${N} Set the application key ${G}lint${N} Check that nothing goes past column 72 ${G}test${N} Run the feature tests ${Y}db${N} ${G}db:seed${N} Seed the database ${Y}make${N} ${G}make:controller${N} Create a new controller program ${G}make:middleware${N} Create a new middleware program ${Y}migrate${N} ${G}migrate${N} Run the database migrations [--seed] ${G}migrate:fresh${N} Drop all datasets and re-run all migrations [--seed] ${Y}route${N} ${G}route:list${N} List all registered routes EOF } case "${1:-list}" in build) cmd_build ;; migrate) cmd_migrate "${2:-}" ;; migrate:fresh) cmd_fresh "${2:-}" ;; db:seed) cmd_seed ;; route:list) cobcrun ROUTE-LIST ;; inspire) cobcrun INSPIRE ;; key:generate) cmd_key ;; make:controller) cmd_make controller "${2:-}" ;; make:middleware) cmd_make middleware "${2:-}" ;; lint) cmd_lint ;; test) cmd_test ;; about) cmd_about ;; serve) exec lighttpd -D -f /app/docker/lighttpd.conf ;; list|help|-h|--help) cmd_list ;; *) fail "Command \"$1\" is not defined. Have you tried turning the mainframe off and on again?" ;; esac