@@ -46,6 +46,8 @@ type BuildSSAOutput struct {
4646 Msg string `json:"msg"`
4747}
4848
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.
4951func BuildSSA (c * gin.Context ) {
5052 // 1. create a folder in config.Get().Static/buildbox
5153 out := BuildSSAOutput {
@@ -65,7 +67,7 @@ func BuildSSA(c *gin.Context) {
6567 err = c .BindJSON (& in )
6668 if err != nil {
6769 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 )
6971 c .JSON (http .StatusInternalServerError , out )
7072 return
7173 }
@@ -86,24 +88,35 @@ func BuildSSA(c *gin.Context) {
8688 err = ioutil .WriteFile (buildFile , []byte (in .Code ), os .ModePerm )
8789 if err != nil {
8890 os .Remove (path )
89- out .Msg = err . Error ( )
91+ out .Msg = fmt . Sprintf ( "cannot save your code, err: \n %v" , err )
9092 c .JSON (http .StatusInternalServerError , out )
9193 return
9294 }
9395
94- // 3. goimports && GOSSAFUNC=foo go build
96+ // 3.1 goimports
9597 err = autoimports (buildFile )
9698 if err != nil {
9799 os .Remove (path )
98- out .Msg = err . Error ( )
100+ out .Msg = fmt . Sprintf ( "cannot run autoimports for your code, err: \n %v" , err )
99101 c .JSON (http .StatusBadRequest , out )
100102 return
101103 }
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
102115 outFile := filepath .Join (path , "/main.out" )
103116 err = buildSSA (in .FuncName , in .GcFlags , outFile , buildFile , isTest )
104117 if err != nil {
105118 os .Remove (path )
106- out .Msg = err . Error ( )
119+ out .Msg = fmt . Sprintf ( "cannot build ssa for your code, err: \n %v" , err )
107120 c .JSON (http .StatusBadRequest , out )
108121 return
109122 }
@@ -138,6 +151,32 @@ func autoimports(outf string) error {
138151 return nil
139152}
140153
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+
141180func buildSSA (funcname , gcflags , outf , buildf string , isTest bool ) error {
142181 var cmd * exec.Cmd
143182 if ! isTest {
0 commit comments