@@ -8,42 +8,43 @@ import (
88 "encoding/json"
99 "os"
1010 "os/exec"
11- "strings"
1211
1312 "github.com/arduino/go-paths-helper"
1413 "github.com/sirupsen/logrus"
1514 "github.com/spf13/cobra"
1615)
1716
18- var (
19- fqbn string
20- compileOutput CompileOutput
21- )
17+ var fqbn string
2218
2319// compileOutput represents the json returned by the arduino-cli compile command
2420type CompileOutput struct {
25- CompilerOut string `json:"compiler_out"`
2621 CompilerErr string `json:"compiler_err"`
2722 BuilderResult * BuilderResult `json:"builder_result"`
2823 Success bool `json:"success"`
2924}
3025
3126type 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"`
3436}
3537
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"`
4142}
4243
4344// returnJson contains information regarding the core and libraries used during the compile process
4445type ReturnJson struct {
45- CoreInfo * Info `json:"coreInfo"`
46- LibsInfo []* Info `json:"libsInfo"`
46+ CoreInfo * BuildPlatform `json:"coreInfo"`
47+ LibsInfo []* UsedLibrary `json:"libsInfo"`
4748}
4849
4950// compileCmd represents the compile command
@@ -121,25 +122,14 @@ func compileSketch(cmd *cobra.Command, args []string) {
121122// the function extracts and returns the paths of the .o files
122123// (generated during the compile phase) and a ReturnJson object
123124func parseOutput (cmdOutToParse []byte ) ([]* paths.Path , * ReturnJson ) {
125+ var compileOutput CompileOutput
124126 err := json .Unmarshal (cmdOutToParse , & compileOutput )
125127 if err != nil {
126128 logrus .Fatal (err )
127129 } else if ! compileOutput .Success {
128130 logrus .Fatalf ("sketch compile was not successful: %s" , compileOutput .CompilerErr )
129131 }
130132
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-
143133 // this dir contains all the obj files we need (the sketch related ones and not the core or libs)
144134 sketchDir := paths .New (compileOutput .BuilderResult .BuildPath ).Join ("sketch" )
145135 sketchFilesPaths , err := sketchDir .ReadDir ()
@@ -156,40 +146,9 @@ func parseOutput(cmdOutToParse []byte) ([]*paths.Path, *ReturnJson) {
156146 }
157147
158148 returnJson := ReturnJson {
159- CoreInfo : getCoreInfo ( platformPath ) ,
149+ CoreInfo : compileOutput . BuilderResult . BuildPlatform ,
160150 LibsInfo : compileOutput .BuilderResult .UsedLibraries ,
161151 }
162152
163153 return returnObjectFilesList , & returnJson
164154}
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