-
Notifications
You must be signed in to change notification settings - Fork 59
/
build.go
221 lines (181 loc) · 4.57 KB
/
build.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
)
import "os"
import "os/exec"
import "path"
var inkscapeBin = "inkscape"
var epsToPdfBin = "epstopdf"
var mode = "release"
var outputDirName = "out"
func run(c string) {
println(c)
args := strings.Split(c, " ")
cmd := exec.Command(args[0], args[1:]...)
var out bytes.Buffer
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
println(fmt.Sprintf("Error running cmd '%s'", c))
println(out.String())
os.Exit(1)
}
}
func isOlder(src string, than string) bool {
stat, err := os.Stat(src)
// If the file does not exist, it is not older.
if os.IsNotExist(err) {
return false
}
statThan, _ := os.Stat(than)
return stat.ModTime().After(statThan.ModTime())
}
func checkExecutable(bin string) {
path, err := exec.LookPath(bin)
if err != nil {
fmt.Println(fmt.Sprintf("Could not find executable '%s'", bin))
os.Exit(1)
}
fmt.Println(fmt.Sprintf("Found '%s' -> '%s'", bin, path))
}
func cwd() string {
cwd, _ := os.Getwd()
cwd += "/"
return cwd
}
func convertEPStoPDF(folder string) {
items, _ := ioutil.ReadDir(folder)
for _, item := range items {
if item.IsDir() {
convertEPStoPDF(folder + item.Name() + "/")
} else {
if !strings.HasSuffix(item.Name(), ".eps") {
continue
}
src := folder + item.Name()
dst := folder + item.Name()
dst = strings.ReplaceAll(dst, ".eps", ".pdf")
if isOlder(dst, src) {
continue
}
cmd := epsToPdfBin + " " + src
run(cmd)
}
}
}
func convertSVGtoPNG(folder string) {
items, _ := ioutil.ReadDir(folder)
for _, item := range items {
if item.IsDir() {
println("Directories in ", folder, " are not supported")
os.Exit(1)
} else {
if !strings.HasSuffix(item.Name(), ".svg") {
continue
}
src := folder + item.Name()
dirs := []string{cwd() + "src/screenshots/", cwd() + "src/screenshots_300dpi/"}
dpis := []string{"100", "300"}
for i, dir := range dirs {
dst := dir + item.Name()
dst = strings.ReplaceAll(dst, ".svg", ".png")
if isOlder(dst, src) {
continue
}
cmd := inkscapeBin + ` --export-filename=` + dst + ` --export-dpi=` + dpis[i] + ` ` + src
run(cmd)
}
}
}
}
func toString(fs []string) string {
var b bytes.Buffer
for _, s := range fs {
fmt.Fprintf(&b, "%s ", s)
}
return b.String()
}
func makeCover(src string, dst string) {
src = cwd() + src
dst = cwd() + dst
if isOlder(dst, src) {
return
}
bin := inkscapeBin
args := make([]string, 0)
args = append(args, "--export-filename="+dst)
args = append(args, "--export-dpi=300")
args = append(args, "--export-type=pdf")
args = append(args, "--export-text-to-path")
args = append(args, src)
fmt.Printf("%s %s\n", inkscapeBin, toString(args))
output, err := exec.Command(bin, args...).CombinedOutput()
if err != nil {
fmt.Println("%s %s", string(output), err)
return
}
}
func currentDir() string {
cwd, err := os.Getwd()
if err != nil {
panic(err.Error())
}
return path.Base(cwd)
}
func getMode() string {
var args = os.Args
if len(args) > 1 {
return args[1]
}
return mode
}
func main() {
mode = getMode()
fmt.Println("Building in", mode, "mode...")
checkExecutable(inkscapeBin)
checkExecutable(epsToPdfBin)
if mode != "debug" && mode != "release" && mode != "print" {
fmt.Println("Mode must be either 'debug' or 'release' or 'print'.")
return
}
os.MkdirAll(outputDirName, os.ModePerm)
// Convert SVG to PNG
convertSVGtoPNG(cwd() + "src/screenshots_svg/")
// Convert EPS to PDF
convertEPStoPDF(cwd())
// Make front and back cover (this is only used in the non-print version)
os.MkdirAll(outputDirName+"/cover", os.ModePerm)
if mode != "print" {
makeCover("cover/master/front.svg", outputDirName+"/cover/front.pdf")
makeCover("cover/master/back.svg", outputDirName+"/cover/back.pdf")
}
compileOptions := `\def\for` + mode + `{}`
bin := "pdflatex"
arg0 := "-output-directory"
arg1 := outputDirName
arg2 := compileOptions + ` \input{src/book.tex}`
draftMode := "-draftmode"
var err error
var out []byte
// Compile in draft mode to generate only necessary files for Table of Contents
if mode != "debug" {
fmt.Println(bin, draftMode, arg0, arg1, arg2)
_, err = exec.Command(bin, draftMode, arg0, arg1, arg2).CombinedOutput()
}
// Full Compile to generate PDF
if err == nil {
fmt.Println(bin, arg0, arg1, arg2)
out, err = exec.Command(bin, arg0, arg1, arg2).CombinedOutput()
}
if err != nil {
fmt.Println("%s %s", string(out), err)
}
// Rename
var src = outputDirName + "/book.pdf"
var dst = outputDirName + "/" + currentDir() + "_" + getMode() + ".pdf"
os.Rename(src, dst)
}