#!/bin/bash job_start=0 job_start() { # [*] job start - [target]:[job] # bold echo -e "\e[1m[*] job start - $1:$2\e[0m" job_start=$(date +%s.%N) } job_success() { # [*] success! [target]:[job] finished in 241.2s :) end=$(date +%s.%N) time=$(echo "$end - $job_start" | bc -l) echo -e "\e[1m\e[32m[*] success! $1:$2 finished in $time seconds :)\e[0m" } job_fail() { # [x] FAIL! [target]:[job] failed in 241.2s :( end=$(date +%s.%N) time=$(echo "$end - $job_start" | bc -l) echo -e "\e[1m\e[31m[*] FAIL! $1:$2 failed in $time seconds :(\e[0m" } job_skip() { # [-] skipped - [target]:[job] skipped in 241.2s end=$(date +%s.%N) time=$(echo "$end - $job_start" | bc -l) echo -e "\e[1m\e[34m[-] skipped - $1:$2 skipped after $time seconds\e[0m" } sub_clone() { job_start "scratch-blocks" "clone" if [ -d "$(pwd)/scratch-blocks" ]; then echo "Clone dir already exists" job_skip "scratch-blocks" "clone" else git clone https://github.com/scratchfoundation/scratch-blocks scratch-blocks if [ "$?" != "0" ]; then job_fail "scratch-blocks" "clone" exit 1 fi job_success "scratch-blocks" "clone" fi job_start "scratch-blocks" "checkout" pbase=$(cat patches/blocks/PATCHBASE | tr -d '\n') echo "Checking out scratch-blocks patchbase $pbase" cd scratch-blocks || exit git checkout "$pbase" if [ "$?" != "0" ]; then job_fail "scratch-blocks" "checkout" exit 1 fi cd .. || exit job_success "scratch-blocks" "checkout" job_start "pybricks-micropython" "clone" if [ -d "$(pwd)/pybricks-micropython" ]; then echo "Clone dir already exists" job_skip "pybricks-micropython" "clone" else git clone https://github.com/pybricks/pybricks-micropython pybricks-micropython if [ "$?" != "0" ]; then job_fail "pybricks-micropython" "clone" exit 1 fi job_success "pybricks-micropython" "clone" fi job_start "pybricks-micropython" "env" cd "pybricks-micropython" || exit poetry env info | grep 'Python: ' | grep -q "3.10" if [ "$?" != "0" ]; then echo "Your python version is not supported. 3.10.x needed. Your version:" poetry env info | grep 'Python: ' | grep "3.*" echo "Run 'poetry env use /path/to/python3.10' to set the correct version." job_fail "pybricks-micropython" "env" fi cd .. || exit job_success "pybricks-micropython" "env" } sub_patch() { sub_clone job_start "scratch-blocks" "patch" cd scratch-blocks || exit git am --abort >/dev/null 2>&1 git am --3way "../patches/blocks/"*.patch if [ "$?" != "0" ]; then echo "One or more patches did not apply cleanly to scratch-blocks. Check above message and try again". job_fail "scratch-blocks" "patch" exit 1 fi cd .. || exit echo "[*] Patches applied cleanly to scratch-blocks" job_success "scratch-blocks" "patch" job_start "pybricks-micropython:micropython" "patch" cd pybricks-micropython/micropython || exit git am --abort >/dev/null 2>&1 git am --3way "../../patches/mpy/"*.patch if [ "$?" != "0" ]; then echo "One or more patches did not apply cleanly to pybricks-micropython. Check above message and try again". job_fail "pybricks-micropython:micropython" "patch" exit 1 fi echo "[*] Patches applied cleanly to pybricks-micropython" job_success "pybricks-micropython:micropython" "patch" cd ../.. || exit } sub_compile() { sub_patch cd scratch-blocks || exit job_start "scratch-blocks" "compile" NODE_OPTIONS=--openssl-legacy-provider npm i if [ "$?" != "0" ]; then job_fail "scratch-blocks" "compile" exit 1 fi cd .. || exit job_success "scratch-blocks" "compile" job_start "pybricks-micropython" "compile" cd pybricks-micropython || exit poetry run make primehub if [ "$?" != "0" ]; then job_fail "pybricks-micropython" "compile" exit 1 fi poetry run make technichub if [ "$?" != "0" ]; then job_fail "pybricks-micropython" "compile" exit 1 fi echo "[*] Compile complete!" echo "Wrote technichub firmware to pybricks-micropython/bricks/technichub/build/firmware.zip" echo "Wrote primehub firmware to pybricks-micropython/bricks/primehub/build/firmware.zip" cd .. || exit job_success "pybricks-micropython" "compile" } sub_patchc() { job_start "generic" "patch-create:collect-info" read -rp "Enter the current project name: " project read -rp "Enter the patchlist ID (blocks for scratch-blocks): " plist job_success "generic" "patch-create:collect-info" job_start "$project" "patch-create" cd "$project" || exit fname=$(git format-patch -1 HEAD -o ../patches/"$plist"/) base=$(basename "$fname") patchnumnp=$(find ../patches/"$plist" -type f -name "*.patch" | wc -l) patchnum=$(printf "%04d" "$patchnumnp") nonumfname="$(echo "$base" | cut -c 5-)" newfname="$patchnum$nonumfname" newpath=$(echo "$fname" | sed "s/$base/$newfname/" -) mv "$fname" "$newpath" echo "Wrote out patch file to $newfname" job_success "$project" "patch-create" echo "Checking that patch file applies cleanly" job_start "$project" "patch-check" cd .. || exit sub_patch job_success "$project" "patch-check" } sub_reset() { sub_clone } sub_help() { echo "--- PyBlocks Build Tool ---" echo "usage: ./pbt.sh [command]" echo " clone - clone upstream source code" echo " patch - (re)apply patches" echo " compile - compile bundles" echo " bundle - create prod bundles" echo " patchc - create a patch for the current project" echo " reset - reset to upstream patch base (equivalent to clone)" } subcommand=$1 case $subcommand in "" | "-h" | "--help" | "help") sub_help ;; *) echo "[*] Running build command $subcommand" shift sub_"${subcommand}" "$@" if [ $? = 127 ]; then echo "Error: '$subcommand' is not a known subcommand." >&2 echo " Run 'pbt.sh --help' for a list of known subcommands." >&2 exit 1 fi ;; esac