mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 22:50:59 +08:00
Merge pull request #40 from wailsapp/fix-linting-issues
Fix linting issues
This commit is contained in:
commit
eb0d4bc42f
100
cmd/helpers.go
100
cmd/helpers.go
@ -4,9 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/leaanthony/slicer"
|
"github.com/leaanthony/slicer"
|
||||||
"github.com/leaanthony/spinner"
|
"github.com/leaanthony/spinner"
|
||||||
@ -30,6 +32,7 @@ func ValidateFrontendConfig(projectOptions *ProjectOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InstallGoDependencies will run go get in the current directory
|
||||||
func InstallGoDependencies() error {
|
func InstallGoDependencies() error {
|
||||||
depSpinner := spinner.New("Installing Dependencies...")
|
depSpinner := spinner.New("Installing Dependencies...")
|
||||||
depSpinner.SetSpinSpeed(50)
|
depSpinner.SetSpinSpeed(50)
|
||||||
@ -43,6 +46,7 @@ func InstallGoDependencies() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BuildApplication will attempt to build the project based on the given inputs
|
||||||
func BuildApplication(binaryName string, forceRebuild bool, buildMode string) error {
|
func BuildApplication(binaryName string, forceRebuild bool, buildMode string) error {
|
||||||
compileMessage := "Packing + Compiling project"
|
compileMessage := "Packing + Compiling project"
|
||||||
if buildMode == "debug" {
|
if buildMode == "debug" {
|
||||||
@ -76,6 +80,7 @@ func BuildApplication(binaryName string, forceRebuild bool, buildMode string) er
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PackageApplication will attempt to package the application in a pltform dependent way
|
||||||
func PackageApplication(projectOptions *ProjectOptions) error {
|
func PackageApplication(projectOptions *ProjectOptions) error {
|
||||||
// Package app
|
// Package app
|
||||||
packageSpinner := spinner.New("Packaging Application")
|
packageSpinner := spinner.New("Packaging Application")
|
||||||
@ -90,6 +95,7 @@ func PackageApplication(projectOptions *ProjectOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BuildFrontend runs the given build command
|
||||||
func BuildFrontend(buildCommand string) error {
|
func BuildFrontend(buildCommand string) error {
|
||||||
buildFESpinner := spinner.New("Building frontend...")
|
buildFESpinner := spinner.New("Building frontend...")
|
||||||
buildFESpinner.SetSpinSpeed(50)
|
buildFESpinner.SetSpinSpeed(50)
|
||||||
@ -103,6 +109,7 @@ func BuildFrontend(buildCommand string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckPackr checks if packr is installed and if not, attempts to fetch it
|
||||||
func CheckPackr() (err error) {
|
func CheckPackr() (err error) {
|
||||||
programHelper := NewProgramHelper()
|
programHelper := NewProgramHelper()
|
||||||
if !programHelper.IsInstalled("packr") {
|
if !programHelper.IsInstalled("packr") {
|
||||||
@ -119,6 +126,7 @@ func CheckPackr() (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InstallFrontendDeps attempts to install the frontend dependencies based on the given options
|
||||||
func InstallFrontendDeps(projectDir string, projectOptions *ProjectOptions, forceRebuild bool) error {
|
func InstallFrontendDeps(projectDir string, projectOptions *ProjectOptions, forceRebuild bool) error {
|
||||||
|
|
||||||
// Install frontend deps
|
// Install frontend deps
|
||||||
@ -177,7 +185,7 @@ func InstallFrontendDeps(projectDir string, projectOptions *ProjectOptions, forc
|
|||||||
|
|
||||||
// Copy bridge to project
|
// Copy bridge to project
|
||||||
_, filename, _, _ := runtime.Caller(1)
|
_, 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")
|
bridgeFileTarget := filepath.Join(projectDir, projectOptions.FrontEnd.Dir, projectOptions.FrontEnd.Bridge, "wailsbridge.js")
|
||||||
err = fs.CopyFile(bridgeFileSource, bridgeFileTarget)
|
err = fs.CopyFile(bridgeFileSource, bridgeFileTarget)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -192,74 +200,26 @@ func InstallFrontendDeps(projectDir string, projectOptions *ProjectOptions, forc
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// func CopyBridgeFile(projectDir string, projectOptions ProjectOptions, bridgeMode bool) error {
|
// ServeProject attempts to serve up the current project so that it may be connected to
|
||||||
// // Copy bridge to project
|
// via the Wails bridge
|
||||||
// fs := NewFSHelper()
|
func ServeProject(projectOptions *ProjectOptions, logger *Logger) error {
|
||||||
// var bridgeFile = "wailsbridge.prod.js"
|
go func() {
|
||||||
// if bridgeMode {
|
time.Sleep(2 * time.Second)
|
||||||
// bridgeFile = "wailsbridge.js"
|
logger.Green(">>>>> To connect, you will need to run '" + projectOptions.FrontEnd.Serve + "' in the '" + projectOptions.FrontEnd.Dir + "' directory <<<<<")
|
||||||
// }
|
}()
|
||||||
// _, filename, _, _ := runtime.Caller(1)
|
location, err := filepath.Abs(projectOptions.BinaryName)
|
||||||
// bridgeFileSource := filepath.Join(path.Dir(filename), "..", "assets", "default", bridgeFile)
|
if err != nil {
|
||||||
// bridgeFileTarget := filepath.Join(projectDir, projectOptions.FrontEnd.Dir, projectOptions.FrontEnd.Bridge, bridgeFile)
|
return err
|
||||||
// err := fs.CopyFile(bridgeFileSource, bridgeFileTarget)
|
}
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func InstallFrontend(projectOptions *ProjectOptions) error {
|
logger.Yellow("Serving Application: " + location)
|
||||||
// // Install frontend deps
|
cmd := exec.Command(location)
|
||||||
// err := os.Chdir(projectOptions.FrontEnd.Dir)
|
cmd.Stdout = os.Stdout
|
||||||
// if err != nil {
|
cmd.Stderr = os.Stderr
|
||||||
// return err
|
err = cmd.Run()
|
||||||
// }
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// // Check if frontend deps have been updated
|
return nil
|
||||||
// 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
|
|
||||||
// }
|
|
||||||
|
@ -178,7 +178,6 @@ func (po *ProjectOptions) Defaults() {
|
|||||||
func (po *ProjectOptions) PromptForInputs() error {
|
func (po *ProjectOptions) PromptForInputs() error {
|
||||||
|
|
||||||
var questions []*survey.Question
|
var questions []*survey.Question
|
||||||
fs := NewFSHelper()
|
|
||||||
|
|
||||||
if po.Name == "" {
|
if po.Name == "" {
|
||||||
questions = append(questions, InputQuestion("Name", "The name of the project", "My Project", true))
|
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 == "" {
|
if po.BinaryName == "" {
|
||||||
var binaryNameComputed string
|
var binaryNameComputed = computeBinaryName(po.Name)
|
||||||
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)
|
|
||||||
}
|
|
||||||
questions = append(questions, InputQuestion("BinaryName", "The output binary name", binaryNameComputed, true))
|
questions = append(questions, InputQuestion("BinaryName", "The output binary name", binaryNameComputed, true))
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Output binary Name: " + po.BinaryName)
|
fmt.Println("Output binary Name: " + po.BinaryName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if po.OutputDirectory != "" {
|
err := processOutputDirectory(po.OutputDirectory, &questions)
|
||||||
projectPath, err := filepath.Abs(po.OutputDirectory)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
templateDetails, err := po.templates.GetTemplateDetails()
|
templateDetails, err := po.templates.GetTemplateDetails()
|
||||||
@ -320,3 +303,33 @@ func (po *ProjectOptions) LoadConfig(projectDir string) error {
|
|||||||
}
|
}
|
||||||
return json.Unmarshal(rawBytes, po)
|
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
|
||||||
|
}
|
||||||
|
@ -49,8 +49,6 @@ func init() {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check pre-requisites are installed
|
|
||||||
|
|
||||||
// Program checker
|
// Program checker
|
||||||
program := cmd.NewProgramHelper()
|
program := cmd.NewProgramHelper()
|
||||||
|
|
||||||
@ -61,7 +59,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// packr
|
// Check Packr is installed
|
||||||
err = cmd.CheckPackr()
|
err = cmd.CheckPackr()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -2,13 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/leaanthony/spinner"
|
"github.com/leaanthony/spinner"
|
||||||
"github.com/wailsapp/wails/cmd"
|
"github.com/wailsapp/wails/cmd"
|
||||||
@ -51,8 +45,6 @@ func init() {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check pre-requisites are installed
|
|
||||||
|
|
||||||
// Program checker
|
// Program checker
|
||||||
program := cmd.NewProgramHelper()
|
program := cmd.NewProgramHelper()
|
||||||
|
|
||||||
@ -63,15 +55,10 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// packr
|
// Check Packr is installed
|
||||||
if !program.IsInstalled("packr") {
|
err = cmd.CheckPackr()
|
||||||
buildSpinner.Start("Installing packr...")
|
if err != nil {
|
||||||
err := program.InstallGoPackage("github.com/gobuffalo/packr/...")
|
return err
|
||||||
if err != nil {
|
|
||||||
buildSpinner.Error()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
buildSpinner.Success()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save project directory
|
// Save project directory
|
||||||
@ -79,70 +66,7 @@ func init() {
|
|||||||
|
|
||||||
// Install deps
|
// Install deps
|
||||||
if projectOptions.FrontEnd != nil {
|
if projectOptions.FrontEnd != nil {
|
||||||
// Install frontend deps
|
err = cmd.InstallFrontendDeps(projectDir, projectOptions, forceRebuild)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -167,25 +91,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.Yellow("Awesome! Project '%s' built!", projectOptions.Name)
|
logger.Yellow("Awesome! Project '%s' built!", projectOptions.Name)
|
||||||
|
return cmd.ServeProject(projectOptions, logger)
|
||||||
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
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user