Skip to content

Commit 304c7c4

Browse files
committed
fix path with spaces not being recognized as a valid platform
isolate `parsePlatformLine` function
1 parent a1bb303 commit 304c7c4

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

cmd/compile.go

+28-16
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,15 @@ func parseOutput(cmdOutToParse []byte) ([]*paths.Path, *ReturnJson) {
129129
}
130130

131131
compilerOutLines := strings.Split(compileOutput.CompilerOut, "\n")
132-
var coreLine string
132+
var platformPath *paths.Path
133133
for _, compilerOutLine := range compilerOutLines {
134134
logrus.Debug(compilerOutLine)
135-
if matched := strings.Contains(compilerOutLine, "Using core"); matched {
136-
coreLine = compilerOutLine
137-
break // we should already have coreLine
135+
if platformPath = parsePlatformLine(compilerOutLine); platformPath != nil {
136+
break
138137
}
139138
}
140-
if coreLine == "" {
141-
logrus.Fatal("cannot find core used")
139+
if platformPath == nil {
140+
logrus.Fatal("cannot find platform used")
142141
}
143142

144143
// this dir contains all the obj files we need (the sketch related ones and not the core or libs)
@@ -157,27 +156,40 @@ func parseOutput(cmdOutToParse []byte) ([]*paths.Path, *ReturnJson) {
157156
}
158157

159158
returnJson := ReturnJson{
160-
CoreInfo: parseCoreLine(coreLine),
159+
CoreInfo: getCoreInfo(platformPath),
161160
LibsInfo: compileOutput.BuilderResult.UsedLibraries,
162161
}
163162

164163
return returnObjectFilesList, &returnJson
165164
}
166165

167-
// parseCoreLine takes the line containing info regarding the core and
168-
// returns a Info object
169-
func parseCoreLine(coreLine string) *Info {
170-
words := strings.Split(coreLine, " ")
171-
strCorePath := words[len(words)-1] // last string has the path of the core
172-
corePath := paths.New(strCorePath)
173-
if !corePath.Exist() {
174-
logrus.Fatalf("the path of the core does not exists: %s", corePath)
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)
175171
}
176-
corePathParents := corePath.Parents()
172+
corePathParents := platformPath.Parents()
177173
coreInfo := &Info{
178174
Packager: corePathParents[3].Base(),
179175
Name: corePathParents[1].Base(),
180176
Version: corePathParents[0].Base(),
181177
}
182178
return coreInfo
183179
}
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)