5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 22:31:06 +08:00

Initial port of packager

This commit is contained in:
Lea Anthony 2019-01-11 21:16:52 +11:00
parent ee355659ce
commit bcca09563c
4 changed files with 62 additions and 45 deletions

View File

@ -16,16 +16,16 @@ import (
"github.com/jackmordaunt/icns" "github.com/jackmordaunt/icns"
) )
// BundleHelper helps with the 'wails bundle' command // PackageHelper helps with the 'wails package' command
type BundleHelper struct { type PackageHelper struct {
fs *FSHelper fs *FSHelper
log *Logger log *Logger
system *SystemHelper system *SystemHelper
} }
// NewBundleHelper creates a new BundleHelper! // NewPackageHelper creates a new PackageHelper!
func NewBundleHelper() *BundleHelper { func NewPackageHelper() *PackageHelper {
return &BundleHelper{ return &PackageHelper{
fs: NewFSHelper(), fs: NewFSHelper(),
log: NewLogger(), log: NewLogger(),
system: NewSystemHelper(), system: NewSystemHelper(),
@ -33,23 +33,23 @@ func NewBundleHelper() *BundleHelper {
} }
type plistData struct { type plistData struct {
Title string Title string
Exe string Exe string
BundleID string PackageID string
Version string Version string
Author string Author string
Date string Date string
} }
func newPlistData(title, exe, bundleID, version, author string) *plistData { func newPlistData(title, exe, packageID, version, author string) *plistData {
now := time.Now().Format(time.RFC822) now := time.Now().Format(time.RFC822)
return &plistData{ return &plistData{
Title: title, Title: title,
Exe: exe, Exe: exe,
Version: version, Version: version,
BundleID: bundleID, PackageID: packageID,
Author: author, Author: author,
Date: now, Date: now,
} }
} }
@ -60,26 +60,32 @@ func defaultString(val string, defaultVal string) string {
return defaultVal return defaultVal
} }
func (b *BundleHelper) getBundleFileBaseDir() string { func (b *PackageHelper) getPackageFileBaseDir() string {
return filepath.Join(b.system.homeDir, "go", "src", "github.com", "wailsapp", "wails", "cmd", "bundle", runtime.GOOS) // Calculate template base dir
_, filename, _, _ := runtime.Caller(1)
return filepath.Join(path.Dir(filename), "packages", runtime.GOOS)
} }
// Bundle the application into a platform specific package // Package the application into a platform specific package
func (b *BundleHelper) Bundle(po *ProjectOptions) error { func (b *PackageHelper) Package(po *ProjectOptions) error {
// Check we have the exe // Check we have the exe
if !b.fs.FileExists(po.BinaryName) { if !b.fs.FileExists(po.BinaryName) {
return fmt.Errorf("cannot bundle non-existant binary file '%s'. Please build with 'wails build' first", po.BinaryName) return fmt.Errorf("cannot bundle non-existant binary file '%s'. Please build with 'wails build' first", po.BinaryName)
} }
switch runtime.GOOS { switch runtime.GOOS {
case "darwin": case "darwin":
return b.bundleOSX(po) return b.packageOSX(po)
case "windows":
return fmt.Errorf("windows is not supported at this time. Please see https://github.com/wailsapp/wails/issues/3")
case "linux":
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", runtime.GOOS) return fmt.Errorf("platform '%s' not supported for bundling yet", runtime.GOOS)
} }
} }
// Bundle the application // Package the application for OSX
func (b *BundleHelper) bundleOSX(po *ProjectOptions) error { func (b *PackageHelper) packageOSX(po *ProjectOptions) error {
system := NewSystemHelper() system := NewSystemHelper()
config, err := system.LoadConfig() config, err := system.LoadConfig()
@ -91,8 +97,8 @@ func (b *BundleHelper) bundleOSX(po *ProjectOptions) error {
exe := defaultString(po.BinaryName, name) exe := defaultString(po.BinaryName, name)
version := defaultString(po.Version, "0.1.0") version := defaultString(po.Version, "0.1.0")
author := defaultString(config.Name, "Anonymous") author := defaultString(config.Name, "Anonymous")
bundleID := strings.Join([]string{"wails", name, version}, ".") packageID := strings.Join([]string{"wails", name, version}, ".")
plistData := newPlistData(name, exe, bundleID, version, author) plistData := newPlistData(name, exe, packageID, version, author)
appname := po.Name + ".app" appname := po.Name + ".app"
// Check binary exists // Check binary exists
@ -102,7 +108,7 @@ func (b *BundleHelper) bundleOSX(po *ProjectOptions) error {
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)
} }
// REmove the existing bundle // Remove the existing package
os.RemoveAll(appname) os.RemoveAll(appname)
exeDir := path.Join(b.fs.Cwd(), appname, "/Contents/MacOS") exeDir := path.Join(b.fs.Cwd(), appname, "/Contents/MacOS")
@ -110,7 +116,7 @@ func (b *BundleHelper) bundleOSX(po *ProjectOptions) error {
resourceDir := path.Join(b.fs.Cwd(), appname, "/Contents/Resources") resourceDir := path.Join(b.fs.Cwd(), appname, "/Contents/Resources")
b.fs.MkDirs(resourceDir, 0755) b.fs.MkDirs(resourceDir, 0755)
tmpl := template.New("infoPlist") tmpl := template.New("infoPlist")
plistFile := filepath.Join(b.getBundleFileBaseDir(), "info.plist") plistFile := filepath.Join(b.getPackageFileBaseDir(), "info.plist")
infoPlist, err := ioutil.ReadFile(plistFile) infoPlist, err := ioutil.ReadFile(plistFile)
if err != nil { if err != nil {
return err return err
@ -140,11 +146,11 @@ func (b *BundleHelper) bundleOSX(po *ProjectOptions) error {
if err != nil { if err != nil {
return err return err
} }
err = b.bundleIcon(resourceDir) err = b.packageIcon(resourceDir)
return err return err
} }
func (b *BundleHelper) bundleIcon(resourceDir string) error { func (b *PackageHelper) packageIcon(resourceDir string) error {
// TODO: Read this from project.json // TODO: Read this from project.json
const appIconFilename = "appicon.png" const appIconFilename = "appicon.png"
@ -155,7 +161,7 @@ func (b *BundleHelper) bundleIcon(resourceDir string) error {
if !b.fs.FileExists(srcIcon) { if !b.fs.FileExists(srcIcon) {
// Install default icon // Install default icon
iconfile := filepath.Join(b.getBundleFileBaseDir(), "icon.png") iconfile := filepath.Join(b.getPackageFileBaseDir(), "icon.png")
iconData, err := ioutil.ReadFile(iconfile) iconData, err := ioutil.ReadFile(iconfile)
if err != nil { if err != nil {
return err return err

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

View File

@ -0,0 +1,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
<key>CFBundleName</key><string>{{.Title}}</string>
<key>CFBundleExecutable</key><string>{{.Exe}}</string>
<key>CFBundleIdentifier</key><string>{{.PackageID}}</string>
<key>CFBundleVersion</key><string>{{.Version}}</string>
<key>CFBundleGetInfoString</key><string>Built by {{.Author}} at {{.Date}} using Wails (https://wails.app)</string>
<key>CFBundleShortVersionString</key><string>{{.Version}}</string>
<key>CFBundleIconFile</key><string>iconfile</string>
<key>NSHighResolutionCapable</key><string>true</string>
</dict></plist>

View File

@ -12,7 +12,7 @@ import (
func init() { func init() {
var bundle = false var packageApp = false
var forceRebuild = false var forceRebuild = false
var releaseMode = false var releaseMode = false
buildSpinner := spinner.NewSpinner() buildSpinner := spinner.NewSpinner()
@ -21,7 +21,7 @@ func init() {
commandDescription := `This command will check to ensure all pre-requistes are installed prior to building. If not, it will attempt to install them. Building comprises of a number of steps: install frontend dependencies, build frontend, pack frontend, compile main application.` commandDescription := `This command will check to ensure all pre-requistes are installed prior to building. If not, it will attempt to install them. Building comprises of a number of steps: install frontend dependencies, build frontend, pack frontend, compile main application.`
initCmd := app.Command("build", "Builds your Wails project"). initCmd := app.Command("build", "Builds your Wails project").
LongDescription(commandDescription). LongDescription(commandDescription).
BoolFlag("b", "Bundle application on successful build", &bundle). BoolFlag("p", "Package application on successful build (Implies -r)", &packageApp).
BoolFlag("f", "Force rebuild of application components", &forceRebuild). BoolFlag("f", "Force rebuild of application components", &forceRebuild).
BoolFlag("r", "Build in Release mode", &releaseMode) BoolFlag("r", "Build in Release mode", &releaseMode)
@ -190,7 +190,7 @@ func init() {
depSpinner.Success() depSpinner.Success()
compileMessage := "Packing + Compiling project" compileMessage := "Packing + Compiling project"
if releaseMode { if releaseMode || packageApp {
compileMessage += " (Release Mode)" compileMessage += " (Release Mode)"
} }
@ -219,7 +219,7 @@ func init() {
} }
// Release mode // Release mode
if releaseMode { if releaseMode || packageApp {
buildCommand.AddSlice([]string{"-ldflags", "-X github.com/wailsapp/wails.DebugMode=false"}) buildCommand.AddSlice([]string{"-ldflags", "-X github.com/wailsapp/wails.DebugMode=false"})
} }
err = program.RunCommandArray(buildCommand.AsSlice()) err = program.RunCommandArray(buildCommand.AsSlice())
@ -229,22 +229,22 @@ func init() {
} }
packSpinner.Success() packSpinner.Success()
if bundle == false { if packageApp == false {
logger.Yellow("Awesome! Project '%s' built!", projectOptions.Name) logger.Yellow("Awesome! Project '%s' built!", projectOptions.Name)
return nil return nil
} }
// Bundle app // Package app
bundleSpinner := spinner.New("Bundling Application") packageSpinner := spinner.New("Packaging Application")
bundleSpinner.SetSpinSpeed(50) packageSpinner.SetSpinSpeed(50)
bundleSpinner.Start() packageSpinner.Start()
bundler := cmd.NewBundleHelper() packager := cmd.NewPackageHelper()
err = bundler.Bundle(projectOptions) err = packager.Package(projectOptions)
if err != nil { if err != nil {
bundleSpinner.Error() packageSpinner.Error()
return err return err
} }
bundleSpinner.Success() packageSpinner.Success()
logger.Yellow("Awesome! Project '%s' built!", projectOptions.Name) logger.Yellow("Awesome! Project '%s' built!", projectOptions.Name)
return nil return nil
}) })