18
18
package cli
19
19
20
20
import (
21
+ "context"
21
22
"encoding/json"
22
23
"fmt"
23
24
"os"
24
25
"path/filepath"
25
26
26
- paths "github.com/arduino/go-paths-helper "
27
+ "github.com/arduino/arduino-cli/commands "
27
28
28
- "github.com/arduino/arduino-cli/arduino/libraries "
29
+ "github.com/arduino/arduino-cli/arduino/cores/packagemanager "
29
30
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
30
31
"github.com/arduino/arduino-cli/arduino/sketches"
31
- "github.com/arduino/arduino-cli/configs"
32
-
33
- "github.com/arduino/arduino-cli/arduino/cores/packagemanager"
34
32
"github.com/arduino/arduino-cli/common/formatter"
33
+ "github.com/arduino/arduino-cli/configs"
34
+ "github.com/arduino/arduino-cli/rpc"
35
+ paths "github.com/arduino/go-paths-helper"
35
36
"github.com/sirupsen/logrus"
36
37
)
37
38
@@ -63,20 +64,20 @@ var GlobalFlags struct {
63
64
OutputJSON bool // true output in JSON, false output as Text
64
65
}
65
66
66
- // OutputJSON outputs the JSON encoding of v if the JSON output format has been
67
- // selected by the user and returns true . Otherwise no output is produced and the
68
- // function returns false .
69
- func OutputJSON (v interface {}) bool {
67
+ // OutputJSONOrElse outputs the JSON encoding of v if the JSON output format has been
68
+ // selected by the user and returns false . Otherwise no output is produced and the
69
+ // function returns true .
70
+ func OutputJSONOrElse (v interface {}) bool {
70
71
if ! GlobalFlags .OutputJSON {
71
- return false
72
+ return true
72
73
}
73
74
d , err := json .MarshalIndent (v , "" , " " )
74
75
if err != nil {
75
76
formatter .PrintError (err , "Error during JSON encoding of the output" )
76
77
os .Exit (ErrGeneric )
77
78
}
78
79
fmt .Print (string (d ))
79
- return true
80
+ return false
80
81
}
81
82
82
83
// AppName is the command line name of the Arduino CLI executable
@@ -95,78 +96,60 @@ func InitPackageManagerWithoutBundles() *packagemanager.PackageManager {
95
96
return InitPackageManager ()
96
97
}
97
98
98
- // InitPackageManager initializes the PackageManager
99
- // TODO: for the daemon mode, this might be called at startup, but for now only commands needing the PM will call it
100
- func InitPackageManager () * packagemanager.PackageManager {
101
- logrus .Info ("Initializing package manager" )
102
-
103
- pm := packagemanager .NewPackageManager (
104
- Config .IndexesDir (),
105
- Config .PackagesDir (),
106
- Config .DownloadsDir (),
107
- Config .DataDir .Join ("tmp" ))
108
-
99
+ func packageManagerInitReq () * rpc.InitReq {
100
+ urls := []string {}
109
101
for _ , URL := range Config .BoardManagerAdditionalUrls {
110
- if err := pm .LoadPackageIndex (URL ); err != nil {
111
- formatter .PrintError (err , "Failed to load " + URL .String ()+ " package index.\n " +
112
- "Try updating all indexes with `" + AppName + " core update-index`." )
113
- os .Exit (ErrCoreConfig )
114
- }
102
+ urls = append (urls , URL .String ())
115
103
}
116
104
117
- if err := pm .LoadHardware (Config ); err != nil {
118
- formatter .PrintError (err , "Error loading hardware packages." )
119
- os .Exit (ErrCoreConfig )
105
+ req := & rpc.InitReq {
106
+ Configuration : & rpc.Configuration {
107
+ DataDir : Config .DataDir .String (),
108
+ DownloadsDir : Config .DownloadsDir ().String (),
109
+ BoardManagerAdditionalUrls : urls ,
110
+ },
120
111
}
121
-
122
- return pm
112
+ return req
123
113
}
124
114
125
- // InitLibraryManager initializes the LibraryManager using the underlying packagemanager
126
- func InitLibraryManager (cfg * configs.Configuration , pm * packagemanager.PackageManager ) * librariesmanager.LibrariesManager {
127
- logrus .Info ("Starting libraries manager" )
128
- lm := librariesmanager .NewLibraryManager (
129
- cfg .IndexesDir (),
130
- Config .DownloadsDir ())
131
-
132
- // Add IDE builtin libraries dir
133
- if bundledLibsDir := Config .IDEBundledLibrariesDir (); bundledLibsDir != nil {
134
- lm .AddLibrariesDir (bundledLibsDir , libraries .IDEBuiltIn )
115
+ // CreateInstance creates and return an instance of the Arduino Core engine
116
+ func CreateInstance () * rpc.Instance {
117
+ logrus .Info ("Initializing package manager" )
118
+ resp , err := commands .Init (context .Background (), packageManagerInitReq ())
119
+ if err != nil {
120
+ formatter .PrintError (err , "Error initializing package manager" )
121
+ os .Exit (rpc .ErrGeneric )
135
122
}
123
+ return resp .GetInstance ()
124
+ }
136
125
137
- // Add sketchbook libraries dir
138
- lm .AddLibrariesDir (Config .LibrariesDir (), libraries .Sketchbook )
139
-
140
- // Add libraries dirs from installed platforms
141
- if pm != nil {
142
- for _ , targetPackage := range pm .GetPackages ().Packages {
143
- for _ , platform := range targetPackage .Platforms {
144
- if platformRelease := pm .GetInstalledPlatformRelease (platform ); platformRelease != nil {
145
- lm .AddPlatformReleaseLibrariesDir (platformRelease , libraries .PlatformBuiltIn )
146
- }
147
- }
148
- }
126
+ // InitPackageManager initializes the PackageManager
127
+ // TODO: for the daemon mode, this might be called at startup, but for now only commands needing the PM will call it
128
+ func InitPackageManager () * packagemanager.PackageManager {
129
+ logrus .Info ("Initializing package manager" )
130
+ resp , err := commands .Init (context .Background (), packageManagerInitReq ())
131
+ if err != nil {
132
+ formatter .PrintError (err , "Error initializing package manager" )
133
+ os .Exit (rpc .ErrGeneric )
149
134
}
135
+ return commands .GetPackageManager (resp )
136
+ }
150
137
151
- // Auto-update index if needed
152
- if err := lm .LoadIndex (); err != nil {
153
- logrus .WithError (err ).Warn ("Error during libraries index loading, trying to auto-update index" )
154
- UpdateLibrariesIndex (lm )
155
- }
156
- if err := lm .LoadIndex (); err != nil {
157
- logrus .WithError (err ).Error ("Error during libraries index loading" )
158
- formatter .PrintError (err , "Error loading libraries index" )
159
- os .Exit (ErrGeneric )
138
+ // InitLibraryManager initializes the LibraryManager. If pm is nil, the library manager will not handle core-libraries.
139
+ // TODO: for the daemon mode, this might be called at startup, but for now only commands needing the PM will call it
140
+ func InitLibraryManager (cfg * configs.Configuration , pm * packagemanager.PackageManager ) * librariesmanager.LibrariesManager {
141
+ req := packageManagerInitReq ()
142
+ if pm == nil {
143
+ req .LibraryManagerOnly = true
160
144
}
161
145
162
- // Scan for libraries
163
- if err := lm . RescanLibraries (); err != nil {
164
- logrus . WithError ( err ). Error ( "Error during libraries rescan" )
165
- formatter .PrintError (err , "Error during libraries rescan " )
166
- os .Exit (ErrGeneric )
146
+ logrus . Info ( "Initializing library manager" )
147
+ resp , err := commands . Init ( context . Background (), req )
148
+ if err != nil {
149
+ formatter .PrintError (err , "Error initializing library manager " )
150
+ os .Exit (rpc . ErrGeneric )
167
151
}
168
-
169
- return lm
152
+ return commands .GetLibraryManager (resp )
170
153
}
171
154
172
155
// UpdateLibrariesIndex updates the library_index.json
0 commit comments