forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibrariesmanager.go
217 lines (197 loc) · 6.54 KB
/
librariesmanager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package librariesmanager
import (
"errors"
"fmt"
"os"
"slices"
"strings"
"github.com/arduino/arduino-cli/internal/arduino/cores"
"github.com/arduino/arduino-cli/internal/arduino/libraries"
"github.com/arduino/arduino-cli/internal/arduino/libraries/librariesindex"
"github.com/arduino/arduino-cli/internal/i18n"
paths "github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// LibrariesManager keeps the current status of the libraries in the system
// (the list of libraries, revisions, installed paths, etc.)
type LibrariesManager struct {
LibrariesDir []*LibrariesDir
Libraries map[string]libraries.List `json:"libraries"`
Index *librariesindex.Index
IndexFile *paths.Path
IndexFileSignature *paths.Path
DownloadsDir *paths.Path
}
// LibrariesDir is a directory containing libraries
type LibrariesDir struct {
Path *paths.Path
Location libraries.LibraryLocation
PlatformRelease *cores.PlatformRelease
IsSingleLibrary bool // true if Path points directly to a library instad of a dir of libraries
}
var tr = i18n.Tr
// Names returns an array with all the names of the installed libraries.
func (lm LibrariesManager) Names() []string {
res := make([]string, len(lm.Libraries))
i := 0
for n := range lm.Libraries {
res[i] = n
i++
}
slices.SortFunc(res, func(a, b string) int {
if strings.ToLower(a) < strings.ToLower(b) {
return -1
}
return 1
})
return res
}
// NewLibraryManager creates a new library manager
func NewLibraryManager(indexDir *paths.Path, downloadsDir *paths.Path) *LibrariesManager {
var indexFile, indexFileSignature *paths.Path
if indexDir != nil {
indexFile = indexDir.Join("library_index.json")
indexFileSignature = indexDir.Join("library_index.json.sig")
}
return &LibrariesManager{
Libraries: map[string]libraries.List{},
IndexFile: indexFile,
IndexFileSignature: indexFileSignature,
DownloadsDir: downloadsDir,
Index: librariesindex.EmptyIndex,
}
}
// LoadIndex reads a library_index.json from a file and returns
// the corresponding Index structure.
func (lm *LibrariesManager) LoadIndex() error {
logrus.WithField("index", lm.IndexFile).Info("Loading libraries index file")
index, err := librariesindex.LoadIndex(lm.IndexFile)
if err != nil {
lm.Index = librariesindex.EmptyIndex
return err
}
lm.Index = index
return nil
}
// AddLibrariesDir adds path to the list of directories
// to scan when searching for libraries. If a path is already
// in the list it is ignored.
func (lm *LibrariesManager) AddLibrariesDir(libDir *LibrariesDir) {
if libDir.Path == nil {
return
}
for _, dir := range lm.LibrariesDir {
if dir.Path.EquivalentTo(libDir.Path) {
return
}
}
logrus.WithField("dir", libDir.Path).
WithField("location", libDir.Location.String()).
WithField("isSingleLibrary", libDir.IsSingleLibrary).
Info("Adding libraries dir")
lm.LibrariesDir = append(lm.LibrariesDir, libDir)
}
// RescanLibraries reload all installed libraries in the system.
func (lm *LibrariesManager) RescanLibraries() []*status.Status {
lm.clearLibraries()
statuses := []*status.Status{}
for _, dir := range lm.LibrariesDir {
if errs := lm.loadLibrariesFromDir(dir); len(errs) > 0 {
statuses = append(statuses, errs...)
}
}
return statuses
}
func (lm *LibrariesManager) getLibrariesDir(installLocation libraries.LibraryLocation) (*paths.Path, error) {
for _, dir := range lm.LibrariesDir {
if dir.Location == installLocation {
return dir.Path, nil
}
}
switch installLocation {
case libraries.User:
return nil, errors.New(tr("user directory not set"))
case libraries.IDEBuiltIn:
return nil, errors.New(tr("built-in libraries directory not set"))
default:
return nil, fmt.Errorf("libraries directory not set: %s", installLocation.String())
}
}
// loadLibrariesFromDir loads all libraries in the given directory. Returns
// nil if the directory doesn't exists.
func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*status.Status {
statuses := []*status.Status{}
var libDirs paths.PathList
if librariesDir.IsSingleLibrary {
libDirs.Add(librariesDir.Path)
} else {
d, err := librariesDir.Path.ReadDir()
if os.IsNotExist(err) {
return statuses
}
if err != nil {
s := status.Newf(codes.FailedPrecondition, tr("reading dir %[1]s: %[2]s"), librariesDir.Path, err)
return append(statuses, s)
}
d.FilterDirs()
d.FilterOutHiddenFiles()
libDirs = d
}
for _, subDir := range libDirs {
library, err := libraries.Load(subDir, librariesDir.Location)
if err != nil {
s := status.Newf(codes.Internal, tr("loading library from %[1]s: %[2]s"), subDir, err)
statuses = append(statuses, s)
continue
}
library.ContainerPlatform = librariesDir.PlatformRelease
alternatives := lm.Libraries[library.Name]
alternatives.Add(library)
lm.Libraries[library.Name] = alternatives
}
return statuses
}
// FindByReference return the installed libraries matching the Reference
// name and version or, if the version is nil, the libraries installed
// in the installLocation.
func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference, installLocation libraries.LibraryLocation) libraries.List {
alternatives := lm.Libraries[libRef.Name]
if alternatives == nil {
return nil
}
return alternatives.FilterByVersionAndInstallLocation(libRef.Version, installLocation)
}
// FindAllInstalled returns all the installed libraries
func (lm *LibrariesManager) FindAllInstalled() libraries.List {
var res libraries.List
for _, libAlternatives := range lm.Libraries {
for _, libRelease := range libAlternatives {
if libRelease.InstallDir == nil {
continue
}
res.Add(libRelease)
}
}
return res
}
func (lm *LibrariesManager) clearLibraries() {
for k := range lm.Libraries {
delete(lm.Libraries, k)
}
}