|
18 | 18 | package lib
|
19 | 19 |
|
20 | 20 | import (
|
21 |
| - "context" |
22 | 21 | "fmt"
|
23 | 22 | "net/http"
|
24 | 23 |
|
| 24 | + "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" |
25 | 25 | "github.com/arduino/arduino-cli/commands"
|
26 |
| - rpc "github.com/arduino/arduino-cli/rpc/commands" |
27 | 26 | )
|
28 | 27 |
|
29 |
| -// LibraryUpgradeAll FIXMEDOC |
30 |
| -func LibraryUpgradeAll(ctx context.Context, req *rpc.LibraryUpgradeAllReq, downloadCB commands.DownloadProgressCB, |
| 28 | +// LibraryUpgradeAll upgrades all the available libraries |
| 29 | +func LibraryUpgradeAll(instanceID int32, downloadCB commands.DownloadProgressCB, |
| 30 | + taskCB commands.TaskProgressCB, headers http.Header) error { |
| 31 | + // get the library manager |
| 32 | + lm := commands.GetLibraryManager(instanceID) |
| 33 | + |
| 34 | + if err := upgrade(lm, listLibraries(lm, true, true), downloadCB, taskCB, headers); err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + if _, err := commands.Rescan(instanceID); err != nil { |
| 39 | + return fmt.Errorf("rescanning libraries: %s", err) |
| 40 | + } |
| 41 | + |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +// LibraryUpgrade upgrades only the given libraries |
| 46 | +func LibraryUpgrade(instanceID int32, libraryNames []string, downloadCB commands.DownloadProgressCB, |
| 47 | + taskCB commands.TaskProgressCB, headers http.Header) error { |
| 48 | + // get the library manager |
| 49 | + lm := commands.GetLibraryManager(instanceID) |
| 50 | + |
| 51 | + // get the libs to upgrade |
| 52 | + libs := filterByName(listLibraries(lm, true, true), libraryNames) |
| 53 | + |
| 54 | + // do it |
| 55 | + return upgrade(lm, libs, downloadCB, taskCB, headers) |
| 56 | +} |
| 57 | + |
| 58 | +func upgrade(lm *librariesmanager.LibrariesManager, libs []*installedLib, downloadCB commands.DownloadProgressCB, |
31 | 59 | taskCB commands.TaskProgressCB, downloaderHeaders http.Header) error {
|
32 |
| - lm := commands.GetLibraryManager(req) |
33 | 60 |
|
34 |
| - // Obtain the list of upgradable libraries |
35 |
| - list := listLibraries(lm, true, true) |
| 61 | + // Go through the list and download them |
36 | 62 |
|
37 |
| - for _, upgradeDesc := range list { |
38 |
| - if err := downloadLibrary(lm, upgradeDesc.Available, downloadCB, taskCB, downloaderHeaders); err != nil { |
| 63 | + for _, lib := range libs { |
| 64 | + if err := downloadLibrary(lm, lib.Available, downloadCB, taskCB, downloaderHeaders); err != nil { |
39 | 65 | return err
|
40 | 66 | }
|
41 | 67 | }
|
42 |
| - for _, upgradeDesc := range list { |
43 |
| - installLibrary(lm, upgradeDesc.Available, taskCB) |
44 |
| - } |
45 | 68 |
|
46 |
| - if _, err := commands.Rescan(ctx, &rpc.RescanReq{Instance: req.Instance}); err != nil { |
47 |
| - return fmt.Errorf("rescanning libraries: %s", err) |
| 69 | + // Go through the list and install them |
| 70 | + for _, lib := range libs { |
| 71 | + if err := installLibrary(lm, lib.Available, taskCB); err != nil { |
| 72 | + return err |
| 73 | + } |
48 | 74 | }
|
| 75 | + |
49 | 76 | return nil
|
50 | 77 | }
|
| 78 | + |
| 79 | +func filterByName(libs []*installedLib, names []string) []*installedLib { |
| 80 | + // put the names in a map to ease lookup |
| 81 | + queryMap := make(map[string]struct{}) |
| 82 | + for _, name := range names { |
| 83 | + queryMap[name] = struct{}{} |
| 84 | + } |
| 85 | + |
| 86 | + ret := []*installedLib{} |
| 87 | + for _, lib := range libs { |
| 88 | + // skip if library name wasn't in the query |
| 89 | + if _, found := queryMap[lib.Library.Name]; found { |
| 90 | + ret = append(ret, lib) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return ret |
| 95 | +} |
0 commit comments