Skip to content

Commit

Permalink
Fewer utils
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 10, 2023
1 parent ae5268a commit 6852dd7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 69 deletions.
7 changes: 4 additions & 3 deletions src/bundle/mac/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {copyFile, mkdir, stat, writeFile} from 'node:fs/promises';
import {copyFile, mkdir, readFile, stat, writeFile} from 'node:fs/promises';
import {join as pathJoin, basename, dirname} from 'node:path';

import {fsLstatExists} from '@shockpkg/archive-files';
import {Plist} from '@shockpkg/plist-dom';

import {trimExtension} from '../../util';
import {
plistRead,
infoPlistBundleExecutableSet,
infoPlistBundleIconFileGet,
machoTypesFile,
Expand Down Expand Up @@ -77,7 +77,8 @@ export class BundleMacApp extends BundleMac {
const appPkgInfo = pathJoin(appContents, 'PkgInfo');

// Read the projector Info.plist.
const plist = await plistRead(projector.infoPlistPath);
const plist = new Plist();
plist.fromXml(await readFile(projector.infoPlistPath, 'utf8'));

// Get the binary path and read the types.
const projBinaryPath = projector.binaryPath;
Expand Down
76 changes: 35 additions & 41 deletions src/projector/mac/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import {
trimExtension
} from '../../util';
import {
plistRead,
plistParse,
infoPlistBundleExecutableSet,
infoPlistBundleIconFileSet,
infoPlistBundleNameSet
Expand Down Expand Up @@ -431,23 +429,22 @@ export class ProjectorMacApp extends ProjectorMac {
}

/**
* Get Info.plist data if any specified, document, data, or file.
* Get Info.plist data if any specified, from data or file.
*
* @returns Info.plist data or null.
*/
public async getInfoPlistDocument() {
public async getInfoPlistData() {
const {infoPlistData, infoPlistFile} = this;
let xml;
if (typeof infoPlistData === 'string') {
xml = infoPlistData;
} else if (infoPlistData) {
xml = infoPlistData.toString('utf8');
} else if (infoPlistFile) {
xml = await readFile(infoPlistFile, 'utf8');
} else {
return null;
return infoPlistData;
}
if (infoPlistData) {
return infoPlistData.toString('utf8');
}
return plistParse(xml);
if (infoPlistFile) {
return readFile(infoPlistFile, 'utf8');
}
return null;
}

/**
Expand Down Expand Up @@ -731,24 +728,43 @@ export class ProjectorMacApp extends ProjectorMac {
* Update the projector Info.plist if needed.
*/
protected async _updateInfoPlist() {
const customPlist = await this.getInfoPlistDocument();
const path = this.infoPlistPath;
const xml = await this._generateInfoPlist();
if (xml === null) {
return;
}

await rm(path, {force: true});
await mkdir(dirname(path), {recursive: true});
await writeFile(path, xml, 'utf8');
}

/**
* Generate Info.plist XML string, if any.
*
* @returns XML string or null.
*/
protected async _generateInfoPlist() {
const customPlist = await this.getInfoPlistData();
const bundleName = this.getBundleName();
const {appBinaryNameCustom, appIconNameCustom} = this;
if (
!(
customPlist ||
customPlist !== null ||
appIconNameCustom ||
appBinaryNameCustom ||
bundleName !== false
)
) {
return;
return null;
}

// Use a custom plist or the existing one.
const plist = customPlist || (await this._readInfoPlist());
const xml = customPlist ?? (await readFile(this.infoPlistPath, 'utf8'));

const plist = new Plist();
plist.fromXml(xml);

// Update values.
if (appIconNameCustom) {
infoPlistBundleIconFileSet(plist, appIconNameCustom);
}
Expand All @@ -759,28 +775,6 @@ export class ProjectorMacApp extends ProjectorMac {
infoPlistBundleNameSet(plist, bundleName);
}

// Write out the plist.
await this._writeInfoPlist(plist);
}

/**
* Read the projector Info.plist file.
*
* @returns Plist document.
*/
protected async _readInfoPlist() {
return plistRead(this.infoPlistPath);
}

/**
* Write the projector Info.plist file.
*
* @param plist Plist document.
*/
protected async _writeInfoPlist(plist: Plist) {
const path = this.infoPlistPath;
await rm(path, {force: true});
await mkdir(dirname(path), {recursive: true});
await writeFile(path, plist.toXml(), 'utf8');
return plist.toXml();
}
}
25 changes: 0 additions & 25 deletions src/util/mac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,6 @@ export interface IMachoType {
cpuSubtype: number;
}

/**
* Parse plist data.
* Currently only supports XML plist.
*
* @param data Plist XML.
* @returns Plist document.
*/
// eslint-disable-next-line @typescript-eslint/require-await
export async function plistParse(data: string) {
const plist = new Plist();
plist.fromXml(data);
return plist;
}

/**
* Read plist file.
* Currently only supports XML plist.
*
* @param path Plist file.
* @returns Plist document.
*/
export async function plistRead(path: string) {
return plistParse(await readFile(path, 'utf8'));
}

/**
* Get Info.plist dictionary or throw.
*
Expand Down

0 comments on commit 6852dd7

Please sign in to comment.