mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 21:43:08 +08:00
feat: multiple template support
This commit is contained in:
parent
cd152f0cd7
commit
b30031d025
23
.vscode/launch.json
vendored
23
.vscode/launch.json
vendored
@ -5,12 +5,33 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch",
|
||||
"name": "Wails Init",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}/cmd/wails/main.go",
|
||||
"env": {},
|
||||
"cwd": "/tmp",
|
||||
"args": [
|
||||
"init",
|
||||
"-name",
|
||||
"runtime",
|
||||
"-dir",
|
||||
"runtime",
|
||||
"-output",
|
||||
"runtime",
|
||||
"-template",
|
||||
"vuebasic"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Wails Update Pre",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}/cmd/wails/main.go",
|
||||
"env": {},
|
||||
"cwd": "/tmp",
|
||||
"args": [
|
||||
"update",
|
||||
"-pre"
|
||||
|
File diff suppressed because one or more lines are too long
55
cmd/fs.go
55
cmd/fs.go
@ -7,7 +7,11 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/leaanthony/slicer"
|
||||
)
|
||||
|
||||
// FSHelper - Wrapper struct for File System utility commands
|
||||
@ -104,11 +108,30 @@ func (fs *FSHelper) RemoveFiles(files []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type Dir struct {
|
||||
localPath string
|
||||
fullPath string
|
||||
}
|
||||
|
||||
func (fs *FSHelper) Dir(dir string) (*Dir, error) {
|
||||
fullPath, err := filepath.Abs(dir)
|
||||
return &Dir{fullPath: fullPath}, err
|
||||
}
|
||||
|
||||
func (fs *FSHelper) LocalDir(dir string) (*Dir, error) {
|
||||
_, filename, _, _ := runtime.Caller(1)
|
||||
fullPath, err := filepath.Abs(filepath.Join(path.Dir(filename), dir))
|
||||
return &Dir{
|
||||
localPath: dir,
|
||||
fullPath: fullPath,
|
||||
}, err
|
||||
}
|
||||
|
||||
// GetSubdirs will return a list of FQPs to subdirectories in the given directory
|
||||
func (fs *FSHelper) GetSubdirs(dir string) (map[string]string, error) {
|
||||
func (d *Dir) GetSubdirs() (map[string]string, error) {
|
||||
|
||||
// Read in the directory information
|
||||
fileInfo, err := ioutil.ReadDir(dir)
|
||||
fileInfo, err := ioutil.ReadDir(d.fullPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -120,12 +143,31 @@ func (fs *FSHelper) GetSubdirs(dir string) (map[string]string, error) {
|
||||
// map["directoryName"] = "path/to/directoryName"
|
||||
for _, file := range fileInfo {
|
||||
if file.IsDir() {
|
||||
subdirs[file.Name()] = filepath.Join(dir, file.Name())
|
||||
subdirs[file.Name()] = filepath.Join(d.fullPath, file.Name())
|
||||
}
|
||||
}
|
||||
return subdirs, nil
|
||||
}
|
||||
|
||||
// GetAllFilenames returns all filename in and below this directory
|
||||
func (d *Dir) GetAllFilenames() (*slicer.StringSlicer, error) {
|
||||
result := slicer.String()
|
||||
err := filepath.Walk(d.fullPath, func(dir string, info os.FileInfo, err error) error {
|
||||
if dir == d.fullPath {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Don't copy template metadata
|
||||
result.Add(dir)
|
||||
|
||||
return nil
|
||||
})
|
||||
return result, err
|
||||
}
|
||||
|
||||
// MkDir creates the given directory.
|
||||
// Returns error on failure
|
||||
func (fs *FSHelper) MkDir(dir string) error {
|
||||
@ -135,10 +177,15 @@ func (fs *FSHelper) MkDir(dir string) error {
|
||||
// LoadAsString will attempt to load the given file and return
|
||||
// its contents as a string
|
||||
func (fs *FSHelper) LoadAsString(filename string) (string, error) {
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
bytes, err := fs.LoadAsBytes(filename)
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
// LoadAsBytes returns the contents of the file as a byte slice
|
||||
func (fs *FSHelper) LoadAsBytes(filename string) ([]byte, error) {
|
||||
return ioutil.ReadFile(filename)
|
||||
}
|
||||
|
||||
// FileMD5 returns the md5sum of the given file
|
||||
func (fs *FSHelper) FileMD5(filename string) (string, error) {
|
||||
f, err := os.Open(filename)
|
||||
|
@ -50,12 +50,6 @@ func NewProjectHelper() *ProjectHelper {
|
||||
// GenerateProject generates a new project using the options given
|
||||
func (ph *ProjectHelper) GenerateProject(projectOptions *ProjectOptions) error {
|
||||
|
||||
// exists := ph.templates.TemplateExists(projectOptions.Template)
|
||||
|
||||
// if !exists {
|
||||
// return fmt.Errorf("template '%s' is invalid", projectOptions.Template)
|
||||
// }
|
||||
|
||||
// Calculate project path
|
||||
projectPath, err := filepath.Abs(projectOptions.OutputDirectory)
|
||||
if err != nil {
|
||||
@ -173,20 +167,42 @@ func (po *ProjectOptions) PromptForInputs() error {
|
||||
// Process Templates
|
||||
templateList := slicer.Interface()
|
||||
options := slicer.String()
|
||||
for _, templateDetails := range po.templates.TemplateList.details {
|
||||
templateList.Add(templateDetails)
|
||||
options.Add(fmt.Sprintf("%s - %s", templateDetails.Metadata.Name, templateDetails.Metadata.ShortDescription))
|
||||
templateDetails, err := po.templates.GetTemplateDetails()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
templateIndex := 0
|
||||
if po.Template != "" {
|
||||
// Check template is valid if given
|
||||
if templateDetails[po.Template] == nil {
|
||||
keys := make([]string, 0, len(templateDetails))
|
||||
for k := range templateDetails {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return fmt.Errorf("invalid template name '%s'. Valid options: %s", po.Template, strings.Join(keys, ", "))
|
||||
}
|
||||
po.selectedTemplate = templateDetails[po.Template]
|
||||
} else {
|
||||
|
||||
if len(options.AsSlice()) > 1 {
|
||||
templateIndex = PromptSelection("Please select a template", options.AsSlice(), 0)
|
||||
for _, templateDetail := range templateDetails {
|
||||
templateList.Add(templateDetail)
|
||||
options.Add(fmt.Sprintf("%s - %s", templateDetail.Metadata.Name, templateDetail.Metadata.ShortDescription))
|
||||
}
|
||||
|
||||
templateIndex := 0
|
||||
|
||||
if len(options.AsSlice()) > 1 {
|
||||
templateIndex = PromptSelection("Please select a template", options.AsSlice(), 0)
|
||||
}
|
||||
|
||||
if len(templateList.AsSlice()) == 0 {
|
||||
return fmt.Errorf("aborting: no templates found")
|
||||
}
|
||||
|
||||
// After selection do this....
|
||||
po.selectedTemplate = templateList.AsSlice()[templateIndex].(*TemplateDetails)
|
||||
}
|
||||
|
||||
// After selection do this....
|
||||
po.selectedTemplate = templateList.AsSlice()[templateIndex].(*TemplateDetails)
|
||||
|
||||
// Setup NPM Project name
|
||||
po.NPMProjectName = strings.ToLower(strings.Replace(po.Name, " ", "_", -1))
|
||||
|
||||
|
149
cmd/templates.go
149
cmd/templates.go
@ -3,20 +3,20 @@ package cmd
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
mewn "github.com/leaanthony/mewn"
|
||||
mewnlib "github.com/leaanthony/mewn/lib"
|
||||
"github.com/leaanthony/slicer"
|
||||
)
|
||||
|
||||
// TemplateMetadata holds all the metadata for a Wails template
|
||||
type TemplateMetadata struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
ShortDescription string `json:"shortdescription"`
|
||||
Description string `json:"description"`
|
||||
Install string `json:"install"`
|
||||
@ -26,92 +26,120 @@ type TemplateMetadata struct {
|
||||
FrontendDir string `json:"frontenddir"`
|
||||
Serve string `json:"serve"`
|
||||
Bridge string `json:"bridge"`
|
||||
WailsDir string `json:"wailsdir"`
|
||||
}
|
||||
|
||||
// TemplateDetails holds information about a specific template
|
||||
type TemplateDetails struct {
|
||||
BasePath string
|
||||
Name string
|
||||
Path string
|
||||
Metadata *TemplateMetadata
|
||||
}
|
||||
|
||||
// TemplateList is a list of available templates
|
||||
type TemplateList struct {
|
||||
details map[string]*TemplateDetails
|
||||
}
|
||||
|
||||
// NewTemplateList creates a new TemplateList object
|
||||
func NewTemplateList(filenames *mewnlib.FileGroup) *TemplateList {
|
||||
// Iterate each template and store information
|
||||
|
||||
result := &TemplateList{details: make(map[string]*TemplateDetails)}
|
||||
|
||||
entries := slicer.String()
|
||||
entries.AddSlice(filenames.Entries())
|
||||
|
||||
// Find all template.json files
|
||||
metadataFiles := entries.Filter(func(filename string) bool {
|
||||
match, _ := regexp.MatchString("(.)+template.json$", filename)
|
||||
return match
|
||||
})
|
||||
|
||||
// Load each metadata file
|
||||
metadataFiles.Each(func(filename string) {
|
||||
fileData := filenames.Bytes(filename)
|
||||
var metadata TemplateMetadata
|
||||
err := json.Unmarshal(fileData, &metadata)
|
||||
if err != nil {
|
||||
log.Fatalf("corrupt metadata for template: %s", filename)
|
||||
}
|
||||
path := strings.Split(filename, "/")[0]
|
||||
thisTemplate := &TemplateDetails{Path: path, Metadata: &metadata}
|
||||
result.details[filename] = thisTemplate
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Template holds details about a Wails template
|
||||
type Template struct {
|
||||
Name string
|
||||
Path string
|
||||
Description string
|
||||
fs *FSHelper
|
||||
}
|
||||
|
||||
// TemplateHelper is a utility object to help with processing templates
|
||||
type TemplateHelper struct {
|
||||
TemplateList *TemplateList
|
||||
Files *mewnlib.FileGroup
|
||||
templateDir *Dir
|
||||
fs *FSHelper
|
||||
metadataFilename string
|
||||
}
|
||||
|
||||
// NewTemplateHelper creates a new template helper
|
||||
func NewTemplateHelper() *TemplateHelper {
|
||||
files := mewn.Group("./templates")
|
||||
|
||||
templateDir, err := fs.LocalDir("./templates")
|
||||
if err != nil {
|
||||
log.Fatal("Unable to find the template directory. Please reinstall Wails.")
|
||||
}
|
||||
|
||||
return &TemplateHelper{
|
||||
TemplateList: NewTemplateList(files),
|
||||
Files: files,
|
||||
templateDir: templateDir,
|
||||
metadataFilename: "template.json",
|
||||
}
|
||||
}
|
||||
|
||||
// LoadMetadata loads the template's 'metadata.json' file
|
||||
func (t *TemplateHelper) LoadMetadata(dir string) (*TemplateMetadata, error) {
|
||||
templateFile := filepath.Join(dir, t.metadataFilename)
|
||||
result := &TemplateMetadata{}
|
||||
if !t.fs.FileExists(templateFile) {
|
||||
return nil, nil
|
||||
}
|
||||
rawJSON, err := ioutil.ReadFile(templateFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(rawJSON, &result)
|
||||
return result, err
|
||||
}
|
||||
|
||||
// GetTemplateDetails returns a map of Template structs containing details
|
||||
// of the found templates
|
||||
func (t *TemplateHelper) GetTemplateDetails() (map[string]*TemplateDetails, error) {
|
||||
|
||||
// Get the subdirectory details
|
||||
templateDirs, err := t.templateDir.GetSubdirs()
|
||||
|
||||
// templateDirs, err := t.fs.GetSubdirs(t.templateDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf("%+v\n", templateDirs)
|
||||
|
||||
result := make(map[string]*TemplateDetails)
|
||||
|
||||
for name, dir := range templateDirs {
|
||||
result[name] = &TemplateDetails{
|
||||
Path: dir,
|
||||
}
|
||||
_ = &TemplateMetadata{}
|
||||
metadata, err := t.LoadMetadata(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[name].Metadata = metadata
|
||||
if metadata.Name != "" {
|
||||
result[name].Name = metadata.Name
|
||||
} else {
|
||||
// Ignore bad templates?
|
||||
result[name] = nil
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetTemplateFilenames returns all the filenames of the given template
|
||||
func (t *TemplateHelper) GetTemplateFilenames(template *TemplateDetails) (*slicer.StringSlicer, error) {
|
||||
|
||||
// Get the subdirectory details
|
||||
templateDir, err := t.fs.Dir(template.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return templateDir.GetAllFilenames()
|
||||
}
|
||||
|
||||
// InstallTemplate installs the template given in the project options to the
|
||||
// project path given
|
||||
func (t *TemplateHelper) InstallTemplate(projectPath string, projectOptions *ProjectOptions) error {
|
||||
|
||||
// Get template files
|
||||
templateFilenames, err := t.GetTemplateFilenames(projectOptions.selectedTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
templatePath := projectOptions.selectedTemplate.Path
|
||||
|
||||
templateFilenames := slicer.String()
|
||||
templateFilenames.AddSlice(projectOptions.templates.Files.Entries())
|
||||
|
||||
templateJSONFilename := filepath.Join(templatePath, "template.json")
|
||||
templateJSONFilename := filepath.Join(templatePath, t.metadataFilename)
|
||||
|
||||
templateFiles := templateFilenames.Filter(func(filename string) bool {
|
||||
filename = filepath.FromSlash(filename)
|
||||
return strings.HasPrefix(filename, templatePath) && filename != templateJSONFilename
|
||||
})
|
||||
|
||||
var err error
|
||||
templateFiles.Each(func(templateFile string) {
|
||||
|
||||
// Setup filenames
|
||||
@ -120,11 +148,14 @@ func (t *TemplateHelper) InstallTemplate(projectPath string, projectOptions *Pro
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filedata := projectOptions.templates.Files.Bytes(templateFile)
|
||||
filedata, err := t.fs.LoadAsBytes(templateFile)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// If file is a template, process it
|
||||
if strings.HasSuffix(templateFile, ".template") {
|
||||
templateData := projectOptions.templates.Files.String(templateFile)
|
||||
templateData := string(filedata)
|
||||
tmpl := template.New(templateFile)
|
||||
tmpl.Parse(templateData)
|
||||
var tpl bytes.Buffer
|
||||
|
21
cmd/templates/vuebasic/.gitignore
vendored
21
cmd/templates/vuebasic/.gitignore
vendored
@ -1,21 +0,0 @@
|
||||
.DS_Store
|
||||
node_modules
|
||||
/dist
|
||||
|
||||
# local env files
|
||||
.env.local
|
||||
.env.*.local
|
||||
|
||||
# Log files
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# Editor directories and files
|
||||
.idea
|
||||
.vscode
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw*
|
@ -17,7 +17,7 @@ Any flags that are required and not given will be prompted for.`
|
||||
LongDescription(commandDescription).
|
||||
BoolFlag("f", "Use defaults", &projectOptions.UseDefaults).
|
||||
StringFlag("dir", "Directory to create project in", &projectOptions.OutputDirectory).
|
||||
// StringFlag("template", "Template name", &projectOptions.Template).
|
||||
StringFlag("template", "Template name", &projectOptions.Template).
|
||||
StringFlag("name", "Project name", &projectOptions.Name).
|
||||
StringFlag("description", "Project description", &projectOptions.Description).
|
||||
StringFlag("output", "Output binary name", &projectOptions.BinaryName)
|
||||
|
Loading…
Reference in New Issue
Block a user