Skip to content

Commit 08135da

Browse files
committed
Updates to libraries-api.
Now some libraries may be tagged as dependencies, and also be automatically removed if no more needed.
1 parent f61b73f commit 08135da

File tree

7 files changed

+42
-31
lines changed

7 files changed

+42
-31
lines changed

cmd/gendoc/docs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,8 +1042,9 @@ Contains a JSON object with the details of an error.
10421042
Method: http.MethodDelete,
10431043
Path: "/v1/apps/{appID}/sketch/libraries/{libRef}",
10441044
Parameters: (*struct {
1045-
ID string `path:"appID" description:"application identifier."`
1046-
LibRef string `path:"libRef" description:"library reference (\"LibraryName\" or \"LibraryName@Version\")."`
1045+
ID string `path:"appID" description:"application identifier."`
1046+
LibRef string `path:"libRef" description:"library reference (\"LibraryName\" or \"LibraryName@Version\")."`
1047+
RemoveDependencies string `query:"remove_deps" description:"if set to \"true\", the library's dependencies will be removed as well if not needed anymore."`
10471048
})(nil),
10481049
CustomSuccessResponse: &CustomResponseDef{
10491050
ContentType: "application/json",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ replace (
1414
)
1515

1616
// Required until https://github.com/arduino/arduino-cli/pull/3019 is merged and released
17-
replace github.com/arduino/arduino-cli => github.com/cmaglie/arduino-cli v0.0.0-20251006122726-27fe68b0a18a
17+
replace github.com/arduino/arduino-cli => github.com/cmaglie/arduino-cli v0.0.0-20251029100020-2327b357349d
1818

1919
require (
2020
github.com/Andrew-M-C/go.emoji v1.1.4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ github.com/cloudflare/cfssl v0.0.0-20180223231731-4e2dcbde5004 h1:lkAMpLVBDaj17e
170170
github.com/cloudflare/cfssl v0.0.0-20180223231731-4e2dcbde5004/go.mod h1:yMWuSON2oQp+43nFtAV/uvKQIFpSPerB57DCt9t8sSA=
171171
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
172172
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
173-
github.com/cmaglie/arduino-cli v0.0.0-20251006122726-27fe68b0a18a h1:xjGwmLNEUTb+QWUmUxwrb24Awbt4/rdpFG7lVUR3skI=
174-
github.com/cmaglie/arduino-cli v0.0.0-20251006122726-27fe68b0a18a/go.mod h1:aH/Lfub80ymf3vpF0gUWx/sckVUNB0gDoPlABAx97SE=
173+
github.com/cmaglie/arduino-cli v0.0.0-20251029100020-2327b357349d h1:Hpm/DaAji25rxW1JPr1DK7A1wnUygOw0ftAs6UiHAq0=
174+
github.com/cmaglie/arduino-cli v0.0.0-20251029100020-2327b357349d/go.mod h1:aH/Lfub80ymf3vpF0gUWx/sckVUNB0gDoPlABAx97SE=
175175
github.com/cmaglie/pb v1.0.27 h1:ynGj8vBXR+dtj4B7Q/W/qGt31771Ux5iFfRQBnwdQiA=
176176
github.com/cmaglie/pb v1.0.27/go.mod h1:GilkKZMXYjBA4NxItWFfO+lwkp59PLHQ+IOW/b/kmZI=
177177
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=

internal/api/docs/openapi.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ paths:
302302
from the sketch project file.
303303
operationId: appSketchRemoveLibrary
304304
parameters:
305+
- description: if set to "true", the library's dependencies will be removed
306+
as well if not needed anymore.
307+
in: query
308+
name: remove_deps
309+
schema:
310+
description: if set to "true", the library's dependencies will be removed
311+
as well if not needed anymore.
312+
type: string
305313
- description: application identifier.
306314
in: path
307315
name: appID

internal/api/handlers/app_sketch_libs.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ func HandleSketchRemoveLibrary(idProvider *app.IDProvider) http.HandlerFunc {
9090
return
9191
}
9292

93-
if removedLib, err := orchestrator.RemoveSketchLibrary(r.Context(), app, libRef); err != nil {
93+
// Get query param addDeps (default false)
94+
removeDeps, _ := strconv.ParseBool(r.URL.Query().Get("remove_deps"))
95+
if removedLibs, err := orchestrator.RemoveSketchLibrary(r.Context(), app, libRef, removeDeps); err != nil {
9496
render.EncodeResponse(w, http.StatusInternalServerError, models.ErrorResponse{Details: "unable to remove sketch library"})
9597
return
9698
} else {
9799
render.EncodeResponse(w, http.StatusOK, SketchRemoveLibraryResponse{
98-
RemovedLibraries: []orchestrator.LibraryReleaseID{removedLib},
100+
RemovedLibraries: removedLibs,
99101
})
100102
return
101103
}

internal/orchestrator/sketch_libs.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryRel
4646
resp, err := srv.ProfileLibAdd(ctx, &rpc.ProfileLibAddRequest{
4747
Instance: inst,
4848
SketchPath: app.MainSketchPath.String(),
49-
Library: &rpc.SketchProfileLibraryReference{
50-
Library: &rpc.SketchProfileLibraryReference_IndexLibrary_{
51-
IndexLibrary: &rpc.SketchProfileLibraryReference_IndexLibrary{
49+
Library: &rpc.ProfileLibraryReference{
50+
Library: &rpc.ProfileLibraryReference_IndexLibrary_{
51+
IndexLibrary: &rpc.ProfileLibraryReference_IndexLibrary{
5252
Name: libRef.Name,
5353
Version: libRef.Version,
5454
},
@@ -62,11 +62,11 @@ func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryRel
6262
return f.Map(resp.GetAddedLibraries(), rpcProfileLibReferenceToLibReleaseID), nil
6363
}
6464

65-
func RemoveSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryReleaseID) (LibraryReleaseID, error) {
65+
func RemoveSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryReleaseID, removeDeps bool) ([]LibraryReleaseID, error) {
6666
srv := commands.NewArduinoCoreServer()
6767
var inst *rpc.Instance
6868
if res, err := srv.Create(ctx, &rpc.CreateRequest{}); err != nil {
69-
return LibraryReleaseID{}, err
69+
return nil, err
7070
} else {
7171
inst = res.Instance
7272
}
@@ -77,23 +77,24 @@ func RemoveSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef Library
7777
// TODO: LOG progress/error?
7878
return nil
7979
})); err != nil {
80-
return LibraryReleaseID{}, err
80+
return nil, err
8181
}
8282

8383
resp, err := srv.ProfileLibRemove(ctx, &rpc.ProfileLibRemoveRequest{
84-
Library: &rpc.SketchProfileLibraryReference{
85-
Library: &rpc.SketchProfileLibraryReference_IndexLibrary_{
86-
IndexLibrary: &rpc.SketchProfileLibraryReference_IndexLibrary{
84+
Library: &rpc.ProfileLibraryReference{
85+
Library: &rpc.ProfileLibraryReference_IndexLibrary_{
86+
IndexLibrary: &rpc.ProfileLibraryReference_IndexLibrary{
8787
Name: libRef.Name,
8888
},
8989
},
9090
},
91-
SketchPath: app.MainSketchPath.String(),
91+
SketchPath: app.MainSketchPath.String(),
92+
RemoveDependencies: &removeDeps,
9293
})
9394
if err != nil {
94-
return LibraryReleaseID{}, err
95+
return nil, err
9596
}
96-
return rpcProfileLibReferenceToLibReleaseID(resp.GetLibrary()), nil
97+
return f.Map(resp.GetRemovedLibraries(), rpcProfileLibReferenceToLibReleaseID), nil
9798
}
9899

99100
func ListSketchLibraries(ctx context.Context, app app.ArduinoApp) ([]LibraryReleaseID, error) {
@@ -107,19 +108,17 @@ func ListSketchLibraries(ctx context.Context, app app.ArduinoApp) ([]LibraryRele
107108
}
108109

109110
// Keep only index libraries
110-
libs := f.Filter(resp.Libraries, func(l *rpc.SketchProfileLibraryReference) bool {
111+
libs := f.Filter(resp.Libraries, func(l *rpc.ProfileLibraryReference) bool {
111112
return l.GetIndexLibrary() != nil
112113
})
113-
res := f.Map(libs, func(l *rpc.SketchProfileLibraryReference) LibraryReleaseID {
114-
return LibraryReleaseID{
115-
Name: l.GetIndexLibrary().GetName(),
116-
Version: l.GetIndexLibrary().GetVersion(),
117-
}
118-
})
119-
return res, nil
114+
return f.Map(libs, rpcProfileLibReferenceToLibReleaseID), nil
120115
}
121116

122-
func rpcProfileLibReferenceToLibReleaseID(ref *rpc.SketchProfileLibraryReference) LibraryReleaseID {
117+
func rpcProfileLibReferenceToLibReleaseID(ref *rpc.ProfileLibraryReference) LibraryReleaseID {
123118
l := ref.GetIndexLibrary()
124-
return NewLibraryReleaseID(l.GetName(), l.GetVersion())
119+
return LibraryReleaseID{
120+
Name: l.GetName(),
121+
Version: l.GetVersion(),
122+
IsDependency: l.GetIsDependency(),
123+
}
125124
}

internal/orchestrator/sketch_libs_release_id.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ import (
2727
// - name[@version]
2828
// Version is optional, if not provided, the latest version available will be used.
2929
type LibraryReleaseID struct {
30-
Name string
31-
Version string
30+
Name string
31+
Version string
32+
IsDependency bool
3233
}
3334

3435
func NewLibraryReleaseID(name string, version string) LibraryReleaseID {

0 commit comments

Comments
 (0)