Skip to content

Commit c631b3b

Browse files
committed
feat: Add library installation before sketch builds in CI scripts
1 parent c386501 commit c631b3b

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

.github/scripts/on-push.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function build {
1414
shift 6
1515
local sketches=("$@")
1616

17+
local INSTALL_LIBS="${SCRIPTS_DIR}/sketch_utils.sh install_libs"
1718
local BUILD_SKETCH="${SCRIPTS_DIR}/sketch_utils.sh build"
1819
local BUILD_SKETCHES="${SCRIPTS_DIR}/sketch_utils.sh chunk_build"
1920

@@ -34,6 +35,10 @@ function build {
3435
local ctags_version
3536
local preprocessor_version
3637
sargs+=("-s" "$(dirname "$sketch")")
38+
39+
# Install libraries before building
40+
${INSTALL_LIBS} -ai "$ARDUINO_IDE_PATH" -s "$(dirname "$sketch")"
41+
3742
if [ "$OS_IS_WINDOWS" == "1" ] && [ -d "$ARDUINO_IDE_PATH/tools-builder" ]; then
3843
ctags_version=$(ls "$ARDUINO_IDE_PATH/tools-builder/ctags/")
3944
preprocessor_version=$(ls "$ARDUINO_IDE_PATH/tools-builder/arduino-preprocessor/")

.github/scripts/sketch_utils.sh

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,15 @@ function build_sketches { # build_sketches <ide_path> <user_path> <target> <path
559559
fi
560560
echo ""
561561
echo "Building Sketch Index $sketchnum - $sketchdirname"
562+
563+
# Install libraries from ci.json if they exist
564+
install_libs -ai "$ide_path" -s "$sketchdir"
565+
local install_result=$?
566+
if [ $install_result -ne 0 ]; then
567+
echo "ERROR: Library installation failed for $sketchdirname"
568+
return $install_result
569+
fi
570+
562571
build_sketch "${args[@]}" -s "$sketchdir" "${xtra_opts[@]}"
563572
local result=$?
564573
if [ $result -ne 0 ]; then
@@ -580,13 +589,173 @@ function build_sketches { # build_sketches <ide_path> <user_path> <target> <path
580589
return 0
581590
}
582591

592+
function install_libs { # install_libs <ide_path> <sketchdir> [-v]
593+
local ide_path=""
594+
local sketchdir=""
595+
local verbose=false
596+
597+
# Parse arguments
598+
while [ -n "$1" ]; do
599+
case "$1" in
600+
-ai )
601+
shift
602+
ide_path=$1
603+
;;
604+
-s )
605+
shift
606+
sketchdir=$1
607+
;;
608+
-v )
609+
verbose=true
610+
;;
611+
* )
612+
echo "ERROR: Unknown argument: $1"
613+
echo "USAGE: install_libs -ai <ide_path> -s <sketchdir> [-v]"
614+
return 1
615+
;;
616+
esac
617+
shift
618+
done
619+
620+
# Validate required arguments
621+
if [ -z "$ide_path" ]; then
622+
echo "ERROR: IDE path not provided"
623+
echo "USAGE: install_libs -ai <ide_path> -s <sketchdir> [-v]"
624+
return 1
625+
fi
626+
627+
if [ -z "$sketchdir" ]; then
628+
echo "ERROR: Sketch directory not provided"
629+
echo "USAGE: install_libs -ai <ide_path> -s <sketchdir> [-v]"
630+
return 1
631+
fi
632+
633+
# Check if arduino-cli exists
634+
if [ ! -f "$ide_path/arduino-cli" ]; then
635+
echo "ERROR: arduino-cli not found at $ide_path/arduino-cli"
636+
return 1
637+
fi
638+
639+
# Check if ci.json exists
640+
if [ ! -f "$sketchdir/ci.json" ]; then
641+
[ "$verbose" = true ] && echo "No ci.json found in $sketchdir, skipping library installation"
642+
return 0
643+
fi
644+
645+
# Check if libs array exists in ci.json
646+
libs_type=$(jq -r '.libs | type' "$sketchdir/ci.json" 2>/dev/null)
647+
if [ "$libs_type" = "null" ]; then
648+
[ "$verbose" = true ] && echo "No libs field found in ci.json, skipping library installation"
649+
return 0
650+
elif [ "$libs_type" != "array" ]; then
651+
echo "ERROR: libs field in ci.json must be an array, found: $libs_type"
652+
return 1
653+
fi
654+
655+
libs_count=$(jq -r '.libs | length' "$sketchdir/ci.json" 2>/dev/null)
656+
if [ "$libs_count" -eq 0 ]; then
657+
[ "$verbose" = true ] && echo "libs array is empty in ci.json, skipping library installation"
658+
return 0
659+
fi
660+
661+
echo "Installing $libs_count libraries from $sketchdir/ci.json"
662+
663+
# Check if any unsafe installations are needed (GitHub URLs only)
664+
needs_unsafe=false
665+
libs=$(jq -r '.libs[]? // empty' "$sketchdir/ci.json")
666+
for lib in $libs; do
667+
if [[ "$lib" == https://github.com/* ]]; then
668+
needs_unsafe=true
669+
break
670+
fi
671+
done
672+
673+
# Enable unsafe installs if needed
674+
original_unsafe_setting=""
675+
if [ "$needs_unsafe" = true ]; then
676+
[ "$verbose" = true ] && echo "Checking current unsafe install setting..."
677+
# Get current setting
678+
original_unsafe_setting=$("$ide_path/arduino-cli" config get library.enable_unsafe_install 2>/dev/null || echo "false")
679+
680+
if [ "$original_unsafe_setting" = "false" ]; then
681+
[ "$verbose" = true ] && echo "Enabling unsafe installs for Git URLs..."
682+
# Enable unsafe installs
683+
"$ide_path/arduino-cli" config set library.enable_unsafe_install true
684+
enable_status=$?
685+
if [ $enable_status -ne 0 ]; then
686+
echo "WARNING: Failed to enable unsafe installs, some libraries may fail to install"
687+
fi
688+
else
689+
[ "$verbose" = true ] && echo "Unsafe installs already enabled"
690+
fi
691+
fi
692+
693+
# Install libraries
694+
for lib in $libs; do
695+
[ "$verbose" = true ] && echo "Processing library: $lib"
696+
697+
# Determine the type of library reference and install accordingly
698+
if [[ "$lib" == https://github.com/* ]]; then
699+
# GitHub URL
700+
[ "$verbose" = true ] && echo "Installing library from GitHub URL: $lib"
701+
if [ "$verbose" = true ]; then
702+
"$ide_path/arduino-cli" lib install --git-url "$lib"
703+
install_status=$?
704+
else
705+
# Capture both stdout and stderr, show only errors
706+
output=$("$ide_path/arduino-cli" lib install --git-url "$lib" 2>&1)
707+
install_status=$?
708+
if [ $install_status -ne 0 ]; then
709+
echo "$output" | grep -E "Error|WARNING|WARN" || echo "$output"
710+
fi
711+
fi
712+
else
713+
# Library name (with optional version)
714+
[ "$verbose" = true ] && echo "Installing library by name: $lib"
715+
if [ "$verbose" = true ]; then
716+
"$ide_path/arduino-cli" lib install "$lib"
717+
install_status=$?
718+
else
719+
# Capture both stdout and stderr, show only errors
720+
output=$("$ide_path/arduino-cli" lib install "$lib" 2>&1)
721+
install_status=$?
722+
if [ $install_status -ne 0 ]; then
723+
echo "$output" | grep -E "Error|WARNING|WARN" || echo "$output"
724+
fi
725+
fi
726+
fi
727+
728+
if [ $install_status -ne 0 ]; then
729+
echo "ERROR: Failed to install library: $lib"
730+
# Restore original setting if we changed it
731+
if [ "$needs_unsafe" = true ] && [ "$original_unsafe_setting" = "false" ]; then
732+
[ "$verbose" = true ] && echo "Restoring original unsafe install setting..."
733+
"$ide_path/arduino-cli" config set library.enable_unsafe_install false >/dev/null 2>&1
734+
fi
735+
return $install_status
736+
else
737+
[ "$verbose" = true ] && echo "Successfully installed library: $lib"
738+
fi
739+
done
740+
741+
# Restore original setting if we changed it
742+
if [ "$needs_unsafe" = true ] && [ "$original_unsafe_setting" = "false" ]; then
743+
[ "$verbose" = true ] && echo "Restoring original unsafe install setting..."
744+
"$ide_path/arduino-cli" config set library.enable_unsafe_install false >/dev/null 2>&1
745+
fi
746+
747+
echo "Library installation completed"
748+
return 0
749+
}
750+
583751
USAGE="
584752
USAGE: ${0} [command] [options]
585753
Available commands:
586754
count: Count sketches.
587755
build: Build a sketch.
588756
chunk_build: Build a chunk of sketches.
589757
check_requirements: Check if target meets sketch requirements.
758+
install_libs: Install libraries from ci.json file.
590759
"
591760

592761
cmd=$1
@@ -606,6 +775,8 @@ case "$cmd" in
606775
;;
607776
"check_requirements") check_requirements "$@"
608777
;;
778+
"install_libs") install_libs "$@"
779+
;;
609780
*)
610781
echo "ERROR: Unrecognized command"
611782
echo "$USAGE"

.github/scripts/tests_build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ if [ $chunk_build -eq 1 ]; then
7575
else
7676
BUILD_CMD="${SCRIPTS_DIR}/sketch_utils.sh build"
7777
args+=("-s" "$test_folder/$sketch")
78+
79+
# Install libraries before building individual sketch
80+
"${SCRIPTS_DIR}/sketch_utils.sh" install_libs -ai "$ARDUINO_IDE_PATH" -s "$test_folder/$sketch"
7881
fi
7982

8083
${BUILD_CMD} "${args[@]}" "$@"

0 commit comments

Comments
 (0)