5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 23:20:51 +08:00

Support git init. Get author details.

This commit is contained in:
Lea Anthony 2020-11-21 11:43:13 +11:00
parent 9e6ae4d762
commit aa271d6498
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
4 changed files with 105 additions and 2 deletions

View File

@ -7,8 +7,10 @@ import (
"time"
"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"
)
// AddSubcommand adds the `init` command for the Wails application
@ -39,6 +41,13 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
quiet := false
command.BoolFlag("q", "Supress output to console", &quiet)
initGit := false
gitInstalled := git.IsInstalled()
if gitInstalled {
// Git Init
command.BoolFlag("g", "Initialise git repository", &initGit)
}
// VSCode project files
vscode := false
command.BoolFlag("vscode", "Generate VSCode project files", &vscode)
@ -92,6 +101,13 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
TemplateName: templateName,
Logger: logger,
GenerateVSCode: vscode,
InitGit: initGit,
}
// Try to discover author details from git config
err := findAuthorDetails(options)
if err != nil {
return err
}
return initProject(options)
@ -112,6 +128,13 @@ func initProject(options *templates.Options) error {
return err
}
if options.InitGit {
err = initGit(options)
if err != nil {
return err
}
}
// Output stats
elapsed := time.Since(start)
options.Logger.Println("")
@ -122,9 +145,41 @@ func initProject(options *templates.Options) error {
if options.GenerateVSCode {
options.Logger.Println("VSCode config files generated.")
}
if options.InitGit {
options.Logger.Println("Git repository initialised.")
}
options.Logger.Println("")
options.Logger.Println(fmt.Sprintf("Initialised project '%s' in %s.", options.ProjectName, elapsed.Round(time.Millisecond).String()))
options.Logger.Println("")
return nil
}
func initGit(options *templates.Options) error {
err := git.InitRepo(options.TargetDir)
if err != nil {
return errors.Wrap(err, "Unable to initialise git repository:")
}
return nil
}
func findAuthorDetails(options *templates.Options) error {
if git.IsInstalled() {
name, err := git.Name()
if err != nil {
return err
}
options.AuthorName = strings.TrimSpace(name)
email, err := git.Email()
if err != nil {
return err
}
options.AuthorEmail = strings.TrimSpace(email)
println("Name", name, "Email", email)
}
return nil
}

View File

@ -26,7 +26,8 @@ type Data struct {
BinaryName string
WailsVersion string
NPMProjectName string
Author string
AuthorName string
AuthorEmail string
WailsDirectory string
}
@ -40,6 +41,9 @@ type Options struct {
GenerateVSCode bool
PathToDesktopBinary string
PathToServerBinary string
InitGit bool
AuthorName string
AuthorEmail string
}
// Template holds data relating to a template
@ -224,6 +228,8 @@ func Install(options *Options) error {
BinaryName: filepath.Base(options.TargetDir),
NPMProjectName: NPMProjectName,
WailsDirectory: localWailsDirectory,
AuthorEmail: options.AuthorEmail,
AuthorName: options.AuthorName,
}
// Extract the template

View File

@ -1,5 +1,9 @@
{
"name": "{{.ProjectName}}",
"outputfilename": "{{.BinaryName}}",
"html": "frontend/index.html"
"html": "frontend/index.html",
"author": {
"name": "{{.AuthorName}}",
"email": "{{.AuthorEmail}}"
}
}

38
v2/pkg/git/git.go Normal file
View File

@ -0,0 +1,38 @@
package git
import (
"runtime"
"github.com/wailsapp/wails/v2/internal/shell"
)
func gitcommand() string {
gitcommand := "git"
if runtime.GOOS == "windows" {
gitcommand = "git.exe"
}
return gitcommand
}
// IsInstalled returns true if git is installed for the given platform
func IsInstalled() bool {
return shell.CommandExists(gitcommand())
}
// Email tries to retrieve the
func Email() (string, error) {
stdout, _, err := shell.RunCommand(".", gitcommand(), "config", "user.email")
return stdout, err
}
// Name tries to retrieve the
func Name() (string, error) {
stdout, _, err := shell.RunCommand(".", gitcommand(), "config", "user.name")
return stdout, err
}
func InitRepo(projectDir string) error {
_, _, err := shell.RunCommand(projectDir, gitcommand(), "init")
return err
}