Skip to content

Commit ebb1e0d

Browse files
committed
Added automatic lib deps add to profile
1 parent 116a917 commit ebb1e0d

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

commands/service_profile_lib_add.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
8787
addedLibs = append(addedLibs, libRefToAdd)
8888
return
8989
}
90+
// If the same version of the library has been already added to the profile, skip it
91+
if existingLibRef.Version.Equal(libReleaseToAdd.GetVersion()) {
92+
skippedLibs = append(skippedLibs, libRefToAdd)
93+
return
94+
}
9095
// If the library has been already added to the profile, just update the version
9196
if req.GetNoOverwrite() {
9297
skippedLibs = append(skippedLibs, libRefToAdd)

internal/cli/profile/lib.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func initLibCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
4949

5050
func initLibAddCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
5151
var destDir string
52-
52+
var noDeps bool
53+
var noOverwrite bool
5354
addCommand := &cobra.Command{
5455
Use: fmt.Sprintf("add %s[@%s]...", i18n.Tr("LIBRARY"), i18n.Tr("VERSION_NUMBER")),
5556
Short: i18n.Tr("Adds a library to the profile."),
@@ -59,27 +60,31 @@ func initLibAddCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
5960
" " + os.Args[0] + " profile lib add Arduino_JSON@0.2.0 --profile my_profile\n",
6061
Args: cobra.MinimumNArgs(1),
6162
Run: func(cmd *cobra.Command, args []string) {
62-
runLibAddCommand(cmd.Context(), args, srv, destDir)
63+
runLibAddCommand(cmd.Context(), args, srv, destDir, noDeps, noOverwrite)
6364
},
6465
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
6566
return arguments.GetInstallableLibs(cmd.Context(), srv), cobra.ShellCompDirectiveDefault
6667
},
6768
}
6869

6970
addCommand.Flags().StringVar(&destDir, "dest-dir", "", i18n.Tr("Location of the sketch project file."))
71+
addCommand.Flags().BoolVar(&noDeps, "no-deps", false, i18n.Tr("Do not add dependencies."))
72+
addCommand.Flags().BoolVar(&noOverwrite, "no-overwrite", false, i18n.Tr("Do not overwrite already added libraries."))
73+
7074
profileArg.AddToCommand(addCommand, srv)
7175

7276
return addCommand
7377
}
7478

75-
func runLibAddCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer, destDir string) {
79+
func runLibAddCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer, destDir string, noAddDeps, noOverwrite bool) {
7680
sketchPath := arguments.InitSketchPath(destDir)
7781

7882
instance := instance.CreateAndInit(ctx, srv)
7983
libRefs, err := lib.ParseLibraryReferenceArgsAndAdjustCase(ctx, srv, instance, args)
8084
if err != nil {
8185
feedback.Fatal(i18n.Tr("Arguments error: %v", err), feedback.ErrBadArgument)
8286
}
87+
addDeps := !noAddDeps
8388
for _, lib := range libRefs {
8489
resp, err := srv.ProfileLibAdd(ctx, &rpc.ProfileLibAddRequest{
8590
Instance: instance,
@@ -93,16 +98,23 @@ func runLibAddCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreSer
9398
},
9499
},
95100
},
101+
AddDependencies: &addDeps,
102+
NoOverwrite: &noOverwrite,
96103
})
97104
if err != nil {
98105
feedback.Fatal(i18n.Tr("Error adding %s to the profile %s: %v", lib.Name, profileArg.Get(), err), feedback.ErrGeneric)
99106
}
100107
added := f.Map(resp.GetAddedLibraries(), func(l *rpc.ProfileLibraryReference) *result.ProfileLibraryReference_IndexLibraryResult {
101108
return result.NewProfileLibraryReference_IndexLibraryResult(l.GetIndexLibrary())
102109
})
110+
skipped := f.Map(resp.GetSkippedLibraries(), func(l *rpc.ProfileLibraryReference) *result.ProfileLibraryReference_IndexLibraryResult {
111+
return result.NewProfileLibraryReference_IndexLibraryResult(l.GetIndexLibrary())
112+
})
103113
feedback.PrintResult(libAddResult{
104-
AddedLibraries: added,
105-
ProfileName: resp.ProfileName})
114+
AddedLibraries: added,
115+
SkippedLibraries: skipped,
116+
ProfileName: resp.ProfileName,
117+
})
106118
}
107119
}
108120

@@ -179,6 +191,12 @@ func (lr libAddResult) String() string {
179191
res += fmt.Sprintf(" - %s@%s\n", l.Name, l.Version)
180192
}
181193
}
194+
if len(lr.SkippedLibraries) > 0 {
195+
res += fmt.Sprintln(i18n.Tr("The following libraries were already present in the profile %s and were not modified:", lr.ProfileName))
196+
for _, l := range lr.SkippedLibraries {
197+
res += fmt.Sprintf(" - %s@%s\n", l.Name, l.Version)
198+
}
199+
}
182200
return res
183201
}
184202

0 commit comments

Comments
 (0)