@@ -40,6 +40,7 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
40
40
}
41
41
42
42
// Generate compile_flags.txt
43
+ cppPath = filepath .Join (tempDir , name + ".cpp" )
43
44
flagsPath , err := generateCompileFlags (tempDir , inoPath , fqbn )
44
45
if err != nil {
45
46
return
@@ -49,51 +50,43 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
49
50
}
50
51
51
52
// Generate target file
52
- preprocessCmd := exec .Command (globalCliPath , "compile" , "--fqbn" , fqbn , "--preprocess" , inoPath )
53
- cppCode , err = preprocessCmd .Output ()
54
- if err != nil {
55
- err = logCommandErr (globalCliPath , cppCode , err , errMsgFilter (tempDir ))
56
- return
57
- }
58
-
59
- // Write target file to temp dir
60
- cppPath = filepath .Join (tempDir , name + ".cpp" )
61
- err = ioutil .WriteFile (cppPath , cppCode , 0600 )
62
- if err != nil {
63
- err = errors .Wrap (err , "Error while writing target file to temporary directory." )
64
- } else if enableLogging {
65
- log .Println ("Target file written to" , cppPath )
66
- }
53
+ cppCode , err = generateTargetFile (tempDir , inoPath , cppPath , fqbn )
67
54
return
68
55
}
69
56
70
- func updateCpp (inoCode []byte , fqbn , cppPath string ) (cppCode []byte , err error ) {
71
- // Write source file to temp dir
57
+ func updateCpp (inoCode []byte , fqbn string , fqbnChanged bool , cppPath string ) (cppCode []byte , err error ) {
58
+ tempDir := filepath . Dir ( cppPath )
72
59
inoPath := strings .TrimSuffix (cppPath , ".cpp" )
73
- err = ioutil .WriteFile (inoPath , inoCode , 0600 )
74
- if err != nil {
75
- err = errors .Wrap (err , "Error while writing source file to temporary directory." )
76
- return
60
+ if inoCode != nil {
61
+ // Write source file to temp dir
62
+ err = ioutil .WriteFile (inoPath , inoCode , 0600 )
63
+ if err != nil {
64
+ err = errors .Wrap (err , "Error while writing source file to temporary directory." )
65
+ return
66
+ }
77
67
}
78
68
79
- // Generate target file
80
- preprocessCmd := exec . Command ( globalCliPath , "compile" , "--fqbn" , fqbn , "--preprocess" , inoPath )
81
- cppCode , err = preprocessCmd . Output ( )
82
- if err != nil {
83
- err = logCommandErr ( globalCliPath , cppCode , err , errMsgFilter ( filepath . Dir ( inoPath )))
84
- return
69
+ if fqbnChanged {
70
+ // Generate compile_flags.txt
71
+ _ , err = generateCompileFlags ( tempDir , inoPath , fqbn )
72
+ if err != nil {
73
+ return
74
+ }
85
75
}
86
76
87
- // Write target file to temp dir
88
- err = ioutil .WriteFile (cppPath , cppCode , 0600 )
89
- if err != nil {
90
- err = errors .Wrap (err , "Error while writing target file to temporary directory." )
91
- }
77
+ // Generate target file
78
+ cppCode , err = generateTargetFile (tempDir , inoPath , cppPath , fqbn )
92
79
return
93
80
}
94
81
95
82
func generateCompileFlags (tempDir , inoPath , fqbn string ) (string , error ) {
96
- propertiesCmd := exec .Command (globalCliPath , "compile" , "--fqbn" , fqbn , "--show-properties" , inoPath )
83
+ var cliArgs []string
84
+ if len (fqbn ) > 0 {
85
+ cliArgs = []string {"compile" , "--fqbn" , fqbn , "--show-properties" , inoPath }
86
+ } else {
87
+ cliArgs = []string {"compile" , "--show-properties" , inoPath }
88
+ }
89
+ propertiesCmd := exec .Command (globalCliPath , cliArgs ... )
97
90
output , err := propertiesCmd .Output ()
98
91
if err != nil {
99
92
err = logCommandErr (globalCliPath , output , err , errMsgFilter (tempDir ))
@@ -136,6 +129,42 @@ func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) {
136
129
return flagsPath , nil
137
130
}
138
131
132
+ func generateTargetFile (tempDir , inoPath , cppPath , fqbn string ) (cppCode []byte , err error ) {
133
+ var cliArgs []string
134
+ if len (fqbn ) > 0 {
135
+ cliArgs = []string {"compile" , "--fqbn" , fqbn , "--preprocess" , inoPath }
136
+ } else {
137
+ cliArgs = []string {"compile" , "--preprocess" , inoPath }
138
+ }
139
+ preprocessCmd := exec .Command (globalCliPath , cliArgs ... )
140
+ cppCode , err = preprocessCmd .Output ()
141
+ if err != nil {
142
+ err = logCommandErr (globalCliPath , cppCode , err , errMsgFilter (tempDir ))
143
+ return
144
+ }
145
+
146
+ err = ioutil .WriteFile (cppPath , cppCode , 0600 )
147
+ if err != nil {
148
+ err = errors .Wrap (err , "Error while writing target file to temporary directory." )
149
+ } else if enableLogging {
150
+ log .Println ("Target file written to" , cppPath )
151
+ }
152
+ return
153
+ }
154
+
155
+ func copyIno2Cpp (inoCode []byte , cppPath string ) (cppCode []byte , err error ) {
156
+ cppCode = inoCode
157
+ err = ioutil .WriteFile (cppPath , cppCode , 0600 )
158
+ if err != nil {
159
+ err = errors .Wrap (err , "Error while writing target file to temporary directory." )
160
+ return
161
+ }
162
+ if enableLogging {
163
+ log .Println ("Target file written to" , cppPath )
164
+ }
165
+ return
166
+ }
167
+
139
168
func logCommandErr (command string , stdout []byte , err error , filter func (string ) string ) error {
140
169
message := ""
141
170
log .Println ("Command error:" , command , err )
0 commit comments