@@ -14,23 +14,29 @@ import (
1414)
1515
1616func generateCpp (inoCode []byte , sourcePath , fqbn string ) (cppPath string , cppCode []byte , err error ) {
17- rawTempDir , err := ioutil .TempDir ("" , "ino2cpp-" )
17+ // The CLI expects the `theSketchName.ino` file to be in `some/path/theSketchName` folder.
18+ // Expected folder structure: `/path/to/temp/ino2cpp-${random}/theSketchName/theSketchName.ino`.
19+ rawRootTempDir , err := ioutil .TempDir ("" , "ino2cpp-" )
1820 if err != nil {
1921 err = errors .Wrap (err , "Error while creating temporary directory." )
2022 return
2123 }
22- tempDir , err := filepath .EvalSymlinks (rawTempDir )
24+ rootTempDir , err := filepath .EvalSymlinks (rawRootTempDir )
2325 if err != nil {
2426 err = errors .Wrap (err , "Error while resolving symbolic links of temporary directory." )
2527 return
2628 }
2729
28- // Write source file to temp dir
29- name := filepath .Base (sourcePath )
30- if ! strings .HasSuffix (name , ".ino" ) {
31- name += ".ino"
30+ sketchName := filepath .Base (sourcePath )
31+ if strings .HasSuffix (sketchName , ".ino" ) {
32+ sketchName = sketchName [:len (sketchName )- len (".ino" )]
3233 }
33- inoPath := filepath .Join (tempDir , name )
34+ sketchTempPath := filepath .Join (rootTempDir , sketchName )
35+ createDirIfNotExist (sketchTempPath )
36+
37+ // Write source file to temp dir
38+ sketchFileName := sketchName + ".ino"
39+ inoPath := filepath .Join (sketchTempPath , sketchFileName )
3440 err = ioutil .WriteFile (inoPath , inoCode , 0600 )
3541 if err != nil {
3642 err = errors .Wrap (err , "Error while writing source file to temporary directory." )
@@ -41,14 +47,14 @@ func generateCpp(inoCode []byte, sourcePath, fqbn string) (cppPath string, cppCo
4147 }
4248
4349 // Copy all header files to temp dir
44- err = copyHeaderFiles (filepath .Dir (sourcePath ), tempDir )
50+ err = copyHeaderFiles (filepath .Dir (sourcePath ), rootTempDir )
4551 if err != nil {
4652 return
4753 }
4854
4955 // Generate compile_flags.txt
50- cppPath = filepath .Join (tempDir , name + ".cpp" )
51- flagsPath , err := generateCompileFlags (tempDir , inoPath , sourcePath , fqbn )
56+ cppPath = filepath .Join (rootTempDir , sketchFileName + ".cpp" )
57+ flagsPath , err := generateCompileFlags (rootTempDir , inoPath , sourcePath , fqbn )
5258 if err != nil {
5359 return
5460 }
@@ -57,10 +63,19 @@ func generateCpp(inoCode []byte, sourcePath, fqbn string) (cppPath string, cppCo
5763 }
5864
5965 // Generate target file
60- cppCode , err = generateTargetFile (tempDir , inoPath , cppPath , fqbn )
66+ cppCode , err = generateTargetFile (rootTempDir , inoPath , cppPath , fqbn )
6167 return
6268}
6369
70+ func createDirIfNotExist (dir string ) {
71+ if _ , err := os .Stat (dir ); os .IsNotExist (err ) {
72+ err = os .MkdirAll (dir , os .ModePerm )
73+ if err != nil {
74+ panic (err )
75+ }
76+ }
77+ }
78+
6479func copyHeaderFiles (sourceDir string , destDir string ) error {
6580 fileInfos , err := ioutil .ReadDir (sourceDir )
6681 if err != nil {
0 commit comments