-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools-android): add primitives for build and run commands (#3322)
- Loading branch information
Showing
21 changed files
with
565 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@rnx-kit/cli": patch | ||
--- | ||
|
||
Added Android support to the experimental commands for building and running | ||
apps. Again, this still needs more testing and will not be publicly available | ||
yet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@rnx-kit/tools-android": patch | ||
--- | ||
|
||
Added primitives for building 'build' and 'run' commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Config } from "@react-native-community/cli-types"; | ||
import ora from "ora"; | ||
import type { AndroidBuildParams } from "./types"; | ||
|
||
export async function buildAndroid( | ||
config: Config, | ||
buildParams: AndroidBuildParams, | ||
logger = ora() | ||
): Promise<string | number | null> { | ||
const { sourceDir } = config.project.android ?? {}; | ||
if (!sourceDir) { | ||
logger.fail("No Android project was found"); | ||
process.exitCode = 1; | ||
return null; | ||
} | ||
|
||
return import("@rnx-kit/tools-android").then(({ assemble }) => { | ||
return new Promise((resolve) => { | ||
logger.start("Building"); | ||
|
||
const errors: Buffer[] = []; | ||
const gradle = assemble(sourceDir, buildParams); | ||
|
||
gradle.stdout.on("data", () => (logger.text += ".")); | ||
gradle.stderr.on("data", (data) => errors.push(data)); | ||
|
||
gradle.on("close", (code) => { | ||
if (code === 0) { | ||
logger.succeed("Build succeeded"); | ||
resolve(sourceDir); | ||
} else { | ||
logger.fail(Buffer.concat(errors).toString()); | ||
process.exitCode = code ?? 1; | ||
resolve(code); | ||
} | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copy of types from `@rnx-kit/tools-apple`. If Jest wasn't such a pain to | ||
* configure, we would have added an `import type` at the top instead: | ||
* | ||
* import type { BuildParams as AndroidBuildParams } from "@rnx-kit/tools-android" with { "resolution-mode": "import" }; | ||
* import type { BuildParams as AppleBuildParams } from "@rnx-kit/tools-apple" with { "resolution-mode": "import" }; | ||
* | ||
* But Jest doesn't like import attributes and it doesn't matter if we add | ||
* `@babel/plugin-syntax-import-attributes` in the config. | ||
* | ||
* TOOD: Remove this file when we can migrate away from Jest in this package. | ||
*/ | ||
|
||
export type DeviceType = "device" | "emulator" | "simulator"; | ||
|
||
export type BuildConfiguration = "Debug" | "Release"; | ||
|
||
export type AndroidBuildParams = { | ||
platform: "android"; | ||
destination?: DeviceType; | ||
configuration?: BuildConfiguration; | ||
archs?: string; | ||
}; | ||
|
||
export type AppleBuildParams = | ||
| { | ||
platform: "ios" | "visionos"; | ||
scheme?: string; | ||
destination?: DeviceType; | ||
configuration?: BuildConfiguration; | ||
archs?: string; | ||
isBuiltRemotely?: boolean; | ||
} | ||
| { | ||
platform: "macos"; | ||
scheme?: string; | ||
configuration?: BuildConfiguration; | ||
isBuiltRemotely?: boolean; | ||
}; | ||
|
||
export type AndroidInputParams = AndroidBuildParams & { | ||
device?: string; | ||
}; | ||
|
||
export type AppleInputParams = AppleBuildParams & { | ||
device?: string; | ||
workspace?: string; | ||
}; | ||
|
||
export type InputParams = AndroidInputParams | AppleInputParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { Config } from "@react-native-community/cli-types"; | ||
import * as path from "node:path"; | ||
import ora from "ora"; | ||
import { buildAndroid } from "../build/android"; | ||
import type { AndroidInputParams } from "../build/types"; | ||
|
||
export async function runAndroid( | ||
config: Config, | ||
buildParams: AndroidInputParams | ||
) { | ||
const logger = ora(); | ||
|
||
const projectDir = await buildAndroid(config, buildParams, logger); | ||
if (typeof projectDir !== "string") { | ||
return; | ||
} | ||
|
||
logger.start("Preparing to launch app"); | ||
|
||
const { findOutputFile, getPackageName, install, selectDevice, start } = | ||
await import("@rnx-kit/tools-android"); | ||
|
||
const { configuration = "Debug" } = buildParams; | ||
const apks = findOutputFile(projectDir, configuration); | ||
if (apks.length === 0) { | ||
logger.fail("Failed to find the APK that was just built"); | ||
process.exitCode = 1; | ||
return; | ||
} | ||
|
||
if (apks.length > 1) { | ||
const currentStatus = logger.text; | ||
const choices = apks.map((p) => path.basename(p)).join(", "); | ||
logger.info(`Multiple APKs were found; picking the first one: ${choices}`); | ||
logger.info("If this is wrong, remove the others and try again"); | ||
logger.start(currentStatus); | ||
} | ||
|
||
const apk = apks[0]; | ||
const info = getPackageName(apk); | ||
if (info instanceof Error) { | ||
logger.fail(info.message); | ||
process.exitCode = 1; | ||
return; | ||
} | ||
|
||
const device = await selectDevice(buildParams.device, logger); | ||
if (!device) { | ||
logger.fail("Failed to launch app: Could not find an appropriate device"); | ||
process.exitCode = 1; | ||
return; | ||
} | ||
|
||
logger.start(`Installing ${apk}`); | ||
|
||
const { packageName, activityName } = info; | ||
const error = await install(device, apk, packageName); | ||
if (error) { | ||
logger.fail(error.message); | ||
process.exitCode = 1; | ||
return; | ||
} | ||
|
||
logger.text = `Starting ${packageName}`; | ||
await start(device, packageName, activityName); | ||
|
||
logger.succeed(`Started ${packageName}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.