-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathInstaller.ts
169 lines (142 loc) · 4.7 KB
/
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
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import * as io from "@actions/io";
import * as exec from "@actions/exec";
import fs from "fs";
import path from "path";
import { Platform } from "./platform";
import { DownloadURLFactory } from "./DownloadURLFactory";
export type InstallSpec = {
version: string;
platform: Platform;
language: string;
};
export default interface Installer {
install(spec: InstallSpec): Promise<string>;
testVersion(bin: string): Promise<string>;
}
const commonTestVersion = async (bin: string): Promise<string> => {
const output = await exec.getExecOutput(`"${bin}"`, ["--version"]);
if (output.exitCode !== 0) {
throw new Error(
`firefox exits with status ${output.exitCode}: ${output.stderr}`
);
}
if (!output.stdout.startsWith("Mozilla Firefox ")) {
throw new Error(`firefox outputs unexpected results: ${output.stdout}`);
}
return output.stdout.trimEnd().replace("Mozilla Firefox ", "");
};
export class LinuxInstaller implements Installer {
async install(spec: InstallSpec): Promise<string> {
return this.download(spec);
}
private async download({
version,
platform,
language,
}: InstallSpec): Promise<string> {
const toolPath = tc.find("firefox", version);
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
return toolPath;
}
core.info(`Attempting to download firefox ${version}...`);
const url = new DownloadURLFactory(version, platform, language)
.create()
.getURL();
core.info(`Acquiring ${version} from ${url}`);
const archivePath = await tc.downloadTool(url);
core.info("Extracting Firefox...");
const extPath = await tc.extractTar(archivePath, "", [
"xj",
"--strip-components=1",
]);
core.info(`Successfully extracted firefox ${version} to ${extPath}`);
core.info("Adding to the cache ...");
const cachedDir = await tc.cacheDir(extPath, "firefox", version);
core.info(`Successfully cached firefox ${version} to ${cachedDir}`);
return cachedDir;
}
testVersion = commonTestVersion;
}
export class MacOSInstaller implements Installer {
async install(spec: InstallSpec): Promise<string> {
const installPath = await this.download(spec);
return path.join(installPath, "Contents", "MacOS");
}
async download({
version,
platform,
language,
}: InstallSpec): Promise<string> {
const toolPath = tc.find("firefox", version);
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
return toolPath;
}
core.info(`Attempting to download firefox ${version}...`);
const url = new DownloadURLFactory(version, platform, language)
.create()
.getURL();
core.info(`Acquiring ${version} from ${url}`);
const archivePath = await tc.downloadTool(url);
core.info("Extracting Firefox...");
const mountpoint = path.join("/Volumes", path.basename(archivePath));
const appPath = path.join(mountpoint, "Firefox.app");
await exec.exec("hdiutil", [
"attach",
"-quiet",
"-noautofsck",
"-noautoopen",
"-mountpoint",
mountpoint,
archivePath,
]);
core.info(`Successfully extracted firefox ${version} to ${appPath}`);
core.info("Adding to the cache ...");
const cachedDir = await tc.cacheDir(appPath, "firefox", version);
core.info(`Successfully cached firefox ${version} to ${cachedDir}`);
return cachedDir;
}
testVersion = commonTestVersion;
}
export class WindowsInstaller implements Installer {
install(spec: InstallSpec): Promise<string> {
return this.download(spec);
}
async download({
version,
platform,
language,
}: InstallSpec): Promise<string> {
const installPath = `C:\\Program Files\\Firefox_${version}`;
if (await this.checkInstall(installPath)) {
core.info(`Already installed @ ${installPath}`);
return installPath;
}
core.info(`Attempting to download firefox ${version}...`);
const url = new DownloadURLFactory(version, platform, language)
.create()
.getURL();
core.info(`Acquiring ${version} from ${url}`);
const installerPath = await tc.downloadTool(url);
await io.mv(installerPath, `${installerPath}.exe`);
core.info("Extracting Firefox...");
await exec.exec(installerPath, [
"/S",
`/InstallDirectoryName=${path.basename(installPath)}`,
]);
core.info(`Successfully installed firefox ${version} to ${installPath}`);
return installPath;
}
private async checkInstall(dir: string): Promise<boolean> {
try {
await fs.promises.access(dir, fs.constants.F_OK);
} catch (err) {
return false;
}
return true;
}
testVersion = commonTestVersion;
}