-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
156 lines (152 loc) · 6.54 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import core = require('@actions/core');
import exec = require('@actions/exec');
import glob = require('@actions/glob');
import path = require('path');
import fs = require('fs');
const main = async () => {
try {
if (process.platform !== `win32`) { throw new Error(`This action can only run on Windows runner.`); }
let projectPath = core.getInput(`project-path`, { required: true });
core.debug(`project-path: "${projectPath}"`);
if (!projectPath.endsWith(`.sln`)) {
projectPath = path.join(projectPath, `**/*.sln`);
}
let buildPath = projectPath;
if (projectPath.includes('*')) {
const globber = await glob.create(projectPath, { matchDirectories: false });
const files = await globber.glob();
core.debug(`Found solution files:`);
files.forEach(file => core.debug(` - "${file}"`));
if (files.length === 0) { throw new Error(`No solution file found.`); }
buildPath = files[0];
}
core.info(`Building ${buildPath}...`);
projectPath = path.dirname(buildPath);
try {
await fs.promises.access(buildPath, fs.constants.R_OK);
} catch (error) {
throw new Error(`Solution file not found: "${buildPath}"`);
}
const appPackagesPath = path.join(projectPath, `AppPackages`);
if (fs.existsSync(appPackagesPath)) {
core.info(`Cleaning AppPackages directory: ${appPackagesPath}...`);
await fs.promises.rm(appPackagesPath, { recursive: true, force: true });
}
let projectName = path.basename(buildPath, `.sln`);
core.debug(`projectName: "${projectName}"`);
const configuration = core.getInput(`configuration`, { required: true });
const buildArgs = [
`/t:Build`,
`/p:Configuration=${configuration}`
];
const architecture = core.getInput(`architecture`);
if (architecture) {
core.debug(`architecture: "${architecture}"`);
buildArgs.push(`/p:Platform=${architecture}`);
}
const packageType = core.getInput(`package-type`, { required: true });
core.debug(`package-type: "${packageType}"`);
switch (packageType) {
case `upload`:
buildArgs.push(
`/p:UapAppxPackageBuildMode=StoreUpload`,
`/p:GenerateAppInstallerFile=false`,
`/p:AppxPackageSigningEnabled=false`,
`/p:BuildAppxUploadPackageForUap=true`
);
break;
case `sideload`:
const certificatePath = await getCertificatePath(projectPath);
const thumbprint = await getCertificateThumbprint(certificatePath);
buildArgs.push(
`/p:UapAppxPackageBuildMode=SideloadOnly`,
`/p:AppxPackageSigningEnabled=true`,
`/p:PackageCertificateThumbprint=${thumbprint}`,
`/p:PackageCertificateKeyFile="${certificatePath}"`
);
break;
default:
throw new Error(`Invalid package type: "${packageType}"`);
}
const additionalArgs = core.getInput(`additional-args`);
if (additionalArgs) {
core.debug(`additional-args: "${additionalArgs}"`);
buildArgs.push(...additionalArgs.split(` `));
}
if (!core.isDebug()) {
buildArgs.push(`/verbosity:minimal`);
}
await exec.exec(`msbuild`, [`"${buildPath}"`, ...buildArgs], {
windowsVerbatimArguments: true
});
const outputDirectory = path.join(projectPath, `AppPackages`);
core.debug(`outputDirectory: ${outputDirectory}`);
core.setOutput(`output-directory`, outputDirectory);
const patterns = [
`${outputDirectory}/**/*.appx`,
`${outputDirectory}/**/*.msix`,
`${outputDirectory}/**/*.appxbundle`,
`${outputDirectory}/**/*.msixbundle`,
`${outputDirectory}/**/*.appxupload`,
`${outputDirectory}/**/*.msixupload`,
`!${outputDirectory}/**/dependencies/**`
];
const executableGlobber = await glob.create(patterns.join(`\n`));
const executables = await executableGlobber.glob();
if (executables.length === 0) {
core.warning(`No executables found.`);
return;
}
core.debug(`Found executables:`);
executables.forEach(executable => core.debug(` - "${executable}"`));
let executable: string;
switch (packageType) {
case `upload`:
executable = executables.find(file => file.endsWith(`.appxupload`) || file.endsWith(`.msixupload`));
break;
case `sideload`:
executable = executables.find(file => file.endsWith(`.appx`) || file.endsWith(`.msix`));
break;
}
core.debug(`Found executable: "${executable}"`);
core.setOutput(`executable`, executable);
} catch (error) {
core.setFailed(error);
}
}
main();
async function getCertificatePath(projectPath: string): Promise<string> {
let certificatePath = core.getInput(`certificate-path`) || `${projectPath}/**/*.pfx`;
core.debug(`certificatePath: "${certificatePath}"`);
if (!certificatePath.endsWith(`.pfx`)) {
certificatePath = path.join(certificatePath, `**/*.pfx`);
}
if (certificatePath.includes(`*`)) {
const certificateGlobber = await glob.create(certificatePath);
const certificateFiles = await certificateGlobber.glob();
core.debug(`Found certificate files:`);
certificateFiles.forEach(file => core.debug(` - "${file}"`));
if (certificateFiles.length === 0) {
throw new Error(`No certificate file found: "${certificatePath}"`);
}
certificatePath = certificateFiles[0];
}
try {
await fs.promises.access(certificatePath, fs.constants.R_OK);
} catch (error) {
throw new Error(`Certificate file not found: "${certificatePath}"`);
}
return certificatePath;
}
async function getCertificateThumbprint(certificatePath: string): Promise<string> {
const thumbprintCmd = `powershell -command "(Get-PfxCertificate -FilePath '${certificatePath}').Thumbprint"`;
let thumbprint = ``;
await exec.exec(thumbprintCmd, [], {
listeners: {
stdout: (data: Buffer) => {
thumbprint += data.toString();
}
}
});
return thumbprint.trim();
}