Skip to content

Commit

Permalink
Detach event handlers after action is done (#2703)
Browse files Browse the repository at this point in the history
In order to not emit `buildOutput` event multiple times during build in a long running process (fusion) we have to detach event handlers after build is done.
  • Loading branch information
Mitko-Kerezov authored Apr 11, 2017
1 parent 99c9bf5 commit 1606b2a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/common
Submodule common updated 3 files
+11 −0 helpers.ts
+12 −16 logger.ts
+119 −0 test/unit-tests/logger.ts
15 changes: 10 additions & 5 deletions lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as constants from "../constants";
import * as semver from "semver";
import * as projectServiceBaseLib from "./platform-project-service-base";
import { DeviceAndroidDebugBridge } from "../common/mobile/android/device-android-debug-bridge";
import { attachAwaitDetach } from "../common/helpers";
import { EOL } from "os";
import { Configurations } from "../common/constants";
import { SpawnOptions } from "child_process";
Expand Down Expand Up @@ -257,13 +258,17 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject

buildOptions.unshift("buildapk");

this.$childProcess.on(constants.BUILD_OUTPUT_EVENT_NAME, (data: any) => {
const handler = (data: any) => {
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
});
};

await this.executeGradleCommand(this.getPlatformData(projectData).projectRoot, buildOptions,
{ stdio: buildConfig.buildOutputStdio || "inherit" },
{ emitOptions: { eventName: constants.BUILD_OUTPUT_EVENT_NAME }, throwError: true });
await attachAwaitDetach(constants.BUILD_OUTPUT_EVENT_NAME,
this.$childProcess,
handler,
this.executeGradleCommand(this.getPlatformData(projectData).projectRoot,
buildOptions,
{ stdio: buildConfig.buildOutputStdio || "inherit" },
{ emitOptions: { eventName: constants.BUILD_OUTPUT_EVENT_NAME }, throwError: true }));
} else {
this.$errors.failWithoutHelp("Cannot complete build because this project is ANT-based." + EOL +
"Run `tns platform remove android && tns platform add android` to switch to Gradle and try again.");
Expand Down
19 changes: 13 additions & 6 deletions lib/services/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as semver from "semver";
import * as xcode from "xcode";
import * as constants from "../constants";
import * as helpers from "../common/helpers";
import { attachAwaitDetach } from "../common/helpers";
import * as projectServiceBaseLib from "./platform-project-service-base";
import { PlistSession } from "plist-merge-patch";
import { EOL } from "os";
Expand Down Expand Up @@ -180,7 +181,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
let projectRoot = this.getPlatformData(projectData).projectRoot;
let archivePath = options && options.archivePath ? path.resolve(options.archivePath) : path.join(projectRoot, "/build/archive/", projectData.projectName + ".xcarchive");
let args = ["archive", "-archivePath", archivePath, "-configuration",
(!buildConfig || buildConfig.release) ? "Release" : "Debug" ]
(!buildConfig || buildConfig.release) ? "Release" : "Debug"]
.concat(this.xcbuildProjectArgs(projectRoot, projectData, "scheme"));
await this.$childProcess.spawnFromEvent("xcodebuild", args, "exit", { stdio: 'inherit' });
return archivePath;
Expand Down Expand Up @@ -287,15 +288,21 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
// }
// }

this.$childProcess.on(constants.BUILD_OUTPUT_EVENT_NAME, (data: any) => {
const handler = (data: any) => {
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
});
};

if (buildConfig.buildForDevice) {
await this.buildForDevice(projectRoot, basicArgs, buildConfig, projectData);
await attachAwaitDetach(constants.BUILD_OUTPUT_EVENT_NAME,
this.$childProcess,
handler,
this.buildForDevice(projectRoot, basicArgs, buildConfig, projectData));
} else {
await this.buildForSimulator(projectRoot, basicArgs, projectData, buildConfig.buildOutputStdio);
await attachAwaitDetach(constants.BUILD_OUTPUT_EVENT_NAME,
this.$childProcess,
handler,
this.buildForSimulator(projectRoot, basicArgs, projectData, buildConfig.buildOutputStdio));
}

}

public async validatePlugins(projectData: IProjectData): Promise<void> {
Expand Down
10 changes: 6 additions & 4 deletions lib/services/local-build-service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { EventEmitter } from "events";
import { BUILD_OUTPUT_EVENT_NAME } from "../constants";
import { attachAwaitDetach } from "../common/helpers";

export class LocalBuildService extends EventEmitter {
constructor(private $projectData: IProjectData,
private $platformService: IPlatformService) {
private $platformService: IPlatformService) {
super();
}

public async build(platform: string, platformBuildOptions: IPlatformBuildData, platformTemplate?: string): Promise<string> {
this.$projectData.initializeProjectData(platformBuildOptions.projectDir);
await this.$platformService.preparePlatform(platform, platformBuildOptions, platformTemplate, this.$projectData, { provision: platformBuildOptions.provision, sdk: null });
this.$platformService.on(BUILD_OUTPUT_EVENT_NAME, (data: any) => {
const handler = (data: any) => {
data.projectDir = platformBuildOptions.projectDir;
this.emit(BUILD_OUTPUT_EVENT_NAME, data);
});
};
platformBuildOptions.buildOutputStdio = "pipe";
await this.$platformService.buildPlatform(platform, platformBuildOptions, this.$projectData);

await attachAwaitDetach(BUILD_OUTPUT_EVENT_NAME, this.$platformService, handler, this.$platformService.buildPlatform(platform, platformBuildOptions, this.$projectData));
return this.$platformService.lastOutputPath(platform, platformBuildOptions, this.$projectData);
}
}
Expand Down
10 changes: 7 additions & 3 deletions lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as helpers from "../common/helpers";
import * as semver from "semver";
import { EventEmitter } from "events";
import { AppFilesUpdater } from "./app-files-updater";
import { attachAwaitDetach } from "../common/helpers";
import * as temp from "temp";
temp.track();
let clui = require("clui");
Expand Down Expand Up @@ -425,10 +426,13 @@ export class PlatformService extends EventEmitter implements IPlatformService {
await this.trackActionForPlatform({ action: "Build", platform, isForDevice });

let platformData = this.$platformsData.getPlatformData(platform, projectData);
platformData.platformProjectService.on(constants.BUILD_OUTPUT_EVENT_NAME, (data: any) => {
const handler = (data: any) => {
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
});
await platformData.platformProjectService.buildProject(platformData.projectRoot, projectData, buildConfig);
this.$logger.printInfoMessageOnSameLine(data.data.toString());
};

await attachAwaitDetach(constants.BUILD_OUTPUT_EVENT_NAME, platformData.platformProjectService, handler, platformData.platformProjectService.buildProject(platformData.projectRoot, projectData, buildConfig));

let prepareInfo = this.$projectChangesService.getPrepareInfo(platform, projectData);
let buildInfoFilePath = this.getBuildOutputPath(platform, platformData, buildConfig);
let buildInfoFile = path.join(buildInfoFilePath, buildInfoFileName);
Expand Down

0 comments on commit 1606b2a

Please sign in to comment.