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"
)
// BundleHelper helps with the 'wails bundle' command
type BundleHelper struct {
// PackageHelper helps with the 'wails package' command
type PackageHelper struct {
fs *FSHelper
log *Logger
system *SystemHelper
}
// NewBundleHelper creates a new BundleHelper!
func NewBundleHelper() *BundleHelper {
return &BundleHelper{
// NewPackageHelper creates a new PackageHelper!
func NewPackageHelper() *PackageHelper {
return &PackageHelper{
fs: NewFSHelper(),
log: NewLogger(),
system: NewSystemHelper(),
@ -35,19 +35,19 @@ func NewBundleHelper() *BundleHelper {
type plistData struct {
Title string
Exe string
BundleID string
PackageID string
Version string
Author 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)
return &plistData{
Title: title,
Exe: exe,
Version: version,
BundleID: bundleID,
PackageID: packageID,
Author: author,
Date: now,
}
@ -60,26 +60,32 @@ func defaultString(val string, defaultVal string) string {
return defaultVal
}
func (b *BundleHelper) getBundleFileBaseDir() string {
return filepath.Join(b.system.homeDir, "go", "src", "github.com", "wailsapp", "wails", "cmd", "bundle", runtime.GOOS)
func (b *PackageHelper) getPackageFileBaseDir() string {
// 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
func (b *BundleHelper) Bundle(po *ProjectOptions) error {
// Package the application into a platform specific package
func (b *PackageHelper) Package(po *ProjectOptions) error {
// Check we have the exe
if !b.fs.FileExists(po.BinaryName) {
return fmt.Errorf("cannot bundle non-existant binary file '%s'. Please build with 'wails build' first", po.BinaryName)
}
switch runtime.GOOS {
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:
return fmt.Errorf("platform '%s' not supported for bundling yet", runtime.GOOS)
}
}
// Bundle the application
func (b *BundleHelper) bundleOSX(po *ProjectOptions) error {
// Package the application for OSX
func (b *PackageHelper) packageOSX(po *ProjectOptions) error {
system := NewSystemHelper()
config, err := system.LoadConfig()
@ -91,8 +97,8 @@ func (b *BundleHelper) bundleOSX(po *ProjectOptions) error {
exe := defaultString(po.BinaryName, name)
version := defaultString(po.Version, "0.1.0")
author := defaultString(config.Name, "Anonymous")
bundleID := strings.Join([]string{"wails", name, version}, ".")
plistData := newPlistData(name, exe, bundleID, version, author)
packageID := strings.Join([]string{"wails", name, version}, ".")
plistData := newPlistData(name, exe, packageID, version, author)
appname := po.Name + ".app"
// 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)
}
// REmove the existing bundle
// Remove the existing package
os.RemoveAll(appname)
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")
b.fs.MkDirs(resourceDir, 0755)
tmpl := template.New("infoPlist")
plistFile := filepath.Join(b.getBundleFileBaseDir(), "info.plist")
plistFile := filepath.Join(b.getPackageFileBaseDir(), "info.plist")
infoPlist, err := ioutil.ReadFile(plistFile)
if err != nil {
return err
@ -140,11 +146,11 @@ func (b *BundleHelper) bundleOSX(po *ProjectOptions) error {
if err != nil {
return err
}
err = b.bundleIcon(resourceDir)
err = b.packageIcon(resourceDir)
return err
}
func (b *BundleHelper) bundleIcon(resourceDir string) error {
func (b *PackageHelper) packageIcon(resourceDir string) error {
// TODO: Read this from project.json
const appIconFilename = "appicon.png"
@ -155,7 +161,7 @@ func (b *BundleHelper) bundleIcon(resourceDir string) error {
if !b.fs.FileExists(srcIcon) {
// Install default icon
iconfile := filepath.Join(b.getBundleFileBaseDir(), "icon.png")
iconfile := filepath.Join(b.getPackageFileBaseDir(), "icon.png")
iconData, err := ioutil.ReadFile(iconfile)
if err != nil {
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() {
var bundle = false
var packageApp = false
var forceRebuild = false
var releaseMode = false
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.`
initCmd := app.Command("build", "Builds your Wails project").
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("r", "Build in Release mode", &releaseMode)
@ -190,7 +190,7 @@ func init() {
depSpinner.Success()
compileMessage := "Packing + Compiling project"
if releaseMode {
if releaseMode || packageApp {
compileMessage += " (Release Mode)"
}
@ -219,7 +219,7 @@ func init() {
}
// Release mode
if releaseMode {
if releaseMode || packageApp {
buildCommand.AddSlice([]string{"-ldflags", "-X github.com/wailsapp/wails.DebugMode=false"})
}
err = program.RunCommandArray(buildCommand.AsSlice())
@ -229,22 +229,22 @@ func init() {
}
packSpinner.Success()
if bundle == false {
if packageApp == false {
logger.Yellow("Awesome! Project '%s' built!", projectOptions.Name)
return nil
}
// Bundle app
bundleSpinner := spinner.New("Bundling Application")
bundleSpinner.SetSpinSpeed(50)
bundleSpinner.Start()
bundler := cmd.NewBundleHelper()
err = bundler.Bundle(projectOptions)
// Package app
packageSpinner := spinner.New("Packaging Application")
packageSpinner.SetSpinSpeed(50)
packageSpinner.Start()
packager := cmd.NewPackageHelper()
err = packager.Package(projectOptions)
if err != nil {
bundleSpinner.Error()
packageSpinner.Error()
return err
}
bundleSpinner.Success()
packageSpinner.Success()
logger.Yellow("Awesome! Project '%s' built!", projectOptions.Name)
return nil
})