2023-06-19 14:19:09 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
2023-07-29 20:11:20 +00:00
|
|
|
from .log import *
|
2023-07-27 02:28:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def attach_bundle_commands(subparsers):
|
|
|
|
mkapp_cmd = subparsers.add_parser("bundle", help="Package a WebAssembly module into a standalone Orca application.")
|
|
|
|
init_parser(mkapp_cmd)
|
|
|
|
|
|
|
|
|
|
|
|
def init_parser(parser):
|
|
|
|
parser.add_argument("-d", "--resource", action="append", dest="resource_files", help="copy a file to the app's resource directory")
|
|
|
|
parser.add_argument("-D", "--resource-dir", action="append", dest="resource_dirs", help="copy a directory to the app's resource directory")
|
|
|
|
parser.add_argument("-i", "--icon", help="an image file to use as the application's icon")
|
|
|
|
parser.add_argument("-C", "--out-dir", default=os.getcwd(), help="where to place the final application bundle (defaults to the current directory)")
|
|
|
|
parser.add_argument("-n", "--name", default="out", help="the app's name")
|
|
|
|
parser.add_argument("-O", "--orca-dir", default=".")
|
|
|
|
parser.add_argument("--version", default="0.0.0", help="a version number to embed in the application bundle")
|
|
|
|
parser.add_argument("module", help="a .wasm file containing the application's wasm module")
|
|
|
|
parser.set_defaults(func=shellish(make_app))
|
|
|
|
|
|
|
|
|
|
|
|
def make_app(args):
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
# Dispatch to platform-specific function
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
platformName = platform.system()
|
|
|
|
if platformName == 'Darwin':
|
|
|
|
macos_make_app(args)
|
|
|
|
elif platformName == 'Windows':
|
|
|
|
windows_make_app(args)
|
|
|
|
else:
|
|
|
|
log_error("Platform '" + platformName + "' is not supported for now...")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
2023-06-19 14:19:09 +00:00
|
|
|
def macos_make_app(args):
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE: make bundle directory structure
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
app_name = args.name
|
|
|
|
bundle_name = app_name + '.app'
|
2023-07-27 02:28:39 +00:00
|
|
|
bundle_path = os.path.join(args.out_dir, bundle_name)
|
|
|
|
contents_dir = os.path.join(bundle_path, 'Contents')
|
|
|
|
exe_dir = os.path.join(contents_dir, 'MacOS')
|
|
|
|
res_dir = os.path.join(contents_dir, 'resources')
|
|
|
|
guest_dir = os.path.join(contents_dir, 'app')
|
|
|
|
wasm_dir = os.path.join(guest_dir, 'wasm')
|
|
|
|
data_dir = os.path.join(guest_dir, 'data')
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
if os.path.exists(bundle_path):
|
|
|
|
shutil.rmtree(bundle_path)
|
|
|
|
os.mkdir(bundle_path)
|
|
|
|
os.mkdir(contents_dir)
|
|
|
|
os.mkdir(exe_dir)
|
|
|
|
os.mkdir(res_dir)
|
|
|
|
os.mkdir(guest_dir)
|
|
|
|
os.mkdir(wasm_dir)
|
|
|
|
os.mkdir(data_dir)
|
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE: copy orca runtime executable and libraries
|
|
|
|
#-----------------------------------------------------------
|
2023-07-27 02:28:39 +00:00
|
|
|
orca_exe = os.path.join(args.orca_dir, 'build/bin/orca')
|
|
|
|
milepost_lib = os.path.join(args.orca_dir, 'build/bin/libmilepost.dylib')
|
|
|
|
gles_lib = os.path.join(args.orca_dir, 'build/bin/libGLESv2.dylib')
|
|
|
|
egl_lib = os.path.join(args.orca_dir, 'build/bin/libEGL.dylib')
|
|
|
|
renderer_lib = os.path.join(args.orca_dir, 'build/bin/mtl_renderer.metallib')
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
shutil.copy(orca_exe, exe_dir)
|
|
|
|
shutil.copy(milepost_lib, exe_dir)
|
|
|
|
shutil.copy(gles_lib, exe_dir)
|
|
|
|
shutil.copy(egl_lib, exe_dir)
|
|
|
|
shutil.copy(renderer_lib, exe_dir)
|
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE: copy wasm module and data
|
|
|
|
#-----------------------------------------------------------
|
2023-07-27 02:28:39 +00:00
|
|
|
shutil.copy(args.module, os.path.join(wasm_dir, 'module.wasm'))
|
2023-06-19 14:19:09 +00:00
|
|
|
|
2023-06-26 10:24:22 +00:00
|
|
|
if args.resource_files != None:
|
|
|
|
for resource in args.resource_files:
|
2023-07-27 02:28:39 +00:00
|
|
|
shutil.copytree(resource, os.path.join(data_dir, os.path.basename(resource)), dirs_exist_ok=True)
|
2023-06-19 14:19:09 +00:00
|
|
|
|
2023-06-26 10:24:22 +00:00
|
|
|
if args.resource_dirs != None:
|
|
|
|
for resource_dir in args.resource_dirs:
|
|
|
|
for resource in os.listdir(resource_dir):
|
2023-07-27 02:28:39 +00:00
|
|
|
src = os.path.join(resource_dir, resource)
|
2023-06-26 10:24:22 +00:00
|
|
|
if os.path.isdir(src):
|
2023-07-27 02:28:39 +00:00
|
|
|
shutil.copytree(src, os.path.join(data_dir, os.path.basename(resource)), dirs_exist_ok=True)
|
2023-06-26 10:24:22 +00:00
|
|
|
else:
|
|
|
|
shutil.copy(src, data_dir)
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE: copy runtime resources
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
# default fonts
|
2023-07-27 02:28:39 +00:00
|
|
|
shutil.copy(os.path.join(args.orca_dir, 'resources/OpenSansLatinSubset.ttf'), res_dir)
|
|
|
|
shutil.copy(os.path.join(args.orca_dir, 'resources/Menlo.ttf'), res_dir)
|
|
|
|
shutil.copy(os.path.join(args.orca_dir, 'resources/Menlo Bold.ttf'), res_dir)
|
|
|
|
shutil.copy(os.path.join(args.orca_dir, 'resources/Menlo Italics.ttf'), res_dir)
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE make icon
|
|
|
|
#-----------------------------------------------------------
|
2023-07-27 02:28:39 +00:00
|
|
|
src_image = args.icon
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
#if src_image == None:
|
|
|
|
# src_image = orca_dir + '/resources/default_app_icon.png'
|
|
|
|
|
|
|
|
if src_image != None:
|
|
|
|
iconset = os.path.splitext(src_image)[0] + '.iconset'
|
|
|
|
|
|
|
|
if os.path.exists(iconset):
|
|
|
|
shutil.rmtree(iconset)
|
|
|
|
|
|
|
|
os.mkdir(iconset)
|
|
|
|
|
|
|
|
size = 16
|
|
|
|
for i in range(0, 7):
|
|
|
|
size_str = str(size)
|
|
|
|
icon = 'icon_' + size_str + 'x' + size_str + '.png'
|
|
|
|
subprocess.run(['sips', '-z', size_str, size_str, src_image, '--out', iconset + '/' + icon],
|
|
|
|
stdout = subprocess.DEVNULL,
|
|
|
|
stderr = subprocess.DEVNULL)
|
|
|
|
|
|
|
|
size_str_retina = str(size*2)
|
|
|
|
icon = 'icon_' + size_str + 'x' + size_str + '@2x.png'
|
|
|
|
subprocess.run(['sips', '-z', size_str_retina, size_str_retina, src_image, '--out', iconset + '/' + icon],
|
|
|
|
stdout = subprocess.DEVNULL,
|
|
|
|
stderr = subprocess.DEVNULL)
|
|
|
|
|
|
|
|
size = size*2
|
|
|
|
|
2023-07-27 02:28:39 +00:00
|
|
|
subprocess.run(['iconutil', '-c', 'icns', '-o', os.path.join(res_dir, 'icon.icns'), iconset])
|
2023-06-19 14:19:09 +00:00
|
|
|
shutil.rmtree(iconset)
|
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE: write plist file
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
version = args.version
|
|
|
|
bundle_sig = "????"
|
|
|
|
icon_file = ''
|
|
|
|
|
2023-07-27 02:28:39 +00:00
|
|
|
plist_contents = f"""
|
2023-06-19 14:19:09 +00:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
<plist version="1.0">
|
|
|
|
<dict>
|
|
|
|
<key>CFBundleName</key>
|
|
|
|
<string>{app_name}</string>
|
|
|
|
<key>CFBundleDisplayName</key>
|
|
|
|
<string>{app_name}</string>
|
|
|
|
<key>CFBundleIdentifier</key>
|
|
|
|
<string>{app_name}</string>
|
|
|
|
<key>CFBundleVersion</key>
|
|
|
|
<string>{version}</string>
|
|
|
|
<key>CFBundlePackageType</key>
|
|
|
|
<string>APPL</string>
|
|
|
|
<key>CFBundleSignature</key>
|
|
|
|
<string>{bundle_sig}</string>
|
|
|
|
<key>CFBundleExecutable</key>
|
|
|
|
<string>orca</string>
|
|
|
|
<key>CFBundleIconFile</key>
|
|
|
|
<string>icon.icns</string>
|
|
|
|
<key>NSHighResolutionCapable</key>
|
|
|
|
<string>True</string>
|
|
|
|
</dict>
|
|
|
|
</plist>
|
2023-07-27 02:28:39 +00:00
|
|
|
"""
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
plist_file = open(contents_dir + '/Info.plist', 'w')
|
|
|
|
print(plist_contents, file=plist_file)
|
|
|
|
|
|
|
|
def windows_make_app(args):
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE: make bundle directory structure
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
app_name = args.name
|
|
|
|
bundle_name = app_name
|
2023-08-08 03:10:32 +00:00
|
|
|
bundle_dir = os.path.join(args.out_dir, bundle_name)
|
|
|
|
exe_dir = os.path.join(bundle_dir, 'bin')
|
|
|
|
res_dir = os.path.join(bundle_dir, 'resources')
|
|
|
|
guest_dir = os.path.join(bundle_dir, 'app')
|
|
|
|
wasm_dir = os.path.join(guest_dir, 'wasm')
|
|
|
|
data_dir = os.path.join(guest_dir, 'data')
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
if os.path.exists(bundle_dir):
|
|
|
|
shutil.rmtree(bundle_dir)
|
|
|
|
os.mkdir(bundle_dir)
|
|
|
|
os.mkdir(exe_dir)
|
|
|
|
os.mkdir(res_dir)
|
|
|
|
os.mkdir(guest_dir)
|
|
|
|
os.mkdir(wasm_dir)
|
|
|
|
os.mkdir(data_dir)
|
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE: copy orca runtime executable and libraries
|
|
|
|
#-----------------------------------------------------------
|
2023-08-08 03:10:32 +00:00
|
|
|
orca_exe = os.path.join(args.orca_dir, 'build/bin/orca.exe')
|
|
|
|
milepost_lib = os.path.join(args.orca_dir, 'build/bin/milepost.dll')
|
|
|
|
gles_lib = os.path.join(args.orca_dir, 'milepost/build/bin/libGLESv2.dll')
|
|
|
|
egl_lib = os.path.join(args.orca_dir, 'milepost/build/bin/libEGL.dll')
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
shutil.copy(orca_exe, exe_dir)
|
|
|
|
shutil.copy(milepost_lib, exe_dir)
|
|
|
|
shutil.copy(gles_lib, exe_dir)
|
|
|
|
shutil.copy(egl_lib, exe_dir)
|
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE: copy wasm module and data
|
|
|
|
#-----------------------------------------------------------
|
2023-07-02 10:50:32 +00:00
|
|
|
|
2023-06-19 14:19:09 +00:00
|
|
|
shutil.copy(args.module, wasm_dir + '/module.wasm')
|
|
|
|
|
2023-07-02 10:50:32 +00:00
|
|
|
if args.resource_files != None:
|
|
|
|
for resource in args.resource_files:
|
|
|
|
shutil.copytree(resource, data_dir + '/' + os.path.basename(resource), dirs_exist_ok=True)
|
2023-06-19 14:19:09 +00:00
|
|
|
|
2023-07-02 10:50:32 +00:00
|
|
|
if args.resource_dirs != None:
|
|
|
|
for resource_dir in args.resource_dirs:
|
|
|
|
for resource in os.listdir(resource_dir):
|
|
|
|
src = resource_dir + '/' + resource
|
|
|
|
if os.path.isdir(src):
|
|
|
|
shutil.copytree(src, data_dir + '/' + os.path.basename(resource), dirs_exist_ok=True)
|
|
|
|
else:
|
|
|
|
shutil.copy(src, data_dir)
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE: copy runtime resources
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
# default fonts
|
2023-08-08 03:10:32 +00:00
|
|
|
shutil.copy(os.path.join(args.orca_dir, 'resources/OpenSansLatinSubset.ttf'), res_dir)
|
|
|
|
shutil.copy(os.path.join(args.orca_dir, 'resources/Menlo.ttf'), res_dir)
|
|
|
|
shutil.copy(os.path.join(args.orca_dir, 'resources/Menlo Bold.ttf'), res_dir)
|
|
|
|
shutil.copy(os.path.join(args.orca_dir, 'resources/Menlo Italics.ttf'), res_dir)
|
2023-06-19 14:19:09 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#NOTE make icon
|
|
|
|
#-----------------------------------------------------------
|
|
|
|
#TODO
|
|
|
|
|
2023-08-08 02:17:24 +00:00
|
|
|
|
2023-07-27 02:28:39 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = ArgumentParser(prog='mkapp')
|
|
|
|
init_parser(parser)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2023-08-08 02:17:24 +00:00
|
|
|
make_app(args)
|