diff --git a/v3/internal/debug/debug.go b/v3/internal/debug/debug.go new file mode 100644 index 000000000..47db8b626 --- /dev/null +++ b/v3/internal/debug/debug.go @@ -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 +} diff --git a/v3/internal/flags/init.go b/v3/internal/flags/init.go index d0de09d37..ff12bebd0 100644 --- a/v3/internal/flags/init.go +++ b/v3/internal/flags/init.go @@ -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"` } diff --git a/v3/internal/templates/_base/default/go.mod.tmpl b/v3/internal/templates/_base/default/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/_base/default/go.mod.tmpl +++ b/v3/internal/templates/_base/default/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/lit-ts/go.mod.tmpl b/v3/internal/templates/lit-ts/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/lit-ts/go.mod.tmpl +++ b/v3/internal/templates/lit-ts/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/lit/go.mod.tmpl b/v3/internal/templates/lit/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/lit/go.mod.tmpl +++ b/v3/internal/templates/lit/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/preact-ts/go.mod.tmpl b/v3/internal/templates/preact-ts/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/preact-ts/go.mod.tmpl +++ b/v3/internal/templates/preact-ts/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/preact/go.mod.tmpl b/v3/internal/templates/preact/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/preact/go.mod.tmpl +++ b/v3/internal/templates/preact/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/react-swc-ts/go.mod.tmpl b/v3/internal/templates/react-swc-ts/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/react-swc-ts/go.mod.tmpl +++ b/v3/internal/templates/react-swc-ts/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/react-swc/go.mod.tmpl b/v3/internal/templates/react-swc/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/react-swc/go.mod.tmpl +++ b/v3/internal/templates/react-swc/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/react-ts/go.mod.tmpl b/v3/internal/templates/react-ts/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/react-ts/go.mod.tmpl +++ b/v3/internal/templates/react-ts/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/react/go.mod.tmpl b/v3/internal/templates/react/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/react/go.mod.tmpl +++ b/v3/internal/templates/react/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/svelte-ts/go.mod.tmpl b/v3/internal/templates/svelte-ts/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/svelte-ts/go.mod.tmpl +++ b/v3/internal/templates/svelte-ts/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/svelte/go.mod.tmpl b/v3/internal/templates/svelte/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/svelte/go.mod.tmpl +++ b/v3/internal/templates/svelte/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/templates.go b/v3/internal/templates/templates.go index 465c08833..e0da5a48b 100644 --- a/v3/internal/templates/templates.go +++ b/v3/internal/templates/templates.go @@ -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) } diff --git a/v3/internal/templates/vanilla-ts/go.mod.tmpl b/v3/internal/templates/vanilla-ts/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/vanilla-ts/go.mod.tmpl +++ b/v3/internal/templates/vanilla-ts/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/vanilla/go.mod.tmpl b/v3/internal/templates/vanilla/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/vanilla/go.mod.tmpl +++ b/v3/internal/templates/vanilla/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/vue-ts/go.mod.tmpl b/v3/internal/templates/vue-ts/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/vue-ts/go.mod.tmpl +++ b/v3/internal/templates/vue-ts/go.mod.tmpl @@ -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}} diff --git a/v3/internal/templates/vue/go.mod.tmpl b/v3/internal/templates/vue/go.mod.tmpl index 270f8c6d7..5fbc49867 100644 --- a/v3/internal/templates/vue/go.mod.tmpl +++ b/v3/internal/templates/vue/go.mod.tmpl @@ -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}}