-
Notifications
You must be signed in to change notification settings - Fork 1
/
customization.go
180 lines (142 loc) · 5.3 KB
/
customization.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
// logo generation and theme settings for PWA needs
package main
import (
"crypto/md5"
"encoding/hex"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
cp "github.com/otiai10/copy"
log "github.com/sirupsen/logrus"
)
// code for updating frontend compiled static files, in order to handle customization
// (color and title) settings.
func updateFrontendFiles() {
updateManifestJSON()
updateIndexHTML()
// has to be after index.html and manifest.json updates, so generated checksums match
updateServiceWorkerJS()
generatePWAIcons()
// if static mode, copy files for docker volume share
if *staticMode {
err := cp.Copy(compiledVuePath, "./dist")
if err != nil {
log.Error("Error copying Vue dist files: ", err)
return
}
}
}
const (
pwaIconsFolderPath = compiledVuePath + "/img/icons"
sourceLogoPath = sourceVuePath + "/assets/logo.svg"
sourceSmallLogoPath = sourceVuePath + "/assets/logo_small.svg"
manifestJSON = compiledVuePath + "/manifest.json"
indexHtmlPath = compiledVuePath + "/index.html"
faviconPath = compiledVuePath + "/favicon.ico"
)
type Manifest struct {
Name string `json:"name"`
ShortName string `json:"short_name"`
ThemeColor string `json:"theme_color"`
BackgroundColor string `json:"background_color"`
}
func updateManifestJSON() {
// Read the original manifest.json file
originalManifest, err := readJSONFile(manifestJSON)
if err != nil {
log.Error("Error reading original manifest:", err)
return
}
// JSON object with precedence (overwriting original values)
overrideObject := map[string]interface{}{
"name": config.Customization.Name,
"short_name": config.Customization.Name,
"theme_color": config.Customization.Colors.Theme,
"background_color": config.Customization.Colors.Theme,
}
// Merge the overrideObject into the originalManifest
mergedManifest := mergeJSON(originalManifest, overrideObject)
// Write the merged manifest back to the original file
err = writeJSONFile(manifestJSON, mergedManifest)
if err != nil {
log.Error("Error writing merged manifest: ", err)
return
}
log.Info("manifest.json merged and written successfully!")
}
func updateIndexHTML() {
// Read the entire content of the file
content := readStringFile(indexHtmlPath)
// Perform the replacements
regexpReplaceString(&content, `<meta name="msapplication-TileColor" content="`, `#[0-9a-fA-F]+`, config.Customization.Colors.Theme, `">`)
regexpReplaceString(&content, `<link rel="mask-icon" href="img/icons/safari-pinned-tab.svg" color="`, `#[0-9a-fA-F]+`, config.Customization.Colors.Theme, `">`)
regexpReplaceString(&content, `<meta name="theme-color" content="`, `#[0-9a-fA-F]+`, config.Customization.Colors.Theme, `">`)
regexpReplaceString(&content, "<title>", `.*?`, config.Customization.Name, "</title>")
// Write the modified content back to the same file
writeStringFile(indexHtmlPath, content)
log.Info("index.html replacement completed successfully!")
}
func updateServiceWorkerJS() {
inputFiles := []string{"config.json", "index.html", "manifest.json"}
for _, inputFile := range inputFiles {
configJsonFilePath := compiledVuePath + "/" + inputFile
data, err := os.ReadFile(configJsonFilePath)
if err != nil {
log.Fatal("Error reading file:", err)
}
// Calculate the MD5 sum into a string
hash := md5.Sum(data)
md5sum := hex.EncodeToString(hash[:])
// Define the file path
inputFilePath := compiledVuePath + "/service-worker.js"
// Read the entire content of the file
content := readStringFile(inputFilePath)
// make replacement
regexpReplaceString(&content, `url:"`+inputFile+`",revision:"`, `[0-9a-f]{32}`, md5sum, `"`)
// Write the modified content back to the same file
writeStringFile(inputFilePath, content)
log.Info("service-worker.js: replacement for file '" + inputFile + "' completed successfully!")
// Define the file path
inputFilePath = compiledVuePath + "/service-worker.js.map"
// Read the entire content of the file
content = readStringFile(inputFilePath)
regexpReplaceString(&content, `"url\\": \\"`+inputFile+`\\",\\n \\"revision\\": \\"`, `[0-9a-f]{32}`, md5sum, `\\"`)
// Write the modified content back to the same file
writeStringFile(inputFilePath, content)
log.Info("service-worker.js.map: replacement for file '" + inputFile + "' completed successfully!")
}
}
func extractSize(path string) (int, error) {
_, filename := filepath.Split(path)
// Remove extension
filename = strings.TrimSuffix(filename, filepath.Ext(filename))
// Extract size
size := strings.Split(filename, "-")
sizeNumber := size[len(size)-1]
// Split 'NxN' format and convert to integer
n, err := strconv.Atoi(strings.Split(sizeNumber, "x")[0])
if err != nil {
return 180, err
}
return n, nil
}
func generatePWAIcons() {
convertSvgToFavicons(sourceSmallLogoPath)
paths, _ := listLogoFiles()
for _, path := range paths {
ext := filepath.Ext(path)
if ext != ".png" {
continue
}
size, _ := extractSize(path)
convertSvgToPng(sourceLogoPath, path, size)
}
}
func regexpReplaceString(s *string, prefix string, swapRegex string, swapValue string, sufix string) {
// Define regular expressions for the replacements
re := regexp.MustCompile(prefix + "(" + swapRegex + ")" + sufix)
// Perform the replacements
*s = re.ReplaceAllString(*s, prefix+swapValue+sufix)
}