-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathbuildassets.go
More file actions
162 lines (135 loc) · 4.39 KB
/
Copy pathbuildassets.go
File metadata and controls
162 lines (135 loc) · 4.39 KB
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
package buildassets
import (
"bytes"
"embed"
"errors"
"fmt"
iofs "io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/leaanthony/gosod"
"github.com/samber/lo"
"github.com/wailsapp/wails/v2/internal/fs"
"github.com/wailsapp/wails/v2/internal/project"
)
//go:embed build
var assets embed.FS
// Same as assets but chrooted into /build/
var buildAssets iofs.FS
func init() {
buildAssets = lo.Must(iofs.Sub(assets, "build"))
}
// Install will install all default project assets
func Install(targetDir string) error {
templateDir := gosod.New(assets)
err := templateDir.Extract(targetDir, nil)
if err != nil {
return err
}
return nil
}
// GetLocalPath returns the local path of the requested build asset file
func GetLocalPath(projectData *project.Project, file string) string {
return filepath.Clean(filepath.Join(projectData.GetBuildDir(), filepath.FromSlash(file)))
}
// ReadFile reads the file from the project build folder.
// If the file does not exist it falls back to the embedded file and the file will be written
// to the disk for customisation.
func ReadFile(projectData *project.Project, file string) ([]byte, error) {
localFilePath := GetLocalPath(projectData, file)
content, err := os.ReadFile(localFilePath)
if errors.Is(err, iofs.ErrNotExist) {
// The file does not exist, let's read it from the assets FS and write it to disk
content, err := iofs.ReadFile(buildAssets, file)
if err != nil {
return nil, err
}
if err := writeFileSystemFile(projectData, file, content); err != nil {
return nil, fmt.Errorf("Unable to create file in build folder: %s", err)
}
return content, nil
}
return content, err
}
// ReadFileWithProjectData reads the file from the project build folder and replaces ProjectInfo if necessary.
// If the file does not exist it falls back to the embedded file and the file will be written
// to the disk for customisation. The file written is the original unresolved one.
func ReadFileWithProjectData(projectData *project.Project, file string) ([]byte, error) {
content, err := ReadFile(projectData, file)
if err != nil {
return nil, err
}
content, err = resolveProjectData(content, projectData)
if err != nil {
return nil, fmt.Errorf("Unable to resolve data in %s: %w", file, err)
}
return content, nil
}
// ReadOriginalFileWithProjectDataAndSave reads the file from the embedded assets and replaces
// ProjectInfo if necessary.
// It will also write the resolved final file back to the project build folder.
func ReadOriginalFileWithProjectDataAndSave(projectData *project.Project, file string) ([]byte, error) {
content, err := iofs.ReadFile(buildAssets, file)
if err != nil {
return nil, fmt.Errorf("Unable to read file %s: %w", file, err)
}
content, err = resolveProjectData(content, projectData)
if err != nil {
return nil, fmt.Errorf("Unable to resolve data in %s: %w", file, err)
}
if err := writeFileSystemFile(projectData, file, content); err != nil {
return nil, fmt.Errorf("Unable to create file in build folder: %w", err)
}
return content, nil
}
type assetData struct {
Name string
Info project.Info
OutputFilename string
}
func xmlEscape(s string) string {
var buf bytes.Buffer
template.HTMLEscape(&buf, []byte(s))
return buf.String()
}
var nonAlphaNum = regexp.MustCompile(`[^A-Za-z0-9]+`)
func safeBundleID(s string) string {
result := strings.ToLower(nonAlphaNum.ReplaceAllString(s, "-"))
result = strings.Trim(result, "-")
return result
}
func resolveProjectData(content []byte, projectData *project.Project) ([]byte, error) {
funcMap := template.FuncMap{
"xml": xmlEscape,
"safeBundleID": safeBundleID,
}
tmpl, err := template.New("").Funcs(funcMap).Parse(string(content))
if err != nil {
return nil, err
}
data := &assetData{
Name: projectData.Name,
Info: projectData.Info,
OutputFilename: projectData.OutputFilename,
}
var out bytes.Buffer
if err := tmpl.Execute(&out, data); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func writeFileSystemFile(projectData *project.Project, file string, content []byte) error {
targetPath := GetLocalPath(projectData, file)
if dir := filepath.Dir(targetPath); !fs.DirExists(dir) {
if err := fs.MkDirs(dir, 0o755); err != nil {
return fmt.Errorf("Unable to create directory: %w", err)
}
}
if err := os.WriteFile(targetPath, content, 0o644); err != nil {
return err
}
return nil
}