Skip to content

Commit

Permalink
perf: remove Gradle task check on run-android (#2144) (#2145)
Browse files Browse the repository at this point in the history
* remove gradle task check on run-android

* add loader and README note

* throw error when no tasks found

Co-authored-by: Tomasz Misiukiewicz <[email protected]>
  • Loading branch information
thymikee and TMisiukiewicz authored Nov 3, 2023
1 parent 9d486c8 commit 242c497
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 42 deletions.
8 changes: 8 additions & 0 deletions packages/cli-platform-android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ Build native libraries only for the current device architecture for debug builds
List all available Android devices and simulators and let you choose one to run the app.


#### `--interactive`

Manually select a task and device/simulator you want to run your app on.

> [!WARNING]
> This flag is running `./gradlew tasks` under the hood, which might take some time for more complex apps. If that affects your project, consider using `--mode` and `--deviceId` flags instead.
### `build-android`

Usage:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ async function buildAndroid(
args.mode || args.variant,
tasks,
'bundle',
androidProject.sourceDir,
);

if (args.extraParams) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
import {toPascalCase} from './toPascalCase';
import type {BuildFlags} from '../buildAndroid';
import {getGradleTasks} from './listAndroidTasks';
import {CLIError, logger} from '@react-native-community/cli-tools';

export function getTaskNames(
appName: string,
mode: BuildFlags['mode'] = 'debug',
tasks: BuildFlags['tasks'],
taskPrefix: 'assemble' | 'install' | 'bundle',
sourceDir: string,
): Array<string> {
let appTasks =
const appTasks =
tasks && tasks.length ? tasks : [taskPrefix + toPascalCase(mode)];

// Check against build flavors for "install" task ("assemble" don't care about it so much and will build all)
if (!tasks?.length && taskPrefix === 'install') {
const actionableInstallTasks = getGradleTasks('install', sourceDir);
if (!actionableInstallTasks.find((t) => t.task.includes(appTasks[0]))) {
const installTasksForMode = actionableInstallTasks.filter((t) =>
t.task.toLowerCase().includes(mode),
);
if (!installTasksForMode.length) {
throw new CLIError(
`Couldn't find "${appTasks
.map((taskName) => taskName.replace(taskPrefix, ''))
.join(
', ',
)}" build variant. Available variants are: ${actionableInstallTasks
.map((t) => `"${t.task.replace(taskPrefix, '')}"`)
.join(', ')}.`,
);
}
logger.warn(
`Found multiple tasks for "install" command: ${installTasksForMode
.map((t) => t.task)
.join(', ')}.\nSelecting first available: ${
installTasksForMode[0].task
}.`,
);
appTasks = [installTasksForMode[0].task];
}
}

return appName
? appTasks.map((command) => `${appName}:${command}`)
: appTasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ function runOnSpecificDevice(
args.mode || args.variant,
args.tasks ?? buildTask,
'install',
androidProject.sourceDir,
);

// using '-x lint' in order to ignore linting errors while building the apk
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CLIError} from '@react-native-community/cli-tools';
import {CLIError, getLoader} from '@react-native-community/cli-tools';
import chalk from 'chalk';
import execa from 'execa';
import prompts from 'prompts';
Expand Down Expand Up @@ -32,12 +32,19 @@ export const getGradleTasks = (
taskType: 'install' | 'build',
sourceDir: string,
) => {
const loader = getLoader();
loader.start('Searching for available Gradle tasks...');
const cmd = process.platform.startsWith('win') ? 'gradlew.bat' : './gradlew';

const out = execa.sync(cmd, ['tasks', '--group', taskType], {
cwd: sourceDir,
}).stdout;
return parseTasksFromGradleFile(taskType, out);
try {
const out = execa.sync(cmd, ['tasks', '--group', taskType], {
cwd: sourceDir,
}).stdout;
loader.succeed();
return parseTasksFromGradleFile(taskType, out);
} catch {
loader.fail();
return [];
}
};

export const promptForTaskSelection = async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ async function runOnAllDevices(
args.mode || args.variant,
args.tasks,
'install',
androidProject.sourceDir,
);

if (args.extraParams) {
Expand Down

0 comments on commit 242c497

Please sign in to comment.