@@ -3,7 +3,6 @@ package compile
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "os"
7
6
"path/filepath"
8
7
"sort"
9
8
"strings"
@@ -29,8 +28,9 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
29
28
}
30
29
sketch , err := cli .InitSketch (sketchPath )
31
30
if err != nil {
32
- formatter .PrintError (err , "Error opening sketch." )
33
- os .Exit (cli .ErrGeneric )
31
+ return & rpc.CompileResp {
32
+ Result : rpc .Error ("Error opening sketch" , rpc .ErrGeneric ),
33
+ }, nil
34
34
}
35
35
36
36
fqbnIn := req .GetFqbn ()
@@ -39,12 +39,16 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
39
39
}
40
40
if fqbnIn == "" {
41
41
formatter .PrintErrorMessage ("No Fully Qualified Board Name provided." )
42
- os .Exit (cli .ErrGeneric )
42
+ return & rpc.CompileResp {
43
+ Result : rpc .Error ("No Fully Qualified Board Name provided." , rpc .ErrGeneric ),
44
+ }, nil
43
45
}
44
46
fqbn , err := cores .ParseFQBN (fqbnIn )
45
47
if err != nil {
46
48
formatter .PrintErrorMessage ("Fully Qualified Board Name has incorrect format." )
47
- os .Exit (cli .ErrBadArgument )
49
+ return & rpc.CompileResp {
50
+ Result : rpc .Error ("Fully Qualified Board Name has incorrect format." , rpc .ErrGeneric ),
51
+ }, nil
48
52
}
49
53
50
54
pm , _ := cli .InitPackageAndLibraryManager ()
@@ -58,13 +62,16 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
58
62
core .InstallToolRelease (pm , ctags )
59
63
60
64
if err := pm .LoadHardware (cli .Config ); err != nil {
61
- formatter .PrintError (err , "Could not load hardware packages." )
62
- os .Exit (cli .ErrCoreConfig )
65
+ return & rpc.CompileResp {
66
+ Result : rpc .Error ("Could not load hardware packages." , rpc .ErrGeneric ),
67
+ }, nil
63
68
}
64
69
ctags , _ = getBuiltinCtagsTool (pm )
65
70
if ! ctags .IsInstalled () {
66
71
formatter .PrintErrorMessage ("Missing ctags tool." )
67
- os .Exit (cli .ErrCoreConfig )
72
+ return & rpc.CompileResp {
73
+ Result : rpc .Error ("Missing ctags tool." , rpc .ErrGeneric ),
74
+ }, nil
68
75
}
69
76
}
70
77
@@ -77,7 +84,9 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
77
84
"\" %[1]s:%[2]s\" platform is not installed, please install it by running \" " +
78
85
cli .AppName + " core install %[1]s:%[2]s\" ." , fqbn .Package , fqbn .PlatformArch )
79
86
formatter .PrintErrorMessage (errorMessage )
80
- os .Exit (cli .ErrCoreConfig )
87
+ return & rpc.CompileResp {
88
+ Result : rpc .Error (errorMessage , rpc .ErrGeneric ),
89
+ }, nil
81
90
}
82
91
83
92
builderCtx := & types.Context {}
@@ -89,15 +98,17 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
89
98
if packagesDir , err := cli .Config .HardwareDirectories (); err == nil {
90
99
builderCtx .HardwareDirs = packagesDir
91
100
} else {
92
- formatter .PrintError (err , "Cannot get hardware directories." )
93
- os .Exit (cli .ErrCoreConfig )
101
+ return & rpc.CompileResp {
102
+ Result : rpc .Error ("Cannot get hardware directories." , rpc .ErrGeneric ),
103
+ }, nil
94
104
}
95
105
96
106
if toolsDir , err := cli .Config .BundleToolsDirectories (); err == nil {
97
107
builderCtx .ToolsDirs = toolsDir
98
108
} else {
99
- formatter .PrintError (err , "Cannot get bundled tools directories." )
100
- os .Exit (cli .ErrCoreConfig )
109
+ return & rpc.CompileResp {
110
+ Result : rpc .Error ("Cannot get bundled tools directories." , rpc .ErrGeneric ),
111
+ }, nil
101
112
}
102
113
103
114
builderCtx .OtherLibrariesDirs = paths .NewPathList ()
@@ -107,8 +118,9 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
107
118
builderCtx .BuildPath = paths .New (req .GetBuildPath ())
108
119
err = builderCtx .BuildPath .MkdirAll ()
109
120
if err != nil {
110
- formatter .PrintError (err , "Cannot create the build directory." )
111
- os .Exit (cli .ErrBadCall )
121
+ return & rpc.CompileResp {
122
+ Result : rpc .Error ("Cannot create the build directory." , rpc .ErrGeneric ),
123
+ }, nil
112
124
}
113
125
}
114
126
@@ -131,8 +143,9 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
131
143
builderCtx .BuildCachePath = paths .New (req .GetBuildCachePath ())
132
144
err = builderCtx .BuildCachePath .MkdirAll ()
133
145
if err != nil {
134
- formatter .PrintError (err , "Cannot create the build cache directory." )
135
- os .Exit (cli .ErrBadCall )
146
+ return & rpc.CompileResp {
147
+ Result : rpc .Error ("Cannot create the build cache directory." , rpc .ErrGeneric ),
148
+ }, nil
136
149
}
137
150
}
138
151
@@ -166,8 +179,9 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
166
179
}
167
180
168
181
if err != nil {
169
- formatter .PrintError (err , "Compilation failed." )
170
- os .Exit (cli .ErrGeneric )
182
+ return & rpc.CompileResp {
183
+ Result : rpc .Error ("Compilation failed." , rpc .ErrGeneric ),
184
+ }, nil
171
185
}
172
186
173
187
// FIXME: Make a function to obtain these info...
@@ -197,8 +211,9 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
197
211
dstHex := exportPath .Join (exportFile + ext )
198
212
logrus .WithField ("from" , srcHex ).WithField ("to" , dstHex ).Print ("copying sketch build output" )
199
213
if err = srcHex .CopyTo (dstHex ); err != nil {
200
- formatter .PrintError (err , "Error copying output file." )
201
- os .Exit (cli .ErrGeneric )
214
+ return & rpc.CompileResp {
215
+ Result : rpc .Error ("Error copying output file." , rpc .ErrGeneric ),
216
+ }, nil
202
217
}
203
218
204
219
// Copy .elf file to sketch directory
@@ -207,6 +222,10 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
207
222
logrus .WithField ("from" , srcElf ).WithField ("to" , dstElf ).Print ("copying sketch build output" )
208
223
if err = srcElf .CopyTo (dstElf ); err != nil {
209
224
formatter .PrintError (err , "Error copying elf file." )
210
- os .Exit (cli .ErrGeneric )
225
+ return & rpc.CompileResp {
226
+ Result : rpc .Error ("Error copying elf file." , rpc .ErrGeneric ),
227
+ }, nil
211
228
}
229
+
230
+ return & rpc.CompileResp {}, nil
212
231
}
0 commit comments