-
Notifications
You must be signed in to change notification settings - Fork 990
/
Copy pathbuildToolsJarHelper.ts
70 lines (61 loc) · 2.63 KB
/
buildToolsJarHelper.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
import * as fs from "fs-extra";
import * as os from "os";
import * as path from "path";
import * as spawn from "cross-spawn";
import * as downloadUtils from "../downloadUtils";
import { FirebaseError } from "../error";
import { logger } from "../logger";
import { rmSync } from "node:fs";
import * as utils from "../utils";
const JAR_CACHE_DIR =
process.env.FIREBASE_CRASHLYTICS_BUILDTOOLS_PATH ||
path.join(os.homedir(), ".cache", "firebase", "crashlytics", "buildtools");
const JAR_VERSION = "3.0.3";
const JAR_URL = `https://dl.google.com/android/maven2/com/google/firebase/firebase-crashlytics-buildtools/${JAR_VERSION}/firebase-crashlytics-buildtools-${JAR_VERSION}.jar`;
/**
* Returns the path to the jar file, downloading it if necessary.
*/
export async function fetchBuildtoolsJar(): Promise<string> {
// If you set CRASHLYTICS_LOCAL_JAR to a path it will override the downloaded buildtools.jar
if (process.env.CRASHLYTICS_LOCAL_JAR) {
logger.debug(`Using local Crashlytics Jar override at ${process.env.CRASHLYTICS_LOCAL_JAR}`);
return process.env.CRASHLYTICS_LOCAL_JAR;
}
const jarPath = path.join(JAR_CACHE_DIR, `crashlytics-buildtools-${JAR_VERSION}.jar`);
if (fs.existsSync(jarPath)) {
logger.debug(`Buildtools Jar already downloaded at ${jarPath}`);
return jarPath;
}
// If the Jar cache directory exists, but the jar for the current version
// doesn't, then we're running the CLI with a new Jar version and we can
// delete the old version.
if (fs.existsSync(JAR_CACHE_DIR)) {
logger.debug(
`Deleting Jar cache at ${JAR_CACHE_DIR} because the CLI was run with a newer Jar version`,
);
rmSync(JAR_CACHE_DIR, { recursive: true });
}
utils.logBullet("Downloading crashlytics-buildtools.jar to " + jarPath);
utils.logBullet(
"For open source licenses used by this command, look in the META-INF directory in the buildtools.jar file",
);
const tmpfile = await downloadUtils.downloadToTmp(JAR_URL);
fs.mkdirSync(JAR_CACHE_DIR, { recursive: true });
fs.copySync(tmpfile, jarPath);
return jarPath;
}
/**
* Helper function to invoke the given set of arguments on the the executable jar
*/
export function runBuildtoolsCommand(jarFile: string, args: string[], debug: boolean): void {
const fullArgs = ["-jar", jarFile, ...args, "-clientName", "firebase-cli;crashlytics-buildtools"];
const outputs = spawn.sync("java", fullArgs, {
stdio: debug ? "inherit" : "pipe",
});
if (outputs.status !== 0) {
if (!debug) {
utils.logWarning(outputs.stdout?.toString() || "An unknown error occurred");
}
throw new FirebaseError(`java command failed with args: ${fullArgs}`);
}
}