|  | 
|  | 1 | +// This file is part of arduino-cli. | 
|  | 2 | +// | 
|  | 3 | +// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | 
|  | 4 | +// | 
|  | 5 | +// This software is released under the GNU General Public License version 3, | 
|  | 6 | +// which covers the main part of arduino-cli. | 
|  | 7 | +// The terms of this license can be found at: | 
|  | 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html | 
|  | 9 | +// | 
|  | 10 | +// You can be released from the requirements of the above licenses by purchasing | 
|  | 11 | +// a commercial license. Buying such a license is mandatory if you want to | 
|  | 12 | +// modify or otherwise use the software for commercial activities involving the | 
|  | 13 | +// Arduino software without disclosing the source code of your own applications. | 
|  | 14 | +// To purchase a commercial license, send an email to [email protected]. | 
|  | 15 | + | 
|  | 16 | +package profile | 
|  | 17 | + | 
|  | 18 | +import ( | 
|  | 19 | +	"context" | 
|  | 20 | +	"fmt" | 
|  | 21 | +	"os" | 
|  | 22 | + | 
|  | 23 | +	"github.com/arduino/arduino-cli/internal/cli/arguments" | 
|  | 24 | +	"github.com/arduino/arduino-cli/internal/cli/feedback" | 
|  | 25 | +	"github.com/arduino/arduino-cli/internal/cli/instance" | 
|  | 26 | +	"github.com/arduino/arduino-cli/internal/cli/lib" | 
|  | 27 | +	"github.com/arduino/arduino-cli/internal/i18n" | 
|  | 28 | +	rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | 
|  | 29 | +	"github.com/spf13/cobra" | 
|  | 30 | +) | 
|  | 31 | + | 
|  | 32 | +func initLibCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { | 
|  | 33 | +	libCommand := &cobra.Command{ | 
|  | 34 | +		Use:   "lib", | 
|  | 35 | +		Short: i18n.Tr("Profile commands about libraries."), | 
|  | 36 | +		Long:  i18n.Tr("Profile commands about libraries."), | 
|  | 37 | +		Example: "" + | 
|  | 38 | +			"  " + os.Args[0] + " profile lib add AudioZero -m my_profile\n" + | 
|  | 39 | +			"  " + os.Args[0] + " profile lib remove Arduino_JSON --profile my_profile\n", | 
|  | 40 | +	} | 
|  | 41 | + | 
|  | 42 | +	libCommand.AddCommand(initLibAddCommand(srv)) | 
|  | 43 | + | 
|  | 44 | +	return libCommand | 
|  | 45 | +} | 
|  | 46 | + | 
|  | 47 | +func initLibAddCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { | 
|  | 48 | +	var destDir string | 
|  | 49 | + | 
|  | 50 | +	addCommand := &cobra.Command{ | 
|  | 51 | +		Use:   fmt.Sprintf("add %s[@%s]...", i18n.Tr("LIBRARY"), i18n.Tr("VERSION_NUMBER")), | 
|  | 52 | +		Short: i18n.Tr("Adds a library to the profile."), | 
|  | 53 | +		Long:  i18n.Tr("Adds a library to the profile."), | 
|  | 54 | +		Example: "" + | 
|  | 55 | +			"  " + os.Args[0] + " profile lib add AudioZero -m my_profile\n" + | 
|  | 56 | +			"  " + os.Args [0 ] + " profile lib add [email protected] --profile my_profile\n" , | 
|  | 57 | +		Args: cobra.MinimumNArgs(1), | 
|  | 58 | +		Run: func(cmd *cobra.Command, args []string) { | 
|  | 59 | +			runLibAddCommand(cmd.Context(), args, srv, destDir) | 
|  | 60 | +		}, | 
|  | 61 | +		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | 
|  | 62 | +			return arguments.GetInstallableLibs(cmd.Context(), srv), cobra.ShellCompDirectiveDefault | 
|  | 63 | +		}, | 
|  | 64 | +	} | 
|  | 65 | + | 
|  | 66 | +	addCommand.Flags().StringVar(&destDir, "dest-dir", "", i18n.Tr("Location of the project file.")) | 
|  | 67 | +	profileArg.AddToCommand(addCommand, srv) | 
|  | 68 | + | 
|  | 69 | +	return addCommand | 
|  | 70 | +} | 
|  | 71 | + | 
|  | 72 | +func runLibAddCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer, destDir string) { | 
|  | 73 | +	sketchPath := arguments.InitSketchPath(destDir) | 
|  | 74 | + | 
|  | 75 | +	instance := instance.CreateAndInit(ctx, srv) | 
|  | 76 | +	libRefs, err := lib.ParseLibraryReferenceArgsAndAdjustCase(ctx, srv, instance, args) | 
|  | 77 | +	if err != nil { | 
|  | 78 | +		feedback.Fatal(i18n.Tr("Arguments error: %v", err), feedback.ErrBadArgument) | 
|  | 79 | +	} | 
|  | 80 | +	for _, lib := range libRefs { | 
|  | 81 | +		_, err := srv.ProfileLibAdd(ctx, &rpc.ProfileLibAddRequest{ | 
|  | 82 | +			Instance:    instance, | 
|  | 83 | +			SketchPath:  sketchPath.String(), | 
|  | 84 | +			ProfileName: profileArg.Get(), | 
|  | 85 | +			LibName:     lib.Name, | 
|  | 86 | +			LibVersion:  lib.Version, | 
|  | 87 | +		}) | 
|  | 88 | +		if err != nil { | 
|  | 89 | +			feedback.Fatal(i18n.Tr("Error adding %s to the profile %s: %v", lib.Name, profileArg.Get(), err), feedback.ErrGeneric) | 
|  | 90 | +		} | 
|  | 91 | +	} | 
|  | 92 | + | 
|  | 93 | +	feedback.Printf("Lib added: %s", sketchPath.String()) | 
|  | 94 | +} | 
0 commit comments