-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathproject.go
274 lines (231 loc) · 6.88 KB
/
project.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package project
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/samber/lo"
)
// Project holds the data related to a Wails project
type Project struct {
/*** Application Data ***/
Name string `json:"name"`
AssetDirectory string `json:"assetdir,omitempty"`
ReloadDirectories string `json:"reloaddirs,omitempty"`
BuildCommand string `json:"frontend:build"`
InstallCommand string `json:"frontend:install"`
// Commands used in `wails dev`
DevCommand string `json:"frontend:dev"`
DevBuildCommand string `json:"frontend:dev:build"`
DevInstallCommand string `json:"frontend:dev:install"`
DevWatcherCommand string `json:"frontend:dev:watcher"`
// The url of the external wails dev server. If this is set, this server is used for the frontend. Default ""
FrontendDevServerURL string `json:"frontend:dev:serverUrl"`
// Directory to generate the API Module
WailsJSDir string `json:"wailsjsdir"`
Version string `json:"version"`
/*** Internal Data ***/
// The path to the project directory
Path string `json:"projectdir"`
// Build directory
BuildDir string `json:"build:dir"`
// The output filename
OutputFilename string `json:"outputfilename"`
// The type of application. EG: Desktop, Server, etc
OutputType string
// The platform to target
Platform string
// RunNonNativeBuildHooks will run build hooks though they are defined for a GOOS which is not equal to the host os
RunNonNativeBuildHooks bool `json:"runNonNativeBuildHooks"`
// Build hooks for different targets, the hooks are executed in the following order
// Key: GOOS/GOARCH - Executed at build level before/after a build of the specific platform and arch
// Key: GOOS/* - Executed at build level before/after a build of the specific platform
// Key: */* - Executed at build level before/after a build
// The following keys are not yet supported.
// Key: GOOS - Executed at platform level before/after all builds of the specific platform
// Key: * - Executed at platform level before/after all builds of a platform
// Key: [empty] - Executed at global level before/after all builds of all platforms
PostBuildHooks map[string]string `json:"postBuildHooks"`
PreBuildHooks map[string]string `json:"preBuildHooks"`
// The application author
Author Author
// The application information
Info Info
// Fully qualified filename
filename string
// The debounce time for hot-reload of the built-in dev server. Default 100
DebounceMS int `json:"debounceMS"`
// The address to bind the wails dev server to. Default "localhost:34115"
DevServer string `json:"devServer"`
// Arguments that are forward to the application in dev mode
AppArgs string `json:"appargs"`
// NSISType to be build
NSISType string `json:"nsisType"`
// Garble
Obfuscated bool `json:"obfuscated"`
GarbleArgs string `json:"garbleargs"`
// Frontend directory
FrontendDir string `json:"frontend:dir"`
Bindings Bindings `json:"bindings"`
}
func (p *Project) GetFrontendDir() string {
if filepath.IsAbs(p.FrontendDir) {
return p.FrontendDir
}
return filepath.Join(p.Path, p.FrontendDir)
}
func (p *Project) GetWailsJSDir() string {
if filepath.IsAbs(p.WailsJSDir) {
return p.WailsJSDir
}
return filepath.Join(p.Path, p.WailsJSDir)
}
func (p *Project) GetBuildDir() string {
if filepath.IsAbs(p.BuildDir) {
return p.BuildDir
}
return filepath.Join(p.Path, p.BuildDir)
}
func (p *Project) GetDevBuildCommand() string {
if p.DevBuildCommand != "" {
return p.DevBuildCommand
}
if p.DevCommand != "" {
return p.DevCommand
}
return p.BuildCommand
}
func (p *Project) GetDevInstallerCommand() string {
if p.DevInstallCommand != "" {
return p.DevInstallCommand
}
return p.InstallCommand
}
func (p *Project) IsFrontendDevServerURLAutoDiscovery() bool {
return p.FrontendDevServerURL == "auto"
}
func (p *Project) Save() error {
data, err := json.MarshalIndent(p, "", " ")
if err != nil {
return err
}
return os.WriteFile(p.filename, data, 0o755)
}
func (p *Project) setDefaults() {
if p.Path == "" {
p.Path = lo.Must(os.Getwd())
}
if p.Version == "" {
p.Version = "2"
}
// Create default name if not given
if p.Name == "" {
p.Name = "wailsapp"
}
if p.OutputFilename == "" {
p.OutputFilename = p.Name
}
if p.FrontendDir == "" {
p.FrontendDir = "frontend"
}
if p.WailsJSDir == "" {
p.WailsJSDir = p.FrontendDir
}
if p.BuildDir == "" {
p.BuildDir = "build"
}
if p.DebounceMS == 0 {
p.DebounceMS = 100
}
if p.DevServer == "" {
p.DevServer = "localhost:34115"
}
if p.NSISType == "" {
p.NSISType = "multiple"
}
if p.Info.CompanyName == "" {
p.Info.CompanyName = p.Name
}
if p.Info.ProductName == "" {
p.Info.ProductName = p.Name
}
if p.Info.ProductVersion == "" {
p.Info.ProductVersion = "1.0.0"
}
if p.Info.Copyright == nil {
v := "Copyright........."
p.Info.Copyright = &v
}
if p.Info.Comments == nil {
v := "Built using Wails (https://wails.io)"
p.Info.Comments = &v
}
// Fix up OutputFilename
switch runtime.GOOS {
case "windows":
if !strings.HasSuffix(p.OutputFilename, ".exe") {
p.OutputFilename += ".exe"
}
case "darwin", "linux":
p.OutputFilename = strings.TrimSuffix(p.OutputFilename, ".exe")
}
}
// Author stores details about the application author
type Author struct {
Name string `json:"name"`
Email string `json:"email"`
}
type Info struct {
CompanyName string `json:"companyName"`
ProductName string `json:"productName"`
ProductVersion string `json:"productVersion"`
Copyright *string `json:"copyright"`
Comments *string `json:"comments"`
FileAssociations []FileAssociation `json:"fileAssociations"`
Protocols []Protocol `json:"protocols"`
}
type FileAssociation struct {
Ext string `json:"ext"`
Name string `json:"name"`
Description string `json:"description"`
IconName string `json:"iconName"`
Role string `json:"role"`
}
type Protocol struct {
Scheme string `json:"scheme"`
Description string `json:"description"`
Role string `json:"role"`
}
type Bindings struct {
TsGeneration TsGeneration `json:"ts_generation"`
}
type TsGeneration struct {
Prefix string `json:"prefix"`
Suffix string `json:"suffix"`
OutputType string `json:"outputType"`
}
// Parse the given JSON data into a Project struct
func Parse(projectData []byte) (*Project, error) {
project := &Project{}
err := json.Unmarshal(projectData, project)
if err != nil {
return nil, err
}
project.setDefaults()
return project, nil
}
// Load the project from the current working directory
func Load(projectPath string) (*Project, error) {
projectFile := filepath.Join(projectPath, "wails.json")
rawBytes, err := os.ReadFile(projectFile)
if err != nil {
return nil, err
}
result, err := Parse(rawBytes)
if err != nil {
return nil, err
}
result.filename = projectFile
return result, nil
}