-
Notifications
You must be signed in to change notification settings - Fork 10
/
frob.go
175 lines (142 loc) · 3.42 KB
/
frob.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 gfmrun
import (
"fmt"
"regexp"
"runtime"
"strings"
)
var (
DefaultFrobs = map[string]Frob{
"bash": NewSimpleInterpretedFrob("bash", "bash"),
"go": &GoFrob{},
"java": &JavaFrob{},
"javascript": NewSimpleInterpretedFrob("js", "node"),
"json": NewSimpleInterpretedFrob("json", "node"),
"python": NewSimpleInterpretedFrob("py", "python"),
"ruby": NewSimpleInterpretedFrob("rb", "ruby"),
"shell": NewSimpleInterpretedFrob("bash", "bash"),
"sh": NewSimpleInterpretedFrob("sh", "sh"),
"zsh": NewSimpleInterpretedFrob("zsh", "zsh"),
}
errEmptySource = fmt.Errorf("empty source")
javaPublicClassRe = regexp.MustCompile("public +class +([^ ]+)")
)
type Frob interface {
Extension() string
CanExecute(*Runnable) error
TempFileName(*Runnable) string
Environ(*Runnable) []string
Commands(*Runnable) []*command
}
type command struct {
Main bool
Args []string
}
func NewSimpleInterpretedFrob(ext, interpreter string) Frob {
return &InterpretedFrob{
ext: ext,
env: []string{},
tmpl: []string{interpreter, "--", "{{.FILE}}"},
}
}
type InterpretedFrob struct {
ext string
env []string
tmpl []string
}
func (e *InterpretedFrob) Extension() string {
return e.ext
}
func (e *InterpretedFrob) CanExecute(rn *Runnable) error {
if len(rn.Lines) < 1 {
return errEmptySource
}
return nil
}
func (e *InterpretedFrob) TempFileName(rn *Runnable) string {
return fmt.Sprintf("example-L%d.%s", rn.LineOffset, e.ext)
}
func (e *InterpretedFrob) Environ(_ *Runnable) []string {
return e.env
}
func (e *InterpretedFrob) Commands(_ *Runnable) []*command {
return []*command{
&command{
Main: true,
Args: e.tmpl,
},
}
}
type GoFrob struct{}
func (e *GoFrob) Extension() string {
return "go"
}
func (e *GoFrob) TempFileName(rn *Runnable) string {
return fmt.Sprintf("example-L%d.go", rn.LineOffset)
}
func (e *GoFrob) CanExecute(rn *Runnable) error {
if len(rn.Lines) < 1 {
return errEmptySource
}
trimmedLine0 := strings.TrimSpace(rn.Lines[0])
if trimmedLine0 != "package main" {
return fmt.Errorf("first line is not \"package main\": %q", trimmedLine0)
}
return nil
}
func (e *GoFrob) Environ(_ *Runnable) []string {
return []string{}
}
func (e *GoFrob) Commands(_ *Runnable) []*command {
goExe := ""
if runtime.GOOS == "windows" {
goExe = ".exe"
}
return []*command{
&command{
Args: []string{"go", "build", "-o", "{{.NAMEBASE}}" + goExe, "{{.FILE}}"},
},
&command{
Main: true,
Args: []string{"{{.NAMEBASE}}" + goExe},
},
}
}
type JavaFrob struct{}
func (e *JavaFrob) Extension() string {
return "java"
}
func (e *JavaFrob) CanExecute(rn *Runnable) error {
if len(rn.Lines) < 1 {
return errEmptySource
}
for _, line := range rn.Lines {
if javaPublicClassRe.MatchString(line) {
return nil
}
}
return fmt.Errorf("no public class found")
}
func (e *JavaFrob) TempFileName(rn *Runnable) string {
return fmt.Sprintf("%s.java", e.getClassName(rn.String()))
}
func (e *JavaFrob) Environ(_ *Runnable) []string {
return []string{}
}
func (e *JavaFrob) Commands(rn *Runnable) []*command {
return []*command{
&command{
Args: []string{"javac", "{{.BASENAME}}"},
},
&command{
Main: true,
Args: []string{"java", e.getClassName(rn.String())},
},
}
}
func (e *JavaFrob) getClassName(source string) string {
if m := javaPublicClassRe.FindStringSubmatch(source); len(m) > 1 {
return m[1]
}
return "Unknown"
}