5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 19:50:15 +08:00

Merge pull request #40 from wailsapp/fix-linting-issues

Fix linting issues
This commit is contained in:
Lea Anthony 2019-02-05 08:45:08 +11:00 committed by GitHub
commit eb0d4bc42f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 195 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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)
})
}