mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 10:41:14 +08:00
Support git init. Get author details.
This commit is contained in:
parent
9e6ae4d762
commit
aa271d6498
@ -7,8 +7,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/leaanthony/clir"
|
"github.com/leaanthony/clir"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/wailsapp/wails/v2/internal/templates"
|
"github.com/wailsapp/wails/v2/internal/templates"
|
||||||
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
"github.com/wailsapp/wails/v2/pkg/clilogger"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/git"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddSubcommand adds the `init` command for the Wails application
|
// AddSubcommand adds the `init` command for the Wails application
|
||||||
@ -39,6 +41,13 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
|||||||
quiet := false
|
quiet := false
|
||||||
command.BoolFlag("q", "Supress output to console", &quiet)
|
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 project files
|
||||||
vscode := false
|
vscode := false
|
||||||
command.BoolFlag("vscode", "Generate VSCode project files", &vscode)
|
command.BoolFlag("vscode", "Generate VSCode project files", &vscode)
|
||||||
@ -92,6 +101,13 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
|||||||
TemplateName: templateName,
|
TemplateName: templateName,
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
GenerateVSCode: vscode,
|
GenerateVSCode: vscode,
|
||||||
|
InitGit: initGit,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to discover author details from git config
|
||||||
|
err := findAuthorDetails(options)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return initProject(options)
|
return initProject(options)
|
||||||
@ -112,6 +128,13 @@ func initProject(options *templates.Options) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if options.InitGit {
|
||||||
|
err = initGit(options)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Output stats
|
// Output stats
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
options.Logger.Println("")
|
options.Logger.Println("")
|
||||||
@ -122,9 +145,41 @@ func initProject(options *templates.Options) error {
|
|||||||
if options.GenerateVSCode {
|
if options.GenerateVSCode {
|
||||||
options.Logger.Println("VSCode config files generated.")
|
options.Logger.Println("VSCode config files generated.")
|
||||||
}
|
}
|
||||||
|
if options.InitGit {
|
||||||
|
options.Logger.Println("Git repository initialised.")
|
||||||
|
}
|
||||||
options.Logger.Println("")
|
options.Logger.Println("")
|
||||||
options.Logger.Println(fmt.Sprintf("Initialised project '%s' in %s.", options.ProjectName, elapsed.Round(time.Millisecond).String()))
|
options.Logger.Println(fmt.Sprintf("Initialised project '%s' in %s.", options.ProjectName, elapsed.Round(time.Millisecond).String()))
|
||||||
options.Logger.Println("")
|
options.Logger.Println("")
|
||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
@ -26,7 +26,8 @@ type Data struct {
|
|||||||
BinaryName string
|
BinaryName string
|
||||||
WailsVersion string
|
WailsVersion string
|
||||||
NPMProjectName string
|
NPMProjectName string
|
||||||
Author string
|
AuthorName string
|
||||||
|
AuthorEmail string
|
||||||
WailsDirectory string
|
WailsDirectory string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,6 +41,9 @@ type Options struct {
|
|||||||
GenerateVSCode bool
|
GenerateVSCode bool
|
||||||
PathToDesktopBinary string
|
PathToDesktopBinary string
|
||||||
PathToServerBinary string
|
PathToServerBinary string
|
||||||
|
InitGit bool
|
||||||
|
AuthorName string
|
||||||
|
AuthorEmail string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Template holds data relating to a template
|
// Template holds data relating to a template
|
||||||
@ -224,6 +228,8 @@ func Install(options *Options) error {
|
|||||||
BinaryName: filepath.Base(options.TargetDir),
|
BinaryName: filepath.Base(options.TargetDir),
|
||||||
NPMProjectName: NPMProjectName,
|
NPMProjectName: NPMProjectName,
|
||||||
WailsDirectory: localWailsDirectory,
|
WailsDirectory: localWailsDirectory,
|
||||||
|
AuthorEmail: options.AuthorEmail,
|
||||||
|
AuthorName: options.AuthorName,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the template
|
// Extract the template
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "{{.ProjectName}}",
|
"name": "{{.ProjectName}}",
|
||||||
"outputfilename": "{{.BinaryName}}",
|
"outputfilename": "{{.BinaryName}}",
|
||||||
"html": "frontend/index.html"
|
"html": "frontend/index.html",
|
||||||
|
"author": {
|
||||||
|
"name": "{{.AuthorName}}",
|
||||||
|
"email": "{{.AuthorEmail}}"
|
||||||
|
}
|
||||||
}
|
}
|
38
v2/pkg/git/git.go
Normal file
38
v2/pkg/git/git.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user