Skip to content

Commit f46073f

Browse files
Fatme HavaluovaFatme Havaluova
authored andcommitted
Show confirmation dialog when xcodeproject files are different, backup the old one and override it with the new.
1 parent df49daa commit f46073f

File tree

6 files changed

+87
-30
lines changed

6 files changed

+87
-30
lines changed

lib/declarations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ interface INodePackageManager {
55
load(config?: any): IFuture<void>;
66
install(packageName: string, options?: INpmInstallOptions): IFuture<string>;
77
getLatestVersion(packageName: string): IFuture<string>;
8+
getCachedPackagePath(packageName: string, version: string): string;
89
}
910

1011
interface INpmInstallOptions {

lib/definitions/project.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ interface IPlatformProjectService {
3030
buildProject(projectRoot: string): IFuture<void>;
3131
isPlatformPrepared(projectRoot: string): IFuture<boolean>;
3232
addLibrary(platformData: IPlatformData, libraryPath: string): IFuture<void>;
33+
canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean>;
34+
updatePlatform(currentVersion: string, newVersion: string): IFuture<void>;
3335
}

lib/node-package-manager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export class NodePackageManager implements INodePackageManager {
8080
}).future<string>()();
8181
}
8282

83+
public getCachedPackagePath(packageName: string, version: string): string {
84+
return path.join(npm.cache, packageName, version, "package");
85+
}
86+
8387
private installCore(packageName: string, pathToSave: string, version: string): IFuture<string> {
8488
return (() => {
8589
if (options.frameworkPath) {
@@ -92,7 +96,7 @@ export class NodePackageManager implements INodePackageManager {
9296
return options.frameworkPath;
9397
} else {
9498
version = version || this.getLatestVersion(packageName).wait();
95-
var packagePath = path.join(npm.cache, packageName, version, "package");
99+
var packagePath = this.getCachedPackagePath(packageName, version);
96100
if (!this.isPackageCached(packagePath).wait()) {
97101
this.addToCacheCore(packageName, version).wait();
98102
}

lib/services/android-project-service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ class AndroidProjectService implements IPlatformProjectService {
122122
}).future<string>()();
123123
}
124124

125+
public canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
126+
return (() => {
127+
return true;
128+
}).future<boolean>()();
129+
}
130+
131+
updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
132+
return (() => { }).future<void>()();
133+
}
134+
125135
private updateMetadata(projectRoot: string): void {
126136
var projMetadataDir = path.join(projectRoot, "assets", "metadata");
127137
var libsmetadataDir = path.join(projectRoot, "../../lib", this.platformData.normalizedPlatformName, AndroidProjectService.METADATA_DIRNAME);

lib/services/ios-project-service.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class IOSProjectService implements IPlatformProjectService {
2020
private $childProcess: IChildProcess,
2121
private $errors: IErrors,
2222
private $logger: ILogger,
23-
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices) { }
23+
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices,
24+
private $npm: INodePackageManager) { }
2425

2526
public get platformData(): IPlatformData {
2627
return {
@@ -72,7 +73,7 @@ class IOSProjectService implements IPlatformProjectService {
7273
var xcodeProjectName = util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER);
7374

7475
shell.cp("-R", path.join(frameworkDir, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, "*"), path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
75-
shell.cp("-R", path.join(frameworkDir, xcodeProjectName), path.join(projectRoot));
76+
shell.cp("-R", path.join(frameworkDir, xcodeProjectName), projectRoot);
7677

7778
var directoryContent = this.$fs.readDirectory(frameworkDir).wait();
7879
var frameworkFiles = _.difference(directoryContent, [IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, xcodeProjectName]);
@@ -195,7 +196,44 @@ class IOSProjectService implements IPlatformProjectService {
195196
this.$fs.writeFile(pbxProjPath, project.writeSync()).wait();
196197
this.$logger.info("The iOS Deployment Target is now 8.0 in order to support Cocoa Touch Frameworks.");
197198
}).future<void>()();
198-
}
199+
}
200+
201+
public canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
202+
return (() => {
203+
var currentXcodeProjectFile = this.buildPathToXcodeProjectFile(currentVersion);
204+
var currentXcodeProjectFileContent = this.$fs.readFile(currentXcodeProjectFile).wait();
205+
206+
var newXcodeProjectFile = this.buildPathToXcodeProjectFile(newVersion);
207+
var newXcodeProjectFileContent = this.$fs.readFile(newXcodeProjectFile).wait();
208+
209+
return currentXcodeProjectFileContent === newXcodeProjectFileContent;
210+
211+
}).future<boolean>()();
212+
}
213+
214+
public updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
215+
return (() => {
216+
// Copy old file to options["profile-dir"]
217+
var sourceFile = path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName));
218+
var destinationFile = path.join(options.profileDir, "xcodeproj");
219+
//this.$fs.copyFile(sourceFile, destinationFile).wait();
220+
this.$logger.info("Backup file %s at location %s", sourceFile, destinationFile);
221+
this.$fs.deleteDirectory(path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName))).wait();
222+
223+
// Copy xcodeProject file
224+
var cachedPackagePath = path.join(this.$npm.getCachedPackagePath(this.platformData.frameworkPackageName, newVersion), constants.PROJECT_FRAMEWORK_FOLDER_NAME, util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
225+
shell.cp("-R", path.join(cachedPackagePath, "*"), path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName)));
226+
this.$logger.info("Copied from %s at %s.", cachedPackagePath, this.platformData.projectRoot);
227+
228+
229+
var pbxprojFilePath = path.join(this.platformData.projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
230+
this.replaceFileContent(pbxprojFilePath).wait();
231+
}).future<void>()();
232+
}
233+
234+
private buildPathToXcodeProjectFile(version: string): string {
235+
return path.join(this.$npm.getCachedPackagePath(this.platformData.frameworkPackageName, version), constants.PROJECT_FRAMEWORK_FOLDER_NAME, util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER), "project.pbxproj");
236+
}
199237

200238
private validateDynamicFramework(libraryPath: string): IFuture<void> {
201239
return (() => {

lib/services/platform-service.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,28 @@ export class PlatformService implements IPlatformService {
443443
var currentVersion = data && data.version ? data.version : "0.2.0";
444444
var newVersion = version || this.$npm.getLatestVersion(platformData.frameworkPackageName).wait();
445445

446-
if(!semver.valid(newVersion)) {
447-
this.$errors.fail("The version %s is not valid. The version should consists from 3 parts seperated by dot.", newVersion);
448-
}
446+
if(platformData.platformProjectService.canUpdatePlatform(currentVersion, newVersion).wait()) {
449447

450-
if(semver.gt(currentVersion, newVersion)) { // Downgrade
451-
var isUpdateConfirmed = this.$prompter.confirm("You are going to update to lower version. Are you sure?", () => "n").wait();
452-
if(isUpdateConfirmed) {
448+
if(!semver.valid(newVersion)) {
449+
this.$errors.fail("The version %s is not valid. The version should consists from 3 parts separated by dot.", newVersion);
450+
}
451+
452+
if(semver.gt(currentVersion, newVersion)) { // Downgrade
453+
var isUpdateConfirmed = this.$prompter.confirm("You are going to update to lower version. Are you sure?", () => "n").wait();
454+
if(isUpdateConfirmed) {
455+
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
456+
}
457+
} else if(semver.eq(currentVersion, newVersion)) {
458+
this.$errors.fail("Current and new version are the same.");
459+
} else {
453460
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
454461
}
455-
} else if(semver.eq(currentVersion, newVersion)) {
456-
this.$errors.fail("Current and new version are the same.");
457462
} else {
458-
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
463+
var isUpdateConfirmed = this.$prompter.confirm(util.format("We need to override xcodeproj file. The old one will be saved at %s Are you sure?", options.profileDir), () => "y").wait();
464+
if(isUpdateConfirmed) {
465+
platformData.platformProjectService.updatePlatform(currentVersion, newVersion).wait();
466+
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
467+
}
459468
}
460469

461470
}).future<void>()();
@@ -480,7 +489,7 @@ export class PlatformService implements IPlatformService {
480489

481490
// Add new framework files
482491
var newFrameworkData = this.getFrameworkFiles(platformData, newVersion).wait();
483-
var cacheDirectoryPath = this.getNpmCacheDirectoryCore(platformData.frameworkPackageName, newVersion);
492+
var cacheDirectoryPath = this.$npm.getCachedPackagePath(platformData.frameworkPackageName, newVersion);
484493

485494
_.each(newFrameworkData.frameworkFiles, file => {
486495
var sourceFile = path.join(cacheDirectoryPath, constants.PROJECT_FRAMEWORK_FOLDER_NAME, file);
@@ -507,36 +516,29 @@ export class PlatformService implements IPlatformService {
507516

508517
private getFrameworkFiles(platformData: IPlatformData, version: string): IFuture<any> {
509518
return (() => {
510-
var npmCacheDirectoryPath = this.getNpmCacheDirectory(platformData.frameworkPackageName, version).wait();
519+
var cachedPackagePath = this.$npm.getCachedPackagePath(platformData.frameworkPackageName, version);
520+
this.ensurePackageIsCached(cachedPackagePath, platformData.frameworkPackageName, version).wait();
511521

512-
var allFiles = this.$fs.enumerateFilesInDirectorySync(npmCacheDirectoryPath);
522+
var allFiles = this.$fs.enumerateFilesInDirectorySync(cachedPackagePath);
513523
var filteredFiles = _.filter(allFiles, file => _.contains(platformData.frameworkFilesExtensions, path.extname(file)));
514524

515-
var allFrameworkDirectories = _.map(this.$fs.readDirectory(path.join(npmCacheDirectoryPath, constants.PROJECT_FRAMEWORK_FOLDER_NAME)).wait(), dir => path.join(npmCacheDirectoryPath, constants.PROJECT_FRAMEWORK_FOLDER_NAME, dir));
525+
var allFrameworkDirectories = _.map(this.$fs.readDirectory(path.join(cachedPackagePath, constants.PROJECT_FRAMEWORK_FOLDER_NAME)).wait(), dir => path.join(cachedPackagePath, constants.PROJECT_FRAMEWORK_FOLDER_NAME, dir));
516526
var filteredFrameworkDirectories = _.filter(allFrameworkDirectories, dir => this.$fs.getFsStats(dir).wait().isDirectory() && (_.contains(platformData.frameworkFilesExtensions, path.extname(dir)) || _.contains(platformData.frameworkDirectoriesNames, path.basename(dir))));
517527

518528
return {
519-
frameworkFiles: this.mapFrameworkFiles(npmCacheDirectoryPath, filteredFiles),
520-
frameworkDirectories: this.mapFrameworkFiles(npmCacheDirectoryPath, filteredFrameworkDirectories)
529+
frameworkFiles: this.mapFrameworkFiles(cachedPackagePath, filteredFiles),
530+
frameworkDirectories: this.mapFrameworkFiles(cachedPackagePath, filteredFrameworkDirectories)
521531
}
522532

523533
}).future<any>()();
524534
}
525535

526-
private getNpmCacheDirectory(packageName: string, version: string): IFuture<string> {
536+
private ensurePackageIsCached(cachedPackagePath: string, packageName: string, version: string): IFuture<void> {
527537
return (() => {
528-
var npmCacheDirectoryPath = this.getNpmCacheDirectoryCore(packageName, version);
529-
530-
if(!this.$fs.exists(npmCacheDirectoryPath).wait()) {
538+
if(!this.$fs.exists(cachedPackagePath).wait()) {
531539
this.$npm.addToCache(packageName, version).wait();
532540
}
533-
534-
return npmCacheDirectoryPath;
535-
}).future<string>()();
536-
}
537-
538-
private getNpmCacheDirectoryCore(packageName: string, version: string): string {
539-
return path.join(this.$npm.getCacheRootPath(), packageName, version, "package");
541+
}).future<void>()();
540542
}
541543

542544
private mapFrameworkFiles(npmCacheDirectoryPath: string, files: string[]): string[] {

0 commit comments

Comments
 (0)