Skip to content

Commit ab664ac

Browse files
committed
feat(mac): control the localization files
Close #708
1 parent ba9e9da commit ab664ac

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

packages/electron-builder-lib/src/configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export interface Configuration extends PlatformSpecificBuildOptions {
155155
* Whether to fail if the application is not signed (to prevent unsigned app if code signing configuration is not correct).
156156
* @default false
157157
*/
158-
readonly forceCodeSigning?: boolean
158+
readonly ?: boolean
159159

160160
/**
161161
* The version of muon you are packaging for.

packages/electron-builder-lib/src/macPackager.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import BluebirdPromise from "bluebird-lst"
22
import { Arch, AsyncTaskManager, exec, InvalidConfigurationError, log } from "builder-util"
33
import { signAsync, SignOptions } from "electron-osx-sign"
4-
import { ensureDir } from "fs-extra-p"
4+
import { ensureDir, readdir, remove } from "fs-extra-p"
55
import { Lazy } from "lazy-val"
66
import * as path from "path"
77
import { deepAssign } from "read-config-file/out/deepAssign"
88
import * as semver from "semver"
9+
import { AsarIntegrity } from "asar-integrity"
10+
import { asArray } from "builder-util-runtime/out"
911
import { AppInfo } from "./appInfo"
1012
import { appleCertificatePrefixes, CertType, CodeSigningInfo, createKeychain, findIdentity, Identity, isSignAllowed, reportError } from "./codeSign"
1113
import { DIR_TARGET, Platform, Target } from "./core"
1214
import { MacConfiguration, MasConfiguration } from "./options/macOptions"
1315
import { Packager } from "./packager"
16+
import { createMacApp } from "./packager/mac"
1417
import { PlatformPackager } from "./platformPackager"
1518
import { ArchiveTarget } from "./targets/ArchiveTarget"
1619
import { DmgTarget } from "./targets/dmg"
1720
import { PkgTarget, prepareProductBuildArgs } from "./targets/pkg"
1821
import { createCommonTarget, NoOpTarget } from "./targets/targetFactory"
22+
import { CONCURRENCY } from "builder-util/out/fs"
1923

2024
export default class MacPackager extends PlatformPackager<MacConfiguration> {
2125
readonly codeSigningInfo: Promise<CodeSigningInfo>
@@ -255,6 +259,30 @@ export default class MacPackager extends PlatformPackager<MacConfiguration> {
255259
public getElectronDestinationDir(appOutDir: string) {
256260
return path.join(appOutDir, this.electronDistMacOsAppName)
257261
}
262+
263+
protected async beforeCopyExtraFiles(appOutDir: string, asarIntegrity: AsarIntegrity | null): Promise<any> {
264+
await createMacApp(this, appOutDir, asarIntegrity)
265+
266+
const wantedLanguages = asArray(this.platformSpecificBuildOptions.electronLanguages)
267+
if (wantedLanguages == null) {
268+
return
269+
}
270+
271+
// noinspection SpellCheckingInspection
272+
const langFileExt = ".lproj"
273+
const resourcesDir = this.getResourcesDir(appOutDir)
274+
await BluebirdPromise.map(readdir(resourcesDir), file => {
275+
if (!file.endsWith(langFileExt)) {
276+
return
277+
}
278+
279+
const language = file.substring(0, file.length - langFileExt.length)
280+
if (!wantedLanguages.includes(language)) {
281+
return remove(path.join(resourcesDir, file))
282+
}
283+
return
284+
}, CONCURRENCY)
285+
}
258286
}
259287

260288
function getCertificateType(isMas: boolean, isDevelopment: boolean): CertType {

packages/electron-builder-lib/src/options/macOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ export interface MacConfiguration extends PlatformSpecificBuildOptions {
9393
* 2.16.0 files
9494
*/
9595
readonly electronUpdaterCompatibility?: string | null
96+
97+
/**
98+
* The electron locales. By default Electron locales used as is.
99+
*/
100+
readonly electronLanguages?: Array<string> | string
96101
}
97102

98103
/**

packages/electron-builder-lib/src/platformPackager.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Lazy } from "lazy-val"
99
import { Minimatch } from "minimatch"
1010
import * as path from "path"
1111
import { deepAssign } from "read-config-file/out/deepAssign"
12+
import { AsarIntegrity } from "asar-integrity"
1213
import { AppInfo } from "./appInfo"
1314
import { checkFileInArchive } from "./asar/asarFileChecker"
1415
import { AsarPackager } from "./asar/asarUtil"
@@ -18,7 +19,6 @@ import { createTransformer, isElectronCompileUsed } from "./fileTransformer"
1819
import { AfterPackContext, AsarOptions, Configuration, FileAssociation, PlatformSpecificBuildOptions } from "./index"
1920
import { Packager } from "./packager"
2021
import { unpackElectron, unpackMuon } from "./packager/dirPackager"
21-
import { createMacApp } from "./packager/mac"
2222
import { PackagerOptions } from "./packagerApi"
2323
import { getAppBuilderTool } from "./targets/tools"
2424
import { copyAppFiles } from "./util/appFileCopier"
@@ -198,11 +198,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
198198
}
199199

200200
await taskManager.awaitTasks()
201-
202-
if (platformName === "darwin" || platformName === "mas") {
203-
await createMacApp(this, appOutDir, asarOptions == null ? null : await computeData(resourcesPath, asarOptions.externalAllowed ? {externalAllowed: true} : null))
204-
}
205-
201+
await this.beforeCopyExtraFiles(appOutDir, asarOptions == null ? null : await computeData(resourcesPath, asarOptions.externalAllowed ? {externalAllowed: true} : null))
206202
await BluebirdPromise.each([extraResourceMatchers, extraFileMatchers], it => copyFiles(it))
207203

208204
if (this.info.cancellationToken.cancelled) {
@@ -215,6 +211,10 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
215211
await this.info.afterSign(packContext)
216212
}
217213

214+
protected async beforeCopyExtraFiles(appOutDir: string, asarIntegrity: AsarIntegrity | null) {
215+
// empty impl
216+
}
217+
218218
private copyAppFiles(taskManager: AsyncTaskManager, asarOptions: AsarOptions | null, resourcePath: string, outDir: string, platformSpecificBuildOptions: DC, excludePatterns: Array<Minimatch>, macroExpander: ((it: string) => string)) {
219219
const appDir = this.info.appDir
220220
const config = this.config
@@ -256,7 +256,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
256256
}
257257

258258
protected signApp(packContext: AfterPackContext): Promise<any> {
259-
return BluebirdPromise.resolve()
259+
return Promise.resolve()
260260
}
261261

262262
async getIconPath(): Promise<string | null> {

test/out/mac/__snapshots__/macPackagerTest.js.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,14 @@ Object {
162162
],
163163
}
164164
`;
165+
166+
exports[`two-package 2`] = `
167+
Array [
168+
"TestApp.icns",
169+
"app-update.yml",
170+
"app.asar",
171+
"bn.lproj",
172+
"electron.asar",
173+
"en.lproj",
174+
]
175+
`;

test/src/mac/macPackagerTest.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { copyOrLinkFile } from "builder-util/out/fs"
22
import { createTargets, DIR_TARGET, Platform } from "electron-builder"
3-
import { readJson } from "fs-extra-p"
3+
import { readdir, readJson } from "fs-extra-p"
44
import * as path from "path"
55
import { assertThat } from "../helpers/fileAssert"
66
import { app, appThrows, assertPack, convertUpdateInfo, platform } from "../helpers/packTester"
@@ -13,12 +13,16 @@ test.ifMac.ifAll("two-package", () => assertPack("test-app", {
1313
},
1414
mac: {
1515
electronUpdaterCompatibility: ">=2.16",
16+
electronLanguages: ["bn", "en"]
1617
},
1718
//tslint:disable-next-line:no-invalid-template-strings
1819
artifactName: "${name}-${version}-${os}.${ext}",
1920
},
2021
}, {
2122
signed: true,
23+
checkMacApp: async appDir => {
24+
expect((await readdir(path.join(appDir, "Contents", "Resources"))).filter(it => !it.startsWith("."))).toMatchSnapshot()
25+
},
2226
}))
2327

2428
test.ifMac("one-package", app({

0 commit comments

Comments
 (0)