Skip to content

Commit

Permalink
feat(mac): control the localization files
Browse files Browse the repository at this point in the history
Close #708
  • Loading branch information
develar committed Jan 31, 2018
1 parent ba9e9da commit ab664ac
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/electron-builder-lib/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export interface Configuration extends PlatformSpecificBuildOptions {
* Whether to fail if the application is not signed (to prevent unsigned app if code signing configuration is not correct).
* @default false
*/
readonly forceCodeSigning?: boolean
readonly ?: boolean

/**
* The version of muon you are packaging for.
Expand Down
30 changes: 29 additions & 1 deletion packages/electron-builder-lib/src/macPackager.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import BluebirdPromise from "bluebird-lst"
import { Arch, AsyncTaskManager, exec, InvalidConfigurationError, log } from "builder-util"
import { signAsync, SignOptions } from "electron-osx-sign"
import { ensureDir } from "fs-extra-p"
import { ensureDir, readdir, remove } from "fs-extra-p"
import { Lazy } from "lazy-val"
import * as path from "path"
import { deepAssign } from "read-config-file/out/deepAssign"
import * as semver from "semver"
import { AsarIntegrity } from "asar-integrity"
import { asArray } from "builder-util-runtime/out"
import { AppInfo } from "./appInfo"
import { appleCertificatePrefixes, CertType, CodeSigningInfo, createKeychain, findIdentity, Identity, isSignAllowed, reportError } from "./codeSign"
import { DIR_TARGET, Platform, Target } from "./core"
import { MacConfiguration, MasConfiguration } from "./options/macOptions"
import { Packager } from "./packager"
import { createMacApp } from "./packager/mac"
import { PlatformPackager } from "./platformPackager"
import { ArchiveTarget } from "./targets/ArchiveTarget"
import { DmgTarget } from "./targets/dmg"
import { PkgTarget, prepareProductBuildArgs } from "./targets/pkg"
import { createCommonTarget, NoOpTarget } from "./targets/targetFactory"
import { CONCURRENCY } from "builder-util/out/fs"

export default class MacPackager extends PlatformPackager<MacConfiguration> {
readonly codeSigningInfo: Promise<CodeSigningInfo>
Expand Down Expand Up @@ -255,6 +259,30 @@ export default class MacPackager extends PlatformPackager<MacConfiguration> {
public getElectronDestinationDir(appOutDir: string) {
return path.join(appOutDir, this.electronDistMacOsAppName)
}

protected async beforeCopyExtraFiles(appOutDir: string, asarIntegrity: AsarIntegrity | null): Promise<any> {
await createMacApp(this, appOutDir, asarIntegrity)

const wantedLanguages = asArray(this.platformSpecificBuildOptions.electronLanguages)
if (wantedLanguages == null) {
return
}

// noinspection SpellCheckingInspection
const langFileExt = ".lproj"
const resourcesDir = this.getResourcesDir(appOutDir)
await BluebirdPromise.map(readdir(resourcesDir), file => {
if (!file.endsWith(langFileExt)) {
return
}

const language = file.substring(0, file.length - langFileExt.length)
if (!wantedLanguages.includes(language)) {
return remove(path.join(resourcesDir, file))
}
return
}, CONCURRENCY)
}
}

function getCertificateType(isMas: boolean, isDevelopment: boolean): CertType {
Expand Down
5 changes: 5 additions & 0 deletions packages/electron-builder-lib/src/options/macOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ export interface MacConfiguration extends PlatformSpecificBuildOptions {
* 2.16.0 files
*/
readonly electronUpdaterCompatibility?: string | null

/**
* The electron locales. By default Electron locales used as is.
*/
readonly electronLanguages?: Array<string> | string
}

/**
Expand Down
14 changes: 7 additions & 7 deletions packages/electron-builder-lib/src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Lazy } from "lazy-val"
import { Minimatch } from "minimatch"
import * as path from "path"
import { deepAssign } from "read-config-file/out/deepAssign"
import { AsarIntegrity } from "asar-integrity"
import { AppInfo } from "./appInfo"
import { checkFileInArchive } from "./asar/asarFileChecker"
import { AsarPackager } from "./asar/asarUtil"
Expand All @@ -18,7 +19,6 @@ import { createTransformer, isElectronCompileUsed } from "./fileTransformer"
import { AfterPackContext, AsarOptions, Configuration, FileAssociation, PlatformSpecificBuildOptions } from "./index"
import { Packager } from "./packager"
import { unpackElectron, unpackMuon } from "./packager/dirPackager"
import { createMacApp } from "./packager/mac"
import { PackagerOptions } from "./packagerApi"
import { getAppBuilderTool } from "./targets/tools"
import { copyAppFiles } from "./util/appFileCopier"
Expand Down Expand Up @@ -198,11 +198,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
}

await taskManager.awaitTasks()

if (platformName === "darwin" || platformName === "mas") {
await createMacApp(this, appOutDir, asarOptions == null ? null : await computeData(resourcesPath, asarOptions.externalAllowed ? {externalAllowed: true} : null))
}

await this.beforeCopyExtraFiles(appOutDir, asarOptions == null ? null : await computeData(resourcesPath, asarOptions.externalAllowed ? {externalAllowed: true} : null))
await BluebirdPromise.each([extraResourceMatchers, extraFileMatchers], it => copyFiles(it))

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

protected async beforeCopyExtraFiles(appOutDir: string, asarIntegrity: AsarIntegrity | null) {
// empty impl
}

private copyAppFiles(taskManager: AsyncTaskManager, asarOptions: AsarOptions | null, resourcePath: string, outDir: string, platformSpecificBuildOptions: DC, excludePatterns: Array<Minimatch>, macroExpander: ((it: string) => string)) {
const appDir = this.info.appDir
const config = this.config
Expand Down Expand Up @@ -256,7 +256,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
}

protected signApp(packContext: AfterPackContext): Promise<any> {
return BluebirdPromise.resolve()
return Promise.resolve()
}

async getIconPath(): Promise<string | null> {
Expand Down
11 changes: 11 additions & 0 deletions test/out/mac/__snapshots__/macPackagerTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,14 @@ Object {
],
}
`;

exports[`two-package 2`] = `
Array [
"TestApp.icns",
"app-update.yml",
"app.asar",
"bn.lproj",
"electron.asar",
"en.lproj",
]
`;
6 changes: 5 additions & 1 deletion test/src/mac/macPackagerTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { copyOrLinkFile } from "builder-util/out/fs"
import { createTargets, DIR_TARGET, Platform } from "electron-builder"
import { readJson } from "fs-extra-p"
import { readdir, readJson } from "fs-extra-p"
import * as path from "path"
import { assertThat } from "../helpers/fileAssert"
import { app, appThrows, assertPack, convertUpdateInfo, platform } from "../helpers/packTester"
Expand All @@ -13,12 +13,16 @@ test.ifMac.ifAll("two-package", () => assertPack("test-app", {
},
mac: {
electronUpdaterCompatibility: ">=2.16",
electronLanguages: ["bn", "en"]
},
//tslint:disable-next-line:no-invalid-template-strings
artifactName: "${name}-${version}-${os}.${ext}",
},
}, {
signed: true,
checkMacApp: async appDir => {
expect((await readdir(path.join(appDir, "Contents", "Resources"))).filter(it => !it.startsWith("."))).toMatchSnapshot()
},
}))

test.ifMac("one-package", app({
Expand Down

0 comments on commit ab664ac

Please sign in to comment.