forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-controller.ts
200 lines (172 loc) · 5.25 KB
/
build-controller.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import * as constants from "../constants";
import { Configurations } from "../common/constants";
import { EventEmitter } from "events";
import { attachAwaitDetach } from "../common/helpers";
import { IProjectDataService } from "../definitions/project";
import {
IBuildController,
IBuildArtefactsService,
IBuildInfoFileService,
IBuildData,
} from "../definitions/build";
import { IPlatformsDataService } from "../definitions/platform";
import { IAnalyticsService, IFileSystem } from "../common/declarations";
import { IInjector } from "../common/definitions/yok";
import { injector } from "../common/yok";
export class BuildController extends EventEmitter implements IBuildController {
constructor(
private $analyticsService: IAnalyticsService,
private $buildArtefactsService: IBuildArtefactsService,
private $buildInfoFileService: IBuildInfoFileService,
private $fs: IFileSystem,
private $logger: ILogger,
private $injector: IInjector,
private $mobileHelper: Mobile.IMobileHelper,
private $projectDataService: IProjectDataService,
private $projectChangesService: IProjectChangesService,
private $prepareController: IPrepareController
) {
super();
}
private get $platformsDataService(): IPlatformsDataService {
return this.$injector.resolve("platformsDataService");
}
public async prepareAndBuild(buildData: IBuildData): Promise<string> {
await this.$prepareController.prepare(buildData);
const result = await this.build(buildData);
return result;
}
public async build(buildData: IBuildData): Promise<string> {
this.$logger.info("Building project...");
const startTime = performance.now();
const platform = buildData.platform.toLowerCase();
const projectData = this.$projectDataService.getProjectData(
buildData.projectDir
);
const platformData = this.$platformsDataService.getPlatformData(
platform,
projectData
);
const action = constants.TrackActionNames.Build;
const isForDevice = this.$mobileHelper.isAndroidPlatform(platform)
? null
: buildData && buildData.buildForDevice;
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action,
isForDevice,
platform,
projectDir: projectData.projectDir,
additionalData: `${
buildData.release ? Configurations.Release : Configurations.Debug
}_${
buildData.clean
? constants.BuildStates.Clean
: constants.BuildStates.Incremental
}`,
});
if (buildData.clean) {
await platformData.platformProjectService.cleanProject(
platformData.projectRoot
);
}
const handler = (data: any) => {
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
this.$logger.info(data.data.toString(), {
[constants.LoggerConfigData.skipNewLine]: true,
});
};
await attachAwaitDetach(
constants.BUILD_OUTPUT_EVENT_NAME,
platformData.platformProjectService,
handler,
platformData.platformProjectService.buildProject(
platformData.projectRoot,
projectData,
buildData
)
);
const buildInfoFileDir = platformData.getBuildOutputPath(buildData);
this.$buildInfoFileService.saveLocalBuildInfo(
platformData,
buildInfoFileDir
);
const endTime = performance.now();
const buildTime = (endTime - startTime) / 1000;
this.$logger.info("Project successfully built.");
this.$logger.info(`Build time: ${buildTime.toFixed(3)} s.`);
const result = await this.$buildArtefactsService.getLatestAppPackagePath(
platformData,
buildData
);
if (buildData.copyTo) {
this.$buildArtefactsService.copyLatestAppPackage(
buildData.copyTo,
platformData,
buildData
);
} else {
this.$logger.info(`The build result is located at: ${result}`);
}
return result;
}
public async buildIfNeeded(buildData: IBuildData): Promise<string> {
let result = null;
const shouldBuildPlatform = await this.shouldBuild(buildData);
if (shouldBuildPlatform) {
result = await this.build(buildData);
}
return result;
}
public async shouldBuild(buildData: IBuildData): Promise<boolean> {
const projectData = this.$projectDataService.getProjectData(
buildData.projectDir
);
const platformData = this.$platformsDataService.getPlatformData(
buildData.platform,
projectData
);
const outputPath =
buildData.outputPath || platformData.getBuildOutputPath(buildData);
const changesInfo =
this.$projectChangesService.currentChanges ||
(await this.$projectChangesService.checkForChanges(
platformData,
projectData,
buildData
));
if (changesInfo.changesRequireBuild) {
return true;
}
if (!this.$fs.exists(outputPath)) {
return true;
}
const validBuildOutputData = platformData.getValidBuildOutputData(
buildData
);
const packages = this.$buildArtefactsService.getAllAppPackages(
outputPath,
validBuildOutputData
);
if (packages.length === 0) {
return true;
}
const prepareInfo = this.$projectChangesService.getPrepareInfo(
platformData
);
const buildInfo = this.$buildInfoFileService.getLocalBuildInfo(
platformData,
buildData
);
if (!prepareInfo || !buildInfo) {
return true;
}
if (buildData.clean) {
return true;
}
if (prepareInfo.time === buildInfo.prepareTime) {
return false;
}
return prepareInfo.changesRequireBuildTime !== buildInfo.prepareTime;
}
}
injector.register("buildController", BuildController);