-
Notifications
You must be signed in to change notification settings - Fork 934
Expand file tree
/
Copy pathindex.ts
More file actions
132 lines (120 loc) · 3.52 KB
/
index.ts
File metadata and controls
132 lines (120 loc) · 3.52 KB
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
import {
CLIError,
logger,
printRunDoctorTip,
} from '@react-native-community/cli-tools';
import {Config} from '@react-native-community/cli-types';
import execa from 'execa';
import {getAndroidProject} from '@react-native-community/cli-config-android';
import adb from '../runAndroid/adb';
import getAdbPath from '../runAndroid/getAdbPath';
import {getTaskNames} from '../runAndroid/getTaskNames';
import {promptForTaskSelection} from '../runAndroid/listAndroidTasks';
export interface BuildFlags {
mode?: string;
activeArchOnly?: boolean;
tasks?: Array<string>;
extraParams?: Array<string>;
interactive?: boolean;
}
async function buildAndroid(
_argv: Array<string>,
config: Config,
args: BuildFlags,
) {
const androidProject = getAndroidProject(config);
if (args.tasks && args.mode) {
logger.warn(
'Both "tasks" and "mode" parameters were passed to "build" command. Using "tasks" for building the app.',
);
}
let {tasks} = args;
if (args.interactive) {
const selectedTask = await promptForTaskSelection(
'build',
androidProject.sourceDir,
);
if (selectedTask) {
tasks = [selectedTask];
}
}
let gradleArgs = getTaskNames(
androidProject.appName,
args.mode,
tasks,
'bundle',
);
if (args.extraParams) {
gradleArgs.push(...args.extraParams);
}
if (args.activeArchOnly) {
const adbPath = getAdbPath();
const devices = adb.getDevices(adbPath);
const architectures = devices
.map((device) => {
return adb.getCPU(adbPath, device);
})
.filter(
(arch, index, array) => arch != null && array.indexOf(arch) === index,
);
if (architectures.length > 0) {
logger.info(`Detected architectures ${architectures.join(', ')}`);
// `reactNativeDebugArchitectures` was renamed to `reactNativeArchitectures` in 0.68.
// Can be removed when 0.67 no longer needs to be supported.
gradleArgs.push(
'-PreactNativeDebugArchitectures=' + architectures.join(','),
);
gradleArgs.push('-PreactNativeArchitectures=' + architectures.join(','));
}
}
return build(gradleArgs, androidProject.sourceDir);
}
export function build(gradleArgs: string[], sourceDir: string) {
process.chdir(sourceDir);
const cmd = process.platform.startsWith('win') ? 'gradlew.bat' : './gradlew';
logger.info('Building the app...');
logger.debug(`Running command "${cmd} ${gradleArgs.join(' ')}"`);
try {
execa.sync(cmd, gradleArgs, {
stdio: 'inherit',
cwd: sourceDir,
});
} catch (error) {
printRunDoctorTip();
throw new CLIError('Failed to build the app.', error as Error);
}
}
export const options = [
{
name: '--mode <string>',
description: "Specify your app's build variant",
},
{
name: '--tasks <list>',
description:
'Run custom Gradle tasks. By default it\'s "assembleDebug". Will override passed mode and variant arguments.',
parse: (val: string) => val.split(','),
},
{
name: '--active-arch-only',
description:
'Build native libraries only for the current device architecture for debug builds.',
default: false,
},
{
name: '--extra-params <string>',
description: 'Custom params passed to gradle build command',
parse: (val: string) => val.split(' '),
},
{
name: '-i --interactive',
description:
'Explicitly select build type and flavour to use before running a build',
},
];
export default {
name: 'build-android',
description: 'builds your app',
func: buildAndroid,
options,
};