forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradle-build-args-service.ts
110 lines (92 loc) · 3.26 KB
/
gradle-build-args-service.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
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
import * as path from "path";
import { Configurations } from "../../common/constants";
import { IGradleBuildArgsService } from "../../definitions/gradle";
import { IAndroidToolsInfo } from "../../declarations";
import { IAndroidBuildData } from "../../definitions/build";
import { IHooksService, IAnalyticsService } from "../../common/declarations";
import { injector } from "../../common/yok";
import { IProjectData } from "../../definitions/project";
export class GradleBuildArgsService implements IGradleBuildArgsService {
constructor(
private $androidToolsInfo: IAndroidToolsInfo,
private $hooksService: IHooksService,
private $analyticsService: IAnalyticsService,
private $staticConfig: Config.IStaticConfig,
private $projectData: IProjectData,
private $logger: ILogger
) {}
public async getBuildTaskArgs(
buildData: IAndroidBuildData
): Promise<string[]> {
const args = this.getBaseTaskArgs(buildData);
args.unshift(this.getBuildTaskName(buildData));
if (
await this.$analyticsService.isEnabled(
this.$staticConfig.TRACK_FEATURE_USAGE_SETTING_NAME
)
) {
args.push("-PgatherAnalyticsData=true");
}
// allow modifying gradle args from a `before-build-task-args` hook
await this.$hooksService.executeBeforeHooks("build-task-args", {
hookArgs: { args },
});
return args;
}
public getCleanTaskArgs(buildData: IAndroidBuildData): string[] {
const args = this.getBaseTaskArgs(buildData);
args.unshift("clean");
return args;
}
private getBaseTaskArgs(buildData: IAndroidBuildData): string[] {
const args = this.getBuildLoggingArgs();
const toolsInfo = this.$androidToolsInfo.getToolsInfo({
projectDir: buildData.projectDir,
});
// ensure we initialize project data
this.$projectData.initializeProjectData(buildData.projectDir);
args.push(
`-PcompileSdk=android-${toolsInfo.compileSdkVersion}`,
`-PtargetSdk=${toolsInfo.targetSdkVersion}`,
`-PbuildToolsVersion=${toolsInfo.buildToolsVersion}`,
`-PgenerateTypings=${toolsInfo.generateTypings}`,
`-PappPath=${this.$projectData.getAppDirectoryPath()}`,
`-PappResourcesPath=${this.$projectData.getAppResourcesDirectoryPath()}`
);
if (buildData.gradleArgs) {
const additionalArgs: string[] = []
buildData.gradleArgs.forEach(arg=>{
additionalArgs.push(...arg.split(' -P').map((a,i) => i === 0 ? a : `-P${a}`));
});
args.push(...additionalArgs);
}
if (buildData.release) {
args.push(
"-Prelease",
`-PksPath=${path.resolve(buildData.keyStorePath)}`,
`-Palias=${buildData.keyStoreAlias}`,
`-Ppassword=${buildData.keyStoreAliasPassword}`,
`-PksPassword=${buildData.keyStorePassword}`
);
}
return args;
}
private getBuildLoggingArgs(): string[] {
const args = [];
const logLevel = this.$logger.getLevel();
if (logLevel === "TRACE") {
args.push("--stacktrace", "--debug");
} else if (logLevel === "INFO") {
args.push("--quiet");
}
return args;
}
private getBuildTaskName(buildData: IAndroidBuildData): string {
const baseTaskName = buildData.androidBundle ? "bundle" : "assemble";
const buildTaskName = buildData.release
? `${baseTaskName}${Configurations.Release}`
: `${baseTaskName}${Configurations.Debug}`;
return buildTaskName;
}
}
injector.register("gradleBuildArgsService", GradleBuildArgsService);