@@ -14,23 +14,29 @@ import (
14
14
)
15
15
16
16
func 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-" )
18
20
if err != nil {
19
21
err = errors .Wrap (err , "Error while creating temporary directory." )
20
22
return
21
23
}
22
- tempDir , err := filepath .EvalSymlinks (rawTempDir )
24
+ rootTempDir , err := filepath .EvalSymlinks (rawRootTempDir )
23
25
if err != nil {
24
26
err = errors .Wrap (err , "Error while resolving symbolic links of temporary directory." )
25
27
return
26
28
}
27
29
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" )]
32
33
}
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 )
34
40
err = ioutil .WriteFile (inoPath , inoCode , 0600 )
35
41
if err != nil {
36
42
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
41
47
}
42
48
43
49
// Copy all header files to temp dir
44
- err = copyHeaderFiles (filepath .Dir (sourcePath ), tempDir )
50
+ err = copyHeaderFiles (filepath .Dir (sourcePath ), rootTempDir )
45
51
if err != nil {
46
52
return
47
53
}
48
54
49
55
// 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 )
52
58
if err != nil {
53
59
return
54
60
}
@@ -57,10 +63,19 @@ func generateCpp(inoCode []byte, sourcePath, fqbn string) (cppPath string, cppCo
57
63
}
58
64
59
65
// Generate target file
60
- cppCode , err = generateTargetFile (tempDir , inoPath , cppPath , fqbn )
66
+ cppCode , err = generateTargetFile (rootTempDir , inoPath , cppPath , fqbn )
61
67
return
62
68
}
63
69
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
+
64
79
func copyHeaderFiles (sourceDir string , destDir string ) error {
65
80
fileInfos , err := ioutil .ReadDir (sourceDir )
66
81
if err != nil {
0 commit comments