From aa271d649836983f7c5ec991c8ecd393e9e8e68e Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sat, 21 Nov 2020 11:43:13 +1100 Subject: [PATCH] Support git init. Get author details. --- .../commands/initialise/initialise.go | 55 +++++++++++++++++++ v2/internal/templates/templates.go | 8 ++- .../templates/vanilla/wails.tmpl.json | 6 +- v2/pkg/git/git.go | 38 +++++++++++++ 4 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 v2/pkg/git/git.go diff --git a/v2/cmd/wails/internal/commands/initialise/initialise.go b/v2/cmd/wails/internal/commands/initialise/initialise.go index 8652d412f..cf0d49505 100644 --- a/v2/cmd/wails/internal/commands/initialise/initialise.go +++ b/v2/cmd/wails/internal/commands/initialise/initialise.go @@ -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 +} diff --git a/v2/internal/templates/templates.go b/v2/internal/templates/templates.go index ea7363456..c0891ebe0 100644 --- a/v2/internal/templates/templates.go +++ b/v2/internal/templates/templates.go @@ -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 diff --git a/v2/internal/templates/templates/vanilla/wails.tmpl.json b/v2/internal/templates/templates/vanilla/wails.tmpl.json index 0386d2ba3..1cf6df051 100644 --- a/v2/internal/templates/templates/vanilla/wails.tmpl.json +++ b/v2/internal/templates/templates/vanilla/wails.tmpl.json @@ -1,5 +1,9 @@ { "name": "{{.ProjectName}}", "outputfilename": "{{.BinaryName}}", - "html": "frontend/index.html" + "html": "frontend/index.html", + "author": { + "name": "{{.AuthorName}}", + "email": "{{.AuthorEmail}}" + } } \ No newline at end of file diff --git a/v2/pkg/git/git.go b/v2/pkg/git/git.go new file mode 100644 index 000000000..844fd452f --- /dev/null +++ b/v2/pkg/git/git.go @@ -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 +}