@@ -46,6 +46,8 @@ type BuildSSAOutput struct {
46
46
Msg string `json:"msg"`
47
47
}
48
48
49
+ // BuildSSA serves the code send by user and builds its SSA IR into html.
50
+ // TODO: speedup for request response, e.g. as async rest api.
49
51
func BuildSSA (c * gin.Context ) {
50
52
// 1. create a folder in config.Get().Static/buildbox
51
53
out := BuildSSAOutput {
@@ -65,7 +67,7 @@ func BuildSSA(c *gin.Context) {
65
67
err = c .BindJSON (& in )
66
68
if err != nil {
67
69
os .Remove (path )
68
- out .Msg = fmt .Sprintf ("cannot bind input params, err: %v" , err )
70
+ out .Msg = fmt .Sprintf ("cannot bind input params, err: \n %v" , err )
69
71
c .JSON (http .StatusInternalServerError , out )
70
72
return
71
73
}
@@ -86,24 +88,35 @@ func BuildSSA(c *gin.Context) {
86
88
err = ioutil .WriteFile (buildFile , []byte (in .Code ), os .ModePerm )
87
89
if err != nil {
88
90
os .Remove (path )
89
- out .Msg = err . Error ( )
91
+ out .Msg = fmt . Sprintf ( "cannot save your code, err: \n %v" , err )
90
92
c .JSON (http .StatusInternalServerError , out )
91
93
return
92
94
}
93
95
94
- // 3. goimports && GOSSAFUNC=foo go build
96
+ // 3.1 goimports
95
97
err = autoimports (buildFile )
96
98
if err != nil {
97
99
os .Remove (path )
98
- out .Msg = err . Error ( )
100
+ out .Msg = fmt . Sprintf ( "cannot run autoimports for your code, err: \n %v" , err )
99
101
c .JSON (http .StatusBadRequest , out )
100
102
return
101
103
}
104
+
105
+ // 3.2 go mod init gossa && go mod tidy
106
+ err = initModules (path )
107
+ if err != nil {
108
+ os .Remove (path )
109
+ out .Msg = fmt .Sprintf ("cannot use go modules for your code, err: \n %v" , err )
110
+ c .JSON (http .StatusBadRequest , out )
111
+ return
112
+ }
113
+
114
+ // 3.3 GOSSAFUNC=foo go build
102
115
outFile := filepath .Join (path , "/main.out" )
103
116
err = buildSSA (in .FuncName , in .GcFlags , outFile , buildFile , isTest )
104
117
if err != nil {
105
118
os .Remove (path )
106
- out .Msg = err . Error ( )
119
+ out .Msg = fmt . Sprintf ( "cannot build ssa for your code, err: \n %v" , err )
107
120
c .JSON (http .StatusBadRequest , out )
108
121
return
109
122
}
@@ -138,6 +151,32 @@ func autoimports(outf string) error {
138
151
return nil
139
152
}
140
153
154
+ func initModules (path string ) error {
155
+ // 1. go mod init
156
+ cmd := exec .Command ("go" , "mod" , "init" , "gossa" )
157
+ cmd .Dir = path
158
+ cmd .Stderr = & bytes.Buffer {}
159
+ err := cmd .Run ()
160
+ if err != nil {
161
+ msg := cmd .Stderr .(* bytes.Buffer ).String ()
162
+ msg = strings .ReplaceAll (msg , path , "$GOSSAPATH" )
163
+ return errors .New (msg )
164
+ }
165
+
166
+ // 2. go mod tidy
167
+ cmd = exec .Command ("go" , "mod" , "tidy" )
168
+ cmd .Dir = path
169
+ cmd .Stderr = & bytes.Buffer {}
170
+ err = cmd .Run ()
171
+ if err != nil {
172
+ msg := cmd .Stderr .(* bytes.Buffer ).String ()
173
+ msg = strings .ReplaceAll (msg , path , "$GOSSAPATH" )
174
+ return errors .New (msg )
175
+ }
176
+
177
+ return nil
178
+ }
179
+
141
180
func buildSSA (funcname , gcflags , outf , buildf string , isTest bool ) error {
142
181
var cmd * exec.Cmd
143
182
if ! isTest {
0 commit comments