181 lines
6.0 KiB
JavaScript
181 lines
6.0 KiB
JavaScript
import {
|
|
mkdirSync,
|
|
readFileSync,
|
|
writeFileSync,
|
|
} from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
import { MACOS_MIN_VERSION } from "./app-package-targets.mjs";
|
|
import semver from "semver";
|
|
|
|
const PNG_SIGNATURE = Buffer.from([
|
|
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
|
|
]);
|
|
|
|
function fail(message) {
|
|
throw new Error(`App icon assets are invalid: ${message}`);
|
|
}
|
|
|
|
function readPngEntries(iconPath) {
|
|
const icon = readFileSync(iconPath);
|
|
if (
|
|
icon.length < 6 ||
|
|
icon.readUInt16LE(0) !== 0 ||
|
|
icon.readUInt16LE(2) !== 1
|
|
) {
|
|
fail("ICO header");
|
|
}
|
|
const count = icon.readUInt16LE(4);
|
|
const entries = new Map();
|
|
for (let index = 0; index < count; index += 1) {
|
|
const offset = 6 + index * 16;
|
|
if (offset + 16 > icon.length) fail("ICO directory");
|
|
const width = icon[offset] || 256;
|
|
const height = icon[offset + 1] || 256;
|
|
const length = icon.readUInt32LE(offset + 8);
|
|
const dataOffset = icon.readUInt32LE(offset + 12);
|
|
const data = icon.subarray(dataOffset, dataOffset + length);
|
|
if (
|
|
data.length !== length ||
|
|
!data.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)
|
|
) {
|
|
fail(`PNG payload ${width}x${height}`);
|
|
}
|
|
if (width !== height) fail(`non-square source ${width}x${height}`);
|
|
entries.set(width, data);
|
|
}
|
|
return entries;
|
|
}
|
|
|
|
function requireEntry(entries, size) {
|
|
const data = entries.get(size);
|
|
if (!data) fail(`missing ${size}px source`);
|
|
return data;
|
|
}
|
|
|
|
function writeIcns(entries, output) {
|
|
const kinds = [
|
|
[16, "icp4"],
|
|
[32, "icp5"],
|
|
[64, "icp6"],
|
|
[128, "ic07"],
|
|
[256, "ic08"],
|
|
];
|
|
const chunks = kinds.map(([size, kind]) => {
|
|
const data = requireEntry(entries, size);
|
|
const header = Buffer.alloc(8);
|
|
header.write(kind, 0, 4, "ascii");
|
|
header.writeUInt32BE(data.length + 8, 4);
|
|
return Buffer.concat([header, data]);
|
|
});
|
|
const result = Buffer.alloc(
|
|
8 + chunks.reduce((total, chunk) => total + chunk.length, 0),
|
|
);
|
|
result.write("icns", 0, 4, "ascii");
|
|
result.writeUInt32BE(result.length, 4);
|
|
let offset = 8;
|
|
for (const chunk of chunks) {
|
|
chunk.copy(result, offset);
|
|
offset += chunk.length;
|
|
}
|
|
writeFileSync(output, result);
|
|
}
|
|
|
|
function writeLinuxAssets(packageRoot, entries) {
|
|
const iconDirectory = join(
|
|
packageRoot,
|
|
"share",
|
|
"icons",
|
|
"hicolor",
|
|
"256x256",
|
|
"apps",
|
|
);
|
|
const applicationDirectory = join(packageRoot, "share", "applications");
|
|
mkdirSync(iconDirectory, { recursive: true });
|
|
mkdirSync(applicationDirectory, { recursive: true });
|
|
writeFileSync(
|
|
join(iconDirectory, "piik-app.png"),
|
|
requireEntry(entries, 256),
|
|
);
|
|
writeFileSync(
|
|
join(applicationDirectory, "piik-app.desktop"),
|
|
[
|
|
"[Desktop Entry]",
|
|
"Version=1.0",
|
|
"Type=Application",
|
|
"Name=Piik App",
|
|
"Exec=piik-app",
|
|
"TryExec=piik-app",
|
|
"Icon=piik-app",
|
|
"Terminal=true",
|
|
"Categories=Network;Utility;",
|
|
"StartupNotify=true",
|
|
"",
|
|
].join("\n"),
|
|
"utf8",
|
|
);
|
|
}
|
|
|
|
function writeMacAssets(packageRoot, version, revision, entries) {
|
|
const parsed = semver.parse(version);
|
|
if (version !== "development" && (!version.startsWith("v") || !parsed || parsed.version !== version.slice(1)))
|
|
throw new Error("macOS bundle requires a valid build version");
|
|
const bundleVersion = parsed ? `${parsed.major}.${parsed.minor}.${parsed.patch}` : "0.0.0";
|
|
const bundleRoot = join(packageRoot, "Piik App.app");
|
|
const contents = join(bundleRoot, "Contents");
|
|
const macos = join(contents, "MacOS");
|
|
const resources = join(contents, "Resources");
|
|
mkdirSync(macos, { recursive: true });
|
|
mkdirSync(resources, { recursive: true });
|
|
writeIcns(entries, join(resources, "piik.icns"));
|
|
writeFileSync(
|
|
join(contents, "Info.plist"),
|
|
[
|
|
'<?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>CFBundleDevelopmentRegion</key><string>en</string>",
|
|
"<key>CFBundleDisplayName</key><string>Piik App</string>",
|
|
"<key>CFBundleExecutable</key><string>piik-app</string>",
|
|
"<key>CFBundleIconFile</key><string>piik.icns</string>",
|
|
"<key>CFBundleIdentifier</key><string>tv.piik.app</string>",
|
|
"<key>CFBundleInfoDictionaryVersion</key><string>6.0</string>",
|
|
"<key>CFBundleName</key><string>Piik App</string>",
|
|
"<key>CFBundlePackageType</key><string>APPL</string>",
|
|
`<key>CFBundleShortVersionString</key><string>${bundleVersion}</string>`,
|
|
`<key>LSMinimumSystemVersion</key><string>${MACOS_MIN_VERSION}</string>`,
|
|
"<key>NSLocalNetworkUsageDescription</key><string>Connect to devices on your local network for Piik rooms and screen sharing.</string>",
|
|
"<key>NSScreenCaptureUsageDescription</key><string>Share a screen or application selected by you.</string>",
|
|
"<key>NSMicrophoneUsageDescription</key><string>Use your microphone for voice communication.</string>",
|
|
"<key>NSAudioCaptureUsageDescription</key><string>Share sound from the selected screen or application.</string>",
|
|
`<key>CFBundleVersion</key><string>${bundleVersion}</string>`,
|
|
`<key>PiikSourceRevision</key><string>${revision}</string>`,
|
|
`<key>PiikProductVersion</key><string>${version}</string>`,
|
|
"</dict></plist>",
|
|
"",
|
|
].join("\n"),
|
|
"utf8",
|
|
);
|
|
}
|
|
|
|
export function writeAppPlatformAssets({
|
|
packageRoot,
|
|
target,
|
|
version,
|
|
revision,
|
|
iconPath,
|
|
}) {
|
|
const entries = readPngEntries(iconPath);
|
|
if (target.goos === "linux") writeLinuxAssets(packageRoot, entries);
|
|
if (target.goos === "darwin") writeMacAssets(packageRoot, version, revision, entries);
|
|
return target.goos === "linux"
|
|
? "share/applications/piik-app.desktop"
|
|
: target.goos === "darwin"
|
|
? "Piik App.app"
|
|
: null;
|
|
}
|
|
|
|
export function writeAndroidAppIcon(iconPath, outputFile) {
|
|
mkdirSync(dirname(outputFile), { recursive: true });
|
|
writeFileSync(outputFile, requireEntry(readPngEntries(iconPath), 256));
|
|
}
|