@@ -7,6 +7,7 @@ package cmd
7
7
import (
8
8
"bytes"
9
9
"encoding/json"
10
+ "fmt"
10
11
"os"
11
12
"os/exec"
12
13
"strings"
@@ -328,7 +329,22 @@ func createLib(sketchName, buildMcu, fqbn string, returnJson *ResultJson, objFil
328
329
329
330
// let's create the files
330
331
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 ) {
332
348
// the library.properties contains the following:
333
349
libraryProperties := `name=` + sketchName + `
334
350
author=TODO
@@ -342,6 +358,15 @@ precompiled=true`
342
358
libraryPropertyPath := libDir .Join ("library.properties" )
343
359
createFile (libraryPropertyPath , libraryProperties )
344
360
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 ) {
345
370
// we calculate the #include part to append at the beginning of the header file here with all the libraries used by the original sketch
346
371
var librariesIncludes []string
347
372
for _ , lib := range returnJson .LibsInfo {
@@ -350,18 +375,19 @@ precompiled=true`
350
375
}
351
376
}
352
377
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
356
378
// the libsketch.h contains the following:
357
379
libsketchHeader := strings .Join (librariesIncludes , "\n " ) + `
358
380
void _setup();
359
381
void _loop();`
360
382
361
383
libsketchFilePath := srcDir .Parent ().Join ("lib" + sketchName + ".h" )
362
384
createFile (libsketchFilePath , libsketchHeader )
385
+ }
363
386
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 {
365
391
// This one will include the libsketch.h and basically is the replacement of main.cpp
366
392
// the sketch.ino contains the following:
367
393
sketchFile := `#include <` + "lib" + sketchName + `.h>
@@ -373,13 +399,20 @@ void loop() {
373
399
}`
374
400
sketchFilePath := sketchDir .Join (sketchName + ".ino" )
375
401
createFile (sketchFilePath , sketchFile )
402
+ return sketchFilePath
403
+ }
376
404
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 ) {
377
408
// generate the commands to run to successfully reproduce the build environment, they will be used as content for the README.md
378
409
var readmeContent []string
379
410
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 )
382
414
}
415
+ readmeContent = append (readmeContent , fmt .Sprintf ("`arduino-cli lib install %s`" , strings .Join (libs , " " )))
383
416
// make the paths relative, absolute paths are too long and are different on the user machine
384
417
sketchFileRelPath , _ := sketchFilePath .RelFrom (workingDir )
385
418
libRelDir , _ := libDir .RelFrom (workingDir )
@@ -397,8 +430,10 @@ The firmware contains additional code licensed with LGPL clause; in order to re-
397
430
398
431
readmeMdPath := rootDir .Join ("README.md" )
399
432
createFile (readmeMdPath , readmeMd )
433
+ }
400
434
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 ) {
402
437
// we exclude the main.cpp.o because we are going to link the archive libsketch.a against sketchName.ino
403
438
objFilePaths .FilterOutPrefix ("main.cpp" )
404
439
archivePath := srcDir .Join ("lib" + sketchName + ".a" )
@@ -413,6 +448,10 @@ The firmware contains additional code licensed with LGPL clause; in order to re-
413
448
} else {
414
449
logrus .Infof ("created %s" , archivePath .String ())
415
450
}
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 ) {
416
455
// save the result.json in the library extra dir
417
456
jsonFilePath := extraDir .Join ("result.json" )
418
457
if jsonContents , err := json .MarshalIndent (returnJson , "" , " " ); err != nil {
0 commit comments