diff --git a/cmd/helpers.go b/cmd/helpers.go index f7814c947..d378ebb68 100644 --- a/cmd/helpers.go +++ b/cmd/helpers.go @@ -4,9 +4,11 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "path" "path/filepath" "runtime" + "time" "github.com/leaanthony/slicer" "github.com/leaanthony/spinner" @@ -30,6 +32,7 @@ func ValidateFrontendConfig(projectOptions *ProjectOptions) error { return nil } +// InstallGoDependencies will run go get in the current directory func InstallGoDependencies() error { depSpinner := spinner.New("Installing Dependencies...") depSpinner.SetSpinSpeed(50) @@ -43,6 +46,7 @@ func InstallGoDependencies() error { return nil } +// BuildApplication will attempt to build the project based on the given inputs func BuildApplication(binaryName string, forceRebuild bool, buildMode string) error { compileMessage := "Packing + Compiling project" if buildMode == "debug" { @@ -76,6 +80,7 @@ func BuildApplication(binaryName string, forceRebuild bool, buildMode string) er return nil } +// PackageApplication will attempt to package the application in a pltform dependent way func PackageApplication(projectOptions *ProjectOptions) error { // Package app packageSpinner := spinner.New("Packaging Application") @@ -90,6 +95,7 @@ func PackageApplication(projectOptions *ProjectOptions) error { return nil } +// BuildFrontend runs the given build command func BuildFrontend(buildCommand string) error { buildFESpinner := spinner.New("Building frontend...") buildFESpinner.SetSpinSpeed(50) @@ -103,6 +109,7 @@ func BuildFrontend(buildCommand string) error { return nil } +// CheckPackr checks if packr is installed and if not, attempts to fetch it func CheckPackr() (err error) { programHelper := NewProgramHelper() if !programHelper.IsInstalled("packr") { @@ -119,6 +126,7 @@ func CheckPackr() (err error) { return nil } +// InstallFrontendDeps attempts to install the frontend dependencies based on the given options func InstallFrontendDeps(projectDir string, projectOptions *ProjectOptions, forceRebuild bool) error { // Install frontend deps @@ -177,7 +185,7 @@ func InstallFrontendDeps(projectDir string, projectOptions *ProjectOptions, forc // Copy bridge to project _, filename, _, _ := runtime.Caller(1) - bridgeFileSource := filepath.Join(path.Dir(filename), "..", "assets", "default", bridgeFile) + bridgeFileSource := filepath.Join(path.Dir(filename), "..", "..", "assets", "default", bridgeFile) bridgeFileTarget := filepath.Join(projectDir, projectOptions.FrontEnd.Dir, projectOptions.FrontEnd.Bridge, "wailsbridge.js") err = fs.CopyFile(bridgeFileSource, bridgeFileTarget) if err != nil { @@ -192,74 +200,26 @@ func InstallFrontendDeps(projectDir string, projectOptions *ProjectOptions, forc return nil } -// func CopyBridgeFile(projectDir string, projectOptions ProjectOptions, bridgeMode bool) error { -// // Copy bridge to project -// fs := NewFSHelper() -// var bridgeFile = "wailsbridge.prod.js" -// if bridgeMode { -// bridgeFile = "wailsbridge.js" -// } -// _, filename, _, _ := runtime.Caller(1) -// bridgeFileSource := filepath.Join(path.Dir(filename), "..", "assets", "default", bridgeFile) -// bridgeFileTarget := filepath.Join(projectDir, projectOptions.FrontEnd.Dir, projectOptions.FrontEnd.Bridge, bridgeFile) -// err := fs.CopyFile(bridgeFileSource, bridgeFileTarget) -// if err != nil { -// return err -// } -// return nil -// } +// ServeProject attempts to serve up the current project so that it may be connected to +// via the Wails bridge +func ServeProject(projectOptions *ProjectOptions, logger *Logger) error { + go func() { + time.Sleep(2 * time.Second) + logger.Green(">>>>> To connect, you will need to run '" + projectOptions.FrontEnd.Serve + "' in the '" + projectOptions.FrontEnd.Dir + "' directory <<<<<") + }() + location, err := filepath.Abs(projectOptions.BinaryName) + if err != nil { + return err + } -// func InstallFrontend(projectOptions *ProjectOptions) error { -// // Install frontend deps -// err := os.Chdir(projectOptions.FrontEnd.Dir) -// if err != nil { -// return err -// } + logger.Yellow("Serving Application: " + location) + cmd := exec.Command(location) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err = cmd.Run() + if err != nil { + return err + } -// // Check if frontend deps have been updated -// feSpinner := spinner.New("Installing frontend dependencies (This may take a while)...") -// feSpinner.SetSpinSpeed(50) -// feSpinner.Start() - -// requiresNPMInstall := true - -// // Read in package.json MD5 -// fs := NewFSHelper() -// packageJSONMD5, err := fs.FileMD5("package.json") -// if err != nil { -// return err -// } - -// const md5sumFile = "package.json.md5" - -// // If we aren't forcing the install and the md5sum file exists -// if !forceRebuild && fs.FileExists(md5sumFile) { -// // Yes - read contents -// savedMD5sum, err := fs.LoadAsString(md5sumFile) -// // File exists -// if err == nil { -// // Compare md5 -// if savedMD5sum == packageJSONMD5 { -// // Same - no need for reinstall -// requiresNPMInstall = false -// feSpinner.Success("Skipped frontend dependencies (-f to force rebuild)") -// } -// } -// } - -// // Md5 sum package.json -// // Different? Build -// if requiresNPMInstall || forceRebuild { -// // Install dependencies -// err = program.RunCommand(projectOptions.FrontEnd.Install) -// if err != nil { -// feSpinner.Error() -// return err -// } -// feSpinner.Success() - -// // Update md5sum file -// ioutil.WriteFile(md5sumFile, []byte(packageJSONMD5), 0644) -// } -// return nil -// } + return nil +} diff --git a/cmd/project.go b/cmd/project.go index 711dba261..c2ed96098 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -178,7 +178,6 @@ func (po *ProjectOptions) Defaults() { func (po *ProjectOptions) PromptForInputs() error { var questions []*survey.Question - fs := NewFSHelper() if po.Name == "" { questions = append(questions, InputQuestion("Name", "The name of the project", "My Project", true)) @@ -187,31 +186,15 @@ func (po *ProjectOptions) PromptForInputs() error { } if po.BinaryName == "" { - var binaryNameComputed string - if po.Name != "" { - binaryNameComputed = strings.ToLower(po.Name) - binaryNameComputed = strings.Replace(binaryNameComputed, " ", "-", -1) - binaryNameComputed = strings.Replace(binaryNameComputed, string(filepath.Separator), "-", -1) - binaryNameComputed = strings.Replace(binaryNameComputed, ":", "-", -1) - } + var binaryNameComputed = computeBinaryName(po.Name) questions = append(questions, InputQuestion("BinaryName", "The output binary name", binaryNameComputed, true)) } else { fmt.Println("Output binary Name: " + po.BinaryName) } - if po.OutputDirectory != "" { - projectPath, err := filepath.Abs(po.OutputDirectory) - if err != nil { - return err - } - - if fs.DirExists(projectPath) { - return fmt.Errorf("directory '%s' already exists", projectPath) - } - - fmt.Println("Project Directory: " + po.OutputDirectory) - } else { - questions = append(questions, InputQuestion("OutputDirectory", "Project directory name", "", true)) + err := processOutputDirectory(po.OutputDirectory, &questions) + if err != nil { + return err } templateDetails, err := po.templates.GetTemplateDetails() @@ -320,3 +303,33 @@ func (po *ProjectOptions) LoadConfig(projectDir string) error { } return json.Unmarshal(rawBytes, po) } + +func computeBinaryName(projectName string) string { + if projectName == "" { + return "" + } + var binaryNameComputed = strings.ToLower(projectName) + binaryNameComputed = strings.Replace(binaryNameComputed, " ", "-", -1) + binaryNameComputed = strings.Replace(binaryNameComputed, string(filepath.Separator), "-", -1) + binaryNameComputed = strings.Replace(binaryNameComputed, ":", "-", -1) + return binaryNameComputed +} + +func processOutputDirectory(outputDirectory string, questions *[]*survey.Question) error { + + if outputDirectory != "" { + projectPath, err := filepath.Abs(outputDirectory) + if err != nil { + return err + } + + if NewFSHelper().DirExists(projectPath) { + return fmt.Errorf("directory '%s' already exists", projectPath) + } + + fmt.Println("Project Directory: " + outputDirectory) + } else { + *questions = append(*questions, InputQuestion("OutputDirectory", "Project directory name", "", true)) + } + return nil +} diff --git a/cmd/wails/4_build.go b/cmd/wails/4_build.go index 2bb85983f..2631c3253 100644 --- a/cmd/wails/4_build.go +++ b/cmd/wails/4_build.go @@ -49,8 +49,6 @@ func init() { return err } - // Check pre-requisites are installed - // Program checker program := cmd.NewProgramHelper() @@ -61,7 +59,7 @@ func init() { } } - // packr + // Check Packr is installed err = cmd.CheckPackr() if err != nil { return err diff --git a/cmd/wails/6_serve.go b/cmd/wails/6_serve.go index 42c3327ac..0ab41f7d3 100644 --- a/cmd/wails/6_serve.go +++ b/cmd/wails/6_serve.go @@ -2,13 +2,7 @@ package main import ( "fmt" - "io/ioutil" "os" - "os/exec" - "path" - "path/filepath" - "runtime" - "time" "github.com/leaanthony/spinner" "github.com/wailsapp/wails/cmd" @@ -51,8 +45,6 @@ func init() { return err } - // Check pre-requisites are installed - // Program checker program := cmd.NewProgramHelper() @@ -63,15 +55,10 @@ func init() { } } - // packr - if !program.IsInstalled("packr") { - buildSpinner.Start("Installing packr...") - err := program.InstallGoPackage("github.com/gobuffalo/packr/...") - if err != nil { - buildSpinner.Error() - return err - } - buildSpinner.Success() + // Check Packr is installed + err = cmd.CheckPackr() + if err != nil { + return err } // Save project directory @@ -79,70 +66,7 @@ func init() { // Install deps if projectOptions.FrontEnd != nil { - // Install frontend deps - err = os.Chdir(projectOptions.FrontEnd.Dir) - if err != nil { - return err - } - - // Check if frontend deps have been updated - feSpinner := spinner.New("Installing frontend dependencies (This may take a while)...") - feSpinner.SetSpinSpeed(50) - feSpinner.Start() - - requiresNPMInstall := true - - // Read in package.json MD5 - packageJSONMD5, err := fs.FileMD5("package.json") - if err != nil { - return err - } - - const md5sumFile = "package.json.md5" - - // If we aren't forcing the install and the md5sum file exists - if !forceRebuild && fs.FileExists(md5sumFile) { - // Yes - read contents - savedMD5sum, err := fs.LoadAsString(md5sumFile) - // File exists - if err == nil { - // Compare md5 - if savedMD5sum == packageJSONMD5 { - // Same - no need for reinstall - requiresNPMInstall = false - feSpinner.Success("Skipped frontend dependencies (-f to force rebuild)") - } - } - } - - // Md5 sum package.json - // Different? Build - if requiresNPMInstall || forceRebuild { - // Install dependencies - err = program.RunCommand(projectOptions.FrontEnd.Install) - if err != nil { - feSpinner.Error() - return err - } - feSpinner.Success() - - // Update md5sum file - ioutil.WriteFile(md5sumFile, []byte(packageJSONMD5), 0644) - } - - bridgeFile := "wailsbridge.prod.js" - - // Copy bridge to project - _, filename, _, _ := runtime.Caller(1) - bridgeFileSource := filepath.Join(path.Dir(filename), "..", "assets", "default", bridgeFile) - bridgeFileTarget := filepath.Join(projectDir, projectOptions.FrontEnd.Dir, projectOptions.FrontEnd.Bridge, "wailsbridge.js") - err = fs.CopyFile(bridgeFileSource, bridgeFileTarget) - if err != nil { - return err - } - - // Build frontend - err = cmd.BuildFrontend(projectOptions.FrontEnd.Build) + err = cmd.InstallFrontendDeps(projectDir, projectOptions, forceRebuild) if err != nil { return err } @@ -167,25 +91,6 @@ func init() { } logger.Yellow("Awesome! Project '%s' built!", projectOptions.Name) - - go func() { - time.Sleep(2 * time.Second) - logger.Green(">>>>> To connect, you will need to run '" + projectOptions.FrontEnd.Serve + "' in the '" + projectOptions.FrontEnd.Dir + "' directory <<<<<") - }() - location, err := filepath.Abs(projectOptions.BinaryName) - if err != nil { - return err - } - - logger.Yellow("Serving Application: " + location) - cmd := exec.Command(location) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Run() - if err != nil { - return err - } - - return nil + return cmd.ServeProject(projectOptions, logger) }) }