/
cpan-installer.ts
254 lines (231 loc) · 7.61 KB
/
cpan-installer.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// install CPAN modules and caching
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as cache from "@actions/cache";
import * as crypto from "crypto";
import * as fs from "fs";
import * as stream from "stream";
import * as util from "util";
import * as path from "path";
import { State, Outputs } from "./constants";
import { getPackagePath } from "./utils";
export interface Options {
// the digest of `perl -V`
perlHash: string;
// path to perl installed
toolPath: string;
install_modules_with: string | null;
install_modules_args: string | null;
install_modules: string | null;
enable_modules_cache: boolean;
working_directory: string | null;
}
export async function install(opt: Options): Promise<void> {
let installer: (opt: Options) => Promise<void>;
switch (opt.install_modules_with || "cpanm") {
case "cpanm":
installer = installWithCpanm;
break;
case "cpm":
installer = installWithCpm;
break;
case "carton":
installer = installWithCarton;
break;
default:
core.error(`unknown installer: ${opt.install_modules_with}`);
return;
}
const workingDirectory = path.join(process.cwd(), opt.working_directory || ".");
const cachePath = path.join(workingDirectory, "local");
const paths = [cachePath];
const baseKey = await cacheKey(opt);
const cpanfileKey = await hashFiles(
opt,
path.join(workingDirectory, "cpanfile"),
path.join(workingDirectory, "cpanfile.snapshot")
);
const installKey = hashString(opt.install_modules || "");
const key = `${baseKey}-${cpanfileKey}-${installKey}`;
const restoreKeys = [`${baseKey}-${cpanfileKey}-`, `${baseKey}-`];
// restore cache
let cachedKey: string | undefined = undefined;
if (opt.enable_modules_cache) {
try {
cachedKey = await cache.restoreCache(paths, key, restoreKeys);
} catch (error) {
if (error instanceof Error) {
if (error.name === cache.ValidationError.name) {
} else {
core.info(`[warning] There was an error restoring the cache ${error.message}`);
}
} else {
core.info(`[warning] There was an error restoring the cache ${error}`);
}
}
if (cachedKey) {
core.info(`Found cache for key: ${cachedKey}`);
core.setOutput(Outputs.CacheHit, "true");
} else {
core.info(`cache not found for input keys: ${key}, ${restoreKeys.join(", ")}`);
core.setOutput(Outputs.CacheHit, "false");
}
}
// install
await installer(opt);
// configure environment values
core.addPath(path.join(cachePath, "bin"));
const archName = await getArchName(opt);
const libPath = path.join(cachePath, "lib", "perl5");
const libArchPath = path.join(cachePath, "lib", "perl5", archName);
core.exportVariable("PERL5LIB", libPath + path.delimiter + libArchPath + path.delimiter + process.env["PERL5LIB"]);
if (opt.enable_modules_cache) {
core.saveState(State.CachePath, cachePath);
core.saveState(State.CachePrimaryKey, key);
core.saveState(State.CacheMatchedKey, cachedKey);
}
return;
}
async function cacheKey(opt: Options): Promise<string> {
let key = "setup-perl-module-cache-v1-";
key += opt.perlHash;
key += "-" + (opt.install_modules_with || "unknown");
return key;
}
// see https://github.com/actions/runner/blob/master/src/Misc/expressionFunc/hashFiles/src/hashFiles.ts
async function hashFiles(opt: Options, ...files: string[]): Promise<string> {
const result = crypto.createHash("sha256");
result.update(opt.install_modules_args || "");
for (const file of files) {
try {
const hash = crypto.createHash("sha256");
const pipeline = util.promisify(stream.pipeline);
await pipeline(fs.createReadStream(file), hash);
result.write(hash.digest());
} catch (err) {
// skip files that doesn't exist.
if ((err as any)?.code !== "ENOENT") {
throw err;
}
}
}
result.end();
return result.digest("hex");
}
function hashString(s: string): string {
const hash = crypto.createHash("sha256");
hash.update(s, "utf-8");
hash.end();
return hash.digest("hex");
}
async function installWithCpanm(opt: Options): Promise<void> {
const perl = path.join(opt.toolPath, "bin", "perl");
const cpanm = path.join(getPackagePath(), "bin", "cpanm");
const workingDirectory = path.join(process.cwd(), opt.working_directory || ".");
const execOpt = {
cwd: workingDirectory,
};
const args = [cpanm, "--local-lib-contained", "local", "--notest"];
if (core.isDebug()) {
args.push("--verbose");
}
args.push(...splitArgs(opt.install_modules_args));
if (opt.install_modules_with) {
if (await exists(path.join(workingDirectory, "cpanfile"))) {
await exec.exec(perl, [...args, "--installdeps", "."], execOpt);
}
}
const modules = splitModules(opt.install_modules);
if (modules.length > 0) {
await exec.exec(perl, [...args, ...modules], execOpt);
}
}
async function installWithCpm(opt: Options): Promise<void> {
const perl = path.join(opt.toolPath, "bin", "perl");
const cpm = path.join(getPackagePath(), "bin", "cpm");
const workingDirectory = path.join(process.cwd(), opt.working_directory || ".");
const execOpt = {
cwd: workingDirectory,
};
const args = [cpm, "install", "--show-build-log-on-failure"];
if (core.isDebug()) {
args.push("--verbose");
}
args.push(...splitArgs(opt.install_modules_args));
if (
(await exists(path.join(workingDirectory, "cpanfile"))) ||
(await exists(path.join(workingDirectory, "cpanfile.snapshot")))
) {
await exec.exec(perl, [...args], execOpt);
}
const modules = splitModules(opt.install_modules);
if (modules.length > 0) {
await exec.exec(perl, [...args, ...modules], execOpt);
}
}
async function installWithCarton(opt: Options): Promise<void> {
const perl = path.join(opt.toolPath, "bin", "perl");
const carton = path.join(getPackagePath(), "bin", "carton");
const workingDirectory = path.join(process.cwd(), opt.working_directory || ".");
const execOpt = {
cwd: workingDirectory,
};
const args = [carton, "install"];
args.push(...splitArgs(opt.install_modules_args));
if (
(await exists(path.join(workingDirectory, "cpanfile"))) ||
(await exists(path.join(workingDirectory, "cpanfile.snapshot")))
) {
await exec.exec(perl, [...args], execOpt);
}
const modules = splitModules(opt.install_modules);
if (modules.length > 0) {
const cpanm = path.join(getPackagePath(), "bin", "cpanm");
const args = [cpanm, "--local-lib-contained", "local", "--notest"];
if (core.isDebug()) {
args.push("--verbose");
}
await exec.exec(perl, [...args, ...modules], execOpt);
}
}
// getArchName gets the arch name such as x86_64-linux, darwin-thread-multi-2level, etc.
async function getArchName(opt: Options): Promise<string> {
const perl = path.join(opt.toolPath, "bin", "perl");
const out = await exec.getExecOutput(perl, ["-MConfig", "-E", "print $Config{archname}"]);
return out.stdout;
}
async function exists(path: string): Promise<boolean> {
return new Promise((resolve, reject) => {
fs.stat(path, (err) => {
if (err) {
if (err.code === "ENOENT") {
resolve(false);
} else {
reject(err);
}
return;
}
resolve(true);
});
});
}
function splitArgs(args: string | null): string[] {
if (!args) {
return [];
}
args = args.trim();
if (args === "") {
return [];
}
return args.split(/\s+/);
}
function splitModules(modules: string | null): string[] {
if (!modules) {
return [];
}
modules = modules.trim();
if (modules === "") {
return [];
}
return modules.split(/\s+/);
}