Init now adds replace lines to generated projects if the CLI was built locally.

This commit is contained in:
Lea Anthony 2023-03-09 21:26:34 +11:00
commit 261555c191
18 changed files with 135 additions and 10 deletions

View 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
}

View file

@ -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"`
}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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)
}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}

View file

@ -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}}