Skip to content

Commit cae2d19

Browse files
author
Dimitar Kerezov
committed
Emit build output when necessary
1 parent 9d200c3 commit cae2d19

12 files changed

+69
-31
lines changed

lib/bootstrap.ts

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ $injector.require("tnsModulesService", "./services/tns-modules-service");
2020

2121
$injector.require("platformsData", "./platforms-data");
2222
$injector.require("platformService", "./services/platform-service");
23-
$injector.requirePublic("localBuildService", "./services/local-build-service");
2423

2524
$injector.require("iOSDebugService", "./services/ios-debug-service");
2625
$injector.require("androidDebugService", "./services/android-debug-service");

lib/common

lib/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ export const ItunesConnectApplicationTypes = new ItunesConnectApplicationTypesCl
6767

6868
export const ANGULAR_NAME = "angular";
6969
export const TYPESCRIPT_NAME = "typescript";
70+
export const BUILD_OUTPUT_EVENT_NAME = "buildOutput";

lib/definitions/platform.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
interface IPlatformService {
1+
interface IPlatformService extends NodeJS.EventEmitter {
22
addPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData): Promise<void>;
33

44
/**

lib/definitions/project.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ interface IBuildConfig extends IAndroidBuildOptionsSettings {
122122
projectDir: string;
123123
clean?: boolean;
124124
architectures?: string[];
125+
buildOutputStdio?: string;
125126
}
126127

127128
/**
@@ -142,7 +143,7 @@ interface IiOSBuildConfig extends IBuildConfig, IRelease, IDeviceIdentifier, IPr
142143
teamIdentifier?: string;
143144
}
144145

145-
interface IPlatformProjectService {
146+
interface IPlatformProjectService extends NodeJS.EventEmitter {
146147
getPlatformData(projectData: IProjectData): IPlatformData;
147148
validate(projectData: IProjectData): Promise<void>;
148149
createProject(frameworkDir: string, frameworkVersion: string, projectData: IProjectData, pathToTemplate?: string): Promise<void>;

lib/nativescript-cli-lib-bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ $injector.overrideAlreadyRequiredModule = true;
88
$injector.requirePublic("companionAppsService", "./common/appbuilder/services/livesync/companion-apps-service");
99
$injector.requirePublicClass("deviceEmitter", "./common/appbuilder/device-emitter");
1010
$injector.requirePublicClass("deviceLogProvider", "./common/appbuilder/device-log-provider");
11+
$injector.requirePublicClass("localBuildService", "./services/local-build-service");

lib/services/android-project-service.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,14 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
250250
if (this.$hostInfo.isWindows) {
251251
gradleBin += ".bat"; // cmd command line parsing rules are weird. Avoid issues with quotes. See https://github.com/apache/cordova-android/blob/master/bin/templates/cordova/lib/builders/GradleBuilder.js for another approach
252252
}
253-
await this.spawn(gradleBin, buildOptions, { stdio: "inherit", cwd: this.getPlatformData(projectData).projectRoot });
253+
254+
this.$childProcess.on(constants.BUILD_OUTPUT_EVENT_NAME, (data: any) => {
255+
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
256+
});
257+
await this.spawn(gradleBin,
258+
buildOptions,
259+
{ stdio: buildConfig.buildOutputStdio || "inherit", cwd: this.getPlatformData(projectData).projectRoot },
260+
{ emitOptions: { shouldEmit: true, eventName: constants.BUILD_OUTPUT_EVENT_NAME }, throwError: false });
254261
} else {
255262
this.$errors.failWithoutHelp("Cannot complete build because this project is ANT-based." + EOL +
256263
"Run `tns platform remove android && tns platform add android` to switch to Gradle and try again.");
@@ -437,8 +444,8 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
437444
shell.cp(cpArg, paths, projectRoot);
438445
}
439446

440-
private async spawn(command: string, args: string[], opts?: any): Promise<ISpawnResult> {
441-
return this.$childProcess.spawnFromEvent(command, args, "close", opts || { stdio: "inherit" });
447+
private async spawn(command: string, args: string[], opts?: any, spawnOpts?: ISpawnFromEventOptions): Promise<ISpawnResult> {
448+
return this.$childProcess.spawnFromEvent(command, args, "close", opts || { stdio: "inherit" }, spawnOpts);
442449
}
443450

444451
private validatePackageName(packageName: string): void {

lib/services/ios-project-service.ts

+24-13
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,13 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
259259
// }
260260
// }
261261

262+
this.$childProcess.on(constants.BUILD_OUTPUT_EVENT_NAME, (data: any) => {
263+
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
264+
});
262265
if (buildConfig.buildForDevice) {
263266
await this.buildForDevice(projectRoot, basicArgs, buildConfig, projectData);
264267
} else {
265-
await this.buildForSimulator(projectRoot, basicArgs, buildConfig);
268+
await this.buildForSimulator(projectRoot, basicArgs, projectData, buildConfig.buildOutputStdio);
266269
}
267270

268271
}
@@ -319,10 +322,14 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
319322
}
320323

321324
// this.$logger.out("xcodebuild...");
322-
await this.$childProcess.spawnFromEvent("xcodebuild", args, "exit", { stdio: 'inherit' });
325+
await this.$childProcess.spawnFromEvent("xcodebuild",
326+
args,
327+
"exit",
328+
{ stdio: buildConfig.buildOutputStdio || "inherit", cwd: this.getPlatformData(projectData).projectRoot },
329+
{ emitOptions: { shouldEmit: true, eventName: constants.BUILD_OUTPUT_EVENT_NAME }, throwError: false });
323330
// this.$logger.out("xcodebuild build succeded.");
324331

325-
await this.createIpa(projectRoot, projectData);
332+
await this.createIpa(projectRoot, projectData, buildConfig.buildOutputStdio);
326333
}
327334

328335
private async setupSigningFromProvision(projectRoot: string, projectData: IProjectData, provision?: any): Promise<void> {
@@ -394,7 +401,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
394401
}
395402
}
396403

397-
private async buildForSimulator(projectRoot: string, args: string[], buildConfig?: IiOSBuildConfig): Promise<void> {
404+
private async buildForSimulator(projectRoot: string, args: string[], projectData: IProjectData, buildOutputStdio?: string): Promise<void> {
398405
args = args.concat([
399406
"-sdk", "iphonesimulator",
400407
"ARCHS=i386 x86_64",
@@ -404,10 +411,12 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
404411
"CODE_SIGN_IDENTITY="
405412
]);
406413

407-
await this.$childProcess.spawnFromEvent("xcodebuild", args, "exit", { stdio: 'inherit' });
414+
await this.$childProcess.spawnFromEvent("xcodebuild", args, "exit",
415+
{ stdio: buildOutputStdio || "inherit", cwd: this.getPlatformData(projectData).projectRoot },
416+
{ emitOptions: { shouldEmit: true, eventName: constants.BUILD_OUTPUT_EVENT_NAME }, throwError: false });
408417
}
409418

410-
private async createIpa(projectRoot: string, projectData: IProjectData): Promise<void> {
419+
private async createIpa(projectRoot: string, projectData: IProjectData, buildOutputStdio?: string): Promise<void> {
411420
let buildOutputPath = path.join(projectRoot, "build", "device");
412421
let xcrunArgs = [
413422
"-sdk", "iphoneos",
@@ -419,7 +428,9 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
419428
xcrunArgs.push("-verbose");
420429
// }
421430
// this.$logger.out("Packaging project...");
422-
await this.$childProcess.spawnFromEvent("xcrun", xcrunArgs, "exit", { stdio: 'inherit' });
431+
await this.$childProcess.spawnFromEvent("xcrun", xcrunArgs, "exit",
432+
{ stdio: buildOutputStdio || "inherit", cwd: this.getPlatformData(projectData).projectRoot },
433+
{ emitOptions: { shouldEmit: true, eventName: constants.BUILD_OUTPUT_EVENT_NAME }, throwError: false });
423434
// this.$logger.out("Project package succeeded.");
424435
}
425436

@@ -623,12 +634,12 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
623634

624635
private getInfoPlistPath(projectData: IProjectData): string {
625636
return path.join(
626-
projectData.projectDir,
627-
constants.APP_FOLDER_NAME,
628-
constants.APP_RESOURCES_FOLDER_NAME,
629-
this.getPlatformData(projectData).normalizedPlatformName,
630-
this.getPlatformData(projectData).configurationFileName
631-
);
637+
projectData.projectDir,
638+
constants.APP_FOLDER_NAME,
639+
constants.APP_RESOURCES_FOLDER_NAME,
640+
this.getPlatformData(projectData).normalizedPlatformName,
641+
this.getPlatformData(projectData).configurationFileName
642+
);
632643
}
633644

634645
public ensureConfigurationFileInAppResources(): void {

lib/services/local-build-service.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import { exportedPromise } from "../common/decorators";
1+
// import { exportedPromise } from "../common/decorators";
2+
import { EventEmitter } from "events";
3+
import { BUILD_OUTPUT_EVENT_NAME } from "../constants";
24

3-
export class LocalBuildService {
5+
export class LocalBuildService extends EventEmitter {
46
constructor(private $projectData: IProjectData,
5-
private $platformService: IPlatformService) { }
7+
private $platformService: IPlatformService) {
8+
super();
9+
}
610

7-
@exportedPromise("localBuildService")
811
public async build(platform: string, platformBuildOptions: IPlatformBuildData, platformTemplate?: string): Promise<string> {
912
this.$projectData.initializeProjectData(platformBuildOptions.projectDir);
1013
await this.$platformService.preparePlatform(platform, platformBuildOptions, platformTemplate, this.$projectData, undefined);
14+
this.$platformService.on(BUILD_OUTPUT_EVENT_NAME, (data: any) => {
15+
data.projectDir = platformBuildOptions.projectDir;
16+
this.emit(BUILD_OUTPUT_EVENT_NAME, data);
17+
});
18+
platformBuildOptions.buildOutputStdio = "pipe";
1119
await this.$platformService.buildPlatform(platform, platformBuildOptions, this.$projectData);
1220
return this.$platformService.lastOutputPath(platform, { isForDevice: platformBuildOptions.buildForDevice }, this.$projectData);
1321
}

lib/services/platform-project-service-base.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
export class PlatformProjectServiceBase implements IPlatformProjectServiceBase {
1+
import { EventEmitter } from "events";
2+
3+
export class PlatformProjectServiceBase extends EventEmitter implements IPlatformProjectServiceBase {
24
constructor(protected $fs: IFileSystem,
35
protected $projectDataService: IProjectDataService) {
6+
super();
47
}
58

69
public getPluginPlatformsFolderPath(pluginData: IPluginData, platform: string): string {

lib/services/platform-service.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import * as shell from "shelljs";
33
import * as constants from "../constants";
44
import * as helpers from "../common/helpers";
55
import * as semver from "semver";
6+
import { EventEmitter } from "events";
67
import { AppFilesUpdater } from "./app-files-updater";
78
import * as temp from "temp";
89
temp.track();
910
let clui = require("clui");
1011

1112
const buildInfoFileName = ".nsbuildinfo";
1213

13-
export class PlatformService implements IPlatformService {
14+
export class PlatformService extends EventEmitter implements IPlatformService {
1415
// Type with hooks needs to have either $hooksService or $injector injected.
1516
// In order to stop TypeScript from failing for not used $hooksService, use it here.
1617
private get _hooksService(): IHooksService {
@@ -39,7 +40,9 @@ export class PlatformService implements IPlatformService {
3940
private $deviceAppDataFactory: Mobile.IDeviceAppDataFactory,
4041
private $projectChangesService: IProjectChangesService,
4142
private $emulatorPlatformService: IEmulatorPlatformService,
42-
private $analyticsService: IAnalyticsService) { }
43+
private $analyticsService: IAnalyticsService) {
44+
super();
45+
}
4346

4447
public async addPlatforms(platforms: string[], platformTemplate: string, projectData: IProjectData): Promise<void> {
4548
let platformsDir = projectData.platformsDir;
@@ -362,6 +365,9 @@ export class PlatformService implements IPlatformService {
362365
await this.trackProjectType(projectData);
363366

364367
let platformData = this.$platformsData.getPlatformData(platform, projectData);
368+
platformData.platformProjectService.on(constants.BUILD_OUTPUT_EVENT_NAME, (data: any) => {
369+
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
370+
});
365371
await platformData.platformProjectService.buildProject(platformData.projectRoot, projectData, buildConfig);
366372
let prepareInfo = this.$projectChangesService.getPrepareInfo(platform, projectData);
367373
let buildInfoFilePath = this.getBuildOutputPath(platform, platformData, buildConfig.buildForDevice);

test/stubs.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as util from "util";
44
import * as chai from "chai";
5+
import { EventEmitter } from "events";
56

67
export class LoggerStub implements ILogger {
78
setLevel(level: string): void { }
@@ -237,12 +238,12 @@ export class ProjectDataStub implements IProjectData {
237238
appResourcesDirectoryPath: string;
238239
devDependencies: IStringDictionary;
239240
projectType: string;
240-
initializeProjectData(projectDir? :string): void {
241+
initializeProjectData(projectDir?: string): void {
241242
this.projectDir = this.projectDir || projectDir;
242243
}
243244
}
244245

245-
export class PlatformsDataStub implements IPlatformsData {
246+
export class PlatformsDataStub extends EventEmitter implements IPlatformsData {
246247
public platformsNames: string[];
247248

248249
public getPlatformData(platform: string, projectData: IProjectData): IPlatformData {
@@ -266,7 +267,7 @@ export class PlatformsDataStub implements IPlatformsData {
266267
}
267268
}
268269

269-
export class PlatformProjectServiceStub implements IPlatformProjectService {
270+
export class PlatformProjectServiceStub extends EventEmitter implements IPlatformProjectService {
270271
getPlatformData(projectData: IProjectData): IPlatformData {
271272
return {
272273
frameworkPackageName: "",
@@ -570,7 +571,7 @@ export class CommandsService implements ICommandsService {
570571
}
571572
}
572573

573-
export class PlatformServiceStub implements IPlatformService {
574+
export class PlatformServiceStub extends EventEmitter implements IPlatformService {
574575

575576
public validateOptions(): Promise<boolean> {
576577
return Promise.resolve(true);

0 commit comments

Comments
 (0)