Skip to content

Commit fa0b51c

Browse files
committed
remove horrible parsing of the the CompilerOut to get the platform
This is possible thanks to arduino/arduino-cli#1608 It's mandatory to use a version of the cli that has that change > 0.20.2
1 parent e21c444 commit fa0b51c

File tree

2 files changed

+21
-62
lines changed

2 files changed

+21
-62
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The json contains information regarding libraries and core to use in order to bu
55

66
## Requisites
77
In order to run this tool you have to install first the [arduino-cli](https://github.com/arduino/arduino-cli) and have `arduino-cli` binary in your path, otherwise `cslt-tool` won't work.
8+
Please use a version of the cli that has [this](https://github.com/arduino/arduino-cli/pull/1608) change, version > 0.20.2
89

910
## Build it
1011
In order to build it just use `go build`
@@ -15,7 +16,7 @@ In order to build it just use `go build`
1516
This is an example execution:
1617
``` bash
1718
$ ./cslt-tool compile -b arduino:samd:mkrwan1310 /home/umberto/getdeveui
18-
INFO[0001] arduino-cli version: 0.20.2
19+
INFO[0001] arduino-cli version: git-snapshot
1920
INFO[0001] running: arduino-cli compile -b arduino:samd:mkrwan1310 /home/umberto/getdeveui -v --format json
2021
INFO[0002] copied file to /home/umberto/Nextcloud/8tb/Lavoro/cslt-tool/build/getdeveui.ino.cpp.o
2122
INFO[0002] created new file in: /home/umberto/Nextcloud/8tb/Lavoro/cslt-tool/build/result.json
@@ -30,8 +31,7 @@ And the content of `build/result.json` is:
3031
```json
3132
{
3233
"coreInfo": {
33-
"packager": "arduino",
34-
"name": "samd",
34+
"id": "arduino:samd",
3535
"version": "1.8.12"
3636
},
3737
"libsInfo": [

cmd/compile.go

+18-59
Original file line numberDiff line numberDiff line change
@@ -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
2420
type 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

3126
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"`
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
4445
type 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
123124
func 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

Comments
 (0)