DRYな備忘録

Don't Repeat Yourself.

ElectronアプリをMac上でWindows向けにビルドする

ゴール

  • MacOS上で動いてるElectronアプリがある
  • electron-packagerを使い、Mac用に.appというフォルダを作って配布できている
  • Windowsのひとたちにも配布したい

参考

手順

  1. electron-packagerを使って、ビルドディレクトリをつくる
  2. electron-winstaller を使って、インストーラ .exe をつくる

Windows用のビルドディレクトリをつくる

Building Windows apps from non-Windows platforms に従って、

% electron-packager . MyApp \
  --platform=win64 \
  --arch=x64 \
  --version=0.0.1 \
  --out=builds/windows \
  --icon=assets/myapp.icns --overwrite

エラー: Unsupported platform=win64 (string); must be a string matching: darwin, linux, mas, win32

Supported Platforms に、

Windows (also known as win32, for both 32/64 bit)

とある。ので、

% electron-packager . MyApp \
-  --platform=win64 \
+  --platform=win32 \
  --arch=x64 \
  --version=0.0.1 \
  --out=builds/windows \
  --icon=assets/myapp.icns --overwrite

エラー: Could not find "wine" on your system.

Packaging app for platform win32 x64 using electron v1.7.5
Could not find "wine" on your system.

Wine is required to use the appCopyright, appVersion, buildVersion, icon, and
win32metadata parameters for Windows targets.

Make sure that the "wine" executable is in your PATH.

Building Windows apps from non-Windows platforms をよく読むと、

so on non-Windows host platforms, Wine 1.6 or later needs to be installed. On OS X, it is installable via Homebrew.

とあるので、

% brew search wine
% brew install wine
wine: XQuartz is required to install this formula.X11Requirement unsatisfied!

You can install with Homebrew-Cask:
  brew cask install xquartz

You can download from:
  https://xquartz.macosforge.org
Error: An unsatisfied requirement failed this build.
# ほう?
% brew cask install xquartz
% brew install wine
==> Summary
🍺  /usr/local/Cellar/wine/2.0.3: 8,252 files, 591.9MB
# 入った
% which wine
/usr/local/bin/wine
# PATHも通ってる

electron-packagerによるビルドの成功

% electron-packager . MyApp \
  --platform=win64 \
  --arch=x64 \
  --version=0.0.1 \
  --out=builds/windows \
  --icon=assets/myapp.icns --overwrite

Packaging app for platform win32 x64 using electron v1.7.5
Wrote new app to builds/windows/MyApp-win32-x64

いい感じ。

インストーラの作成

% mkdir scripts
% vi scripts/windows-build-installer.js
/* eslint no-console:0 */
var winstaller = require('electron-winstaller');

winstaller.createWindowsInstaller({
  appDirectory: './builds/windows/MyApp-win32-x64/',
  outputDirectory: '/tmp/build/installer64',
  authors: 'My App Inc.',
  exe: 'myapp.exe'
})
  .then(() => console.log('It worked!'))
  .catch(e => console.log(`No dice: ${e.message}`));

エラー: No dice: spawn mono ENOENT

% node ./scripts/windows-build-installer.js
No dice: spawn mono ENOENT

ふむ。

ほう?

% brew search mono
% brew info mono
% brew install mono
% wich mono
/usr/local/bin/mono

インストーラ作成成功

ふたたび

% node ./scripts/windows-build-installer.js
It worked!
% ls -la /tmp/build/installer64
RELEASES        MyApp-0.0.1-full.nupkg  Setup.exe

いけてるっぽいぞ。

この.exeWindowsで実行したら、無事デスクトップアプリがWindowsで起動した!

DRYな備忘録として