Skip to content

Commit 2ce6c30

Browse files
committed
refactor installation process
1 parent 6f021b1 commit 2ce6c30

3 files changed

Lines changed: 95 additions & 60 deletions

File tree

src/LinuxInstaller.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,52 @@ import * as tc from "@actions/tool-cache";
44
import { DownloadURLFactory } from "./DownloadURLFactory";
55
import { testBinaryVersion } from "./firefoxUtils";
66
import type { InstallSpec, Installer } from "./installers";
7+
import { Platform } from "./platform";
78

89
export class LinuxInstaller implements Installer {
9-
async install(spec: InstallSpec): Promise<string> {
10-
return this.download(spec);
11-
}
12-
13-
private async download({
14-
version,
15-
platform,
16-
language,
17-
}: InstallSpec): Promise<string> {
10+
async install({ version, platform, language }: InstallSpec): Promise<string> {
1811
const toolPath = tc.find("firefox", version);
1912
if (toolPath) {
2013
core.info(`Found in cache @ ${toolPath}`);
2114
return toolPath;
2215
}
2316
core.info(`Attempting to download firefox ${version}...`);
17+
const archivePath = await this.downloadArchive({
18+
version,
19+
platform,
20+
language,
21+
});
22+
23+
core.info("Extracting Firefox...");
24+
const extPath = await this.extractArchive(archivePath);
25+
core.info(`Successfully extracted firefox ${version} to ${extPath}`);
26+
27+
core.info("Adding to the cache ...");
28+
const cachedDir = await tc.cacheDir(extPath, "firefox", version);
29+
core.info(`Successfully cached firefox ${version} to ${cachedDir}`);
30+
31+
return cachedDir;
32+
}
2433

34+
private async downloadArchive({
35+
version,
36+
platform,
37+
language,
38+
}: InstallSpec): Promise<string> {
2539
const url = new DownloadURLFactory(version, platform, language)
2640
.create()
2741
.getURL();
2842
core.info(`Acquiring ${version} from ${url}`);
2943

3044
const archivePath = await tc.downloadTool(url);
31-
core.info("Extracting Firefox...");
32-
const handle = await fs.promises.open(archivePath, "r");
45+
return archivePath;
46+
}
47+
48+
private async extractArchive(archivePath: string): Promise<string> {
49+
const file = await fs.promises.open(archivePath, "r");
3350
const firstBytes = new Int8Array(3);
34-
if (handle !== null) {
35-
await handle.read(firstBytes, 0, 3, null);
51+
if (file !== null) {
52+
await file.read(firstBytes, 0, 3, null);
3653
core.debug(
3754
`Extracted ${firstBytes[0]}, ${firstBytes[1]} and ${firstBytes[2]}`,
3855
);
@@ -45,12 +62,8 @@ export class LinuxInstaller implements Installer {
4562
options,
4663
"--strip-components=1",
4764
]);
48-
core.info(`Successfully extracted firefox ${version} to ${extPath}`);
4965

50-
core.info("Adding to the cache ...");
51-
const cachedDir = await tc.cacheDir(extPath, "firefox", version);
52-
core.info(`Successfully cached firefox ${version} to ${cachedDir}`);
53-
return cachedDir;
66+
return extPath;
5467
}
5568

5669
async testVersion(bin: string): Promise<string> {

src/MacOSInstaller.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,49 @@ import type { InstallSpec, Installer } from "./installers";
88
import { LatestVersion } from "./versions";
99

1010
export class MacOSInstaller implements Installer {
11-
async install(spec: InstallSpec): Promise<string> {
12-
const installPath = await this.download(spec);
13-
return path.join(installPath, "Contents", "MacOS");
14-
}
15-
16-
async download({
17-
version,
18-
platform,
19-
language,
20-
}: InstallSpec): Promise<string> {
11+
async install({ version, platform, language }: InstallSpec): Promise<string> {
2112
const toolPath = tc.find("firefox", version);
2213
if (toolPath) {
2314
core.info(`Found in cache @ ${toolPath}`);
2415
return toolPath;
2516
}
2617
core.info(`Attempting to download firefox ${version}...`);
18+
const archivePath = await this.downloadArchive({
19+
version,
20+
platform,
21+
language,
22+
});
23+
24+
core.info("Extracting Firefox...");
25+
const appPath = await this.extractArchive(archivePath, version);
26+
core.info(`Successfully extracted firefox ${version} to ${appPath}`);
27+
28+
core.info("Adding to the cache ...");
29+
const cachedDir = await tc.cacheDir(appPath, "firefox", version);
30+
core.info(`Successfully cached firefox ${version} to ${cachedDir}`);
2731

32+
return path.join(cachedDir, "Contents", "MacOS");
33+
}
34+
35+
private async downloadArchive({
36+
version,
37+
platform,
38+
language,
39+
}: InstallSpec): Promise<string> {
2840
const url = new DownloadURLFactory(version, platform, language)
2941
.create()
3042
.getURL();
3143
core.info(`Acquiring ${version} from ${url}`);
3244

3345
const archivePath = await tc.downloadTool(url);
34-
core.info("Extracting Firefox...");
46+
return archivePath;
47+
}
3548

49+
private async extractArchive(
50+
archivePath: string,
51+
version: string,
52+
): Promise<string> {
3653
const mountpoint = path.join("/Volumes", path.basename(archivePath));
37-
const appPath = (() => {
38-
if (version === LatestVersion.LATEST_NIGHTLY) {
39-
return path.join(mountpoint, "Firefox Nightly.app");
40-
} else if (version.includes("devedition")) {
41-
return path.join(mountpoint, "Firefox Developer Edition.app");
42-
} else {
43-
return path.join(mountpoint, "Firefox.app");
44-
}
45-
})();
4654

4755
await exec.exec("hdiutil", [
4856
"attach",
@@ -53,12 +61,14 @@ export class MacOSInstaller implements Installer {
5361
mountpoint,
5462
archivePath,
5563
]);
56-
core.info(`Successfully extracted firefox ${version} to ${appPath}`);
5764

58-
core.info("Adding to the cache ...");
59-
const cachedDir = await tc.cacheDir(appPath, "firefox", version);
60-
core.info(`Successfully cached firefox ${version} to ${cachedDir}`);
61-
return cachedDir;
65+
if (version === LatestVersion.LATEST_NIGHTLY) {
66+
return path.join(mountpoint, "Firefox Nightly.app");
67+
} else if (version.includes("devedition")) {
68+
return path.join(mountpoint, "Firefox Developer Edition.app");
69+
} else {
70+
return path.join(mountpoint, "Firefox.app");
71+
}
6272
}
6373

6474
async testVersion(bin: string): Promise<string> {

src/WindowsInstaller.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,41 @@ import { testBinaryVersion } from "./firefoxUtils";
99
import type { InstallSpec, Installer } from "./installers";
1010

1111
export class WindowsInstaller implements Installer {
12-
install(spec: InstallSpec): Promise<string> {
13-
return this.download(spec);
14-
}
15-
16-
async download({
17-
version,
18-
platform,
19-
language,
20-
}: InstallSpec): Promise<string> {
12+
async install({ version, platform, language }: InstallSpec): Promise<string> {
2113
const installPath = `C:\\Program Files\\Firefox_${version}`;
2214
if (await this.checkInstall(installPath)) {
2315
core.info(`Already installed @ ${installPath}`);
2416
return installPath;
2517
}
2618

2719
core.info(`Attempting to download firefox ${version}...`);
20+
const installerPath = await this.downloadArchive({
21+
version,
22+
platform,
23+
language,
24+
});
2825

26+
core.info("Extracting Firefox...");
27+
await this.extractArchive(installerPath, installPath);
28+
core.info(`Successfully installed firefox ${version} to ${installPath}`);
29+
30+
return installPath;
31+
}
32+
33+
private async downloadArchive({
34+
version,
35+
platform,
36+
language,
37+
}: InstallSpec): Promise<string> {
2938
const url = new DownloadURLFactory(version, platform, language)
3039
.create()
3140
.getURL();
3241
core.info(`Acquiring ${version} from ${url}`);
3342

3443
const installerPath = await tc.downloadTool(url);
3544
await io.mv(installerPath, `${installerPath}.exe`);
36-
core.info("Extracting Firefox...");
37-
38-
await exec.exec(installerPath, [
39-
"/S",
40-
`/InstallDirectoryName=${path.basename(installPath)}`,
41-
]);
42-
core.info(`Successfully installed firefox ${version} to ${installPath}`);
4345

44-
return installPath;
46+
return installerPath;
4547
}
4648

4749
private async checkInstall(dir: string): Promise<boolean> {
@@ -53,6 +55,16 @@ export class WindowsInstaller implements Installer {
5355
return true;
5456
}
5557

58+
private async extractArchive(
59+
installerPath: string,
60+
installPath: string,
61+
): Promise<void> {
62+
await exec.exec(installerPath, [
63+
"/S",
64+
`/InstallDirectoryName=${path.basename(installPath)}`,
65+
]);
66+
}
67+
5668
async testVersion(bin: string): Promise<string> {
5769
return testBinaryVersion(bin);
5870
}

0 commit comments

Comments
 (0)