Add compiled native_main.exe support on Windows

This commit adds support for comiling `native_main.py` into
`native_main.exe` using [PyInstaller][0].

By default, the Powershell installer script would use the compiled
EXE version for Windows. However, the old behaviour can achieved by
using the `-UsePython` flag to `win_install.ps1`.

Currently, the `native_main.exe` is built every time
`scripts/build.sh` is invoked. However, this behavior can be
adjusted by invoking the build script as shown below:

```
  PYINSTALLER="0" npm run build
```

[0]: https://www.pyinstaller.org
This commit is contained in:
Babil Golam Sarwar 2018-05-18 11:05:22 +10:00
parent 93e62ba777
commit bf920989f3
4 changed files with 140 additions and 25 deletions

4
.gitignore vendored
View file

@ -8,3 +8,7 @@ generated
web-ext-artifacts
yarn.lock
.vscode/
native/__pycache__
native/native_main
native_main.spec
tags

BIN
native/native_main.exe Normal file

Binary file not shown.

View file

@ -1,5 +1,6 @@
Param (
[switch]$Uninstall = $false,
[switch]$UsePython= $false,
[string]$DebugDirBase = "",
[string]$InstallDirBase = ""
)
@ -8,7 +9,8 @@ Param (
# Global constants
#
$global:InstallDirName = ".tridactyl"
$global:MessengerBinName = "native_main.py"
$global:MessengerBinPyName = "native_main.py"
$global:MessengerBinExeName = "native_main.exe"
$global:MessengerBinWrapperFilename = "native_main.bat"
$global:MessengerManifestFilename = "tridactyl.json"
$global:PythonVersionStr = "Python 3"
@ -16,12 +18,13 @@ $global:MessengerManifestReplaceStr = "REPLACE_ME_WITH_SED"
$global:MessengerFilesHttpUriBase = [string]::Format("{0}{1}",
"https://raw.githubusercontent.com",
"/cmcaine/tridactyl/master/native")
"/gsbabil/tridactyl/master/native")
$global:MessengerManifestRegistryPath = `
"HKCU:\Software\Mozilla\NativeMessagingHosts\tridactyl"
$global:Uninstall = $Uninstall
$global:UsePython= $UsePython
$global:InstallDirBase = $InstallDirBase.Trim()
$global:DebugDirBase = $DebugDirBase.Trim()
@ -150,12 +153,22 @@ function Set-InstallDir() {
}
}
function Get-MessengerBinName() {
$messengerBinName = $global:MessengerBinExeName
if ($global:UsePython -eq $true) {
$messengerBinName = $global:MessengerBinPyName
}
Return $messengerBinName
}
function Get-MessengerBinPath() {
$messengerInstallDir = Get-MessengerInstallDir
$messengerBinName = Get-MessengerBinName
$native_messenger_binary_path = [string]::Format("{0}\{1}",
$messengerInstallDir,
$global:MessengerBinName)
$messengerBinName)
Return $native_messenger_binary_path.Trim()
}
@ -165,9 +178,11 @@ function Get-MessengerBinUri() {
-Date ((Get-Date).ToUniversalTime()) `
-UFormat %s)
$messengerBinName = Get-MessengerBinName
$messengerBinUri = [string]::Format("{0}/{1}?{2}",
$global:MessengerFilesHttpUriBase,
$global:MessengerBinName,
$messengerBinName,
$downloadStartTime)
Return $messengerBinUri.Trim()
@ -178,15 +193,18 @@ function Set-MessengerBin() {
$messengerBinUri = Get-MessengerBinUri
if ($global:DebugDirBase.Length -gt 0) {
$messengerBinName = Get-MessengerBinName
$srcPath = [string]::Format("{0}\{1}",
$global:DebugDirBase,
$global:MessengerBinName)
$messengerBinName)
Write-Host "[+] Copying $srcPath ..."
Copy-Item `
-Path $srcPath `
-Destination $messengerBinPath
-Destination $messengerBinPath `
-Force `
} else {
Write-Host "[+] Downloading $messengerBinUri ..."
@ -233,10 +251,18 @@ function Get-MessengerBinWrapperPath() {
function Set-MessengerBinWrapper() {
$messengerBinPath = Get-MessengerBinPath
$messengerBinWrapperPath = Get-MessengerBinWrapperPath
if ($global:UsePython -eq $true) {
$messengerWrapperContent = @"
@echo off
call py -3 -u $messengerBinPath
"@
} else {
$messengerWrapperContent = @"
@echo off
call $messengerBinPath
"@
}
Write-Host "[+] Preparing $messengerBinWrapperPath ..."
@ -298,7 +324,8 @@ function Set-MessengerManifest() {
Copy-Item `
-Path $srcPath `
-Destination $messengerManifestPath
-Destination $messengerManifestPath `
-Force `
} else {
Write-Host "[+] Downloading $messengerManifestUri ..."
@ -437,23 +464,25 @@ function Set-MessengerManifestRegistry() {
}
function Set-MessengerInstall() {
# Check for Python 3
Write-Host "[+] Looking for Python 3 ..."
$pythonVersionStatus = Get-PythonVersionStatus
if (! $pythonVersionStatus) {
Write-Host " - Python 3 not found, quitting ..."
exit -1
} else {
$pythonPath = Get-Command "py" `
if ($global:UsePython -eq $true) {
# Check for Python 3
Write-Host "[+] Looking for Python 3 ..."
$pythonVersionStatus = Get-PythonVersionStatus
if (! $pythonVersionStatus) {
Write-Host " - Python 3 not found, quitting ..."
exit -1
} else {
$pythonPath = Get-Command "py" `
| Select-Object -ExpandProperty "Source"
Write-Host " - Python 3 found at: $pythonPath"
Write-Host " - Python 3 found at: $pythonPath"
}
}
# Prepare `.tridactyl` directory
$result = Set-InstallDir
# Prepare `native_main.py`
# Prepare `native_main.{py,exe}`
if ($result -eq $true) {
$result = Set-MessengerBin
}
@ -503,4 +532,3 @@ if ($global:Uninstall) {
}
Set-MessengerInstall

View file

@ -4,13 +4,100 @@ set -e
CLEANSLATE="node_modules/cleanslate/docs/files/cleanslate.css"
NATIVE_BIN_DIR="native"
WIN_COMPILE_NATIVE_BIN_TARGET="${NATIVE_BIN_DIR}/native_main.py"
WIN_COMPILE_NATIVE_BIN_OUTPUT="${NATIVE_BIN_DIR}/native_main.exe"
if [ $PYINSTALLER = "0" ]; then
WIN_COMPILE_NATIVE_BIN="False"
else
WIN_COMPILE_NATIVE_BIN="True"
fi
isWindowsMinGW() {
local expected_prefix="MINGW"
local is_mingw="False"
if [ "$(uname | cut -c 1-5)" = "MINGW" ]; then
if [ "$(uname | cut -c 1-5)" = "$expected_prefix" ]; then
is_mingw="True"
fi
echo -n "${is_mingw}"
echo -n "$is_mingw"
}
checkPyinstallerStatus() {
local expected_prefix="3.3"
local pyinstaller_found="False"
if [ "$(pyinstaller --version \
| cut -c 1-3)" = "$expected_prefix" ]; then
pyinstaller_found="True"
fi
echo -n "$pyinstaller_found"
}
compileNativeBin() {
local success="False"
local output_dir="$NATIVE_BIN_DIR"
local target_file="${WIN_COMPILE_NATIVE_BIN_TARGET}"
if [ "$(checkPyinstallerStatus)" = "False" ]; then
$WIN_PYTHON -m pip install --upgrade pyinstaller
fi
if [ ! -d "$output_dir" ]; then
mkdir -v -p "$output_dir"
fi
pyinstaller \
--clean \
--console \
--onefile \
--noupx \
--noconfirm \
--workpath "$output_dir" \
--distpath "$output_dir" \
"$target_file"
if [ $? -eq 0 ] \
&& [ -f "$WIN_COMPILE_NATIVE_BIN_OUTPUT" ]; then
success="True"
fi
echo -n "$success"
}
installWindowsNativeMessenger() {
if [ "$WIN_COMPILE_NATIVE_BIN" = "True" ]; then
success="$(compileNativeBin)"
if [ "$success" = "True" ]; then
printf "\n[+] %s -> %s compilation was successful!\n\n" \
$WIN_COMPILE_NATIVE_BIN_TARGET \
$WIN_COMPILE_NATIVE_BIN_OUTPUT
powershell \
-NoProfile \
-InputFormat None \
-ExecutionPolicy Bypass \
native/win_install.ps1 -DebugDirBase native
else
printf "[+] %s -> %s compilation failed, quitting ..." \
$WIN_COMPILE_NATIVE_BIN_TARGET \
$WIN_COMPILE_NATIVE_BIN_OUTPUT
exit -1
fi
else
powershell \
-NoProfile \
-InputFormat None \
-ExecutionPolicy Bypass \
native/win_install.ps1 -DebugDirBase native -UsePython
fi
}
if [ "$(isWindowsMinGW)" = "True" ]; then
@ -41,11 +128,7 @@ nearleyc src/grammars/bracketexpr.ne \
> src/grammars/.bracketexpr.generated.ts
if [ "$(isWindowsMinGW)" = "True" ]; then
powershell \
-NoProfile \
-InputFormat None \
-ExecutionPolicy Bypass \
native/win_install.ps1 -DebugDirBase native
installWindowsNativeMessenger
else
native/install.sh local
fi