Skip to content

Commit

Permalink
feat(pkg): pkg license
Browse files Browse the repository at this point in the history
Close #1547, Close #2212
  • Loading branch information
negorikhin authored and develar committed Oct 30, 2017
1 parent e574db8 commit a0cb477
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/builder-util/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export interface PackageBuilder {
readonly resourceList: Promise<Array<string>>

getTempFile(suffix: string): Promise<string>

getResource(custom: string | null | undefined, ...names: Array<string>): Promise<string | null>
}
14 changes: 14 additions & 0 deletions packages/builder-util/src/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ export function getLicenseAssets(fileNames: Array<string>, packager: PackageBuil
})
}

export async function getNotLocalizedLicenseFiles(custom: string | null | undefined, packager: PackageBuilder): Promise<string | null> {
const possibleFiles: Array<string> = []
for (const name of ["license", "eula"]) {
for (const ext of ["rtf", "txt", "html"]) {
possibleFiles.push(`${name}.${ext}`)
possibleFiles.push(`${name.toUpperCase()}.${ext}`)
possibleFiles.push(`${name}.${ext.toUpperCase()}`)
possibleFiles.push(`${name.toUpperCase()}.${ext.toUpperCase()}`)
}
}

return await packager.getResource(custom, ...possibleFiles)
}

export async function getLicenseFiles(packager: PackageBuilder): Promise<Array<LicenseFile>> {
return getLicenseAssets((await packager.resourceList)
.filter(it => {
Expand Down
5 changes: 5 additions & 0 deletions packages/electron-builder/src/options/macOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ export interface PkgOptions extends TargetSpecificOptions {
* The name of certificate to use when signing. Consider using environment variables [CSC_LINK or CSC_NAME](../code-signing.md) instead of specifying this option.
*/
readonly identity?: string | null

/**
* The path to EULA license file. Defaults to `license.txt` or `eula.txt` (or uppercase variants). In addition to `txt, `rtf` and `html` supported (don't forget to use `target="_blank"` for links).
*/
readonly license?: string | null
}

export interface DmgOptions extends TargetSpecificOptions {
Expand Down
14 changes: 2 additions & 12 deletions packages/electron-builder/src/targets/nsis/nsisLicense.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import { lcid } from "builder-util/out/langs"
import { getLicenseFiles } from "builder-util/out/license"
import { getLicenseFiles, getNotLocalizedLicenseFiles } from "builder-util/out/license"
import * as path from "path"
import { WinPackager } from "../../winPackager"
import { NsisOptions } from "./nsisOptions"
import { NsisScriptGenerator } from "./nsisScriptGenerator"
import { nsisTemplatesDir } from "./nsisUtil"

export async function computeLicensePage(packager: WinPackager, options: NsisOptions, scriptGenerator: NsisScriptGenerator, languages: Array<string>): Promise<void> {
const possibleFiles: Array<string> = []
for (const name of ["license", "eula"]) {
for (const ext of ["rtf", "txt", "html"]) {
possibleFiles.push(`${name}.${ext}`)
possibleFiles.push(`${name.toUpperCase()}.${ext}`)
possibleFiles.push(`${name}.${ext.toUpperCase()}`)
possibleFiles.push(`${name.toUpperCase()}.${ext.toUpperCase()}`)
}
}

const license = await packager.getResource(options.license, ...possibleFiles)
const license = await getNotLocalizedLicenseFiles(options.license, packager)
if (license != null) {
let licensePage: Array<string>
if (license.endsWith(".html")) {
Expand Down
11 changes: 10 additions & 1 deletion packages/electron-builder/src/targets/pkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Target } from "../core"
import MacPackager from "../macPackager"
import { PkgOptions } from "../options/macOptions"
import { filterCFBundleIdentifier } from "../packager/mac"
import { getNotLocalizedLicenseFiles } from "builder-util/out/license"

const certType = "Developer ID Installer"

Expand All @@ -19,7 +20,9 @@ export class PkgTarget extends Target {
readonly options: PkgOptions = {
allowAnywhere: true,
allowCurrentUserHome: true,
allowRootDirectory: true, ...this.packager.config.pkg}
allowRootDirectory: true,
...this.packager.config.pkg,
}

constructor(private readonly packager: MacPackager, readonly outDir: string) {
super("pkg")
Expand Down Expand Up @@ -70,6 +73,12 @@ export class PkgTarget extends Target {
let distInfo = await readFile(distInfoFile, "utf-8")
const insertIndex = distInfo.lastIndexOf("</installer-gui-script>")
distInfo = distInfo.substring(0, insertIndex) + ` <domains enable_anywhere="${options.allowAnywhere}" enable_currentUserHome="${options.allowCurrentUserHome}" enable_localSystem="${options.allowRootDirectory}" />\n` + distInfo.substring(insertIndex)

const license = await getNotLocalizedLicenseFiles(options.license, this.packager)
if (license != null) {
distInfo = distInfo.substring(0, insertIndex) + ` <license file="${license}"/>\n` + distInfo.substring(insertIndex)
}

debug(distInfo)
await writeFile(distInfoFile, distInfo)
}
Expand Down
7 changes: 6 additions & 1 deletion test/src/mac/macArchiveTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { readFile, symlink } from "fs-extra-p"
import * as path from "path"
import pathSorter from "path-sort"
import { assertThat } from "../helpers/fileAssert"
import { app, createMacTargetTest, getFixtureDir, parseFileList } from "../helpers/packTester"
import { app, copyTestAsset, createMacTargetTest, getFixtureDir, parseFileList } from "../helpers/packTester"

test.ifMac("invalid target", () => assertThat(createMacTargetTest(["ttt" as any])()).throws())

Expand All @@ -27,6 +27,11 @@ test.ifAll.ifMac("empty installLocation", app({
}
}, {
signed: false,
projectDirCreated: projectDir => {
return BluebirdPromise.all([
copyTestAsset("license.txt", path.join(projectDir, "build", "license.txt")),
])
},
}))

test.ifAll.ifMac("pkg scripts", app({
Expand Down

0 comments on commit a0cb477

Please sign in to comment.