Skip to content

Commit

Permalink
feat: osx .app bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
BigJk committed Feb 4, 2024
1 parent 10a1d96 commit bdb0cf5
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .github/actions/build-app/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ inputs:
libusb-path:
description: 'Optional path to libusb DLL to include'
required: false
mac-app-bundle:
description: 'Enable or disable OSX app bundle generation'
required: false
runs:
using: composite
steps:
Expand Down Expand Up @@ -46,6 +49,7 @@ runs:
GOARCH: ${{ inputs.arch }}
GOARM: ${{ inputs.armv }}
SND_TAGS: ${{ inputs.tags }}
SND_OSX_APP_BUNDLE_ENABLED: ${{ inputs.mac-app-bundle }}
- name: Run Build (MSYS2)
if: inputs.os == 'windows' && inputs.libusb-path != null
shell: msys2 {0}
Expand Down
54 changes: 42 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- arch: "amd64"
libusb: true
tags: "ELECTRON LIBUSB"
artifact-name: "snd-linux-amd64-gui-usb"
artifact-name: "snd-linux-amd64-gui-libusb"
# HEADLESS
- arch: "amd64"
artifact-name: "snd-linux-amd64-headless"
Expand Down Expand Up @@ -87,11 +87,11 @@ jobs:
- arch: "amd64"
libusb: false
tags: "ELECTRON"
artifact-name: "snd-windows-amd64-gui"
artifact-name: "snd-windows"
- arch: "amd64"
libusb: true
tags: "ELECTRON LIBUSB"
artifact-name: "snd-windows-amd64-gui-usb"
artifact-name: "snd-windows-libusb"
steps:
- name: Checkout Repo
uses: actions/checkout@v3
Expand All @@ -116,26 +116,25 @@ jobs:
tags: ${{ matrix.tags }}
artifact-name: ${{ matrix.artifact-name }}
libusb-path: ${{ matrix.libusb && 'D:/a/_temp/msys64/mingw64/bin/libusb-1.0.dll' || null }}

build-darwin-app:

# macOS AMD64
build-darwin-amd64-app:
name: Build ${{ matrix.artifact-name }}
runs-on: macos-latest
needs: build-frontend
strategy:
matrix:
include:
# ELECTRON
- arch: "amd64"
tags: "ELECTRON"
artifact-name: "snd-macos-amd64-gui"
- arch: "arm64"
tags: "ELECTRON"
artifact-name: "snd-macos-arm64-gui"
artifact-name: "snd-macos-intel"
mac-app-bundle: "true"
- arch: "amd64"
libusb: true
tags: "ELECTRON LIBUSB"
artifact-name: "snd-macos-amd64-gui-usb"
# Note: arm64 + USB not possible until we have an M1 runner
artifact-name: "snd-macos-intel-libusb"
mac-app-bundle: "true"

steps:
- name: Checkout Repo
uses: actions/checkout@v3
Expand All @@ -150,6 +149,37 @@ jobs:
tags: ${{ matrix.tags }}
artifact-name: ${{ matrix.artifact-name }}

# macOS ARM64 (m1, m2, m3, mX)
build-darwin-arm64-app:
name: Build ${{ matrix.artifact-name }}
runs-on: macos-14
needs: build-frontend
strategy:
matrix:
include:
- arch: "arm64"
tags: "ELECTRON"
artifact-name: "snd-macos-m1"
mac-app-bundle: "true"
- arch: "arm64"
libusb: true
tags: "ELECTRON LIBUSB"
artifact-name: "snd-macos-m1-libusb"
mac-app-bundle: "true"
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Install libusb
if: matrix.libusb
run: brew install libusb pkg-config && pkg-config --cflags libusb-1.0
- name: Build App
uses: ./.github/actions/build-app
with:
os: darwin
arch: ${{ matrix.arch }}
tags: ${{ matrix.tags }}
artifact-name: ${{ matrix.artifact-name }}

create-release:
runs-on: ubuntu-latest
needs: [build-linux-app, build-windows-app, build-darwin-app]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: npm ci --loglevel verbose
- name: Lint Frontend
working-directory: frontend
run: npm run lint-check && npm run format-check
run: npm run lint && npm run format-check
- name: Build Frontend
working-directory: frontend
run: npm run build
Expand Down
29 changes: 27 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ GIT_COMMIT=$(git rev-list -1 HEAD)
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
TARGET_DIR=${SND_RELEASE_DIR:='build/release/'}
APP_NAME=${SND_APP_NAME:='Sales & Dungeons'}
OSX_APP_BUNDLE_ENABLED=${SND_OSX_APP_BUNDLE_ENABLED:='false'}
OSX_APP_BUNDLE_DIR=${TARGET_DIR}

# if OSX_APP_BUNDLE_ENABLED we need to append /bundle to SND_RELEASE_DIR and APP_NAME needs to be snd
if [ "${OSX_APP_BUNDLE_ENABLED}" = "true" ]; then
TARGET_DIR="${TARGET_DIR}bundle/"
APP_NAME="snd"
fi

echo "Build Time : ${BUILD_TIME}"
echo "Branch : ${GIT_BRANCH}"
Expand All @@ -18,8 +26,8 @@ echo "Build Tags : ${SND_TAGS}"
echo "CGO Enabled : ${CGO_ENABLED}"

echo "Clearing old data..."
rm -r ${TARGET_DIR}/frontend || true
rm -r ${TARGET_DIR}/data || true
rm -r ${TARGET_DIR}frontend || true
rm -r ${TARGET_DIR}data || true
rm ${TARGET_DIR}/* || true
mkdir -p ${TARGET_DIR}/frontend/dist ${TARGET_DIR}/data

Expand All @@ -46,3 +54,20 @@ echo "Building version.txt..."
echo "Commit: ${GIT_COMMIT}" > ${TARGET_DIR}/version.txt
echo "Branch: ${GIT_BRANCH}" >> ${TARGET_DIR}/version.txt
echo "Build Time: ${BUILD_TIME}" >> ${TARGET_DIR}/version.txt

# if OSX_APP_BUNDLE_ENABLED we need to create a .app bundle
if [ "${OSX_APP_BUNDLE_ENABLED}" = "true" ]; then
echo "Building OSX App Bundle..."

wget -O ${OSX_APP_BUNDLE_DIR}/macapp.go https://gist.githubusercontent.com/mholt/11008646c95d787c30806d3f24b2c844/raw/53d1d4baeeb7ed8ee7dc89ebfd900e444a298323/macapp.go
go run ${OSX_APP_BUNDLE_DIR}/macapp.go -assets "${TARGET_DIR}" -bin "snd" -icon "${TARGET_DIR}data/icon.png" -identifier app.sales-and-dungeons -name "Sales & Dungeons" -o ${OSX_APP_BUNDLE_DIR}

# check if "Sales & Dungeons.app" exists
if [ -d "${OSX_APP_BUNDLE_DIR}Sales & Dungeons.app" ]; then
rm -r "${TARGET_DIR}"
rm "${OSX_APP_BUNDLE_DIR}/macapp.go"
else
echo "Error: Failed to create app bundle"
exit 1
fi
fi
33 changes: 29 additions & 4 deletions cmd/app/electron.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"io/ioutil"
stdlog "log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
"time"

Expand Down Expand Up @@ -50,12 +52,10 @@ func startElectron(db database.Database, debug bool) {
targetWriter = os.Stdout
}

_ = os.Mkdir("./data", 0666)

time.Sleep(time.Millisecond * 500)
log.Info("If no window is opening please wait a few seconds for the dependencies to download...")

a, err := astilectron.New(stdlog.New(targetWriter, "", 0), astilectron.Options{
opts := astilectron.Options{
AppName: "SND",
BaseDirectoryPath: "./data",
DataDirectoryPath: "./data",
Expand All @@ -67,7 +67,32 @@ func startElectron(db database.Database, debug bool) {
ElectronSwitches: []string{
"--disable-http-cache",
},
})
}

if isMacAppBundle() {
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
opts.BaseDirectoryPath = filepath.Join(home, "/Documents/Sales & Dungeons/electron")
opts.DataDirectoryPath = filepath.Join(home, "/Documents/Sales & Dungeons/electron")

err = os.MkdirAll(opts.BaseDirectoryPath, 0777)
log.Info("Changed electron provision folder because of app bundle", log.WithValue("path", opts.BaseDirectoryPath), log.WithValue("mkdir error", err))

// copy icon files to the electron folder
_ = exec.Command("cp", "./icon.png", filepath.Join(opts.BaseDirectoryPath, "icon.png")).Run()
_ = exec.Command("cp", "./icon.icns", filepath.Join(opts.BaseDirectoryPath, "icon.icns")).Run()

// check if vendor folder exists
if _, err := os.Stat(filepath.Join(opts.BaseDirectoryPath, "vendor")); os.IsNotExist(err) {
_ = exec.Command("osascript", "-e", "display dialog \"Thanks for downloading Sales & Dungeons! During the first launch, we're setting things up and downloading dependencies. This may take a few moments. We appreciate your patience!\" buttons {\"OK\"} with title \"Sales & Dungeons First Start\" with icon caution").Run()
}
} else {
_ = os.Mkdir("./data", 0666)
}

a, err := astilectron.New(stdlog.New(targetWriter, "", 0), opts)
if err != nil {
panic(err)
}
Expand Down
32 changes: 31 additions & 1 deletion cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/BigJk/snd"
Expand Down Expand Up @@ -35,8 +36,32 @@ func syncBaseUrl() string {
return ""
}

func isMacAppBundle() bool {
if runtime.GOOS != "darwin" {
return false
}
execDir, err := os.Executable()
if err != nil {
return false
}
return strings.Contains(execDir, "Sales & Dungeons.app")
}

func openDatabase() database.Database {
db, err := badger.New("./userdata/")
userdata := "./userdata/"

if isMacAppBundle() {
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
userdata = filepath.Join(home, "/Documents/Sales & Dungeons/userdata")

err = os.MkdirAll(userdata, 0777)
fmt.Println("INFO: changed userdata folder because of app bundle", userdata, err)
}

db, err := badger.New(userdata)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -101,6 +126,11 @@ func fixWorkingDir() bool {
// Change working dir to executable dir in hopes to resolve the problem
execDir, err := os.Executable()
if err == nil && pwd != execDir {
if isMacAppBundle() {
execDir = filepath.Join(filepath.Dir(execDir), "../Resources/Sales & Dungeons")
fmt.Println("INFO: using mac app bundle directory", filepath.Dir(execDir))
}

if err := os.Chdir(filepath.Dir(execDir)); err != nil {
fmt.Println("ERROR: could not set working dir to", execDir, err)
return false
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/js/ui/views/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default (): m.Component => {
label: 'Type',
description: 'The type of printer you are using',
customComponent: m(Select, {
keys: Object.keys(printer.value).filter((k) => Object.keys(printer.value[k] ?? {}).length > 0),
keys: Object.keys(printer.value),
selected: settingsCopy.printerType,
onInput: (e) => {
settingsCopy = { ...settingsCopy, printerType: e.value };
Expand Down

0 comments on commit bdb0cf5

Please sign in to comment.