@@ -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+
583751USAGE="
584752USAGE: ${0} [command] [options]
585753Available 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
592761cmd=$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 "
0 commit comments