Skip to content

Commit 3182761

Browse files
Fatme HavaluovaFatme Havaluova
Fatme Havaluova
authored and
Fatme Havaluova
committed
Merge branch 'release' into fatme/merge-release
Conflicts: lib/definitions/project.d.ts lib/services/android-project-service.ts test/stubs.ts
1 parent d058742 commit 3182761

File tree

9 files changed

+149
-41
lines changed

9 files changed

+149
-41
lines changed

lib/commands/update-platform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class UpdatePlatformCommand implements ICommand {
1717
this.$errors.fail("No platform specified. Please specify platforms to update.");
1818
}
1919

20-
_.each(args, arg => this.$platformService.validatePlatformInstalled(arg));
20+
_.each(args, arg => this.$platformService.validatePlatformInstalled(arg.split("@")[0]));
2121

2222
return true;
2323
}).future<boolean>()();

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/platform.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ interface IPlatformData {
2727
validPackageNamesForDevice: string[];
2828
validPackageNamesForEmulator?: string[];
2929
frameworkFilesExtensions: string[];
30+
frameworkDirectoriesExtensions?: string[];
31+
frameworkDirectoriesNames?: string[];
3032
targetedOS?: string[];
3133
}
3234

lib/definitions/project.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ interface IPlatformProjectService {
3131
isPlatformPrepared(projectRoot: string): IFuture<boolean>;
3232
addLibrary(platformData: IPlatformData, libraryPath: string): IFuture<void>;
3333
getDebugOnDeviceSetup(): Mobile.IDebugOnDeviceSetup;
34+
canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean>;
35+
updatePlatform(currentVersion: string, newVersion: string): IFuture<void>;
3436
}

lib/node-package-manager.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ export class NodePackageManager implements INodePackageManager {
3030
}
3131

3232
public addToCache(packageName: string, version: string): IFuture<void> {
33-
return this.addToCacheCore(packageName, version);
33+
return (() => {
34+
this.addToCacheCore(packageName, version).wait();
35+
36+
var packagePath = path.join(npm.cache, packageName, version, "package");
37+
if(!this.isPackageUnpacked(packagePath).wait()) {
38+
this.cacheUnpack(packageName, version).wait();
39+
}
40+
}).future<void>()();
3441
}
3542

3643
public load(config?: any): IFuture<void> {
@@ -73,6 +80,10 @@ export class NodePackageManager implements INodePackageManager {
7380
}).future<string>()();
7481
}
7582

83+
public getCachedPackagePath(packageName: string, version: string): string {
84+
return path.join(npm.cache, packageName, version, "package");
85+
}
86+
7687
private installCore(packageName: string, pathToSave: string, version: string): IFuture<string> {
7788
return (() => {
7889
if (options.frameworkPath) {
@@ -84,8 +95,8 @@ export class NodePackageManager implements INodePackageManager {
8495
}
8596
return options.frameworkPath;
8697
} else {
87-
var version: string = version || this.getLatestVersion(packageName).wait();
88-
var packagePath = path.join(npm.cache, packageName, version, "package");
98+
version = version || this.getLatestVersion(packageName).wait();
99+
var packagePath = this.getCachedPackagePath(packageName, version);
89100
if (!this.isPackageCached(packagePath).wait()) {
90101
this.addToCacheCore(packageName, version).wait();
91102
}

lib/services/android-project-service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class AndroidProjectService implements IPlatformProjectService {
2323
private $logger: ILogger,
2424
private $projectData: IProjectData,
2525
private $propertiesParser: IPropertiesParser) {
26-
2726
}
2827

2928
public get platformData(): IPlatformData {
@@ -38,7 +37,7 @@ class AndroidProjectService implements IPlatformProjectService {
3837
util.format("%s-%s.%s", this.$projectData.projectName, "debug", "apk"),
3938
util.format("%s-%s.%s", this.$projectData.projectName, "release", "apk")
4039
],
41-
frameworkFilesExtensions: [".jar", ".dat"]
40+
frameworkFilesExtensions: [".jar", ".dat", ".so"]
4241
};
4342
}
4443

@@ -127,6 +126,16 @@ class AndroidProjectService implements IPlatformProjectService {
127126
return { };
128127
}
129128

129+
public canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
130+
return (() => {
131+
return true;
132+
}).future<boolean>()();
133+
}
134+
135+
public updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
136+
return (() => { }).future<void>()();
137+
}
138+
130139
private updateMetadata(projectRoot: string): void {
131140
var projMetadataDir = path.join(projectRoot, "assets", "metadata");
132141
var libsmetadataDir = path.join(projectRoot, "../../lib", this.platformData.normalizedPlatformName, AndroidProjectService.METADATA_DIRNAME);

lib/services/ios-project-service.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class IOSProjectService implements IPlatformProjectService {
3838
validPackageNamesForEmulator: [
3939
this.$projectData.projectName + ".app"
4040
],
41-
frameworkFilesExtensions: [".a", ".h", ".bin"],
41+
frameworkFilesExtensions: [".a", ".framework", ".bin"],
42+
frameworkDirectoriesExtensions: [".framework"],
43+
frameworkDirectoriesNames: ["Metadata"],
4244
targetedOS: ['darwin']
4345
};
4446
}
@@ -71,7 +73,7 @@ class IOSProjectService implements IPlatformProjectService {
7173
var xcodeProjectName = util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER);
7274

7375
shell.cp("-R", path.join(frameworkDir, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, "*"), path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
74-
shell.cp("-R", path.join(frameworkDir, xcodeProjectName), path.join(projectRoot));
76+
shell.cp("-R", path.join(frameworkDir, xcodeProjectName), projectRoot);
7577

7678
var directoryContent = this.$fs.readDirectory(frameworkDir).wait();
7779
var frameworkFiles = _.difference(directoryContent, [IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, xcodeProjectName]);
@@ -194,7 +196,45 @@ class IOSProjectService implements IPlatformProjectService {
194196
this.$fs.writeFile(pbxProjPath, project.writeSync()).wait();
195197
this.$logger.info("The iOS Deployment Target is now 8.0 in order to support Cocoa Touch Frameworks.");
196198
}).future<void>()();
197-
}
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.deleteDirectory(destinationFile).wait();
220+
shell.cp("-R", path.join(sourceFile, "*"), destinationFile);
221+
this.$logger.info("Backup file %s at location %s", sourceFile, destinationFile);
222+
this.$fs.deleteDirectory(path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName))).wait();
223+
224+
// Copy xcodeProject file
225+
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));
226+
shell.cp("-R", path.join(cachedPackagePath, "*"), path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName)));
227+
this.$logger.info("Copied from %s at %s.", cachedPackagePath, this.platformData.projectRoot);
228+
229+
230+
var pbxprojFilePath = path.join(this.platformData.projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
231+
this.replaceFileContent(pbxprojFilePath).wait();
232+
}).future<void>()();
233+
}
234+
235+
private buildPathToXcodeProjectFile(version: string): string {
236+
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");
237+
}
198238

199239
public getDebugOnDeviceSetup(): Mobile.IDebugOnDeviceSetup {
200240
var tnsIosPackage = "";

lib/services/platform-service.ts

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ export class PlatformService implements IPlatformService {
311311
this.$errors.fail("No platform specified.")
312312
}
313313

314-
platform = platform.toLowerCase();
314+
var parts = platform.split("@");
315+
platform = parts[0].toLowerCase();
315316

316317
if (!this.isValidPlatform(platform)) {
317318
this.$errors.fail("Invalid platform %s. Valid platforms are %s.", platform, helpers.formatListOfNames(this.$platformsData.platformsNames));
@@ -443,19 +444,28 @@ export class PlatformService implements IPlatformService {
443444
var currentVersion = data && data.version ? data.version : "0.2.0";
444445
var newVersion = version || this.$npm.getLatestVersion(platformData.frameworkPackageName).wait();
445446

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-
}
447+
if(platformData.platformProjectService.canUpdatePlatform(currentVersion, newVersion).wait()) {
449448

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) {
449+
if(!semver.valid(newVersion)) {
450+
this.$errors.fail("The version %s is not valid. The version should consists from 3 parts separated by dot.", newVersion);
451+
}
452+
453+
if(semver.gt(currentVersion, newVersion)) { // Downgrade
454+
var isUpdateConfirmed = this.$prompter.confirm(util.format("You are going to downgrade to android runtime v.%s. Are you sure?", newVersion), () => "n").wait();
455+
if(isUpdateConfirmed) {
456+
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
457+
}
458+
} else if(semver.eq(currentVersion, newVersion)) {
459+
this.$errors.fail("Current and new version are the same.");
460+
} else {
453461
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
454462
}
455-
} else if(semver.eq(currentVersion, newVersion)) {
456-
this.$errors.fail("Current and new version are the same.");
457463
} else {
458-
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
464+
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();
465+
if(isUpdateConfirmed) {
466+
platformData.platformProjectService.updatePlatform(currentVersion, newVersion).wait();
467+
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
468+
}
459469
}
460470

461471
}).future<void>()();
@@ -464,16 +474,36 @@ export class PlatformService implements IPlatformService {
464474
private updatePlatformCore(platformData: IPlatformData, currentVersion: string, newVersion: string): IFuture<void> {
465475
return (() => {
466476
// Remove old framework files
467-
var oldFrameworkFiles = this.getFrameworkFiles(platformData, currentVersion).wait();
468-
_.each(oldFrameworkFiles, file => {
469-
this.$fs.deleteFile(path.join(platformData.projectRoot, file)).wait();
477+
var oldFrameworkData = this.getFrameworkFiles(platformData, currentVersion).wait();
478+
479+
_.each(oldFrameworkData.frameworkFiles, file => {
480+
var fileToDelete = path.join(platformData.projectRoot, file);
481+
this.$logger.trace("Deleting %s", fileToDelete);
482+
this.$fs.deleteFile(fileToDelete).wait();
483+
});
484+
485+
_.each(oldFrameworkData.frameworkDirectories, dir => {
486+
var dirToDelete = path.join(platformData.projectRoot, dir);
487+
this.$logger.trace("Deleting %s", dirToDelete);
488+
this.$fs.deleteDirectory(dirToDelete).wait();
470489
});
471490

472491
// Add new framework files
473-
var newFrameworkFiles = this.getFrameworkFiles(platformData, newVersion).wait();
474-
var cacheDirectoryPath = this.getNpmCacheDirectoryCore(platformData.frameworkPackageName, newVersion);
475-
_.each(newFrameworkFiles, file => {
476-
shell.cp("-f", path.join(cacheDirectoryPath, file), path.join(platformData.projectRoot, file));
492+
var newFrameworkData = this.getFrameworkFiles(platformData, newVersion).wait();
493+
var cacheDirectoryPath = this.$npm.getCachedPackagePath(platformData.frameworkPackageName, newVersion);
494+
495+
_.each(newFrameworkData.frameworkFiles, file => {
496+
var sourceFile = path.join(cacheDirectoryPath, constants.PROJECT_FRAMEWORK_FOLDER_NAME, file);
497+
var destinationFile = path.join(platformData.projectRoot, file);
498+
this.$logger.trace("Replacing %s with %s", sourceFile, destinationFile);
499+
shell.cp("-f", sourceFile, destinationFile);
500+
});
501+
502+
_.each(newFrameworkData.frameworkDirectories, dir => {
503+
var sourceDirectory = path.join(cacheDirectoryPath, constants.PROJECT_FRAMEWORK_FOLDER_NAME, dir);
504+
var destinationDirectory = path.join(platformData.projectRoot, dir);
505+
this.$logger.trace("Copying %s to %s", sourceDirectory, destinationDirectory);
506+
shell.cp("-fR", path.join(sourceDirectory, "*"), destinationDirectory);
477507
});
478508

479509
// Update .tnsproject file
@@ -485,32 +515,35 @@ export class PlatformService implements IPlatformService {
485515
}).future<void>()();
486516
}
487517

488-
private getFrameworkFiles(platformData: IPlatformData, version: string): IFuture<string[]> {
518+
private getFrameworkFiles(platformData: IPlatformData, version: string): IFuture<any> {
489519
return (() => {
490-
var npmCacheDirectoryPath = this.getNpmCacheDirectory(platformData.frameworkPackageName, version).wait();
491-
var allFiles = this.$fs.enumerateFilesInDirectorySync(npmCacheDirectoryPath);
520+
var cachedPackagePath = this.$npm.getCachedPackagePath(platformData.frameworkPackageName, version);
521+
this.ensurePackageIsCached(cachedPackagePath, platformData.frameworkPackageName, version).wait();
522+
523+
var allFiles = this.$fs.enumerateFilesInDirectorySync(cachedPackagePath);
492524
var filteredFiles = _.filter(allFiles, file => _.contains(platformData.frameworkFilesExtensions, path.extname(file)));
493-
var relativeToCacheFiles = _.map(filteredFiles, file => file.substr(npmCacheDirectoryPath.length));
494525

495-
return relativeToCacheFiles;
526+
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));
527+
var filteredFrameworkDirectories = _.filter(allFrameworkDirectories, dir => this.$fs.getFsStats(dir).wait().isDirectory() && (_.contains(platformData.frameworkFilesExtensions, path.extname(dir)) || _.contains(platformData.frameworkDirectoriesNames, path.basename(dir))));
496528

497-
}).future<string[]>()();
529+
return {
530+
frameworkFiles: this.mapFrameworkFiles(cachedPackagePath, filteredFiles),
531+
frameworkDirectories: this.mapFrameworkFiles(cachedPackagePath, filteredFrameworkDirectories)
532+
}
533+
534+
}).future<any>()();
498535
}
499536

500-
private getNpmCacheDirectory(packageName: string, version: string): IFuture<string> {
537+
private ensurePackageIsCached(cachedPackagePath: string, packageName: string, version: string): IFuture<void> {
501538
return (() => {
502-
var npmCacheDirectoryPath = this.getNpmCacheDirectoryCore(packageName, version);
503-
504-
if(!this.$fs.exists(npmCacheDirectoryPath).wait()) {
539+
if(!this.$fs.exists(cachedPackagePath).wait()) {
505540
this.$npm.addToCache(packageName, version).wait();
506541
}
507-
508-
return npmCacheDirectoryPath;
509-
}).future<string>()();
542+
}).future<void>()();
510543
}
511544

512-
private getNpmCacheDirectoryCore(packageName: string, version: string): string {
513-
return path.join(this.$npm.getCacheRootPath(), packageName, version, "package");
545+
private mapFrameworkFiles(npmCacheDirectoryPath: string, files: string[]): string[] {
546+
return _.map(files, file => file.substr(npmCacheDirectoryPath.length + constants.PROJECT_FRAMEWORK_FOLDER_NAME.length + 1))
514547
}
515548
}
516549
$injector.register("platformService", PlatformService);

test/stubs.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ export class NPMStub implements INodePackageManager {
201201
getLatestVersion(packageName: string): IFuture<string> {
202202
return Future.fromResult("");
203203
}
204+
205+
getCachedPackagePath(packageName: string, version: string): string {
206+
return "";
207+
}
204208
}
205209

206210
export class ProjectDataStub implements IProjectData {
@@ -276,6 +280,12 @@ export class PlatformProjectServiceStub implements IPlatformProjectService {
276280
getDebugOnDeviceSetup(): Mobile.IDebugOnDeviceSetup {
277281
return null;
278282
}
283+
canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
284+
return Future.fromResult(false);
285+
}
286+
updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
287+
return Future.fromResult();
288+
}
279289
}
280290

281291
export class ProjectDataService implements IProjectDataService {

0 commit comments

Comments
 (0)