@@ -9,15 +9,19 @@ import (
9
9
"os/exec"
10
10
"path/filepath"
11
11
"strings"
12
+
13
+ "github.com/pkg/errors"
12
14
)
13
15
14
16
func generateCpp (inoCode []byte , name , fqbn string ) (cppPath string , cppCode []byte , err error ) {
15
17
rawTempDir , err := ioutil .TempDir ("" , "ino2cpp-" )
16
18
if err != nil {
19
+ err = errors .Wrap (err , "Error while creating temporary directory." )
17
20
return
18
21
}
19
22
tempDir , err := filepath .EvalSymlinks (rawTempDir )
20
23
if err != nil {
24
+ err = errors .Wrap (err , "Error while resolving symbolic links of temporary directory." )
21
25
return
22
26
}
23
27
@@ -28,6 +32,7 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
28
32
inoPath := filepath .Join (tempDir , name )
29
33
err = ioutil .WriteFile (inoPath , inoCode , 0600 )
30
34
if err != nil {
35
+ err = errors .Wrap (err , "Error while writing source file to temporary directory." )
31
36
return
32
37
}
33
38
if enableLogging {
@@ -47,14 +52,16 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
47
52
preprocessCmd := exec .Command (globalCliPath , "compile" , "--fqbn" , fqbn , "--preprocess" , inoPath )
48
53
cppCode , err = preprocessCmd .Output ()
49
54
if err != nil {
50
- logCommandErr (globalCliPath , cppCode , err )
55
+ err = logCommandErr (globalCliPath , cppCode , err , errMsgFilter ( tempDir ) )
51
56
return
52
57
}
53
58
54
59
// Write target file to temp dir
55
60
cppPath = filepath .Join (tempDir , name + ".cpp" )
56
61
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 {
58
65
log .Println ("Target file written to" , cppPath )
59
66
}
60
67
return
@@ -65,37 +72,41 @@ func updateCpp(inoCode []byte, fqbn, cppPath string) (cppCode []byte, err error)
65
72
inoPath := strings .TrimSuffix (cppPath , ".cpp" )
66
73
err = ioutil .WriteFile (inoPath , inoCode , 0600 )
67
74
if err != nil {
75
+ err = errors .Wrap (err , "Error while writing source file to temporary directory." )
68
76
return
69
77
}
70
78
71
79
// Generate target file
72
80
preprocessCmd := exec .Command (globalCliPath , "compile" , "--fqbn" , fqbn , "--preprocess" , inoPath )
73
81
cppCode , err = preprocessCmd .Output ()
74
82
if err != nil {
75
- logCommandErr (globalCliPath , cppCode , err )
83
+ err = logCommandErr (globalCliPath , cppCode , err , errMsgFilter ( filepath . Dir ( inoPath )) )
76
84
return
77
85
}
78
86
79
87
// Write target file to temp dir
80
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
+ }
81
92
return
82
93
}
83
94
84
95
func generateCompileFlags (tempDir , inoPath , fqbn string ) (string , error ) {
85
96
propertiesCmd := exec .Command (globalCliPath , "compile" , "--fqbn" , fqbn , "--show-properties" , inoPath )
86
97
output , err := propertiesCmd .Output ()
87
98
if err != nil {
88
- logCommandErr (globalCliPath , output , err )
99
+ err = logCommandErr (globalCliPath , output , err , errMsgFilter ( tempDir ) )
89
100
return "" , err
90
101
}
91
102
properties , err := readProperties (bytes .NewReader (output ))
92
103
if err != nil {
93
- return "" , err
104
+ return "" , errors . Wrap ( err , "Error while reading build properties." )
94
105
}
95
106
flagsPath := filepath .Join (tempDir , "compile_flags.txt" )
96
107
outFile , err := os .OpenFile (flagsPath , os .O_WRONLY | os .O_CREATE , 0600 )
97
108
if err != nil {
98
- return flagsPath , err
109
+ return flagsPath , errors . Wrap ( err , "Error while creating output file for compile flags." )
99
110
}
100
111
defer outFile .Close ()
101
112
writer := bufio .NewWriter (outFile )
@@ -125,15 +136,30 @@ func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) {
125
136
return flagsPath , nil
126
137
}
127
138
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 := ""
129
141
log .Println ("Command error:" , command , err )
130
142
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 )
132
146
}
133
147
if exitErr , ok := err .(* exec.ExitError ); ok {
134
148
stderr := exitErr .Stderr
135
149
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 )
137
153
}
138
154
}
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
+ }
139
165
}
0 commit comments