forked from go101/go101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
191 lines (160 loc) · 4.21 KB
/
gen.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
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
//"errors"
"net/http"
)
const GeneratedFolderName = "generated"
func genStaticFiles(rootURL string) {
log.SetFlags(log.Lshortfile)
wd, err := os.Getwd()
if err != nil {
log.Fatalln("Can't get current path:", err)
}
fullPath := func(relPath ...string) string {
return filepath.Join(append([]string{wd}, relPath...)...)
}
_, err = os.Stat(fullPath("web", "static", "go101"))
if err != nil {
if os.IsNotExist(err) { //errors.Is(err, os.ErrNotExist) {
log.Fatal("File web/static/go101 not found. Not run in go101 folder?")
}
log.Fatal(err)
}
loadFile := func(uri string) []byte {
fullURL := rootURL + uri
res, err := http.Get(fullURL)
if err != nil {
log.Fatalf("Load file %s error: %s", uri, err)
}
content, err := ioutil.ReadAll(res.Body)
log.Println(len(content), fullURL)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
return content
}
readFile := func(path string) []byte {
data, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Read file %s error: %s", path, err)
}
return data
}
readFolder := func(path string) (filenames, subfolders []string) {
fds, err := ioutil.ReadDir(path)
if err != nil {
log.Fatalf("Read folder %s error: %s", path, err)
}
subfolders, filenames = make([]string, 0, len(fds)), make([]string, 0, len(fds))
for _, fd := range fds {
// ignore links, ...
if fd.IsDir() {
subfolders = append(subfolders, filepath.Join(path, fd.Name()))
continue
}
filenames = append(filenames, filepath.Join(path, fd.Name()))
}
return
}
var readFolderRecursively func(path string) (filenames []string)
readFolderRecursively = func(path string) (filenames []string) {
type Folder struct {
Path string
Next *Folder
}
var head, tail *Folder
regSubfolders := func(folders []string) {
if len(folders) == 0 {
return
}
start := 0
if head == nil {
head = &Folder{
Path: folders[0],
}
tail = head
start = 1
}
for i := start; i < len(folders); i++ {
fldr := &Folder{
Path: folders[i],
}
tail.Next = fldr
tail = fldr
}
}
var subfolders []string
filenames, subfolders = readFolder(path)
regSubfolders(subfolders)
for head != nil {
files, subfolders := readFolder(head.Path)
head = head.Next
regSubfolders(subfolders)
filenames = append(filenames, files...)
}
return
}
// collect ...
files := make(map[string][]byte, 128)
files["index.html"] = readFile(fullPath("web", "index.html"))
{
dir := fullPath("articles", "res")
filenames, _ := readFolder(dir)
for _, f := range filenames {
if strings.HasSuffix(f, ".png") || strings.HasSuffix(f, ".jpg") || strings.HasSuffix(f, ".jpeg") {
name, err := filepath.Rel(dir, f)
if err != nil {
log.Fatalf("filepath.Rel(%s, %s) error: %s", dir, f, err)
}
files["article/res/"+name] = readFile(f)
}
}
}
{
dir := fullPath("articles")
filenames, _ := readFolder(dir)
for _, f := range filenames {
if strings.HasSuffix(f, ".html") {
name, err := filepath.Rel(dir, f)
if err != nil {
log.Fatalf("filepath.Rel(%s, %s) error: %s", dir, f, err)
}
files["article/"+name] = loadFile("article/" + name)
}
}
}
{
dir := fullPath("web", "static")
filenames := readFolderRecursively(dir)
for _, f := range filenames {
name, err := filepath.Rel(dir, f)
if err != nil {
log.Fatalf("filepath.Rel(%s, %s) error: %s", dir, f, err)
}
files["static/"+name] = readFile(f)
}
}
// write ...
err = os.RemoveAll(fullPath(GeneratedFolderName))
if err != nil {
log.Fatalf("Remove folder %s error: %s", GeneratedFolderName, err)
}
for name, data := range files {
fullFilename := fullPath(GeneratedFolderName, name)
fullFilename = strings.Replace(fullFilename, "/", string(filepath.Separator), -1)
fullFilename = strings.Replace(fullFilename, "\\", string(filepath.Separator), -1)
if err := os.MkdirAll(filepath.Dir(fullFilename), 0700); err != nil {
log.Fatalln("Mkdir error:", err)
}
if err := ioutil.WriteFile(fullFilename, data, 0644); err != nil {
log.Fatalln("Write file error:", err)
}
log.Printf("Generated %s (size: %d).", name, len(data))
}
}