@@ -40,6 +40,7 @@ import (
40
40
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
41
41
paths "github.com/arduino/go-paths-helper"
42
42
"github.com/sirupsen/logrus"
43
+ "github.com/spf13/viper"
43
44
"go.bug.st/downloader/v2"
44
45
"google.golang.org/grpc/codes"
45
46
"google.golang.org/grpc/status"
@@ -56,13 +57,21 @@ var instancesCount int32 = 1
56
57
// instantiate as many as needed by providing a different configuration
57
58
// for each one.
58
59
type CoreInstance struct {
60
+ id int32
59
61
packageManager * packagemanager.PackageManager
60
62
librariesManager * librariesmanager.LibrariesManager
63
+ Settings * viper.Viper
61
64
}
62
65
63
- // InstanceContainer FIXMEDOC
64
- type InstanceContainer interface {
65
- GetInstance () * rpc.Instance
66
+ // ToRPC returns a pointer to an rpc.Instance with
67
+ // the same ID as the calling CoreInstance
68
+ func (c * CoreInstance ) ToRPC () * rpc.Instance {
69
+ return & rpc.Instance {Id : c .id }
70
+ }
71
+
72
+ // ID returns the id of this CoreInstance
73
+ func (c * CoreInstance ) ID () int32 {
74
+ return c .id
66
75
}
67
76
68
77
// GetInstance returns a CoreInstance for the given ID, or nil if ID
@@ -107,10 +116,16 @@ func (instance *CoreInstance) installToolIfMissing(tool *cores.ToolRelease, down
107
116
108
117
// Create a new CoreInstance ready to be initialized, supporting directories are also created.
109
118
func Create (req * rpc.CreateRequest ) (* rpc.CreateResponse , error ) {
110
- instance := & CoreInstance {}
119
+ if req .ConfigFile == "" {
120
+ return nil , & arduino.MissingConfigFileError {}
121
+ }
122
+
123
+ instance := & CoreInstance {
124
+ Settings : configuration .Init (req .ConfigFile ),
125
+ }
111
126
112
127
// Setup downloads directory
113
- downloadsDir := paths .New (configuration .Settings .GetString ("directories.Downloads" ))
128
+ downloadsDir := paths .New (instance .Settings .GetString ("directories.Downloads" ))
114
129
if downloadsDir .NotExist () {
115
130
err := downloadsDir .MkdirAll ()
116
131
if err != nil {
@@ -119,8 +134,8 @@ func Create(req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
119
134
}
120
135
121
136
// Setup data directory
122
- dataDir := paths .New (configuration .Settings .GetString ("directories.Data" ))
123
- packagesDir := configuration .PackagesDir (configuration .Settings )
137
+ dataDir := paths .New (instance .Settings .GetString ("directories.Data" ))
138
+ packagesDir := configuration .PackagesDir (instance .Settings )
124
139
if packagesDir .NotExist () {
125
140
err := packagesDir .MkdirAll ()
126
141
if err != nil {
@@ -131,7 +146,7 @@ func Create(req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
131
146
// Create package manager
132
147
instance .packageManager = packagemanager .NewPackageManager (
133
148
dataDir ,
134
- configuration .PackagesDir (configuration .Settings ),
149
+ configuration .PackagesDir (instance .Settings ),
135
150
downloadsDir ,
136
151
dataDir .Join ("tmp" ),
137
152
)
@@ -143,18 +158,19 @@ func Create(req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
143
158
)
144
159
145
160
// Add directories of libraries bundled with IDE
146
- if bundledLibsDir := configuration .IDEBundledLibrariesDir (configuration .Settings ); bundledLibsDir != nil {
161
+ if bundledLibsDir := configuration .IDEBundledLibrariesDir (instance .Settings ); bundledLibsDir != nil {
147
162
instance .librariesManager .AddLibrariesDir (bundledLibsDir , libraries .IDEBuiltIn )
148
163
}
149
164
150
165
// Add libraries directory from config file
151
166
instance .librariesManager .AddLibrariesDir (
152
- configuration .LibrariesDir (configuration .Settings ),
167
+ configuration .LibrariesDir (instance .Settings ),
153
168
libraries .User ,
154
169
)
155
170
156
171
// Save instance
157
172
instanceID := instancesCount
173
+ instance .id = instanceID
158
174
instances [instanceID ] = instance
159
175
instancesCount ++
160
176
@@ -186,7 +202,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
186
202
187
203
// Load Platforms
188
204
urls := []string {globals .DefaultIndexURL }
189
- urls = append (urls , configuration .Settings .GetStringSlice ("board_manager.additional_urls" )... )
205
+ urls = append (urls , instance .Settings .GetStringSlice ("board_manager.additional_urls" )... )
190
206
for _ , u := range urls {
191
207
URL , err := utils .URLParse (u )
192
208
if err != nil {
@@ -227,7 +243,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
227
243
// We load hardware before verifying builtin tools are installed
228
244
// otherwise we wouldn't find them and reinstall them each time
229
245
// and they would never get reloaded.
230
- for _ , err := range instance .packageManager .LoadHardware () {
246
+ hardwareDirs := configuration .HardwareDirectories (instance .Settings )
247
+ bundleToolsDirs := configuration .BundleToolsDirectories (instance .Settings )
248
+ for _ , err := range instance .packageManager .LoadHardware (hardwareDirs , bundleToolsDirs ) {
231
249
responseCallback (& rpc.InitResponse {
232
250
Message : & rpc.InitResponse_Error {
233
251
Error : err .Proto (),
@@ -290,7 +308,9 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
290
308
if toolsHaveBeenInstalled {
291
309
// We installed at least one new tool after loading hardware
292
310
// so we must reload again otherwise we would never found them.
293
- for _ , err := range instance .packageManager .LoadHardware () {
311
+ hardwareDirs := configuration .HardwareDirectories (instance .Settings )
312
+ bundleToolsDirs := configuration .BundleToolsDirectories (instance .Settings )
313
+ for _ , err := range instance .packageManager .LoadHardware (hardwareDirs , bundleToolsDirs ) {
294
314
responseCallback (& rpc.InitResponse {
295
315
Message : & rpc.InitResponse_Error {
296
316
Error : err .Proto (),
@@ -337,7 +357,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
337
357
// Refreshes the locale used, this will change the
338
358
// language of the CLI if the locale is different
339
359
// after started.
340
- i18n .Init (configuration .Settings .GetString ("locale" ))
360
+ i18n .Init (instance .Settings .GetString ("locale" ))
341
361
342
362
return nil
343
363
}
@@ -356,7 +376,8 @@ func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse
356
376
// UpdateLibrariesIndex updates the library_index.json
357
377
func UpdateLibrariesIndex (ctx context.Context , req * rpc.UpdateLibrariesIndexRequest , downloadCB func (* rpc.DownloadProgress )) error {
358
378
logrus .Info ("Updating libraries index" )
359
- lm := GetLibraryManager (req .GetInstance ().GetId ())
379
+ instanceID := req .GetInstance ().GetId ()
380
+ lm := GetLibraryManager (instanceID )
360
381
if lm == nil {
361
382
return & arduino.InvalidInstanceError {}
362
383
}
@@ -424,16 +445,16 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
424
445
425
446
// UpdateIndex FIXMEDOC
426
447
func UpdateIndex (ctx context.Context , req * rpc.UpdateIndexRequest , downloadCB DownloadProgressCB ) (* rpc.UpdateIndexResponse , error ) {
427
- id := req .GetInstance ().GetId ()
428
- _ , ok := instances [id ]
448
+ instanceID := req .GetInstance ().GetId ()
449
+ instance , ok := instances [instanceID ]
429
450
if ! ok {
430
451
return nil , & arduino.InvalidInstanceError {}
431
452
}
432
453
433
- indexpath := paths .New (configuration .Settings .GetString ("directories.Data" ))
454
+ indexpath := paths .New (instance .Settings .GetString ("directories.Data" ))
434
455
435
456
urls := []string {globals .DefaultIndexURL }
436
- urls = append (urls , configuration .Settings .GetStringSlice ("board_manager.additional_urls" )... )
457
+ urls = append (urls , instance .Settings .GetStringSlice ("board_manager.additional_urls" )... )
437
458
for _ , u := range urls {
438
459
logrus .Info ("URL: " , u )
439
460
URL , err := utils .URLParse (u )
0 commit comments