From 938af819e6b136c8742f8982c23af6dec5b18a7c Mon Sep 17 00:00:00 2001 From: DimitarTachev Date: Fri, 14 Feb 2020 19:13:51 +0200 Subject: [PATCH] fix: support request cleanup + options for the commands --- .../cleanup-process-definitions.d.ts | 21 +++++++- lib/detached-processes/cleanup-process.ts | 50 ++++++++++++++++++- .../detached-process-enums.d.ts | 9 ++++ lib/services/cleanup-service.ts | 12 ++++- lib/services/ios-project-service.ts | 14 +++--- 5 files changed, 95 insertions(+), 11 deletions(-) diff --git a/lib/detached-processes/cleanup-process-definitions.d.ts b/lib/detached-processes/cleanup-process-definitions.d.ts index 24eab593a4..e4927f7c58 100644 --- a/lib/detached-processes/cleanup-process-definitions.d.ts +++ b/lib/detached-processes/cleanup-process-definitions.d.ts @@ -22,6 +22,18 @@ interface ISpawnCommandInfo extends ITimeout { * Arguments that will be passed to the child process */ args: string[]; + + /** + * Options to be passed to the child process + */ + options?: any; +} + +interface IRequestInfo extends ITimeout { + url: string, + method: string, + body: any, + headers: any } interface ICleanupMessageBase { @@ -38,6 +50,13 @@ interface ISpawnCommandCleanupMessage extends ICleanupMessageBase { commandInfo: ISpawnCommandInfo; } +interface IRequestCleanupMessage extends ICleanupMessageBase { + /** + * Describes the request that must be executed + */ + requestInfo: IRequestInfo; +} + interface IFileCleanupMessage extends ICleanupMessageBase, IFilePath { } interface IJSCommand extends ITimeout, IFilePath { @@ -46,4 +65,4 @@ interface IJSCommand extends ITimeout, IFilePath { interface IJSCleanupMessage extends ICleanupMessageBase { jsCommand: IJSCommand; - } +} diff --git a/lib/detached-processes/cleanup-process.ts b/lib/detached-processes/cleanup-process.ts index a6bd0c7975..1ddd41968b 100644 --- a/lib/detached-processes/cleanup-process.ts +++ b/lib/detached-processes/cleanup-process.ts @@ -20,6 +20,24 @@ fileLogService.logData({ message: "Initializing Cleanup process." }); const commandsInfos: ISpawnCommandInfo[] = []; const filesToDelete: string[] = []; const jsCommands: IJSCommand[] = []; +const requests: IRequestInfo[] = []; + +const executeRequest = async (request: IRequestInfo) => { + const $httpClient = $injector.resolve("httpClient"); + try { + fileLogService.logData({ message: `Start executing request: ${request.method} ${request.url}` }); + const response = await $httpClient.httpRequest({ + url: request.url, + method: request.method, + headers: request.headers, + body: request.body + }); + const responseStatus = response && response.response && response.response.statusCode; + fileLogService.logData({ message: `Finished executing request: ${request.method} ${request.url} and got status ${responseStatus}` }); + } catch (e) { + fileLogService.logData({ message: `Unable to execute request: ${request.method} ${request.url}` }); + } +}; const executeJSCleanup = async (jsCommand: IJSCommand) => { const $childProcess = $injector.resolve("childProcess"); @@ -28,7 +46,7 @@ const executeJSCleanup = async (jsCommand: IJSCommand) => { fileLogService.logData({ message: `Start executing action for file: ${jsCommand.filePath} and data ${JSON.stringify(jsCommand.data)}` }); await $childProcess.trySpawnFromCloseEvent(process.execPath, [path.join(__dirname, "cleanup-js-subprocess.js"), pathToBootstrap, logFile, jsCommand.filePath, JSON.stringify(jsCommand.data)], {}, { throwError: true, timeout: jsCommand.timeout || 3000 }); - fileLogService.logData({ message: `Finished xecuting action for file: ${jsCommand.filePath} and data ${JSON.stringify(jsCommand.data)}` }); + fileLogService.logData({ message: `Finished executing action for file: ${jsCommand.filePath} and data ${JSON.stringify(jsCommand.data)}` }); } catch (err) { fileLogService.logData({ message: `Unable to execute action for file ${jsCommand.filePath} with data ${JSON.stringify(jsCommand.data)}. Error is: ${err}.`, type: FileLogMessageType.Error }); @@ -38,6 +56,10 @@ const executeJSCleanup = async (jsCommand: IJSCommand) => { const executeCleanup = async () => { const $childProcess = $injector.resolve("childProcess"); + for (const request of requests) { + await executeRequest(request); + } + for (const jsCommand of jsCommands) { await executeJSCleanup(jsCommand); } @@ -46,7 +68,7 @@ const executeCleanup = async () => { try { fileLogService.logData({ message: `Start executing command: ${JSON.stringify(commandInfo)}` }); - await $childProcess.trySpawnFromCloseEvent(commandInfo.command, commandInfo.args, {}, { throwError: true, timeout: commandInfo.timeout || 3000 }); + await $childProcess.trySpawnFromCloseEvent(commandInfo.command, commandInfo.args, commandInfo.options || {}, { throwError: true, timeout: commandInfo.timeout || 3000 }); fileLogService.logData({ message: `Successfully executed command: ${JSON.stringify(commandInfo)}` }); } catch (err) { fileLogService.logData({ message: `Unable to execute command: ${JSON.stringify(commandInfo)}. Error is: ${err}.`, type: FileLogMessageType.Error }); @@ -84,6 +106,24 @@ const removeCleanupAction = (commandInfo: ISpawnCommandInfo): void => { } }; +const addRequest = (requestInfo: IRequestInfo): void => { + if (_.some(requests, currentRequestInfo => _.isEqual(currentRequestInfo, requestInfo))) { + fileLogService.logData({ message: `cleanup-process will not add request for execution as it has been added already: ${JSON.stringify(requestInfo)}` }); + } else { + fileLogService.logData({ message: `cleanup-process added request for execution: ${JSON.stringify(requestInfo)}` }); + requests.push(requestInfo); + } +}; + +const removeRequest = (requestInfo: IRequestInfo): void => { + if (_.some(requests, currentRequestInfo => _.isEqual(currentRequestInfo, currentRequestInfo))) { + _.remove(requests, currentRequestInfo => _.isEqual(currentRequestInfo, requestInfo)); + fileLogService.logData({ message: `cleanup-process removed request for execution: ${JSON.stringify(requestInfo)}` }); + } else { + fileLogService.logData({ message: `cleanup-process cannot remove request for execution as it has not been added before: ${JSON.stringify(requestInfo)}` }); + } +}; + const addDeleteAction = (filePath: string): void => { const fullPath = path.resolve(filePath); @@ -142,6 +182,12 @@ process.on("message", async (cleanupProcessMessage: ICleanupMessageBase) => { case CleanupProcessMessage.RemoveCleanCommand: removeCleanupAction((cleanupProcessMessage).commandInfo); break; + case CleanupProcessMessage.AddRequest: + addRequest((cleanupProcessMessage).requestInfo); + break; + case CleanupProcessMessage.RemoveRequest: + removeRequest((cleanupProcessMessage).requestInfo); + break; case CleanupProcessMessage.AddDeleteFileAction: addDeleteAction((cleanupProcessMessage).filePath); break; diff --git a/lib/detached-processes/detached-process-enums.d.ts b/lib/detached-processes/detached-process-enums.d.ts index 1c250b7cb9..98692f1568 100644 --- a/lib/detached-processes/detached-process-enums.d.ts +++ b/lib/detached-processes/detached-process-enums.d.ts @@ -38,6 +38,15 @@ declare const enum CleanupProcessMessage { * This type of message defines that cleanup procedure should not execute previously defined cleanup command. */ RemoveCleanCommand = "RemoveCleanCommand", + /** + * This type of message defines that cleanup procedure should execute specific request. + */ + AddRequest = "AddRequest", + + /** + * This type of message defines that cleanup procedure should not execute previously defined request. + */ + RemoveRequest = "RemoveRequest", /** * This type of message defines that cleanup procedure should delete specified files. diff --git a/lib/services/cleanup-service.ts b/lib/services/cleanup-service.ts index 723810bd0b..50351e21c5 100644 --- a/lib/services/cleanup-service.ts +++ b/lib/services/cleanup-service.ts @@ -25,6 +25,16 @@ export class CleanupService implements ICleanupService { cleanupProcess.send({ messageType: CleanupProcessMessage.RemoveCleanCommand, commandInfo }); } + public async addRequest(requestInfo: IRequestInfo): Promise { + const cleanupProcess = await this.getCleanupProcess(); + cleanupProcess.send({ messageType: CleanupProcessMessage.AddRequest, requestInfo }); + } + + public async removeRequest(requestInfo: IRequestInfo): Promise { + const cleanupProcess = await this.getCleanupProcess(); + cleanupProcess.send({ messageType: CleanupProcessMessage.RemoveRequest, requestInfo }); + } + public async addCleanupDeleteAction(filePath: string): Promise { const cleanupProcess = await this.getCleanupProcess(); cleanupProcess.send({ messageType: CleanupProcessMessage.AddDeleteFileAction, filePath }); @@ -42,7 +52,7 @@ export class CleanupService implements ICleanupService { public async removeCleanupJS(jsCommand: IJSCommand): Promise { const cleanupProcess = await this.getCleanupProcess(); - cleanupProcess.send({ messageType: CleanupProcessMessage.RemoveJSFileToRequire, jsCommand}); + cleanupProcess.send({ messageType: CleanupProcessMessage.RemoveJSFileToRequire, jsCommand }); } public async addKillProcess(pid: string): Promise { diff --git a/lib/services/ios-project-service.ts b/lib/services/ios-project-service.ts index ec146e5d4c..be80de7751 100644 --- a/lib/services/ios-project-service.ts +++ b/lib/services/ios-project-service.ts @@ -209,29 +209,29 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ } @hook('buildIOS') - public async buildProject(projectRoot: string, projectData: IProjectData, iOSBuildData: IOSBuildData): Promise { + public async buildProject(projectRoot: string, projectData: IProjectData, buildData: IOSBuildData): Promise { const platformData = this.getPlatformData(projectData); const handler = (data: any) => { this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data); }; - if (iOSBuildData.buildForDevice) { - await this.$iOSSigningService.setupSigningForDevice(projectRoot, projectData, iOSBuildData); + if (buildData.buildForDevice) { + await this.$iOSSigningService.setupSigningForDevice(projectRoot, projectData, buildData); await attachAwaitDetach(constants.BUILD_OUTPUT_EVENT_NAME, this.$childProcess, handler, - this.$xcodebuildService.buildForDevice(platformData, projectData, iOSBuildData)); - } else if (iOSBuildData.buildForAppStore) { + this.$xcodebuildService.buildForDevice(platformData, projectData, buildData)); + } else if (buildData.buildForAppStore) { await attachAwaitDetach(constants.BUILD_OUTPUT_EVENT_NAME, this.$childProcess, handler, - this.$xcodebuildService.buildForAppStore(platformData, projectData, iOSBuildData)); + this.$xcodebuildService.buildForAppStore(platformData, projectData, buildData)); } else { await attachAwaitDetach(constants.BUILD_OUTPUT_EVENT_NAME, this.$childProcess, handler, - this.$xcodebuildService.buildForSimulator(platformData, projectData, iOSBuildData)); + this.$xcodebuildService.buildForSimulator(platformData, projectData, buildData)); } this.validateApplicationIdentifier(projectData);