Skip to content

Commit 381e8c0

Browse files
committed
feat: support relative (to project dir) path in the CSC_LINK
Close #1596
1 parent 9e9ed4f commit 381e8c0

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

packages/electron-builder/src/codeSign.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export interface CodeSigningInfo {
1818
keychainName?: string | null
1919
}
2020

21-
export async function downloadCertificate(urlOrBase64: string, tmpDir: TmpDir): Promise<string> {
21+
export async function downloadCertificate(urlOrBase64: string, tmpDir: TmpDir, currentDir: string): Promise<string> {
2222
let file: string | null = null
23-
if ((urlOrBase64.length > 3 && urlOrBase64[1] === ":") || urlOrBase64.startsWith("/")) {
23+
if ((urlOrBase64.length > 3 && urlOrBase64[1] === ":") || urlOrBase64.startsWith("/") || urlOrBase64.startsWith(".")) {
2424
file = urlOrBase64
2525
}
2626
else if (urlOrBase64.startsWith("file://")) {
@@ -30,16 +30,23 @@ export async function downloadCertificate(urlOrBase64: string, tmpDir: TmpDir):
3030
file = path.join(homedir(), urlOrBase64.substring("~/".length))
3131
}
3232
else {
33-
const tempFile = await tmpDir.getTempFile(".p12")
34-
if (urlOrBase64.startsWith("https://")) {
35-
await download(urlOrBase64, tempFile)
33+
const isUrl = urlOrBase64.startsWith("https://")
34+
if (isUrl || urlOrBase64.length > 4096 || urlOrBase64.endsWith("=")) {
35+
const tempFile = await tmpDir.getTempFile(".p12")
36+
if (isUrl) {
37+
await download(urlOrBase64, tempFile)
38+
}
39+
else {
40+
await outputFile(tempFile, new Buffer(urlOrBase64, "base64"))
41+
}
42+
return tempFile
3643
}
3744
else {
38-
await outputFile(tempFile, new Buffer(urlOrBase64, "base64"))
45+
file = urlOrBase64
3946
}
40-
return tempFile
4147
}
4248

49+
file = path.resolve(currentDir, file.trim())
4350
const stat = await statOrNull(file)
4451
if (stat == null) {
4552
throw new Error(`${file} doesn't exist`)
@@ -80,7 +87,16 @@ async function createCustomCertKeychain() {
8087
}
8188
}
8289

83-
export async function createKeychain(tmpDir: TmpDir, cscLink: string, cscKeyPassword: string, cscILink?: string | null, cscIKeyPassword?: string | null): Promise<CodeSigningInfo> {
90+
export interface CreateKeychainOptions {
91+
tmpDir: TmpDir
92+
cscLink: string
93+
cscKeyPassword: string
94+
cscILink?: string | null
95+
cscIKeyPassword?: string | null
96+
currentDir: string
97+
}
98+
99+
export async function createKeychain({tmpDir, cscLink, cscKeyPassword, cscILink, cscIKeyPassword, currentDir}: CreateKeychainOptions): Promise<CodeSigningInfo> {
84100
if (bundledCertKeychainAdded == null) {
85101
bundledCertKeychainAdded = createCustomCertKeychain()
86102
}
@@ -96,7 +112,7 @@ export async function createKeychain(tmpDir: TmpDir, cscLink: string, cscKeyPass
96112
const certPaths = new Array(certLinks.length)
97113
const keychainPassword = randomBytes(8).toString("hex")
98114
return await executeFinally(BluebirdPromise.all([
99-
BluebirdPromise.map(certLinks, (link, i) => downloadCertificate(link, tmpDir).then(it => certPaths[i] = it)),
115+
BluebirdPromise.map(certLinks, (link, i) => downloadCertificate(link, tmpDir, currentDir).then(it => certPaths[i] = it)),
100116
BluebirdPromise.mapSeries([
101117
["create-keychain", "-p", keychainPassword, keychainName],
102118
["unlock-keychain", "-p", keychainPassword, keychainName],

packages/electron-builder/src/macPackager.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ export default class MacPackager extends PlatformPackager<MacOptions> {
2828
this.codeSigningInfo = BluebirdPromise.resolve(Object.create(null))
2929
}
3030
else {
31-
this.codeSigningInfo = createKeychain(info.tempDirManager, this.packagerOptions.cscLink!, this.getCscPassword(), this.packagerOptions.cscInstallerLink, this.packagerOptions.cscInstallerKeyPassword)
31+
this.codeSigningInfo = createKeychain({
32+
tmpDir: info.tempDirManager,
33+
cscLink: this.packagerOptions.cscLink!,
34+
cscKeyPassword: this.getCscPassword(),
35+
cscILink: this.packagerOptions.cscInstallerLink,
36+
cscIKeyPassword: this.packagerOptions.cscInstallerKeyPassword,
37+
currentDir: this.projectDir
38+
})
3239
}
3340
}
3441

packages/electron-builder/src/winPackager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class WinPackager extends PlatformPackager<WinBuildOptions> {
3939
else {
4040
const cscLink = process.env.WIN_CSC_LINK || this.packagerOptions.cscLink
4141
if (cscLink != null) {
42-
return downloadCertificate(cscLink, this.info.tempDirManager)
42+
return downloadCertificate(cscLink, this.info.tempDirManager, this.projectDir)
4343
.then(path => {
4444
return {
4545
file: path,

test/src/mac/CodeSignTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ if (process.env.CSC_KEY_PASSWORD == null) {
1212
const tmpDir = new TmpDir()
1313

1414
test.ifMac("create keychain", async () => {
15-
const result = await createKeychain(tmpDir, CSC_LINK, process.env.CSC_KEY_PASSWORD)
15+
const result = await createKeychain({tmpDir, cscLink: CSC_LINK, cscKeyPassword: process.env.CSC_KEY_PASSWORD, currentDir: process.cwd()})
1616
expect(result.keychainName).not.toEqual("")
1717
})
1818

1919
test.ifMac("create keychain with installers", async () => {
20-
const result = await createKeychain(tmpDir, CSC_LINK, process.env.CSC_KEY_PASSWORD)
20+
const result = await createKeychain({tmpDir, cscLink: CSC_LINK, cscKeyPassword: process.env.CSC_KEY_PASSWORD, currentDir: process.cwd()})
2121
expect(result.keychainName).not.toEqual("")
2222
})
2323

0 commit comments

Comments
 (0)