-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresskit.go
252 lines (216 loc) · 5.3 KB
/
presskit.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
247
248
249
250
251
252
package presskit
import (
"errors"
"io/ioutil"
"os"
//"github.com/sanity-io/litter"
)
// Presskit is created by the caller script and wraps all generation functions
type Presskit struct {
// parser manages the parsing from the data file (e.g. company.xml) to a company object
Parser Parser
// input paths contains the company data, games and assets
InputPath string
// output path contains all generated data
OutputPath string
// if enabled, the output folder will removed and overwritten instead of a error
ForceMode bool
// use original dopresskit style
ClassicStyle bool
}
// Generate handles the whole generation process
func (p Presskit) Generate() error {
// delete previous outputs if forced
if p.ForceMode {
os.RemoveAll(p.OutputPath)
}
// set template names for classic style
if p.ClassicStyle {
GameTemplate = "classic_game"
CompanyTemplate = "classic_company"
}
// setup base folder structure
err := p.setupOutputFolder(p.OutputPath, []string{"zip", "css", "js"})
if err != nil {
return err
}
// add file to tell gh-pages not to use jekyll
// _header, _icon, _logo not rendered there if not generated
err = writeFile(join(p.OutputPath, ".nojekyll"), []byte{})
if err != nil {
return err
}
// copy favicon
err = p.copyFavicon()
if err != nil {
return err
}
// parse company data file
fileData, err := ioutil.ReadFile(join(p.InputPath, "company."+p.Parser.Extension()))
if err != nil {
return err
}
c, err := p.Parser.Company(fileData)
if err != nil {
return err
}
// storage of game slug and title (for releases section in company)
gameList := make(map[string]string)
// process games
files, err := ioutil.ReadDir(join(p.InputPath, "games"))
if err != nil {
return err
}
for _, f := range files {
g, err := p.processGame(f, *c)
if err != nil {
return err
}
// add game to game list
gameList[f.Name()] = g.Title
}
// process company
err = p.processCompany(*c, gameList)
if err != nil {
return err
}
// generate static files (css etc)
err = p.generateStaticFiles()
if err != nil {
return err
}
return nil
}
// processCompany processes the company level
func (p Presskit) processCompany(c company, gl map[string]string) error {
// copy media directories
for _, d := range []string{"images", "videos"} {
copyDir(
join(p.InputPath, d),
join(p.OutputPath, d),
)
}
// used medias
media, err := newMedia(p.OutputPath, c)
if err != nil {
return err
}
// generate image zip files
err = media.generateZip(p.OutputPath)
if err != nil {
return err
}
// render html
html, err := renderCompany(c, *media, gl)
if err != nil {
return err
}
err = writeFile(join(p.OutputPath, "index.html"), html)
if err != nil {
return err
}
return nil
}
// processGame processes a game level
func (p Presskit) processGame(f os.FileInfo, c company) (*game, error) {
// path of the game data
gameInputPath := join(p.InputPath, "games", f.Name())
gameOutputPath := join(p.OutputPath, "games", f.Name())
// setup base folder structure
err := p.setupOutputFolder(gameOutputPath, []string{"zip"})
if err != nil {
return nil, err
}
// copy media directories
for _, d := range []string{"images", "videos"} {
copyDir(
join(gameInputPath, d),
join(gameOutputPath, d),
)
}
// parse data file
fileData, err := ioutil.ReadFile(join(gameInputPath, "game."+p.Parser.Extension()))
if err != nil {
return nil, err
}
g, err := p.Parser.Game(fileData)
if err != nil {
return nil, err
}
// used medias
media, err := newMedia(gameOutputPath, *g)
if err != nil {
return nil, err
}
// generate image zip files
err = media.generateZip(gameOutputPath)
if err != nil {
return nil, err
}
// render html
html, err := renderGame(c, *g, *media)
if err != nil {
return nil, err
}
err = writeFile(join(gameOutputPath, "index.html"), html)
if err != nil {
return nil, err
}
return g, nil
}
// setupOutputFolder creates the output folder and a few subdirs.
// used by company and games
func (p Presskit) setupOutputFolder(outputPath string, folders []string) error {
// don't override
if _, err := os.Stat(outputPath); !os.IsNotExist(err) {
return errors.New("output path already exists")
}
// create necessary subpaths
for _, f := range folders {
err := os.MkdirAll(join(outputPath, f), 0700)
if err != nil {
return err
}
}
return nil
}
// generateStaticFiles generates all needed files stored binary
func (p Presskit) generateStaticFiles() error {
files := []string{"css/uikit.gradient.min.css", "js/mansory.min.js", "js/script.js"}
customStyle := "css/style.css"
if p.ClassicStyle {
customStyle = "css/style_classic.css"
}
files = append(files, customStyle)
for _, path := range files {
// get content
raw, err := Asset(path)
if err != nil {
return err
}
// write file
err = writeFile(join(p.OutputPath, path), raw)
if err != nil {
return err
}
}
return nil
}
// copyFavicon copies given favicon or the default one
func (p Presskit) copyFavicon() error {
faviconPath := join(p.OutputPath, "favicon.ico")
// try to copy
err := copyFile(join(p.InputPath, "favicon.ico"), faviconPath)
// if no icon found, write default favicon
if err != nil {
icoData, err := Asset("favicon.ico")
if err != nil {
return err
}
err = writeFile(faviconPath, icoData)
if err != nil {
return err
}
}
return nil
}