mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 22:55:48 +01:00
Init now adds replace lines to generated projects if the CLI was built locally.
This commit is contained in:
parent
e75e437f6d
commit
261555c191
18 changed files with 135 additions and 10 deletions
56
v3/internal/debug/debug.go
Normal file
56
v3/internal/debug/debug.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package debug
|
||||
|
||||
import (
|
||||
"github.com/samber/lo"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
import "runtime/debug"
|
||||
|
||||
// Why go doesn't provide this as a map already is beyond me.
|
||||
var buildSettings = map[string]string{}
|
||||
var LocalModulePath = ""
|
||||
|
||||
func init() {
|
||||
buildInfo, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
buildSettings = lo.Associate(buildInfo.Settings, func(setting debug.BuildSetting) (string, string) {
|
||||
return setting.Key, setting.Value
|
||||
})
|
||||
if isLocalBuild() {
|
||||
modulePath := RelativePath("..", "..", "..")
|
||||
LocalModulePath, _ = filepath.Abs(modulePath)
|
||||
}
|
||||
}
|
||||
|
||||
func isLocalBuild() bool {
|
||||
return buildSettings["vcs.modified"] == "true"
|
||||
}
|
||||
|
||||
// RelativePath returns a qualified path created by joining the
|
||||
// directory of the calling file and the given relative path.
|
||||
//
|
||||
// Example: RelativePath("..") in *this* file would give you '/path/to/wails2/v2/internal`
|
||||
func RelativePath(relativepath string, optionalpaths ...string) string {
|
||||
_, thisFile, _, _ := runtime.Caller(1)
|
||||
localDir := filepath.Dir(thisFile)
|
||||
|
||||
// If we have optional paths, join them to the relativepath
|
||||
if len(optionalpaths) > 0 {
|
||||
paths := []string{relativepath}
|
||||
paths = append(paths, optionalpaths...)
|
||||
relativepath = filepath.Join(paths...)
|
||||
}
|
||||
result, err := filepath.Abs(filepath.Join(localDir, relativepath))
|
||||
if err != nil {
|
||||
// I'm allowing this for 1 reason only: It's fatal if the path
|
||||
// supplied is wrong as it's only used internally in Wails. If we get
|
||||
// that path wrong, we should know about it immediately. The other reason is
|
||||
// that it cuts down a ton of unnecassary error handling.
|
||||
panic(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
@ -5,10 +5,7 @@ type Init struct {
|
|||
|
||||
TemplateName string `name:"t" description:"Name of built-in template to use, path to template or template url" default:"vanilla"`
|
||||
ProjectName string `name:"n" description:"Name of project" default:""`
|
||||
//CIMode bool `name:"ci" description:"CI Mode"`
|
||||
ProjectDir string `name:"d" description:"Project directory" default:"."`
|
||||
Quiet bool `name:"q" description:"Suppress output to console"`
|
||||
//InitGit bool `name:"g" description:"Initialise git repository"`
|
||||
//IDE string `name:"ide" description:"Generate IDE project files"`
|
||||
List bool `name:"l" description:"List templates"`
|
||||
ProjectDir string `name:"d" description:"Project directory" default:"."`
|
||||
Quiet bool `name:"q" description:"Suppress output to console"`
|
||||
List bool `name:"l" description:"List templates"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package templates
|
|||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"github.com/wailsapp/wails/v3/internal/debug"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
|
|
@ -144,7 +145,17 @@ func GetDefaultTemplates() []TemplateData {
|
|||
return defaultTemplates
|
||||
}
|
||||
|
||||
type TemplateOptions struct {
|
||||
*flags.Init
|
||||
LocalModulePath string
|
||||
}
|
||||
|
||||
func Install(options *flags.Init) error {
|
||||
|
||||
templateData := TemplateOptions{
|
||||
options,
|
||||
debug.LocalModulePath,
|
||||
}
|
||||
template, found := lo.Find(defaultTemplates, func(template TemplateData) bool {
|
||||
return template.Name == options.TemplateName
|
||||
})
|
||||
|
|
@ -153,13 +164,14 @@ func Install(options *flags.Init) error {
|
|||
}
|
||||
|
||||
if options.ProjectDir == "." || options.ProjectDir == "" {
|
||||
options.ProjectDir = lo.Must(os.Getwd())
|
||||
templateData.ProjectDir = lo.Must(os.Getwd())
|
||||
}
|
||||
targetDir := fmt.Sprintf("%s/%s", options.ProjectDir, options.ProjectName)
|
||||
fmt.Printf("Installing template '%s' into '%s'\n", options.TemplateName, targetDir)
|
||||
templateData.ProjectDir = fmt.Sprintf("%s/%s", options.ProjectDir, options.ProjectName)
|
||||
fmt.Printf("Installing template '%s' into '%s'\n", options.TemplateName, options.ProjectDir)
|
||||
tfs, err := fs.Sub(template.FS, options.TemplateName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return gosod.New(tfs).Extract(targetDir, options)
|
||||
|
||||
return gosod.New(tfs).Extract(options.ProjectDir, templateData)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -11,3 +11,7 @@ require (
|
|||
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6 // indirect
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
|
||||
)
|
||||
{{if gt (len .LocalModulePath) 0}}
|
||||
replace github.com/wailsapp/wails/v3 => {{.LocalModulePath}}/v3
|
||||
replace github.com/wailsapp/wails/v2 => {{.LocalModulePath}}/v2
|
||||
{{end}}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue