-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
175 lines (135 loc) · 3.91 KB
/
index.js
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
const core = require("@actions/core");
const tc = require("@actions/tool-cache");
const exec = require("@actions/exec");
const io = require("@actions/io");
const path = require("path");
const os = require("os");
let PERL;
async function install_cpanm_location() {
let out = "";
const options = {};
options.listeners = {
stdout: (data) => {
out += data.toString();
},
};
let p = core.getInput("path");
p.replace("\\", "\\\\");
await exec.exec(PERL, ["-MConfig", "-e", `print "${p}"`], options);
return path.resolve(out);
}
async function install_cpanm(install_to) {
const url = "https://cpanmin.us";
core.info(`Get cpanm from ${url}`);
const cpanmScript = await tc.downloadTool(url);
core.info(`cpanm Script: ${cpanmScript}`);
core.info(`install_to ${install_to}`);
const platform = os.platform();
if (platform == "win32") {
await io.cp(cpanmScript, install_to);
} else {
await do_exec([
PERL,
"-MFile::Copy=cp",
"-e",
`cp("${cpanmScript}", "${install_to}"); chmod(0755, "${install_to}")`,
]);
}
return install_to;
}
async function which_perl() {
const perl = core.getInput("perl");
if (perl == "perl") {
return await io.which("perl", true);
}
return perl;
}
function is_true(b) {
if (b !== null && (b === true || b == "true" || b == "1" || b == "ok")) {
return true;
}
return false;
}
async function do_exec(cmd, env) {
const sudo = is_true(core.getInput("sudo"));
const platform = os.platform();
const bin = sudo && platform != "win32" ? "sudo" : cmd.shift();
core.info(`do_exec: ${JSON.stringify(bin)} ${JSON.stringify(env)}`);
await exec.exec(bin, cmd, env);
}
async function run() {
PERL = await which_perl();
const cpanm_location = await install_cpanm_location();
await install_cpanm(cpanm_location);
// input arguments
const install = core.getInput("install");
const cpanfile = core.getInput("cpanfile");
const tests = core.getInput("tests");
const args = core.getInput("args");
const verbose = core.getInput("verbose");
const local_lib = core.getInput("local-lib");
const w_tests = is_true(tests) ? null : "--notest";
let w_args = [];
let env = {};
if (args !== null && args.length) {
w_args = args.split(/\s+/);
}
if (local_lib !== null && local_lib.length) {
w_args.push("--local-lib", local_lib);
if ( local_lib.startsWith("~") ) {
const home = process.env.HOME;
const expanded_lib_path = local_lib.replace(/^~/, home);
env = { PERL5LIB: expanded_lib_path };
} else {
env = { PERL5LIB: local_lib };
}
}
/* base CMD_install command */
let CMD_install = [PERL, cpanm_location];
if (is_true(verbose)) {
CMD_install.push("-v");
}
if (w_tests != null) {
CMD_install.push(w_tests);
}
if (w_args.length) {
CMD_install = CMD_install.concat(w_args);
}
let has_run = false;
/* install one ore more modules */
if (install !== null && install.length) {
// install one or more modules
core.info(`install: ${install}!`);
const list = install.split("\n");
let cmd = [...CMD_install]; /* clone array */
cmd = cmd.concat(list);
has_run = true;
await do_exec(cmd, env);
}
/* install from cpanfile */
if (cpanfile !== null && cpanfile.length) {
// install one or more modules
core.info(`cpanfile: ${cpanfile}!`);
const cpanfile_full_path = path.resolve(cpanfile);
core.info(`cpanfile: ${cpanfile_full_path}! [resolved]`);
let cmd = [...CMD_install];
cmd.push("--cpanfile", cpanfile_full_path, "--installdeps", ".");
has_run = true;
await do_exec(cmd, env);
}
/* custom run with args */
if ( has_run === false && w_args.length ) {
core.info(`custom run with args`);
let cmd = [...CMD_install];
has_run = true;
await do_exec(cmd, env);
}
return;
}
(async () => {
try {
await run();
} catch (error) {
core.setFailed(error.message);
}
})();