Create a macOS installer (fixes #567)

This commit is contained in:
Marek Dabrowski 2022-07-29 00:01:14 +01:00
parent 9de9f78129
commit 65efd968a5
7 changed files with 366 additions and 2 deletions

Binary file not shown.

View File

@ -51,3 +51,31 @@ coll = COLLECT(exe,
strip=False,
upx=True,
name='manuskript')
version=os.environ['manuskript_version']
app = BUNDLE(coll,
name='manuskript.app',
icon=os.path.join(SPECPATH, 'icons/Manuskript/Manuskript.icns'),
bundle_identifier='ch.theologeek.manuskript',
version=version,
info_plist={
'NSPrincipalClass': 'NSApplication',
'NSAppleScriptEnabled': False,
'NSHighResolutionCapable': True,
'CFBundleURLTypes': [{
'CFBundleURLName': 'MSK',
'CFBundleTypeRole': 'Editor',
'CFBundleURLSchemes': ['msk'],
}],
'CFBundleDocumentTypes': [
{
'CFBundleTypeName': 'MSK',
'CFBundleTypeIconFile': 'icons/Manuscript/manuskript',
'CFBundleTypeExtensions': ['msk'],
'CFBundleTypeRole': 'Editor',
'LSHandlerRank': 'Owner'
}
]
},
)

View File

@ -4,7 +4,16 @@ if [ z"$FILENAME" = "z" ]; then
echo "Error: Environment variable FILENAME is not set"
exit 1
fi
pyinstaller manuskript.spec --clean
filename="${FILENAME%.*}".dmg
export manuskript_version=$TRAVIS_BRANCH
package/osx/rebuild_mac_icon.sh
pyinstaller manuskript.spec --clean --noconfirm
# Fix signing the app - know issue with Qt5
python3 package/osx/fix_app_qt_folder_names_for_codesign.py dist/manuskript.app
codesign -s - --force --all-architectures --timestamp --deep dist/manuskript.app
# Create the installer
dmgbuild -s package/osx/dmg-settings.py "manuskript" dist/${filename}
cd dist && zip $FILENAME -r manuskript && cd ..
ls dist
cp dist/$FILENAME dist/manuskript-osx-develop.zip
cp dist/$filename dist/manuskript-osx-develop.dmg

184
package/osx/dmg-settings.py Normal file
View File

@ -0,0 +1,184 @@
import os.path
import plistlib
#
# Example settings file for dmgbuild
#
# Use like this: dmgbuild -s settings.py "Test Volume" test.dmg
# You can actually use this file by doing e.g.
#
# dmgbuild -s settings.py -D app=/path/to/My.app "My Application" MyApp.dmg
# .. Useful stuff ..............................................................
application = defines.get('app', 'dist/manuskript.app') # noqa: F821
appname = os.path.basename(application)
def icon_from_app(app_path):
plist_path = os.path.join(app_path, 'Contents', 'Info.plist')
with open(plist_path, 'rb') as f:
plist = plistlib.load(f)
icon_name = plist['CFBundleIconFile']
icon_root, icon_ext = os.path.splitext(icon_name)
if not icon_ext:
icon_ext = '.icns'
icon_name = icon_root + icon_ext
return os.path.join(app_path, 'Contents', 'Resources', icon_name)
# .. Basics ....................................................................
# Uncomment to override the output filename
# filename = 'test.dmg'
# Uncomment to override the output volume name
# volume_name = 'Test'
# Volume format (see hdiutil create -help)
format = defines.get('format', 'UDBZ') # noqa: F821
# Compression level (if relevant)
# compression_level = 9
# Volume size
size = defines.get('size', None) # noqa: F821
# Files to include
files = [application]
# Symlinks to create
symlinks = {'Applications': '/Applications'}
# Files to hide
# hide = [ 'Secret.data' ]
# Files to hide the extension of
# hide_extension = [ 'README.rst' ]
# Volume icon
#
# You can either define icon, in which case that icon file will be copied to the
# image, *or* you can define badge_icon, in which case the icon file you specify
# will be used to badge the system's Removable Disk icon. Badge icons require
# pyobjc-framework-Quartz.
#
# icon = '/path/to/icon.icns'
badge_icon = icon_from_app(application)
# Where to put the icons
icon_locations = {
appname: (140, 120),
'Applications': (500, 120)
}
# .. Window configuration ......................................................
# Background
#
# This is a STRING containing any of the following:
#
# #3344ff - web-style RGB color
# #34f - web-style RGB color, short form (#34f == #3344ff)
# rgb(1,0,0) - RGB color, each value is between 0 and 1
# hsl(120,1,.5) - HSL (hue saturation lightness) color
# hwb(300,0,0) - HWB (hue whiteness blackness) color
# cmyk(0,1,0,0) - CMYK color
# goldenrod - X11/SVG named color
# builtin-arrow - A simple built-in background with a blue arrow
# /foo/bar/baz.png - The path to an image file
#
# The hue component in hsl() and hwb() may include a unit; it defaults to
# degrees ('deg'), but also supports radians ('rad') and gradians ('grad'
# or 'gon').
#
# Other color components may be expressed either in the range 0 to 1, or
# as percentages (e.g. 60% is equivalent to 0.6).
background = 'builtin-arrow'
show_status_bar = False
show_tab_view = False
show_toolbar = False
show_pathbar = False
show_sidebar = False
sidebar_width = 180
# Window position in ((x, y), (w, h)) format
window_rect = ((100, 100), (640, 280))
# Select the default view; must be one of
#
# 'icon-view'
# 'list-view'
# 'column-view'
# 'coverflow'
#
default_view = 'icon-view'
# General view configuration
show_icon_preview = False
# Set these to True to force inclusion of icon/list view settings (otherwise
# we only include settings for the default view)
include_icon_view_settings = 'auto'
include_list_view_settings = 'auto'
# .. Icon view configuration ...................................................
arrange_by = None
grid_offset = (0, 0)
grid_spacing = 100
scroll_position = (0, 0)
label_pos = 'bottom' # or 'right'
text_size = 16
icon_size = 128
# .. List view configuration ...................................................
# Column names are as follows:
#
# name
# date-modified
# date-created
# date-added
# date-last-opened
# size
# kind
# label
# version
# comments
#
list_icon_size = 16
list_text_size = 12
list_scroll_position = (0, 0)
list_sort_by = 'name'
list_use_relative_dates = True
list_calculate_all_sizes = False,
list_columns = ('name', 'date-modified', 'size', 'kind', 'date-added')
list_column_widths = {
'name': 300,
'date-modified': 181,
'date-created': 181,
'date-added': 181,
'date-last-opened': 181,
'size': 97,
'kind': 115,
'label': 100,
'version': 75,
'comments': 300,
}
list_column_sort_directions = {
'name': 'ascending',
'date-modified': 'descending',
'date-created': 'descending',
'date-added': 'descending',
'date-last-opened': 'descending',
'size': 'descending',
'kind': 'ascending',
'label': 'ascending',
'version': 'ascending',
'comments': 'ascending',
}

View File

@ -0,0 +1,121 @@
# -*- coding: utf-8 -*-
import os
import shutil
import sys
from pathlib import Path
from typing import Generator, List, Optional
from macholib.MachO import MachO
# Source: https://github.com/pyinstaller/pyinstaller/wiki/Recipe-OSX-Code-Signing-Qt
def create_symlink(folder: Path) -> None:
"""Create the appropriate symlink in the MacOS folder
pointing to the Resources folder.
"""
sibbling = Path(str(folder).replace("MacOS", ""))
# PyQt5/Qt/qml/QtQml/Models.2
root = str(sibbling).partition("Contents")[2].lstrip("/")
# ../../../../
backward = "../" * (root.count("/") + 1)
# ../../../../Resources/PyQt5/Qt/qml/QtQml/Models.2
good_path = f"{backward}Resources/{root}"
folder.symlink_to(good_path)
def fix_dll(dll: Path) -> None:
"""Fix the DLL lookup paths to use relative ones for Qt dependencies.
Inspiration: PyInstaller/depend/dylib.py:mac_set_relative_dylib_deps()
Currently one header is pointing to (we are in the Resources folder):
@loader_path/../../../../QtCore (it is referencing to the old MacOS folder)
It will be converted to:
@loader_path/../../../../../../MacOS/QtCore
"""
def match_func(pth: str) -> Optional[str]:
"""Callback function for MachO.rewriteLoadCommands() that is
called on every lookup path setted in the DLL headers.
By returning None for system libraries, it changes nothing.
Else we return a relative path pointing to the good file
in the MacOS folder.
"""
basename = os.path.basename(pth)
if not basename.startswith("Qt"):
return None
return f"@loader_path{good_path}/{basename}"
# Resources/PyQt5/Qt/qml/QtQuick/Controls.2/Fusion
root = str(dll.parent).partition("Contents")[2][1:]
# /../../../../../../..
backward = "/.." * (root.count("/") + 1)
# /../../../../../../../MacOS
good_path = f"{backward}/MacOS"
# Rewrite Mach headers with corrected @loader_path
dll = MachO(dll)
dll.rewriteLoadCommands(match_func)
with open(dll.filename, "rb+") as f:
for header in dll.headers:
f.seek(0)
dll.write(f)
f.seek(0, 2)
f.flush()
def find_problematic_folders(folder: Path) -> Generator[Path, None, None]:
"""Recursively yields problematic folders (containing a dot in their name)."""
for path in folder.iterdir():
if not path.is_dir() or path.is_symlink():
# Skip simlinks as they are allowed (even with a dot)
continue
if "." in path.name:
yield path
else:
yield from find_problematic_folders(path)
def move_contents_to_resources(folder: Path) -> Generator[Path, None, None]:
"""Recursively move any non symlink file from a problematic folder
to the sibbling one in Resources.
"""
for path in folder.iterdir():
if path.is_symlink():
continue
if path.name == "qml":
yield from move_contents_to_resources(path)
else:
sibbling = Path(str(path).replace("MacOS", "Resources"))
sibbling.parent.mkdir(parents=True, exist_ok=True)
shutil.move(path, sibbling)
yield sibbling
def main(args: List[str]) -> int:
"""
Fix the application to allow codesign (NXDRIVE-1301).
Take one or more .app as arguments: "Nuxeo Drive.app".
To overall process will:
- move problematic folders from MacOS to Resources
- fix the DLLs lookup paths
- create the appropriate symbolic link
"""
for app in args:
name = os.path.basename(app)
print(f">>> [{name}] Fixing Qt folder names")
path = Path(app) / "Contents" / "MacOS"
for folder in find_problematic_folders(path):
for file in move_contents_to_resources(folder):
try:
fix_dll(file)
except (ValueError, IsADirectoryError):
continue
shutil.rmtree(folder)
create_symlink(folder)
print(f" !! Fixed {folder}")
print(f">>> [{name}] Application fixed.")
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))

22
package/osx/rebuild_mac_icon.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/sh
# Build macOS specific icon set
ICON_FOLDER=icons/Manuskript
FULLSIZE_ICON=$ICON_FOLDER/icon-512px.png
TMP_ICONSET_FOLDER=$ICON_FOLDER/Manuskript.iconset
TARGET_ICONSET=$ICON_FOLDER/Manuskript.icns
mkdir $TMP_ICONSET_FOLDER
sips -z 16 16 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_16x16.png
sips -z 32 32 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_16x16@2x.png
sips -z 32 32 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_32x32.png
sips -z 64 64 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_32x32@2x.png
sips -z 128 128 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_128x128.png
sips -z 256 256 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_128x128@2x.png
sips -z 256 256 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_256x256.png
sips -z 512 512 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_256x256@2x.png
sips -z 512 512 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_512x512.png
sips -z 1024 1024 $FULLSIZE_ICON --out $TMP_ICONSET_FOLDER/icon_512x512@2x.png
iconutil -c icns --output $TARGET_ICONSET $TMP_ICONSET_FOLDER
rm -R $TMP_ICONSET_FOLDER

View File

@ -12,7 +12,7 @@ export HOMEBREW_NO_AUTO_UPDATE=1 # (please let it go, homebrew!)
brew install enchant
brew postinstall python # this installs pip
sudo -H pip3 install --upgrade pip setuptools wheel
pip3 install pyinstaller PyQt5 lxml pyenchant pytest pytest-faulthandler
pip3 install pyinstaller PyQt5 lxml pyenchant pytest pytest-faulthandler pillow dmgbuild
brew install hunspell
# Fooling PyEnchant as described in the wiki.
# https://github.com/olivierkes/manuskript/wiki/Package-manuskript-for-OS-X