Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@
"style-loader": "^2.0.0",
"svelte": "^3.44.3",
"svelte-loader": "^3.1.2",
"ts-loader": "^8.4.0",
"typescript": "^4.7.4",
"url-loader": "^4.1.1",
"webpack": "^4.46.0",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-cli": "^4.9.1",
"webpack-dev-server": "^3.11.3",
"webpack-merge": "^5.8.0",
"worker-loader": "^2.0.0"
}
}
122 changes: 0 additions & 122 deletions src/packager/plist.js

This file was deleted.

134 changes: 134 additions & 0 deletions src/packager/plist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Parses and generates Apple Info.plist files
// Example file:
/*
<?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>BuildMachineOSBuild</key>
<string>20F71</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>WebView</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
<key>CFBundleIconName</key>
<string>AppIcon</string>
<key>CFBundleIdentifier</key>
<string>org.turbowarp.webviews.mac</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>WebView</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>DTCompiler</key>
<string>com.apple.compilers.llvm.clang.1_0</string>
<key>DTPlatformBuild</key>
<string>12E507</string>
<key>DTPlatformName</key>
<string>macosx</string>
<key>DTPlatformVersion</key>
<string>11.3</string>
<key>DTSDKBuild</key>
<string>20E214</string>
<key>DTSDKName</key>
<string>macosx11.3</string>
<key>DTXcode</key>
<string>1251</string>
<key>DTXcodeBuild</key>
<string>12E507</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.games</string>
<key>LSMinimumSystemVersion</key>
<string>10.12</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
*/

type PlistValue = string | Plist | PlistValue[];
interface Plist {
[s: string]: PlistValue;
}

const xmlToValue = (node: Element): PlistValue => {
if (node.tagName === 'dict') {
const result: Plist = {};
for (const child of node.children) {
if (child.tagName === 'key') {
const next = child.nextElementSibling;
if (!next) {
throw new Error('Plist dict key is missing value');
}
result[child.textContent!] = xmlToValue(next);
}
}
return result;
} else if (node.tagName === 'array') {
return Array.from(node.children).map(xmlToValue);
} else if (node.tagName === 'string') {
return node.textContent!;
}
throw new Error('Failed to parse plist value');
};

const valueToXml = (doc: XMLDocument, value: PlistValue): Element => {
if (Array.isArray(value)) {
const node = doc.createElement('array');
for (const listItem of value) {
node.appendChild(valueToXml(doc, listItem));
}
return node;
} else if (typeof value === 'object') {
const node = doc.createElement('dict');
for (const [key, keyValue] of Object.entries(value)) {
const keyNode = doc.createElement('key');
keyNode.textContent = key;
const valueNode = valueToXml(doc, keyValue);
node.appendChild(keyNode);
node.appendChild(valueNode);
}
return node;
} else if (typeof value === 'string') {
const node = doc.createElement('string');
node.textContent = value;
return node;
}
console.warn('unknown plist value', value);
return valueToXml(doc, `${value}`);
};

export const parsePlist = (string: string): Plist => {
const xml = new DOMParser().parseFromString(string, 'text/xml');
const rootNode = xml.children[0];
const rootDict = rootNode.children[0];
const plist = xmlToValue(rootDict);
if (typeof plist !== 'object' || Array.isArray(plist)) {
throw new Error('Top level object in plist was not a dict');
}
return plist;
};

export const generatePlist = (values: Plist): string => {
const xml = document.implementation.createDocument(null, "plist");
const rootNode = xml.documentElement;
rootNode.setAttribute('version', '1.0');
rootNode.appendChild(valueToXml(xml, values));
const serialized = new XMLSerializer().serializeToString(xml);
return `<?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">
${serialized}`;
};
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"outDir": "./dist/",
"target": "es2018",
"module": "es6",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"strict": true,
"noImplicitAny": true
}
}
Loading