Compare commits

..

2 Commits

Author SHA1 Message Date
Lea Anthony
a3a13e5a0a Tidy go modules 2020-02-22 14:34:26 +11:00
Lea Anthony
5ea8287f1b Implement verbose output
Works for build and serve
2020-02-22 14:33:27 +11:00
7 changed files with 39 additions and 219 deletions

View File

@@ -12,7 +12,6 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"github.com/leaanthony/slicer" "github.com/leaanthony/slicer"
) )
@@ -48,22 +47,6 @@ func (fs *FSHelper) FileExists(path string) bool {
return fi.Mode().IsRegular() return fi.Mode().IsRegular()
} }
// FindFile returns the first occurrence of match inside path.
func (fs *FSHelper) FindFile(path, match string) (string, error) {
files, err := ioutil.ReadDir(path)
if err != nil {
return "", err
}
for _, f := range files {
if !f.IsDir() && strings.Contains(f.Name(), match) {
return f.Name(), nil
}
}
return "", fmt.Errorf("file not found")
}
// CreateFile creates a file at the given filename location with the contents // CreateFile creates a file at the given filename location with the contents
// set to the given data. It will create intermediary directories if needed. // set to the given data. It will create intermediary directories if needed.
func (fs *FSHelper) CreateFile(filename string, data []byte) error { func (fs *FSHelper) CreateFile(filename string, data []byte) error {

View File

@@ -11,7 +11,6 @@ import (
"time" "time"
"github.com/leaanthony/mewn" "github.com/leaanthony/mewn"
"github.com/leaanthony/mewn/lib"
"github.com/leaanthony/slicer" "github.com/leaanthony/slicer"
"github.com/leaanthony/spinner" "github.com/leaanthony/spinner"
) )
@@ -57,69 +56,23 @@ func InstallGoDependencies(verbose bool) error {
return nil return nil
} }
// EmbedAssets will embed the built frontend assets via mewn.
func EmbedAssets() ([]string, error) {
mewnFiles := lib.GetMewnFiles([]string{}, false)
referencedAssets, err := lib.GetReferencedAssets(mewnFiles)
if err != nil {
return []string{}, err
}
targetFiles := []string{}
for _, referencedAsset := range referencedAssets {
packfileData, err := lib.GeneratePackFileString(referencedAsset, false)
if err != nil {
return []string{}, err
}
targetFile := filepath.Join(referencedAsset.BaseDir, referencedAsset.PackageName+"-mewn.go")
targetFiles = append(targetFiles, targetFile)
ioutil.WriteFile(targetFile, []byte(packfileData), 0644)
}
return targetFiles, nil
}
// BuildApplication will attempt to build the project based on the given inputs // BuildApplication will attempt to build the project based on the given inputs
func BuildApplication(binaryName string, forceRebuild bool, buildMode string, packageApp bool, projectOptions *ProjectOptions) error { func BuildApplication(binaryName string, forceRebuild bool, buildMode string, packageApp bool, projectOptions *ProjectOptions) error {
if buildMode == BuildModeBridge && projectOptions.CrossCompile {
return fmt.Errorf("you cant serve the application in cross-compilation")
}
// Generate Windows assets if needed // Generate Windows assets if needed
if projectOptions.Platform == "windows" { if runtime.GOOS == "windows" {
cleanUp := !packageApp cleanUp := !packageApp
err := NewPackageHelper(projectOptions.Platform).PackageWindows(projectOptions, cleanUp) err := NewPackageHelper().PackageWindows(projectOptions, cleanUp)
if err != nil { if err != nil {
return err return err
} }
} }
if projectOptions.CrossCompile {
// Check build directory
buildDirectory := filepath.Join(fs.Cwd(), "build")
if !fs.DirExists(buildDirectory) {
fs.MkDir(buildDirectory)
}
// Check Docker
if err := CheckIfInstalled("docker"); err != nil {
return err
}
// Check xgo
if err := CheckIfInstalled("xgo"); err != nil {
return err
}
} else {
// Check Mewn is installed // Check Mewn is installed
err := CheckMewn(projectOptions.Verbose) err := CheckMewn(projectOptions.Verbose)
if err != nil { if err != nil {
return err return err
} }
}
compileMessage := "Packing + Compiling project" compileMessage := "Packing + Compiling project"
@@ -136,38 +89,17 @@ func BuildApplication(binaryName string, forceRebuild bool, buildMode string, pa
println(compileMessage) println(compileMessage)
} }
// embed resources
targetFiles, err := EmbedAssets()
if err != nil {
return err
}
// cleanup temporary embedded assets
defer func() {
for _, filename := range targetFiles {
if err := os.Remove(filename); err != nil {
fmt.Println(err)
}
}
}()
buildCommand := slicer.String() buildCommand := slicer.String()
if projectOptions.CrossCompile {
buildCommand.Add("xgo")
} else {
buildCommand.Add("mewn") buildCommand.Add("mewn")
}
if buildMode == BuildModeBridge { if buildMode == BuildModeBridge {
// Ignore errors // Ignore errors
buildCommand.Add("-i") buildCommand.Add("-i")
} }
if !projectOptions.CrossCompile {
buildCommand.Add("build") buildCommand.Add("build")
}
if binaryName != "" && !projectOptions.CrossCompile { if binaryName != "" {
// Alter binary name based on OS // Alter binary name based on OS
switch runtime.GOOS { switch runtime.GOOS {
case "windows": case "windows":
@@ -183,7 +115,7 @@ func BuildApplication(binaryName string, forceRebuild bool, buildMode string, pa
} }
// If we are forcing a rebuild // If we are forcing a rebuild
if forceRebuild && !projectOptions.CrossCompile { if forceRebuild {
buildCommand.Add("-a") buildCommand.Add("-a")
} }
@@ -194,7 +126,7 @@ func BuildApplication(binaryName string, forceRebuild bool, buildMode string, pa
} }
// Add windows flags // Add windows flags
if projectOptions.Platform == "windows" && buildMode == BuildModeProd { if runtime.GOOS == "windows" && buildMode == BuildModeProd {
ldflags += "-H windowsgui " ldflags += "-H windowsgui "
} }
@@ -211,13 +143,6 @@ func BuildApplication(binaryName string, forceRebuild bool, buildMode string, pa
} }
buildCommand.AddSlice([]string{"-ldflags", ldflags}) buildCommand.AddSlice([]string{"-ldflags", ldflags})
if projectOptions.CrossCompile {
buildCommand.Add("-targets", projectOptions.Platform+"/"+projectOptions.Architecture)
buildCommand.Add("-out", "build/"+binaryName)
buildCommand.Add("./")
}
err = NewProgramHelper(projectOptions.Verbose).RunCommandArray(buildCommand.AsSlice()) err = NewProgramHelper(projectOptions.Verbose).RunCommandArray(buildCommand.AsSlice())
if err != nil { if err != nil {
if packSpinner != nil { if packSpinner != nil {
@@ -244,7 +169,7 @@ func BuildApplication(binaryName string, forceRebuild bool, buildMode string, pa
func PackageApplication(projectOptions *ProjectOptions) error { func PackageApplication(projectOptions *ProjectOptions) error {
// Package app // Package app
message := "Generating .app" message := "Generating .app"
if projectOptions.Platform == "windows" { if runtime.GOOS == "windows" {
err := CheckWindres() err := CheckWindres()
if err != nil { if err != nil {
return err return err
@@ -252,15 +177,12 @@ func PackageApplication(projectOptions *ProjectOptions) error {
message = "Generating resource bundle" message = "Generating resource bundle"
} }
var packageSpinner *spinner.Spinner var packageSpinner *spinner.Spinner
if !projectOptions.Verbose { if projectOptions.Verbose {
packageSpinner = spinner.New(message) packageSpinner = spinner.New(message)
packageSpinner.SetSpinSpeed(50) packageSpinner.SetSpinSpeed(50)
packageSpinner.Start() packageSpinner.Start()
} else {
println(message)
} }
err := NewPackageHelper().Package(projectOptions)
err := NewPackageHelper(projectOptions.Platform).Package(projectOptions)
if err != nil { if err != nil {
if packageSpinner != nil { if packageSpinner != nil {
packageSpinner.Error() packageSpinner.Error()
@@ -332,15 +254,6 @@ func CheckWindres() (err error) {
return nil return nil
} }
// CheckIfInstalled returns if application is installed
func CheckIfInstalled(application string) (err error) {
programHelper := NewProgramHelper()
if !programHelper.IsInstalled(application) {
return fmt.Errorf("%s not installed. Ensure you have installed %s correctly", application, application)
}
return nil
}
// InstallFrontendDeps attempts to install the frontend dependencies based on the given options // InstallFrontendDeps attempts to install the frontend dependencies based on the given options
func InstallFrontendDeps(projectDir string, projectOptions *ProjectOptions, forceRebuild bool, caller string) error { func InstallFrontendDeps(projectDir string, projectOptions *ProjectOptions, forceRebuild bool, caller string) error {

View File

@@ -18,16 +18,14 @@ import (
// PackageHelper helps with the 'wails package' command // PackageHelper helps with the 'wails package' command
type PackageHelper struct { type PackageHelper struct {
platform string
fs *FSHelper fs *FSHelper
log *Logger log *Logger
system *SystemHelper system *SystemHelper
} }
// NewPackageHelper creates a new PackageHelper! // NewPackageHelper creates a new PackageHelper!
func NewPackageHelper(platform string) *PackageHelper { func NewPackageHelper() *PackageHelper {
return &PackageHelper{ return &PackageHelper{
platform: platform,
fs: NewFSHelper(), fs: NewFSHelper(),
log: NewLogger(), log: NewLogger(),
system: NewSystemHelper(), system: NewSystemHelper(),
@@ -65,31 +63,24 @@ func defaultString(val string, defaultVal string) string {
func (b *PackageHelper) getPackageFileBaseDir() string { func (b *PackageHelper) getPackageFileBaseDir() string {
// Calculate template base dir // Calculate template base dir
_, filename, _, _ := runtime.Caller(1) _, filename, _, _ := runtime.Caller(1)
return filepath.Join(path.Dir(filename), "packages", b.platform) return filepath.Join(path.Dir(filename), "packages", runtime.GOOS)
} }
// Package the application into a platform specific package // Package the application into a platform specific package
func (b *PackageHelper) Package(po *ProjectOptions) error { func (b *PackageHelper) Package(po *ProjectOptions) error {
switch b.platform { switch runtime.GOOS {
case "darwin": case "darwin":
// Check we have the exe // Check we have the exe
if !b.fs.FileExists(po.BinaryName) { if !b.fs.FileExists(po.BinaryName) {
// Check cross-compiled application
if b.platform == runtime.GOOS {
return fmt.Errorf("cannot bundle non-existent binary file '%s'. Please build with 'wails build' first", po.BinaryName) return fmt.Errorf("cannot bundle non-existent binary file '%s'. Please build with 'wails build' first", po.BinaryName)
} }
if _, err := b.fs.FindFile(path.Join(b.fs.Cwd(), "build"), "darwin"); err != nil {
return fmt.Errorf("cannot bundle non-existent cross-compiled binary file '%s'. Please build with 'wails build -x darwin' first", po.BinaryName)
}
}
return b.packageOSX(po) return b.packageOSX(po)
case "windows": case "windows":
return b.PackageWindows(po, false) return b.PackageWindows(po, false)
case "linux": case "linux":
return fmt.Errorf("linux is not supported at this time. Please see https://github.com/wailsapp/wails/issues/2") return fmt.Errorf("linux is not supported at this time. Please see https://github.com/wailsapp/wails/issues/2")
default: default:
return fmt.Errorf("platform '%s' not supported for bundling yet", b.platform) return fmt.Errorf("platform '%s' not supported for bundling yet", runtime.GOOS)
} }
} }
@@ -112,22 +103,6 @@ func (b *PackageHelper) packageOSX(po *ProjectOptions) error {
// Check binary exists // Check binary exists
source := path.Join(b.fs.Cwd(), exe) source := path.Join(b.fs.Cwd(), exe)
if b.platform != runtime.GOOS {
file, err := b.fs.FindFile(path.Join(b.fs.Cwd(), "build"), "darwin")
if err != nil {
return err
}
// rename to exe
if err := os.Rename(path.Join(b.fs.Cwd(), "build", file), path.Join(b.fs.Cwd(), "build", exe)); err != nil {
return err
}
source = path.Join(b.fs.Cwd(), "build", exe)
}
if !b.fs.FileExists(source) { if !b.fs.FileExists(source) {
// We need to build! // We need to build!
return fmt.Errorf("Target '%s' not available. Has it been compiled yet?", exe) return fmt.Errorf("Target '%s' not available. Has it been compiled yet?", exe)
@@ -217,25 +192,6 @@ func (b *PackageHelper) PackageWindows(po *ProjectOptions, cleanUp bool) error {
// Build syso // Build syso
sysofile := filepath.Join(b.fs.Cwd(), basename+"-res.syso") sysofile := filepath.Join(b.fs.Cwd(), basename+"-res.syso")
// cross-compile
if b.platform != runtime.GOOS {
folder, err := os.Getwd()
if err != nil {
return err
}
args := []string{
"docker", "run", "--rm",
"-v", folder + ":/build",
"--entrypoint", "/bin/sh",
"techknowlogick/xgo",
"-c", "/usr/bin/x86_64-w64-mingw32-windres -o /build/" + basename + "-res.syso /build/" + basename + ".rc",
}
if err := NewProgramHelper().RunCommandArray(args); err != nil {
return err
}
} else {
batfile, err := fs.LocalDir(".") batfile, err := fs.LocalDir(".")
if err != nil { if err != nil {
return err return err
@@ -247,7 +203,6 @@ func (b *PackageHelper) PackageWindows(po *ProjectOptions, cleanUp bool) error {
if err != nil { if err != nil {
return err return err
} }
}
// clean up // clean up
if cleanUp { if cleanUp {

View File

@@ -159,9 +159,6 @@ type ProjectOptions struct {
WailsVersion string WailsVersion string
typescriptDefsFilename string typescriptDefsFilename string
Verbose bool `json:"-"` Verbose bool `json:"-"`
CrossCompile bool `json:"-"`
Platform string `json:"-"`
Architecture string `json:"-"`
} }
// Defaults sets the default project template // Defaults sets the default project template

View File

@@ -3,10 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"runtime"
"strings"
"github.com/leaanthony/slicer"
"github.com/leaanthony/spinner" "github.com/leaanthony/spinner"
"github.com/wailsapp/wails/cmd" "github.com/wailsapp/wails/cmd"
) )
@@ -18,7 +15,6 @@ func init() {
var debugMode = false var debugMode = false
var typescriptFilename = "" var typescriptFilename = ""
var verbose = false var verbose = false
var platform = ""
buildSpinner := spinner.NewSpinner() buildSpinner := spinner.NewSpinner()
buildSpinner.SetSpinSpeed(50) buildSpinner.SetSpinSpeed(50)
@@ -30,8 +26,7 @@ func init() {
BoolFlag("f", "Force rebuild of application components", &forceRebuild). BoolFlag("f", "Force rebuild of application components", &forceRebuild).
BoolFlag("d", "Build in Debug mode", &debugMode). BoolFlag("d", "Build in Debug mode", &debugMode).
BoolFlag("verbose", "Verbose output", &verbose). BoolFlag("verbose", "Verbose output", &verbose).
StringFlag("t", "Generate Typescript definitions to given file (at runtime)", &typescriptFilename). StringFlag("t", "Generate Typescript definitions to given file (at runtime)", &typescriptFilename)
StringFlag("x", "Cross-compile application to specified platform via xgo", &platform)
initCmd.Action(func() error { initCmd.Action(func() error {
@@ -135,29 +130,6 @@ func init() {
buildSpinner.Success() buildSpinner.Success()
} }
// Set cross-compile
projectOptions.Platform = runtime.GOOS
if len(platform) > 0 {
projectOptions.CrossCompile = true
projectOptions.Platform = platform
projectOptions.Architecture = "amd64"
// check build architecture
if strings.Contains(platform, "/") {
p := strings.Split(platform, "/")
projectOptions.Platform = p[0]
projectOptions.Architecture = p[1]
}
// Check supported platforms
supportedPlatforms := slicer.String([]string{"linux/amd64", "linux/386", "windows/amd64", "windows/386", "darwin/amd64"})
targetPlatform := projectOptions.Platform + "/" + projectOptions.Architecture
if !supportedPlatforms.Contains(targetPlatform) {
println("\n*** WARNING: Unsupported target platform", targetPlatform+".", "Supported:", supportedPlatforms.Join(", "))
}
}
err = cmd.BuildApplication(projectOptions.BinaryName, forceRebuild, buildMode, packageApp, projectOptions) err = cmd.BuildApplication(projectOptions.BinaryName, forceRebuild, buildMode, packageApp, projectOptions)
if err != nil { if err != nil {
return err return err

2
go.mod
View File

@@ -10,7 +10,7 @@ require (
github.com/kennygrant/sanitize v1.2.4 github.com/kennygrant/sanitize v1.2.4
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/leaanthony/mewn v0.10.7 github.com/leaanthony/mewn v0.10.7
github.com/leaanthony/slicer v1.4.1 github.com/leaanthony/slicer v1.4.0
github.com/leaanthony/spinner v0.5.3 github.com/leaanthony/spinner v0.5.3
github.com/mattn/go-colorable v0.1.1 // indirect github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect github.com/mattn/go-isatty v0.0.7 // indirect

4
go.sum
View File

@@ -28,8 +28,8 @@ github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/leaanthony/mewn v0.10.7 h1:jCcNJyIUOpwj+I5SuATvCugDjHkoo+j6ubEOxxrxmPA= github.com/leaanthony/mewn v0.10.7 h1:jCcNJyIUOpwj+I5SuATvCugDjHkoo+j6ubEOxxrxmPA=
github.com/leaanthony/mewn v0.10.7/go.mod h1:CRkTx8unLiSSilu/Sd7i1LwrdaAL+3eQ3ses99qGMEQ= github.com/leaanthony/mewn v0.10.7/go.mod h1:CRkTx8unLiSSilu/Sd7i1LwrdaAL+3eQ3ses99qGMEQ=
github.com/leaanthony/slicer v1.4.1 h1:X/SmRIDhkUAolP79mSTO0jTcVX1k504PJBqvV6TwP0w= github.com/leaanthony/slicer v1.4.0 h1:Q9u4w+UBU4WHjXnEDdz+eRLMKF/rnyosRBiqULnc1J8=
github.com/leaanthony/slicer v1.4.1/go.mod h1:FwrApmf8gOrpzEWM2J/9Lh79tyq8KTX5AzRtwV7m4AY= github.com/leaanthony/slicer v1.4.0/go.mod h1:FwrApmf8gOrpzEWM2J/9Lh79tyq8KTX5AzRtwV7m4AY=
github.com/leaanthony/spinner v0.5.3 h1:IMTvgdQCec5QA4qRy0wil4XsRP+QcG1OwLWVK/LPZ5Y= github.com/leaanthony/spinner v0.5.3 h1:IMTvgdQCec5QA4qRy0wil4XsRP+QcG1OwLWVK/LPZ5Y=
github.com/leaanthony/spinner v0.5.3/go.mod h1:oHlrvWicr++CVV7ALWYi+qHk/XNA91D9IJ48IqmpVUo= github.com/leaanthony/spinner v0.5.3/go.mod h1:oHlrvWicr++CVV7ALWYi+qHk/XNA91D9IJ48IqmpVUo=
github.com/leaanthony/synx v0.1.0 h1:R0lmg2w6VMb8XcotOwAe5DLyzwjLrskNkwU7LLWsyL8= github.com/leaanthony/synx v0.1.0 h1:R0lmg2w6VMb8XcotOwAe5DLyzwjLrskNkwU7LLWsyL8=