@@ -8,42 +8,43 @@ import (
8
8
"encoding/json"
9
9
"os"
10
10
"os/exec"
11
- "strings"
12
11
13
12
"github.com/arduino/go-paths-helper"
14
13
"github.com/sirupsen/logrus"
15
14
"github.com/spf13/cobra"
16
15
)
17
16
18
- var (
19
- fqbn string
20
- compileOutput CompileOutput
21
- )
17
+ var fqbn string
22
18
23
19
// compileOutput represents the json returned by the arduino-cli compile command
24
20
type CompileOutput struct {
25
- CompilerOut string `json:"compiler_out"`
26
21
CompilerErr string `json:"compiler_err"`
27
22
BuilderResult * BuilderResult `json:"builder_result"`
28
23
Success bool `json:"success"`
29
24
}
30
25
31
26
type BuilderResult struct {
32
- BuildPath string `json:"build_path"`
33
- UsedLibraries []* Info `json:"used_libraries"`
27
+ BuildPath string `json:"build_path"`
28
+ UsedLibraries []* UsedLibrary `json:"used_libraries"`
29
+ BuildPlatform * BuildPlatform `json:"build_platform"`
30
+ }
31
+
32
+ // UsedLibrary contains information regarding the library used during the compile process
33
+ type UsedLibrary struct {
34
+ Name string `json:"name"`
35
+ Version string `json:"version"`
34
36
}
35
37
36
- // Info contains information regarding the library or the core used during the compile process
37
- type Info struct {
38
- Packager string `json:"packager,omitempty"`
39
- Name string `json:"name"`
40
- Version string `json:"version"`
38
+ // BuildPlatform contains information regarding the platform used during the compile process
39
+ type BuildPlatform struct {
40
+ Id string `json:"id"`
41
+ Version string `json:"version"`
41
42
}
42
43
43
44
// returnJson contains information regarding the core and libraries used during the compile process
44
45
type ReturnJson struct {
45
- CoreInfo * Info `json:"coreInfo"`
46
- LibsInfo []* Info `json:"libsInfo"`
46
+ CoreInfo * BuildPlatform `json:"coreInfo"`
47
+ LibsInfo []* UsedLibrary `json:"libsInfo"`
47
48
}
48
49
49
50
// compileCmd represents the compile command
@@ -121,25 +122,14 @@ func compileSketch(cmd *cobra.Command, args []string) {
121
122
// the function extracts and returns the paths of the .o files
122
123
// (generated during the compile phase) and a ReturnJson object
123
124
func parseOutput (cmdOutToParse []byte ) ([]* paths.Path , * ReturnJson ) {
125
+ var compileOutput CompileOutput
124
126
err := json .Unmarshal (cmdOutToParse , & compileOutput )
125
127
if err != nil {
126
128
logrus .Fatal (err )
127
129
} else if ! compileOutput .Success {
128
130
logrus .Fatalf ("sketch compile was not successful: %s" , compileOutput .CompilerErr )
129
131
}
130
132
131
- compilerOutLines := strings .Split (compileOutput .CompilerOut , "\n " )
132
- var platformPath * paths.Path
133
- for _ , compilerOutLine := range compilerOutLines {
134
- logrus .Debug (compilerOutLine )
135
- if platformPath = parsePlatformLine (compilerOutLine ); platformPath != nil {
136
- break
137
- }
138
- }
139
- if platformPath == nil {
140
- logrus .Fatal ("cannot find platform used" )
141
- }
142
-
143
133
// this dir contains all the obj files we need (the sketch related ones and not the core or libs)
144
134
sketchDir := paths .New (compileOutput .BuilderResult .BuildPath ).Join ("sketch" )
145
135
sketchFilesPaths , err := sketchDir .ReadDir ()
@@ -156,40 +146,9 @@ func parseOutput(cmdOutToParse []byte) ([]*paths.Path, *ReturnJson) {
156
146
}
157
147
158
148
returnJson := ReturnJson {
159
- CoreInfo : getCoreInfo ( platformPath ) ,
149
+ CoreInfo : compileOutput . BuilderResult . BuildPlatform ,
160
150
LibsInfo : compileOutput .BuilderResult .UsedLibraries ,
161
151
}
162
152
163
153
return returnObjectFilesList , & returnJson
164
154
}
165
-
166
- // getCoreInfo takes the path of the platform used and
167
- // returns an Info object
168
- func getCoreInfo (platformPath * paths.Path ) * Info {
169
- if ! platformPath .Exist () {
170
- logrus .Fatalf ("the path of the core does not exists: %s" , platformPath )
171
- }
172
- corePathParents := platformPath .Parents ()
173
- coreInfo := & Info {
174
- Packager : corePathParents [3 ].Base (),
175
- Name : corePathParents [1 ].Base (),
176
- Version : corePathParents [0 ].Base (),
177
- }
178
- return coreInfo
179
- }
180
-
181
- // parsePlatformLine takes compilerOutLine as input and tries to extract the path of the platform used
182
- // compilerOutLine should be something like:
183
- // - Using core 'arduino' from platform in folder: C:\\Users\\Umberto Baldi\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.5\n
184
- // - Using core 'arduino' from platform in folder: /home/umberto/.arduino15/packages/arduino/hardware/avr/1.8.4
185
-
186
- func parsePlatformLine (compilerOutLine string ) * paths.Path {
187
- if matched := strings .Contains (compilerOutLine , "Using core" ); matched {
188
- // we use this approach to avoid path with spaces problem
189
- if startIndex := strings .Index (compilerOutLine , "from platform in folder: " ); startIndex != - 1 {
190
- startIndex = startIndex + len ("from platform in folder: " )
191
- return paths .New (compilerOutLine [startIndex :])
192
- }
193
- }
194
- return nil
195
- }
0 commit comments