@@ -9,15 +9,19 @@ import (
99 "os/exec"
1010 "path/filepath"
1111 "strings"
12+
13+ "github.com/pkg/errors"
1214)
1315
1416func generateCpp (inoCode []byte , name , fqbn string ) (cppPath string , cppCode []byte , err error ) {
1517 rawTempDir , err := ioutil .TempDir ("" , "ino2cpp-" )
1618 if err != nil {
19+ err = errors .Wrap (err , "Error while creating temporary directory." )
1720 return
1821 }
1922 tempDir , err := filepath .EvalSymlinks (rawTempDir )
2023 if err != nil {
24+ err = errors .Wrap (err , "Error while resolving symbolic links of temporary directory." )
2125 return
2226 }
2327
@@ -28,6 +32,7 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
2832 inoPath := filepath .Join (tempDir , name )
2933 err = ioutil .WriteFile (inoPath , inoCode , 0600 )
3034 if err != nil {
35+ err = errors .Wrap (err , "Error while writing source file to temporary directory." )
3136 return
3237 }
3338 if enableLogging {
@@ -47,14 +52,16 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
4752 preprocessCmd := exec .Command (globalCliPath , "compile" , "--fqbn" , fqbn , "--preprocess" , inoPath )
4853 cppCode , err = preprocessCmd .Output ()
4954 if err != nil {
50- logCommandErr (globalCliPath , cppCode , err )
55+ err = logCommandErr (globalCliPath , cppCode , err , errMsgFilter ( tempDir ) )
5156 return
5257 }
5358
5459 // Write target file to temp dir
5560 cppPath = filepath .Join (tempDir , name + ".cpp" )
5661 err = ioutil .WriteFile (cppPath , cppCode , 0600 )
57- if err == nil && enableLogging {
62+ if err != nil {
63+ err = errors .Wrap (err , "Error while writing target file to temporary directory." )
64+ } else if enableLogging {
5865 log .Println ("Target file written to" , cppPath )
5966 }
6067 return
@@ -65,37 +72,41 @@ func updateCpp(inoCode []byte, fqbn, cppPath string) (cppCode []byte, err error)
6572 inoPath := strings .TrimSuffix (cppPath , ".cpp" )
6673 err = ioutil .WriteFile (inoPath , inoCode , 0600 )
6774 if err != nil {
75+ err = errors .Wrap (err , "Error while writing source file to temporary directory." )
6876 return
6977 }
7078
7179 // Generate target file
7280 preprocessCmd := exec .Command (globalCliPath , "compile" , "--fqbn" , fqbn , "--preprocess" , inoPath )
7381 cppCode , err = preprocessCmd .Output ()
7482 if err != nil {
75- logCommandErr (globalCliPath , cppCode , err )
83+ err = logCommandErr (globalCliPath , cppCode , err , errMsgFilter ( filepath . Dir ( inoPath )) )
7684 return
7785 }
7886
7987 // Write target file to temp dir
8088 err = ioutil .WriteFile (cppPath , cppCode , 0600 )
89+ if err != nil {
90+ err = errors .Wrap (err , "Error while writing target file to temporary directory." )
91+ }
8192 return
8293}
8394
8495func generateCompileFlags (tempDir , inoPath , fqbn string ) (string , error ) {
8596 propertiesCmd := exec .Command (globalCliPath , "compile" , "--fqbn" , fqbn , "--show-properties" , inoPath )
8697 output , err := propertiesCmd .Output ()
8798 if err != nil {
88- logCommandErr (globalCliPath , output , err )
99+ err = logCommandErr (globalCliPath , output , err , errMsgFilter ( tempDir ) )
89100 return "" , err
90101 }
91102 properties , err := readProperties (bytes .NewReader (output ))
92103 if err != nil {
93- return "" , err
104+ return "" , errors . Wrap ( err , "Error while reading build properties." )
94105 }
95106 flagsPath := filepath .Join (tempDir , "compile_flags.txt" )
96107 outFile , err := os .OpenFile (flagsPath , os .O_WRONLY | os .O_CREATE , 0600 )
97108 if err != nil {
98- return flagsPath , err
109+ return flagsPath , errors . Wrap ( err , "Error while creating output file for compile flags." )
99110 }
100111 defer outFile .Close ()
101112 writer := bufio .NewWriter (outFile )
@@ -125,15 +136,30 @@ func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) {
125136 return flagsPath , nil
126137}
127138
128- func logCommandErr (command string , stdout []byte , err error ) {
139+ func logCommandErr (command string , stdout []byte , err error , filter func (string ) string ) error {
140+ message := ""
129141 log .Println ("Command error:" , command , err )
130142 if len (stdout ) > 0 {
131- log .Println ("------------------------------BEGIN STDOUT\n " , string (stdout ), "\n ------------------------------END STDOUT" )
143+ stdoutStr := string (stdout )
144+ log .Println ("------------------------------BEGIN STDOUT\n " , stdoutStr , "\n ------------------------------END STDOUT" )
145+ message += filter (stdoutStr )
132146 }
133147 if exitErr , ok := err .(* exec.ExitError ); ok {
134148 stderr := exitErr .Stderr
135149 if len (stderr ) > 0 {
136- log .Println ("------------------------------BEGIN STDERR\n " , string (stderr ), "\n ------------------------------END STDERR" )
150+ stderrStr := string (stderr )
151+ log .Println ("------------------------------BEGIN STDERR\n " , stderrStr , "\n ------------------------------END STDERR" )
152+ message += filter (stderrStr )
137153 }
138154 }
155+ return errors .Wrap (err , message )
156+ }
157+
158+ func errMsgFilter (tempDir string ) func (string ) string {
159+ if ! strings .HasSuffix (tempDir , string (filepath .Separator )) {
160+ tempDir += string (filepath .Separator )
161+ }
162+ return func (s string ) string {
163+ return strings .ReplaceAll (s , tempDir , "" )
164+ }
139165}
0 commit comments