From 4b480bb085a3b087cb5b9a556a0d5fcdf7fd3eba Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 18 Apr 2021 17:10:12 +1000 Subject: [PATCH] Move templates to CLI dir. Use go:embed --- .../commands/initialise/initialise.go | 3 +- .../templates/ides/vscode/launch.json.tmpl | 0 .../templates/ides/vscode/tasks.json.tmpl | 0 .../initialise}/templates/templates.go | 58 +++++++++++-------- .../templates/templates/vanilla/basic.tmpl.go | 0 .../templates/vanilla/frontend/src/index.html | 0 .../templates/vanilla/frontend/src/main.css | 0 .../templates/vanilla/frontend/src/main.js | 0 .../templates/templates/vanilla/go.mod.tmpl | 0 .../templates/templates/vanilla/main.tmpl.go | 0 .../templates/templates/vanilla/template.json | 0 .../templates/vanilla/wails.tmpl.json | 0 .../initialise/templates/templates_test.go | 46 +++++++++++++++ v2/go.mod | 3 +- v2/go.sum | 6 +- v2/internal/fs/fs.go | 4 -- v2/internal/templates/templates_test.go | 20 ------- 17 files changed, 88 insertions(+), 52 deletions(-) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/ides/vscode/launch.json.tmpl (100%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/ides/vscode/tasks.json.tmpl (100%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/templates.go (89%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/templates/vanilla/basic.tmpl.go (100%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/templates/vanilla/frontend/src/index.html (100%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/templates/vanilla/frontend/src/main.css (100%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/templates/vanilla/frontend/src/main.js (100%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/templates/vanilla/go.mod.tmpl (100%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/templates/vanilla/main.tmpl.go (100%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/templates/vanilla/template.json (100%) rename v2/{internal => cmd/wails/internal/commands/initialise}/templates/templates/vanilla/wails.tmpl.json (100%) create mode 100644 v2/cmd/wails/internal/commands/initialise/templates/templates_test.go delete mode 100644 v2/internal/templates/templates_test.go diff --git a/v2/cmd/wails/internal/commands/initialise/initialise.go b/v2/cmd/wails/internal/commands/initialise/initialise.go index 9b20a0e8b..04063d740 100644 --- a/v2/cmd/wails/internal/commands/initialise/initialise.go +++ b/v2/cmd/wails/internal/commands/initialise/initialise.go @@ -6,9 +6,10 @@ import ( "strings" "time" + "github.com/wailsapp/wails/v2/cmd/wails/internal/commands/initialise/templates" + "github.com/leaanthony/clir" "github.com/pkg/errors" - "github.com/wailsapp/wails/v2/internal/templates" "github.com/wailsapp/wails/v2/pkg/clilogger" "github.com/wailsapp/wails/v2/pkg/git" ) diff --git a/v2/internal/templates/ides/vscode/launch.json.tmpl b/v2/cmd/wails/internal/commands/initialise/templates/ides/vscode/launch.json.tmpl similarity index 100% rename from v2/internal/templates/ides/vscode/launch.json.tmpl rename to v2/cmd/wails/internal/commands/initialise/templates/ides/vscode/launch.json.tmpl diff --git a/v2/internal/templates/ides/vscode/tasks.json.tmpl b/v2/cmd/wails/internal/commands/initialise/templates/ides/vscode/tasks.json.tmpl similarity index 100% rename from v2/internal/templates/ides/vscode/tasks.json.tmpl rename to v2/cmd/wails/internal/commands/initialise/templates/ides/vscode/tasks.json.tmpl diff --git a/v2/internal/templates/templates.go b/v2/cmd/wails/internal/commands/initialise/templates/templates.go similarity index 89% rename from v2/internal/templates/templates.go rename to v2/cmd/wails/internal/commands/initialise/templates/templates.go index 6250aa007..9a57907f6 100644 --- a/v2/internal/templates/templates.go +++ b/v2/cmd/wails/internal/commands/initialise/templates/templates.go @@ -1,14 +1,16 @@ package templates import ( + "embed" "encoding/json" "fmt" - "io/ioutil" + gofs "io/fs" "os" "path/filepath" "runtime" "strings" + "github.com/leaanthony/debme" "github.com/leaanthony/gosod" "github.com/leaanthony/slicer" "github.com/olekukonko/tablewriter" @@ -16,6 +18,12 @@ import ( "github.com/wailsapp/wails/v2/pkg/clilogger" ) +//go:embed templates +var templates embed.FS + +//go:embed ides/* +var ides embed.FS + // Cahce for the templates // We use this because we need different views of the same data var templateCache []Template = nil @@ -59,20 +67,21 @@ type Template struct { HelpURL string `json:"helpurl"` // Other data - Directory string `json:"-"` + FS gofs.FS `json:"-"` } -func parseTemplate(directory string) (Template, error) { - templateJSON := filepath.Join(directory, "template.json") +func parseTemplate(template gofs.FS) (Template, error) { var result Template - data, err := ioutil.ReadFile(templateJSON) + data, err := gofs.ReadFile(template, "template.json") if err != nil { return result, err } - - result.Directory = directory err = json.Unmarshal(data, &result) - return result, err + if err != nil { + return result, err + } + result.FS = template + return result, nil } // TemplateShortNames returns a slicer of short template names @@ -134,11 +143,13 @@ func getTemplateByShortname(shortname string) (Template, error) { // Loads the template cache func loadTemplateCache() error { - // Get local template directory - templateDir := fs.RelativePath("templates") + templatesFS, err := debme.FS(templates, "templates") + if err != nil { + return err + } // Get directories - files, err := ioutil.ReadDir(templateDir) + files, err := templatesFS.ReadDir(".") if err != nil { return err } @@ -148,8 +159,11 @@ func loadTemplateCache() error { for _, file := range files { if file.IsDir() { - templateDir := filepath.Join(templateDir, file.Name()) - template, err := parseTemplate(templateDir) + templateFS, err := templatesFS.FS(file.Name()) + if err != nil { + return err + } + template, err := parseTemplate(templateFS) if err != nil { // Cannot parse this template, continue continue @@ -163,7 +177,6 @@ func loadTemplateCache() error { // Install the given template func Install(options *Options) error { - // Get cwd cwd, err := os.Getwd() if err != nil { @@ -211,19 +224,16 @@ func Install(options *Options) error { } // Use Gosod to install the template - installer, err := gosod.TemplateDir(template.Directory) - if err != nil { - return err - } + installer := gosod.New(template.FS) // Ignore template.json files - installer.IgnoreFilename("template.json") + installer.IgnoreFile("template.json") // Setup the data. // We use the directory name for the binary name, like Go BinaryName := filepath.Base(options.TargetDir) NPMProjectName := strings.ToLower(strings.ReplaceAll(BinaryName, " ", "")) - localWailsDirectory := fs.RelativePath("../..") + localWailsDirectory := fs.RelativePath("../../../../../..") templateData := &Data{ ProjectName: options.ProjectName, BinaryName: filepath.Base(options.TargetDir), @@ -295,14 +305,14 @@ func generateIDEFiles(options *Options) error { func generateVSCodeFiles(options *Options) error { targetDir := filepath.Join(options.TargetDir, ".vscode") - sourceDir := fs.RelativePath(filepath.Join("./ides/vscode")) - - // Use Gosod to install the template - installer, err := gosod.TemplateDir(sourceDir) + source, err := debme.FS(ides, "ides/vscode") if err != nil { return err } + // Use gosod to install the template + installer := gosod.New(source) + binaryName := filepath.Base(options.TargetDir) if runtime.GOOS == "windows" { // yay windows diff --git a/v2/internal/templates/templates/vanilla/basic.tmpl.go b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/basic.tmpl.go similarity index 100% rename from v2/internal/templates/templates/vanilla/basic.tmpl.go rename to v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/basic.tmpl.go diff --git a/v2/internal/templates/templates/vanilla/frontend/src/index.html b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/index.html similarity index 100% rename from v2/internal/templates/templates/vanilla/frontend/src/index.html rename to v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/index.html diff --git a/v2/internal/templates/templates/vanilla/frontend/src/main.css b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/main.css similarity index 100% rename from v2/internal/templates/templates/vanilla/frontend/src/main.css rename to v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/main.css diff --git a/v2/internal/templates/templates/vanilla/frontend/src/main.js b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/main.js similarity index 100% rename from v2/internal/templates/templates/vanilla/frontend/src/main.js rename to v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/frontend/src/main.js diff --git a/v2/internal/templates/templates/vanilla/go.mod.tmpl b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/go.mod.tmpl similarity index 100% rename from v2/internal/templates/templates/vanilla/go.mod.tmpl rename to v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/go.mod.tmpl diff --git a/v2/internal/templates/templates/vanilla/main.tmpl.go b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/main.tmpl.go similarity index 100% rename from v2/internal/templates/templates/vanilla/main.tmpl.go rename to v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/main.tmpl.go diff --git a/v2/internal/templates/templates/vanilla/template.json b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/template.json similarity index 100% rename from v2/internal/templates/templates/vanilla/template.json rename to v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/template.json diff --git a/v2/internal/templates/templates/vanilla/wails.tmpl.json b/v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/wails.tmpl.json similarity index 100% rename from v2/internal/templates/templates/vanilla/wails.tmpl.json rename to v2/cmd/wails/internal/commands/initialise/templates/templates/vanilla/wails.tmpl.json diff --git a/v2/cmd/wails/internal/commands/initialise/templates/templates_test.go b/v2/cmd/wails/internal/commands/initialise/templates/templates_test.go new file mode 100644 index 000000000..02a191f38 --- /dev/null +++ b/v2/cmd/wails/internal/commands/initialise/templates/templates_test.go @@ -0,0 +1,46 @@ +package templates + +import ( + "fmt" + "testing" + + "github.com/matryer/is" +) + +func TestList(t *testing.T) { + + is2 := is.New(t) + templates, err := List() + is2.NoErr(err) + + println("Found these templates:") + for _, template := range templates { + fmt.Printf("%+v\n", template) + } +} + +func TestShortname(t *testing.T) { + + is2 := is.New(t) + + template, err := getTemplateByShortname("vanilla") + is2.NoErr(err) + + println("Found this template:") + fmt.Printf("%+v\n", template) +} + +func TestInstall(t *testing.T) { + + is2 := is.New(t) + + options := &Options{ + ProjectName: "test", + TemplateName: "vanilla", + AuthorName: "Lea Anthony", + AuthorEmail: "lea.anthony@gmail.com", + } + + err := Install(options) + is2.NoErr(err) +} diff --git a/v2/go.mod b/v2/go.mod index c539925de..f6d8ce84d 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -10,8 +10,9 @@ require ( github.com/imdario/mergo v0.3.11 github.com/jackmordaunt/icns v1.0.0 github.com/leaanthony/clir v1.0.4 + github.com/leaanthony/debme v1.1.1 github.com/leaanthony/go-ansi-parser v1.0.1 - github.com/leaanthony/gosod v0.0.4 + github.com/leaanthony/gosod v1.0.1 github.com/leaanthony/slicer v1.5.0 github.com/matryer/is v1.4.0 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect diff --git a/v2/go.sum b/v2/go.sum index 6cde95b69..c1db157f5 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -41,10 +41,12 @@ github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eT github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/leaanthony/clir v1.0.4 h1:Dov2y9zWJmZr7CjaCe86lKa4b5CSxskGAt2yBkoDyiU= github.com/leaanthony/clir v1.0.4/go.mod h1:k/RBkdkFl18xkkACMCLt09bhiZnrGORoxmomeMvDpE0= +github.com/leaanthony/debme v1.1.1 h1:2CQjJkfrjr4b2VgpDmkq2aghem5R2bNbg1Yg5cKQGBQ= +github.com/leaanthony/debme v1.1.1/go.mod h1:3V+sCm5tYAgQymvSOfYQ5Xx2JCr+OXiD9Jkw3otUjiA= github.com/leaanthony/go-ansi-parser v1.0.1 h1:97v6c5kYppVsbScf4r/VZdXyQ21KQIfeQOk2DgKxGG4= github.com/leaanthony/go-ansi-parser v1.0.1/go.mod h1:7arTzgVI47srICYhvgUV4CGd063sGEeoSlych5yeSPM= -github.com/leaanthony/gosod v0.0.4 h1:v4hepo4IyL8E8c9qzDsvYcA0KGh7Npf8As74K5ibQpI= -github.com/leaanthony/gosod v0.0.4/go.mod h1:nGMCb1PJfXwBDbOAike78jEYlpqge+xUKFf0iBKjKxU= +github.com/leaanthony/gosod v1.0.1 h1:F+4c3DmEBfigi7oAswCV2RpQ+k4DcNbhuCZUGdBHacQ= +github.com/leaanthony/gosod v1.0.1/go.mod h1:W8RyeSFBXu7RpIxPGEJfW4moSyGGEjlJMLV25wEbAdU= github.com/leaanthony/slicer v1.5.0 h1:aHYTN8xbCCLxJmkNKiLB6tgcMARl4eWmH9/F+S/0HtY= github.com/leaanthony/slicer v1.5.0/go.mod h1:FwrApmf8gOrpzEWM2J/9Lh79tyq8KTX5AzRtwV7m4AY= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= diff --git a/v2/internal/fs/fs.go b/v2/internal/fs/fs.go index ec339e83b..d49b22f26 100644 --- a/v2/internal/fs/fs.go +++ b/v2/internal/fs/fs.go @@ -201,10 +201,6 @@ func GetSubdirectories(rootDir string) (*slicer.StringSlicer, error) { func DirIsEmpty(dir string) (bool, error) { - if !DirExists(dir) { - return false, fmt.Errorf("DirIsEmpty called with a non-existant directory: %s", dir) - } - // CREDIT: https://stackoverflow.com/a/30708914/8325411 f, err := os.Open(dir) if err != nil { diff --git a/v2/internal/templates/templates_test.go b/v2/internal/templates/templates_test.go deleted file mode 100644 index 60302defd..000000000 --- a/v2/internal/templates/templates_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package templates - -import ( - "fmt" - "testing" - - "github.com/matryer/is" -) - -func TestList(t *testing.T) { - - is := is.New(t) - templates, err := List() - is.Equal(err, nil) - - println("Found these templates:") - for _, template := range templates { - fmt.Printf("%+v\n", template) - } -}