Compare commits

..

1 Commits

Author SHA1 Message Date
Lea Anthony
ce4fa06828 Initial support for a serve port flag 2020-05-19 07:26:16 -05:00
12 changed files with 73 additions and 143 deletions

View File

@@ -26,4 +26,4 @@ Wails is what it is because of the time and effort given by these great people.
* [Reuben Thomas-Davis](https://github.com/Rested)
* [Jarek](https://github.com/Jarek-SRT)
* [Konez2k](https://github.com/konez2k)
* [msms](https://github.com/sayuthisobri)
* [msms](https://github.com/sayuthisobri)

File diff suppressed because one or more lines are too long

View File

@@ -510,6 +510,8 @@ func InstallRuntime(caller string, projectDir string, projectOptions *ProjectOpt
// InstallBridge installs the relevant bridge javascript library
func InstallBridge(projectDir string, projectOptions *ProjectOptions) error {
bridgeFileData := mewn.String("../runtime/assets/bridge.js")
// Inject the custom serve port
bridgeFileData = strings.ReplaceAll(bridgeFileData, "$SERVEPORT$", projectOptions.ServePort)
bridgeFileTarget := filepath.Join(projectDir, projectOptions.FrontEnd.Dir, "node_modules", "@wailsapp", "runtime", "init.js")
err := fs.CreateFile(bridgeFileTarget, []byte(bridgeFileData))
return err
@@ -561,6 +563,10 @@ func ldFlags(po *ProjectOptions, buildMode string) string {
ldflags += "-X github.com/wailsapp/wails.BuildMode=" + buildMode
// Inject port if we are using bridge mode
if buildMode == BuildModeBridge {
ldflags += " -X github.com/wailsapp/wails/lib/renderer/bridge.ServePort=" + po.ServePort
}
// If we wish to generate typescript
if po.typescriptDefsFilename != "" {
cwd, err := os.Getwd()

View File

@@ -1,12 +1,9 @@
package cmd
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"image"
"image/png"
"io/ioutil"
"os"
"path"
@@ -16,8 +13,6 @@ import (
"text/template"
"time"
"golang.org/x/image/draw"
"github.com/jackmordaunt/icns"
)
@@ -60,87 +55,6 @@ func newPlistData(title, exe, packageID, version, author string) *plistData {
}
}
type windowsIconHeader struct {
_ uint16
imageType uint16
imageCount uint16
width uint8
height uint8
colours uint8
_ uint8
planes uint16
bpp uint16
size uint32
offset uint32
}
func generateWindowsIcon(pngFilename string, iconfile string) error {
header := &windowsIconHeader{
imageType: 1,
imageCount: 1,
bpp: 32,
planes: 1,
offset: 22,
width: 0,
height: 0,
}
// Load png
pngfile, err := os.Open(pngFilename)
if err != nil {
return err
}
defer pngfile.Close()
// Decode to internal image
pngdata, err := png.Decode(pngfile)
if err != nil {
return err
}
// Scale to 256*256
rect := image.Rect(0, 0, 255, 255)
rawdata := image.NewRGBA(rect)
scale := draw.ApproxBiLinear
scale.Scale(rawdata, rect, pngdata, pngdata.Bounds(), draw.Over, nil)
// Convert back to PNG
icondata := new(bytes.Buffer)
writer := bufio.NewWriter(icondata)
err = png.Encode(writer, rawdata)
if err != nil {
return err
}
err = writer.Flush()
if err != nil {
return err
}
// Save size of PNG data
header.size = uint32(len(icondata.Bytes()))
// Open icon file for writing
outfilename := filepath.Join(filepath.Dir(pngFilename), iconfile)
outfile, err := os.Create(outfilename)
if err != nil {
return err
}
defer outfile.Close()
// Write out the header
err = binary.Write(outfile, binary.LittleEndian, header)
if err != nil {
return err
}
// Write out the image data
_, err = outfile.Write(icondata.Bytes())
if err != nil {
return err
}
return nil
}
func defaultString(val string, defaultVal string) string {
if val != "" {
return val
@@ -263,16 +177,14 @@ func (b *PackageHelper) PackageWindows(po *ProjectOptions, cleanUp bool) error {
outputDir := b.fs.Cwd()
basename := strings.TrimSuffix(po.BinaryName, ".exe")
// Copy default icon if needed
icon, err := b.copyIcon()
if err != nil {
return err
}
// Generate icon from PNG
err = generateWindowsIcon(icon, po.BinaryName+".ico")
if err != nil {
return err
// Copy icon
tgtIconFile := filepath.Join(outputDir, basename+".ico")
if !b.fs.FileExists(tgtIconFile) {
srcIconfile := filepath.Join(b.getPackageFileBaseDir(), "wails.ico")
err := b.fs.CopyFile(srcIconfile, tgtIconFile)
if err != nil {
return err
}
}
// Copy manifest
@@ -331,7 +243,7 @@ func (b *PackageHelper) PackageWindows(po *ProjectOptions, cleanUp bool) error {
return nil
}
func (b *PackageHelper) copyIcon() (string, error) {
func (b *PackageHelper) copyIcon(resourceDir string) (string, error) {
// TODO: Read this from project.json
const appIconFilename = "appicon.png"
@@ -356,7 +268,7 @@ func (b *PackageHelper) copyIcon() (string, error) {
func (b *PackageHelper) packageIconOSX(resourceDir string) error {
srcIcon, err := b.copyIcon()
srcIcon, err := b.copyIcon(resourceDir)
if err != nil {
return err
}

View File

@@ -161,6 +161,7 @@ type ProjectOptions struct {
CrossCompile bool
Platform string
Architecture string
ServePort string
}
// Defaults sets the default project template

View File

@@ -2,7 +2,7 @@ package main
import (
"fmt"
"runtime"
"strconv"
"github.com/leaanthony/spinner"
"github.com/wailsapp/wails/cmd"
@@ -11,6 +11,7 @@ import (
func init() {
var forceRebuild = false
var servePort = "34115"
var verbose = false
buildSpinner := spinner.NewSpinner()
buildSpinner.SetSpinSpeed(50)
@@ -19,7 +20,8 @@ func init() {
initCmd := app.Command("serve", "Run your Wails project in bridge mode").
LongDescription(commandDescription).
BoolFlag("verbose", "Verbose output", &verbose).
BoolFlag("f", "Force rebuild of application components", &forceRebuild)
BoolFlag("f", "Force rebuild of application components", &forceRebuild).
StringFlag("p", "Port to serve on", &servePort)
initCmd.Action(func() error {
@@ -35,8 +37,6 @@ func init() {
// Project options
projectOptions := &cmd.ProjectOptions{}
projectOptions.Verbose = verbose
projectOptions.Platform = runtime.GOOS
// Check we are in project directory
// Check project.json loads correctly
@@ -46,6 +46,16 @@ func init() {
return err
}
// Set Verbose flag
projectOptions.Verbose = verbose
// Check port
port, err := strconv.Atoi(servePort)
if err != nil || port <= 0 {
return fmt.Errorf("invalid port value: %s", servePort)
}
projectOptions.ServePort = servePort
// Save project directory
projectDir := fs.Cwd()

1
go.mod
View File

@@ -22,7 +22,6 @@ require (
github.com/stretchr/testify v1.3.0 // indirect
github.com/syossan27/tebata v0.0.0-20180602121909-b283fe4bc5ba
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 // indirect
golang.org/x/image v0.0.0-20200430140353-33d19683fad8
golang.org/x/net v0.0.0-20190509222800-a4d6f7feada5 // indirect
golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862
golang.org/x/text v0.3.0

2
go.sum
View File

@@ -70,8 +70,6 @@ golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 h1:iMGN4xG0cnqj3t+zOM8wUB0BiPKHEwSxEZCvzcbZuvk=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/image v0.0.0-20200430140353-33d19683fad8 h1:6WW6V3x1P/jokJBpRQYUJnMHRP6isStQwCozxnU7XQw=
golang.org/x/image v0.0.0-20200430140353-33d19683fad8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190509222800-a4d6f7feada5 h1:6M3SDHlHHDCx2PcQw3S4KsR170vGqDhJDOmpVd4Hjak=
golang.org/x/net v0.0.0-20190509222800-a4d6f7feada5/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=

View File

@@ -1,4 +1,4 @@
package renderer
package bridge
import (
"encoding/json"
@@ -14,6 +14,10 @@ import (
type messageType int
// ServePort indicates which port to serve the backend on when using
// `wails serve`
var ServePort = "34115"
const (
jsMessage messageType = iota
cssMessage
@@ -93,7 +97,7 @@ func (h *Bridge) startSession(conn *websocket.Conn) {
// Run the app in Bridge mode!
func (h *Bridge) Run() error {
h.server = &http.Server{Addr: ":34115"}
h.server = &http.Server{Addr: ":" + ServePort}
http.HandleFunc("/bridge", h.wsBridgeHandler)
h.log.Info("Bridge mode started.")

View File

@@ -1,4 +1,4 @@
package renderer
package bridge
import (
"time"

File diff suppressed because one or more lines are too long

View File

@@ -14,7 +14,7 @@ function init() {
window.wailsbridge = {
reconnectOverlay: null,
reconnectTimer: 300,
wsURL: 'ws://' + window.location.hostname + ':34115/bridge',
wsURL: 'ws://' + window.location.hostname + ':$SERVEPORT$/bridge',
connectionState: null,
config: {},
websocket: null,
@@ -149,38 +149,38 @@ function startBridge() {
function handleMessage(message) {
// As a bridge we ignore js and css injections
switch (message.data[0]) {
// Wails library - inject!
case 'w':
addScript(message.data.slice(1));
// Wails library - inject!
case 'w':
addScript(message.data.slice(1));
// Now wails runtime is loaded, wails for the ready event
// and callback to the main app
window.wails.Events.On('wails:loaded', function () {
window.wailsbridge.log('Wails Ready');
if (window.wailsbridge.callback) {
window.wailsbridge.log('Notifying application');
window.wailsbridge.callback(window.wails);
}
});
window.wailsbridge.log('Loaded Wails Runtime');
break;
// Now wails runtime is loaded, wails for the ready event
// and callback to the main app
window.wails.Events.On('wails:loaded', function () {
window.wailsbridge.log('Wails Ready');
if (window.wailsbridge.callback) {
window.wailsbridge.log('Notifying application');
window.wailsbridge.callback(window.wails);
}
});
window.wailsbridge.log('Loaded Wails Runtime');
break;
// Notifications
case 'n':
addScript(message.data.slice(1), true);
break;
case 'n':
addScript(message.data.slice(1), true);
break;
// Binding
case 'b':
var binding = message.data.slice(1);
//log("Binding: " + binding)
window.wails._.NewBinding(binding);
break;
case 'b':
var binding = message.data.slice(1);
//log("Binding: " + binding)
window.wails._.NewBinding(binding);
break;
// Call back
case 'c':
var callbackData = message.data.slice(1);
window.wails._.Callback(callbackData);
break;
default:
window.wails.Log.Error('Unknown message type received: ' + message.data[0]);
case 'c':
var callbackData = message.data.slice(1);
window.wails._.Callback(callbackData);
break;
default:
window.wails.Log.Error('Unknown message type received: ' + message.data[0]);
}
}