From f800172ea024d289af794bee3653110912c83d7b Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Thu, 11 Feb 2016 01:22:54 +0200 Subject: [PATCH] Some improvements for AndroidManifest in App_Resources * Correct the code, so AndroidManifest will not be backed up in case we are unable to find the manifest in the template (current code was always creating the backup and reverting it in case it fails on the extraction from template). * Fix backup name - use guid in case AndroidManifest.backup exists. --- lib/services/android-project-service.ts | 50 ++++++++++++++----------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/services/android-project-service.ts b/lib/services/android-project-service.ts index b96f6da9c8..777db27d25 100644 --- a/lib/services/android-project-service.ts +++ b/lib/services/android-project-service.ts @@ -9,6 +9,7 @@ import * as projectServiceBaseLib from "./platform-project-service-base"; import * as androidDebugBridgePath from "../common/mobile/android/android-debug-bridge"; import {AndroidDeviceHashService} from "../common/mobile/android/android-device-hash-service"; import {EOL} from "os"; +import { createGUID } from "../common/helpers"; export class AndroidProjectService extends projectServiceBaseLib.PlatformProjectServiceBase implements IPlatformProjectService { private static VALUES_DIRNAME = "values"; @@ -271,31 +272,14 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject return (() => { let originalAndroidManifestFilePath = path.join(this.$projectData.appResourcesDirectoryPath, this.$devicePlatformsConstants.Android, this.platformData.configurationFileName), hasAndroidManifestInAppResources = this.$fs.exists(originalAndroidManifestFilePath).wait(), - shouldExtractDefaultManifest = !hasAndroidManifestInAppResources, - isAndroidManifestBackedUp = false; - - if(hasAndroidManifestInAppResources) { - let isFileCorrect = this.isAndroidManifestFileCorrect(originalAndroidManifestFilePath).wait(); - if(!isFileCorrect) { - shouldExtractDefaultManifest = true; - isAndroidManifestBackedUp = true; - this.backupOriginalAndroidManifest(originalAndroidManifestFilePath).wait(); - } - } + shouldExtractDefaultManifest = !hasAndroidManifestInAppResources || !this.isAndroidManifestFileCorrect(originalAndroidManifestFilePath).wait(); // In case we should extract the manifest from default template, but for some reason we cannot, break the execution, // so the original file from Android runtime will be used. if(shouldExtractDefaultManifest && !this.extractAndroidManifestFromDefaultTemplate(originalAndroidManifestFilePath).wait()) { - // now revert back - this.revertBackupOfOriginalAndroidManifest(originalAndroidManifestFilePath).wait(); return; } - if(isAndroidManifestBackedUp) { - this.$logger.warn(`Your ${this.platformData.configurationFileName} in app/App_Resources/Android will be replaced by the default one from hello-world template.`); - this.$logger.printMarkdown(`The original file will be moved to \`${this.configurationFileBackupName}\`. Merge it **manually** with the new \`${this.platformData.configurationFileName}\` in your app/App_Resources/Android.`); - } - // Overwrite the AndroidManifest from runtime. this.$fs.copyFile(originalAndroidManifestFilePath, this.platformData.configurationFilePath).wait(); }).future()(); @@ -494,20 +478,32 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject }).future()(); } - private get configurationFileBackupName(): string { - return this.platformData.configurationFileName + ".backup"; + private _configurationFileBackupName: string; + + private getConfigurationFileBackupName(originalAndroidManifestFilePath: string): IFuture { + return (() => { + if(!this._configurationFileBackupName) { + let defaultBackupName = this.platformData.configurationFileName + ".backup"; + if(this.$fs.exists(path.join(path.dirname(originalAndroidManifestFilePath), defaultBackupName)).wait()) { + defaultBackupName += `_${createGUID(false)}`; + } + this._configurationFileBackupName = defaultBackupName; + } + + return this._configurationFileBackupName; + }).future()(); } private backupOriginalAndroidManifest(originalAndroidManifestFilePath: string): IFuture { return (() => { - let newPathForOriginalManifest = path.join(path.dirname(originalAndroidManifestFilePath), this.configurationFileBackupName); + let newPathForOriginalManifest = path.join(path.dirname(originalAndroidManifestFilePath), this.getConfigurationFileBackupName(originalAndroidManifestFilePath).wait()); shell.mv(originalAndroidManifestFilePath, newPathForOriginalManifest); }).future()(); } private revertBackupOfOriginalAndroidManifest(originalAndroidManifestFilePath: string): IFuture { return (() => { - let pathToBackupFile = path.join(path.dirname(originalAndroidManifestFilePath), this.configurationFileBackupName); + let pathToBackupFile = path.join(path.dirname(originalAndroidManifestFilePath), this.getConfigurationFileBackupName(originalAndroidManifestFilePath).wait()); if(this.$fs.exists(pathToBackupFile).wait()) { this.$logger.trace(`Could not extract ${this.platformData.configurationFileName} from default template. Reverting the change of your app/App_Resources/${this.platformData.configurationFileName}.`); shell.mv(pathToBackupFile, originalAndroidManifestFilePath); @@ -519,12 +515,17 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject return ((): boolean => { let defaultTemplatePath = this.$projectTemplatesService.defaultTemplatePath.wait(); let templateAndroidManifest = path.join(defaultTemplatePath, constants.APP_RESOURCES_FOLDER_NAME, this.$devicePlatformsConstants.Android, this.platformData.configurationFileName); + let alreadyHasAndroidManifest = this.$fs.exists(originalAndroidManifestFilePath).wait(); if (this.$fs.exists(templateAndroidManifest).wait()) { this.$logger.trace(`${originalAndroidManifestFilePath} is missing. Upgrading the source of the project with one from the new project template. Copy ${templateAndroidManifest} to ${originalAndroidManifestFilePath}`); try { + if(alreadyHasAndroidManifest) { + this.backupOriginalAndroidManifest(originalAndroidManifestFilePath).wait(); + } this.$fs.copyFile(templateAndroidManifest, originalAndroidManifestFilePath).wait(); } catch(e) { this.$logger.trace(`Copying template's ${this.platformData.configurationFileName} failed. `, e); + this.revertBackupOfOriginalAndroidManifest(originalAndroidManifestFilePath).wait(); return false; } } else { @@ -532,6 +533,11 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject return false; } + if(alreadyHasAndroidManifest) { + this.$logger.warn(`Your ${this.platformData.configurationFileName} in app/App_Resources/Android will be replaced by the default one from hello-world template.`); + this.$logger.printMarkdown(`The original file will be moved to \`${this.getConfigurationFileBackupName(originalAndroidManifestFilePath).wait()}\`. Merge it **manually** with the new \`${this.platformData.configurationFileName}\` in your app/App_Resources/Android.`); + } + this.interpolateConfigurationFile().wait(); return true; }).future()();