@@ -40,6 +40,7 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
4040 }
4141
4242 // Generate compile_flags.txt
43+ cppPath = filepath .Join (tempDir , name + ".cpp" )
4344 flagsPath , err := generateCompileFlags (tempDir , inoPath , fqbn )
4445 if err != nil {
4546 return
@@ -49,51 +50,43 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
4950 }
5051
5152 // 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 )
6754 return
6855}
6956
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 )
7259 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+ }
7767 }
7868
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+ }
8575 }
8676
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 )
9279 return
9380}
9481
9582func 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 ... )
9790 output , err := propertiesCmd .Output ()
9891 if err != nil {
9992 err = logCommandErr (globalCliPath , output , err , errMsgFilter (tempDir ))
@@ -136,6 +129,42 @@ func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) {
136129 return flagsPath , nil
137130}
138131
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+
139168func logCommandErr (command string , stdout []byte , err error , filter func (string ) string ) error {
140169 message := ""
141170 log .Println ("Command error:" , command , err )
0 commit comments