Skip to content

Commit cad55db

Browse files
committed
apply suggestions from code review and move file creation in functions for better readability
1 parent 3b232ad commit cad55db

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ The firmware contains additional code licensed with LGPL clause; in order to re-
6262

6363
## Install core and libraries
6464
`arduino-cli core install arduino:samd@1.8.12`
65-
`arduino-cli lib install WiFiNINA@1.8.13`
66-
`arduino-cli lib install SPI@1.0`
65+
`arduino-cli lib install WiFiNINA@1.8.13 SPI@1.0`
6766

6867
## Compile
6968
`arduino-cli compile -b arduino:samd:mkrwifi1010 sketch-dist/sketch/sketch.ino --library sketch-dist/libsketch`

cmd/compile.go

+47-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package cmd
77
import (
88
"bytes"
99
"encoding/json"
10+
"fmt"
1011
"os"
1112
"os/exec"
1213
"strings"
@@ -328,7 +329,22 @@ func createLib(sketchName, buildMcu, fqbn string, returnJson *ResultJson, objFil
328329

329330
// let's create the files
330331

331-
// create a library.properties file in the root dir of the lib
332+
createLibraryPropertiesFile(sketchName, libDir)
333+
334+
createLibSketchHeaderFile(sketchName, srcDir, returnJson)
335+
336+
sketchFilePath := createSketchFile(sketchName, sketchDir)
337+
338+
createReadmeMdFile(sketchFilePath, libDir, workingDir, rootDir, returnJson)
339+
340+
createArchiveFile(sketchName, objFilePaths, srcDir)
341+
342+
createResultJsonFile(extraDir, returnJson)
343+
}
344+
345+
// createLibraryPropertiesFile as the name suggests will create a library.properties file in the libDir,
346+
// the sketchName is required in order to correctly set the name of the "library"
347+
func createLibraryPropertiesFile(sketchName string, libDir *paths.Path) {
332348
// the library.properties contains the following:
333349
libraryProperties := `name=` + sketchName + `
334350
author=TODO
@@ -342,6 +358,15 @@ precompiled=true`
342358
libraryPropertyPath := libDir.Join("library.properties")
343359
createFile(libraryPropertyPath, libraryProperties)
344360

361+
}
362+
363+
// createLibSketchHeaderFile as the name suggests will create the libsketch header file,
364+
// the file will be created in the srcDir
365+
// This file has predeclarations of _setup() and _loop() functions declared originally in the main.cpp file (which is not included in the .a archive),
366+
// It is the counterpart of libsketch.a
367+
// we pass resultJson because from there we can extract infos regarding used libs
368+
// sketchName is used to name the file
369+
func createLibSketchHeaderFile(sketchName string, srcDir *paths.Path, returnJson *ResultJson) {
345370
// we calculate the #include part to append at the beginning of the header file here with all the libraries used by the original sketch
346371
var librariesIncludes []string
347372
for _, lib := range returnJson.LibsInfo {
@@ -350,18 +375,19 @@ precompiled=true`
350375
}
351376
}
352377

353-
// create the header file in the src/ dir
354-
// This file has predeclarations of _setup() and _loop() functions declared originally in the main.cpp file (which is not included in the .a archive),
355-
// It is the counterpart of libsketch.a
356378
// the libsketch.h contains the following:
357379
libsketchHeader := strings.Join(librariesIncludes, "\n") + `
358380
void _setup();
359381
void _loop();`
360382

361383
libsketchFilePath := srcDir.Parent().Join("lib" + sketchName + ".h")
362384
createFile(libsketchFilePath, libsketchHeader)
385+
}
363386

364-
// create the sketch file in the sketch-dist dir
387+
// createSketchFile as the name suggest will create the sketch which will be the entrypoint of the compilation with the arduino-cli
388+
// the sketch file will be created in the sketchDir
389+
// the sketchName argument is used to correclty include the right .h file
390+
func createSketchFile(sketchName string, sketchDir *paths.Path) *paths.Path {
365391
// This one will include the libsketch.h and basically is the replacement of main.cpp
366392
// the sketch.ino contains the following:
367393
sketchFile := `#include <` + "lib" + sketchName + `.h>
@@ -373,13 +399,20 @@ void loop() {
373399
}`
374400
sketchFilePath := sketchDir.Join(sketchName + ".ino")
375401
createFile(sketchFilePath, sketchFile)
402+
return sketchFilePath
403+
}
376404

405+
// createReadmeMdFile is a helper function that is reposnible for the generation of the README.md file containing informations on how to reproduce the build environment
406+
// it takes the resultJson and some paths.Paths as input to do the required calculations.. The name of the arguments should be sufficient to understand
407+
func createReadmeMdFile(sketchFilePath, libDir, workingDir, rootDir *paths.Path, returnJson *ResultJson) {
377408
// generate the commands to run to successfully reproduce the build environment, they will be used as content for the README.md
378409
var readmeContent []string
379410
readmeContent = append(readmeContent, "`arduino-cli core install "+returnJson.CoreInfo.Id+"@"+returnJson.CoreInfo.Version+"`")
380-
for _, readmeLib := range returnJson.LibsInfo {
381-
readmeContent = append(readmeContent, "`arduino-cli lib install "+readmeLib.Name+"@"+readmeLib.Version+"`")
411+
libs := []string{}
412+
for _, l := range returnJson.LibsInfo {
413+
libs = append(libs, l.Name+"@"+l.Version)
382414
}
415+
readmeContent = append(readmeContent, fmt.Sprintf("`arduino-cli lib install %s`", strings.Join(libs, " ")))
383416
// make the paths relative, absolute paths are too long and are different on the user machine
384417
sketchFileRelPath, _ := sketchFilePath.RelFrom(workingDir)
385418
libRelDir, _ := libDir.RelFrom(workingDir)
@@ -397,8 +430,10 @@ The firmware contains additional code licensed with LGPL clause; in order to re-
397430

398431
readmeMdPath := rootDir.Join("README.md")
399432
createFile(readmeMdPath, readmeMd)
433+
}
400434

401-
// run gcc-ar to create an archive containing all the object files except the main.cpp.o (we don't need it because we have created a substitute of it before ⬆️)
435+
// createArchiveFile function will run `gcc-ar` to create an archive containing all the object files except the main.cpp.o (we don't need it because we have created a substitute of it before: sketchfile.ino)
436+
func createArchiveFile(sketchName string, objFilePaths *paths.PathList, srcDir *paths.Path) {
402437
// we exclude the main.cpp.o because we are going to link the archive libsketch.a against sketchName.ino
403438
objFilePaths.FilterOutPrefix("main.cpp")
404439
archivePath := srcDir.Join("lib" + sketchName + ".a")
@@ -413,6 +448,10 @@ The firmware contains additional code licensed with LGPL clause; in order to re-
413448
} else {
414449
logrus.Infof("created %s", archivePath.String())
415450
}
451+
}
452+
453+
// createResultJsonFile as the name suggests will generate the result.json file and save it in extraDir
454+
func createResultJsonFile(extraDir *paths.Path, returnJson *ResultJson) {
416455
// save the result.json in the library extra dir
417456
jsonFilePath := extraDir.Join("result.json")
418457
if jsonContents, err := json.MarshalIndent(returnJson, "", " "); err != nil {

0 commit comments

Comments
 (0)