-
Notifications
You must be signed in to change notification settings - Fork 19
/
compile_assets.go
82 lines (67 loc) · 1.65 KB
/
compile_assets.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
package main
import (
"os"
"path/filepath"
"regexp"
"strings"
)
func compileAsset(rootPath, outputPath, packageName string) {
rootPathArr := strings.Split(rootPath, "assets")
if len(rootPathArr) > 0 {
listContent := `package ` + packageName + `
var AssetsList = []string{
`
pathsContent := `package ` + packageName + `
var AssetPaths = map[string]string{
`
fileNames, err := getAllFiles(rootPath)
if err != nil {
return
}
for _, name := range fileNames {
listContent += ` "` + rootPathArr[1] + strings.ReplaceAll(name, rootPath, "")[1:] + `",
`
ext := filepath.Ext(name)
if ext == ".css" || ext == ".js" {
fileName := filepath.Base(name)
reg, _ := regexp.Compile(".min.(.*?)" + ext)
pathsContent += ` "` + reg.ReplaceAllString(fileName, ".min"+ext) + `":"` +
rootPathArr[1] + strings.ReplaceAll(name, rootPath, "")[1:] + `",
`
}
}
pathsContent += `
}`
listContent += `
}`
err = os.WriteFile(outputPath+"/assets_list.go", []byte(listContent), 0644)
if err != nil {
return
}
err = os.WriteFile(outputPath+"/assets_path.go", []byte(pathsContent), 0644)
if err != nil {
return
}
}
}
func getAllFiles(dirPth string) (files []string, err error) {
var dirs []string
dir, err := os.ReadDir(dirPth)
if err != nil {
return nil, err
}
PthSep := string(os.PathSeparator)
for _, fi := range dir {
if fi.IsDir() {
dirs = append(dirs, dirPth+PthSep+fi.Name())
_, _ = getAllFiles(dirPth + PthSep + fi.Name())
} else {
files = append(files, dirPth+PthSep+fi.Name())
}
}
for _, table := range dirs {
temp, _ := getAllFiles(table)
files = append(files, temp...)
}
return files, nil
}