-
Notifications
You must be signed in to change notification settings - Fork 0
/
presentj.go
175 lines (160 loc) · 4.21 KB
/
presentj.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package presentj
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
const working = false
func must(s string, err error) string {
if err != nil {
panic(err)
}
return s
}
func JavaExec(src string) {
class := className(src)
if len(class) == 0 {
log.Fatalf("invalid Java source")
}
workspace, err := ioutil.TempDir("", "presentjava")
if err != nil {
log.Fatalf("failed to create workspace: %v", err) //create temporary workspace
}
defer os.RemoveAll(workspace) // delete workspace on exit
err = os.Mkdir(filepath.Join(workspace, "main"), 0777) // create workspace dir structure
if err != nil && !os.IsExist(err) {
log.Fatalf("failed to create workspace dir structure: %v", err)
return
}
err = ioutil.WriteFile(filepath.Join(workspace, "main", class+".java"), []byte(src), 0666)
if err != nil {
log.Fatalf("failed to write java source: %v", err)
return
}
// compile java source
err = (&exec.Cmd{
Path: must(exec.LookPath("javac")), // lookup javac binary
Args: []string{"javac", filepath.Join("main", class+".java")},
Dir: workspace,
Stdout: os.Stdout,
Stderr: os.Stderr,
}).Run()
if err != nil {
log.Fatalf("failed to compile java source: %v", err)
return
}
// run java snippet
err = (&exec.Cmd{
Path: must(exec.LookPath("java")), // lookup java binary
Args: []string{"java", "main." + class},
Dir: workspace,
Stdout: os.Stdout,
Stderr: os.Stderr,
}).Run()
if err != nil {
if working {
log.Fatalf("failed to run java snippet: %v", err)
} else {
log.Printf("failed to run java snippet: %v", err)
return
}
}
}
func MavenExec(src, pomFile string) {
maven(src, pomFile, false)
}
func MavenTest(src, pomFile string) {
maven(src, pomFile, true)
}
func className(src string) string {
fields := strings.Fields(src)
var class string
for i, s := range fields {
if s == "class" {
class = fields[i+1]
break
}
}
return class
}
func maven(src, pomFile string, test bool) {
class := className(src)
if len(class) == 0 {
log.Fatalf("invalid Java source")
}
b, err := ioutil.ReadFile(pomFile) // read maven pom file
if err != nil {
log.Fatalf("failed to read pom file: %v", err)
}
pom := string(b) // convert content to a 'string'
workspace, err := ioutil.TempDir("", "presentjava") // create temporary workspace for maven project
if err != nil {
log.Fatalf("failed to create workspace: %v", err)
}
defer os.RemoveAll(workspace) // delete workspace on exit
folder := "main"
if test {
folder = "test"
}
err = os.MkdirAll(filepath.Join(workspace, "src", folder, "java", folder), 0777) // create workspace dir structure
if err != nil && !os.IsExist(err) {
log.Fatalf("failed to create workspace dir structure: %v", err)
return
}
err = ioutil.WriteFile(filepath.Join(workspace, "pom.xml"), []byte(pom), 0666) // write pom file
if err != nil {
log.Fatalf("failed to write pom file: %v", err)
return
}
err = ioutil.WriteFile(filepath.Join(workspace, "src", folder, "java", folder, class+".java"), []byte(src), 0666) // write java source
if err != nil {
log.Fatalf("failed to write java source: %v", err)
return
}
if test {
// run unit test
err = (&exec.Cmd{
Path: must(exec.LookPath("mvn")), // lookup maven binary
Args: []string{"mvn", "-q", "-Dtest=" + class, "test"},
Dir: workspace,
Stdout: os.Stdout,
Stderr: os.Stderr,
}).Run()
if err != nil {
log.Printf("failed to run unit test: %v", err)
return
}
return
}
// compile maven project
err = (&exec.Cmd{
Path: must(exec.LookPath("mvn")), // lookup maven binary
Args: []string{"mvn", "-q", "compile"},
Dir: workspace,
Stdout: os.Stdout,
Stderr: os.Stderr,
}).Run()
if err != nil {
log.Fatalf("failed to compile maven project: %v", err)
return
}
// run maven project
err = (&exec.Cmd{
Path: must(exec.LookPath("mvn")), // lookup maven binary
Args: []string{"mvn", "-q", "exec:java", "-Dexec.mainClass=main." + class},
Dir: workspace,
Stdout: os.Stdout,
Stderr: os.Stderr,
}).Run()
if err != nil {
if working {
log.Fatalf("failed to run java snippet: %v", err)
} else {
log.Printf("failed to run java snippet: %v", err)
return
}
}
}