mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 06:20:48 +08:00
Removed prompt library requirements
This commit is contained in:
parent
03c479c890
commit
94e9447e1c
107
cmd/project.go
107
cmd/project.go
@ -6,8 +6,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/AlecAivazis/survey.v1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type author struct {
|
type author struct {
|
||||||
@ -120,36 +118,36 @@ func (ph *ProjectHelper) NewProjectOptions() *ProjectOptions {
|
|||||||
return &result
|
return &result
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectQuestion creates a new select type question for Survey
|
// // SelectQuestion creates a new select type question for Survey
|
||||||
func SelectQuestion(name, message string, options []string, defaultValue string, required bool) *survey.Question {
|
// func SelectQuestion(name, message string, options []string, defaultValue string, required bool) *survey.Question {
|
||||||
result := survey.Question{
|
// result := survey.Question{
|
||||||
Name: name,
|
// Name: name,
|
||||||
Prompt: &survey.Select{
|
// Prompt: &survey.Select{
|
||||||
Message: message,
|
// Message: message,
|
||||||
Options: options,
|
// Options: options,
|
||||||
Default: defaultValue,
|
// Default: defaultValue,
|
||||||
},
|
// },
|
||||||
}
|
// }
|
||||||
if required {
|
// if required {
|
||||||
result.Validate = survey.Required
|
// result.Validate = survey.Required
|
||||||
}
|
// }
|
||||||
return &result
|
// return &result
|
||||||
}
|
// }
|
||||||
|
|
||||||
// InputQuestion creates a new input type question for Survey
|
// InputQuestion creates a new input type question for Survey
|
||||||
func InputQuestion(name, message string, defaultValue string, required bool) *survey.Question {
|
// func InputQuestion(name, message string, defaultValue string, required bool) *survey.Question {
|
||||||
result := survey.Question{
|
// result := survey.Question{
|
||||||
Name: name,
|
// Name: name,
|
||||||
Prompt: &survey.Input{
|
// Prompt: &survey.Input{
|
||||||
Message: message + ":",
|
// Message: message + ":",
|
||||||
Default: defaultValue,
|
// Default: defaultValue,
|
||||||
},
|
// },
|
||||||
}
|
// }
|
||||||
if required {
|
// if required {
|
||||||
result.Validate = survey.Required
|
// result.Validate = survey.Required
|
||||||
}
|
// }
|
||||||
return &result
|
// return &result
|
||||||
}
|
// }
|
||||||
|
|
||||||
// ProjectOptions holds all the options available for a project
|
// ProjectOptions holds all the options available for a project
|
||||||
type ProjectOptions struct {
|
type ProjectOptions struct {
|
||||||
@ -177,13 +175,11 @@ func (po *ProjectOptions) Defaults() {
|
|||||||
// PromptForInputs asks the user to input project details
|
// PromptForInputs asks the user to input project details
|
||||||
func (po *ProjectOptions) PromptForInputs() error {
|
func (po *ProjectOptions) PromptForInputs() error {
|
||||||
|
|
||||||
var questions []*survey.Question
|
processProjectName(po)
|
||||||
|
|
||||||
processProjectName(po.Name, &questions)
|
processBinaryName(po)
|
||||||
|
|
||||||
processBinaryName(po.BinaryName, po.Name, &questions)
|
err := processOutputDirectory(po)
|
||||||
|
|
||||||
err := processOutputDirectory(po.OutputDirectory, &questions)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -212,15 +208,12 @@ func (po *ProjectOptions) PromptForInputs() error {
|
|||||||
if po.Template != "" {
|
if po.Template != "" {
|
||||||
if _, ok := templateDetails[po.Template]; !ok {
|
if _, ok := templateDetails[po.Template]; !ok {
|
||||||
po.log.Error("Template '%s' invalid.", po.Template)
|
po.log.Error("Template '%s' invalid.", po.Template)
|
||||||
questions = append(questions, SelectQuestion("Template", "Select template", templates, templates[0], true))
|
templateSelected := PromptSelection("Select template", templates)
|
||||||
|
po.Template = templates[templateSelected]
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
questions = append(questions, SelectQuestion("Template", "Select template", templates, templates[0], true))
|
templateSelected := PromptSelection("Select template", templates)
|
||||||
}
|
po.Template = templates[templateSelected]
|
||||||
|
|
||||||
err = survey.Ask(questions, po)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup NPM Project name
|
// Setup NPM Project name
|
||||||
@ -281,10 +274,10 @@ func computeBinaryName(projectName string) string {
|
|||||||
return binaryNameComputed
|
return binaryNameComputed
|
||||||
}
|
}
|
||||||
|
|
||||||
func processOutputDirectory(outputDirectory string, questions *[]*survey.Question) error {
|
func processOutputDirectory(po *ProjectOptions) error {
|
||||||
|
// po.OutputDirectory
|
||||||
if outputDirectory != "" {
|
if po.OutputDirectory != "" {
|
||||||
projectPath, err := filepath.Abs(outputDirectory)
|
projectPath, err := filepath.Abs(po.OutputDirectory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -293,27 +286,27 @@ func processOutputDirectory(outputDirectory string, questions *[]*survey.Questio
|
|||||||
return fmt.Errorf("directory '%s' already exists", projectPath)
|
return fmt.Errorf("directory '%s' already exists", projectPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Project Directory: " + outputDirectory)
|
fmt.Println("Project Directory: " + po.OutputDirectory)
|
||||||
} else {
|
} else {
|
||||||
*questions = append(*questions, InputQuestion("OutputDirectory", "Project directory name", "", true))
|
po.OutputDirectory = PromptRequired("Project directory name")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processProjectName(name string, questions *[]*survey.Question) {
|
func processProjectName(po *ProjectOptions) {
|
||||||
if name == "" {
|
if po.Name == "" {
|
||||||
*questions = append(*questions, InputQuestion("Name", "The name of the project", "My Project", true))
|
po.Name = Prompt("The name of the project", "My Project")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Project Name: " + name)
|
fmt.Println("Project Name: " + po.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func processBinaryName(binaryName string, name string, questions *[]*survey.Question) {
|
func processBinaryName(po *ProjectOptions) {
|
||||||
if binaryName == "" {
|
if po.BinaryName == "" {
|
||||||
var binaryNameComputed = computeBinaryName(name)
|
var binaryNameComputed = computeBinaryName(po.Name)
|
||||||
*questions = append(*questions, InputQuestion("BinaryName", "The output binary name", binaryNameComputed, true))
|
po.BinaryName = Prompt("The output binary name", binaryNameComputed)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Output binary Name: " + binaryName)
|
fmt.Println("Output binary Name: " + po.BinaryName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
65
cmd/prompt.go
Normal file
65
cmd/prompt.go
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Prompt asks the user for a value
|
||||||
|
func Prompt(question string, defaultValue ...string) string {
|
||||||
|
var answer string
|
||||||
|
haveDefault := len(defaultValue) > 0
|
||||||
|
|
||||||
|
if haveDefault {
|
||||||
|
question = fmt.Sprintf("%s (%s)", question, defaultValue[0])
|
||||||
|
}
|
||||||
|
fmt.Printf(question + ": ")
|
||||||
|
fmt.Scanln(&answer)
|
||||||
|
if haveDefault {
|
||||||
|
if len(answer) == 0 {
|
||||||
|
answer = defaultValue[0]
|
||||||
|
fmt.Println(" -> " + answer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return answer
|
||||||
|
}
|
||||||
|
|
||||||
|
// PromptRequired calls Prompt repeatedly until a value is given
|
||||||
|
func PromptRequired(question string, defaultValue ...string) string {
|
||||||
|
for {
|
||||||
|
result := Prompt(question, defaultValue...)
|
||||||
|
if result != "" {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PromptSelection asks the user to choose an option
|
||||||
|
func PromptSelection(question string, options []string) int {
|
||||||
|
|
||||||
|
fmt.Println(question + ":")
|
||||||
|
for index, option := range options {
|
||||||
|
fmt.Printf(" %d: %s\n", index+1, option)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
selectedValue := -1
|
||||||
|
|
||||||
|
for {
|
||||||
|
choice := Prompt("Please choose an option")
|
||||||
|
|
||||||
|
// index
|
||||||
|
number, err := strconv.Atoi(choice)
|
||||||
|
if err == nil {
|
||||||
|
if number > 0 && number <= len(options) {
|
||||||
|
selectedValue = number - 1
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return selectedValue
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user