@@ -86,10 +86,37 @@ func compileSketch(cmd *cobra.Command, args []string) {
86
86
if err != nil {
87
87
logrus .Fatal (err )
88
88
}
89
- parseOutput (cmdOutput ) // TODO save the return
89
+ objFilePath , returnJson := parseOutput (cmdOutput )
90
+
91
+ workingDir , err := paths .Getwd ()
92
+ if err != nil {
93
+ logrus .Fatal (err )
94
+ }
95
+ buildDir := workingDir .Join ("build" )
96
+ if ! buildDir .Exist () {
97
+ if err = buildDir .Mkdir (); err != nil {
98
+ logrus .Fatal (err )
99
+ }
100
+ }
101
+
102
+ // Copy the object files from the `<tempdir>/arduino-sketch_stuff/sketch` folder
103
+ destObjFilePath := buildDir .Join (objFilePath .Base ())
104
+ if err = objFilePath .CopyTo (destObjFilePath ); err != nil {
105
+ logrus .Errorf ("error copying object file: %s" , err )
106
+ } else {
107
+ logrus .Infof ("copied file to %s" , destObjFilePath )
108
+ }
109
+
110
+ // save the result.json in the build dir
111
+ jsonFilePath := buildDir .Join ("result.json" )
112
+ if jsonContents , err := json .MarshalIndent (returnJson , "" , " " ); err != nil {
113
+ logrus .Errorf ("error serializing json: %s" , err )
114
+ } else if err := jsonFilePath .WriteFile (jsonContents ); err != nil {
115
+ logrus .Errorf ("error writing result.json: %s" , err )
116
+ } else {
117
+ logrus .Infof ("created new file in: %s" , jsonFilePath )
118
+ }
90
119
91
- // TODO:
92
- // copy sketch.ino.o from tmp/... in current dir or in --output dir
93
120
}
94
121
95
122
// parseOutput function takes cmdOutToParse as argument,
@@ -106,55 +133,60 @@ func parseOutput(cmdOutToParse []byte) (*paths.Path, *ReturnJson) {
106
133
107
134
compilerOutLines := strings .Split (compileOutput .CompilerOut , "\n " )
108
135
var coreLine string
109
- var objFilePath string
136
+ var objFile string
110
137
for _ , compilerOutLine := range compilerOutLines {
111
- logrus .Info (compilerOutLine )
138
+ logrus .Debug (compilerOutLine )
112
139
if matched := strings .Contains (compilerOutLine , "Using core" ); matched {
113
140
coreLine = compilerOutLine
114
141
}
115
- if objFilePath = ParseObjFilePath (compilerOutLine ); objFilePath != "" {
142
+ if objFile = ParseObjFilePath (compilerOutLine ); objFile != "" {
116
143
break // we should already have coreLine
117
144
}
118
145
119
146
}
120
147
if coreLine == "" {
121
148
logrus .Fatal ("cannot find core used" )
122
149
}
123
- if objFilePath == "" {
150
+ if objFile == "" {
124
151
logrus .Fatal ("cannot find sketch object file" )
125
152
}
126
153
154
+ objFilePath := paths .New (objFile )
155
+ if objFilePath == nil {
156
+ logrus .Fatal ("path cannot be created" )
157
+ }
158
+
127
159
returnJson := ReturnJson {
128
160
CoreInfo : parseCoreLine (coreLine ),
129
161
LibsInfo : compileOutput .BuilderResult .UsedLibraries ,
130
162
}
131
163
132
- // TODO missing calculation of <sketch>.ino.o file
133
164
// TODO there could be multiple `.o` files, see zube comment. The correct approach is to go in `<tempdir>/arduino-sketch_stuff/sketch` and copy all the `.o` files
134
165
// TODO add also the packager -> maybe ParseReference could be used from the cli
135
166
// TODO core could be calculated from fqbn
136
167
137
- return nil , & returnJson //TODO remove
168
+ return objFilePath , & returnJson
138
169
}
139
170
140
171
// ParseObjFilePath is a function that takes a line from the compiler output
141
172
// it returns a string with the path of the object file generated during the compile process
142
173
//(recognizing it by .ino.cpp.o extension) if the extension is found in the string, "" otherwise
143
174
func ParseObjFilePath (compilerOutLine string ) (objFilePath string ) {
144
175
var endChar string
145
- if index := strings .Index (compilerOutLine , ".ino.cpp.o" ); index != - 1 {
146
- sketchEndIndex := index + len (".ino.cpp.o" )
147
- if sketchEndIndex >= len (compilerOutLine ) { // this means the path terminates with the `o` and thus the end character is space
176
+ extension := ".ino.cpp.o"
177
+ if extensionIndex := strings .Index (compilerOutLine , extension ); extensionIndex != - 1 {
178
+ objFileEndIndex := extensionIndex + len (extension )
179
+ if objFileEndIndex >= len (compilerOutLine ) { // this means the path terminates with the `o` and thus the end character is space
148
180
endChar = " " // we set it this way to avoid index out of bound
149
181
} else {
150
- endChar = string (compilerOutLine [sketchEndIndex ])
182
+ endChar = string (compilerOutLine [objFileEndIndex ])
151
183
}
152
- sketchStartIndex := strings .LastIndex (compilerOutLine [:sketchEndIndex ], endChar )
153
- // we continue to search if the endChar is preceded by backslash (this means the endChar it's escaped)
154
- for string (compilerOutLine [sketchStartIndex - 1 ]) == `\` {
155
- sketchStartIndex = strings .LastIndex (compilerOutLine [:sketchStartIndex ], endChar )
184
+ objFileStartIndex := strings .LastIndex (compilerOutLine [:objFileEndIndex ], endChar )
185
+ // we continue to search if the endChar is preceded by backslash (this means the endChar it's escaped and thus is not an endChar :D )
186
+ for string (compilerOutLine [objFileStartIndex - 1 ]) == `\` {
187
+ objFileStartIndex = strings .LastIndex (compilerOutLine [:objFileStartIndex ], endChar )
156
188
}
157
- objFilePath = compilerOutLine [sketchStartIndex + 1 : sketchEndIndex ]
189
+ objFilePath = compilerOutLine [objFileStartIndex + 1 : objFileEndIndex ]
158
190
return objFilePath
159
191
} else {
160
192
return ""
0 commit comments