-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathmake.go
246 lines (204 loc) · 6 KB
/
make.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"regexp"
"sort"
"strings"
"text/template"
)
var flag_vet = flag.Bool("vet", false, "only vet source files, don't run them")
var flag_examples = flag.Bool("examples", false, "run mumax3 examples")
var flag_forced = flag.Bool("forced", false, "force to re-run mumax3 examples")
var flag_builddir = flag.String("builddir", "build", "build directory")
var buildDir string
const templateDir = "templates"
func main() {
flag.Parse()
buildDir = *flag_builddir + "/"
buildAPI()
// read template
b, err := ioutil.ReadFile(path.Join(templateDir, "examples-template.html"))
check(err)
replaceInRaw(b, '\n', '@') // hack to allow raw strings spanning multi lines
templ := template.Must(template.New("guide").Parse(string(b)))
// output file
f, err2 := os.OpenFile(path.Join(buildDir, "examples.html"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
check(err2)
// execute!
if *flag_examples {
state := &State{}
check(templ.Execute(f, state))
}
renderAPI()
createIndexPage()
createDownloadPage()
createHeaderPage()
}
func createIndexPage() {
b, err := ioutil.ReadFile(path.Join(templateDir, "index-template.html"))
replaceInRaw(b, '\n', '@') // hack to allow raw strings spanning multi lines
check(err)
templ := template.Must(template.New("guid").Parse(string(b)))
f, err2 := os.OpenFile(path.Join(buildDir, "index.html"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
check(err2)
state := &State{}
check(templ.Execute(f, state))
}
func createDownloadPage() {
b, err := ioutil.ReadFile(path.Join(templateDir, "download-template.html"))
replaceInRaw(b, '\n', '@') // hack to allow raw strings spanning multi lines
check(err)
templ := template.Must(template.New("download").Parse(string(b)))
f, err2 := os.OpenFile(path.Join(buildDir, "download.html"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
check(err2)
state := &State{}
check(templ.Execute(f, state))
}
func createHeaderPage() {
b, err := ioutil.ReadFile(path.Join(templateDir, "headerpage-template.html"))
replaceInRaw(b, '\n', '@') // hack to allow raw strings spanning multi lines
check(err)
templ := template.Must(template.New("headerpage").Parse(string(b)))
f, err2 := os.OpenFile(path.Join(buildDir, "header.html"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
check(err2)
state := &State{}
check(templ.Execute(f, state))
}
type State struct {
count int
}
func (s *State) Example(in string) string {
s.count++
// extract example source
in = strings.Replace(in, "@", "\n", -1) // undo raw string hack
in = strings.Trim(in, "\n")
// exec input file
check(ioutil.WriteFile(s.infile(), []byte(in), 0666))
arg := "-v"
if *flag_vet {
arg = "-vet"
}
if _, err := os.Stat(s.outfile()); os.IsNotExist(err) || *flag_forced {
cmd("mumax3", "-cache", "/tmp", arg, s.infile())
}
recordExamples(in, s.count)
return `<a id=example` + fmt.Sprint(s.count) + `></a><pre>` + template.HTMLEscapeString(in) + `</pre>`
}
var api_examples = make(map[string][]int)
func recordExamples(input string, num int) {
in := strings.ToLower(input)
for k, _ := range api_ident {
if ok, _ := regexp.MatchString(k, in); ok {
api_examples[k] = append(api_examples[k], num)
}
}
}
func (s *State) Img(fname string) string {
cmd("mumax3-convert", "-png", "-arrows", "16", path.Join(s.outfile(), fname+".ovf"))
pngfile := path.Join(s.relativeOutfile(), fname+".png")
return fmt.Sprintf(`
<figure style="float:left">
<img src="%v"/>
<figcaption> %v </figcaption>
</figure>`, pngfile, fname)
}
func (s *State) Include(fname string) string {
b, err := ioutil.ReadFile(path.Join(templateDir, fname))
check(err)
return string(b)
}
func (s *State) Output() string {
out := `<h3>output</h3> `
dir, err := os.Open(s.outfile())
check(err)
files, err2 := dir.Readdirnames(-1)
check(err2)
sort.Strings(files)
for _, f := range files {
if path.Ext(f) == ".ovf" {
out += s.Img(f[:len(f)-len(".ovf")])
}
}
out += `<br style="clear:both"/> `
for _, f := range files {
if f == "table.txt" {
cmd("mumax3-plot", path.Join(s.outfile(), f))
}
}
dir, err = os.Open(s.outfile())
check(err)
files, err2 = dir.Readdirnames(-1)
check(err2)
sort.Strings(files)
for _, f := range files {
if path.Ext(f) == ".svg" {
src := path.Join(s.relativeOutfile(), f)
out += fmt.Sprintf(`
<figure>
<img src="%v"/>
<figcaption> %v </figcaption>
</figure>`, src, f)
}
}
return out
}
// State.output gives a nice otuput for all examples except for the
// hysteresis example. State.OutputHysteresis is the custom output function
// for the hysteresis example.
func (s *State) OutputHysteresis() string {
tableName := path.Join(s.outfile(), "table.txt")
figureName := path.Join(s.outfile(), "hysteresis.svg")
relFigureName := path.Join(s.relativeOutfile(), "hysteresis.svg")
gnuplotCmd := `set term svg noenhanced size 400 300 font 'Arial,10';`
gnuplotCmd += fmt.Sprintf(`set output "%s";`, figureName)
gnuplotCmd += `set xlabel "B_ext(T)";`
gnuplotCmd += `set ylabel "m_x";`
gnuplotCmd += fmt.Sprintf(`plot "%s" u 5:2 w lp notitle;`, tableName)
gnuplotCmd += "set output;"
gnuplotOut, err := exec.Command("gnuplot", "-e", gnuplotCmd).CombinedOutput()
os.Stderr.Write(gnuplotOut)
check(err)
out := fmt.Sprintf(`
<h3>output</h3>
<figure>
<img src="%v"/>
</figure>`, relFigureName)
return out
}
func (s *State) infile() string {
return path.Join(buildDir, fmt.Sprintf("example%v.mx3", s.count))
}
func (s *State) outfile() string {
return path.Join(buildDir, fmt.Sprintf("example%v.out", s.count))
}
// Relative output directory path from the build directory
func (s *State) relativeOutfile() string {
return fmt.Sprintf("example%v.out", s.count)
}
func cmd(cmd string, args ...string) {
out, err := exec.Command(cmd, args...).CombinedOutput()
os.Stdout.Write(out)
check(err)
}
func replaceInRaw(bytes []byte, old, new byte) {
inraw := false
for i, b := range bytes {
if b == '`' {
inraw = !inraw
}
if inraw && b == old {
bytes[i] = new
}
}
}
func check(err error) {
if err != nil {
log.Panic(err)
}
}