Skip to content

Commit 68537d8

Browse files
FatmeFatme
Fatme
authored and
Fatme
committed
Merge pull request #886 from NativeScript/fatme/fix-platform-update-from-ant-template
Update correctly ant template to gradle
2 parents e82ceac + f3f7ea3 commit 68537d8

File tree

5 files changed

+92
-71
lines changed

5 files changed

+92
-71
lines changed

lib/definitions/project.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ interface IPlatformProjectService {
4242
isPlatformPrepared(projectRoot: string): IFuture<boolean>;
4343
addLibrary(libraryPath: string): IFuture<void>;
4444
canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean>;
45-
updatePlatform(currentVersion: string, newVersion: string): IFuture<void>;
45+
/**
46+
* Provides a platform specific update logic for the specified runtime versions.
47+
* @return true in cases when the update procedure should continue.
48+
*/
49+
updatePlatform(currentVersion: string, newVersion: string, canUpdate: boolean, addPlatform?: Function, removePlatform?: (platforms: string[]) => IFuture<void>): IFuture<boolean>;
4650
preparePluginNativeCode(pluginData: IPluginData, options?: any): IFuture<void>;
4751
removePluginNativeCode(pluginData: IPluginData): IFuture<void>;
4852
afterPrepareAllPlugins(): IFuture<void>;

lib/services/android-project-service.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ class AndroidProjectService extends projectServiceBaseLib.PlatformProjectService
2121
private $androidToolsInfo: IAndroidToolsInfo,
2222
private $childProcess: IChildProcess,
2323
private $errors: IErrors,
24+
$fs: IFileSystem,
2425
private $hostInfo: IHostInfo,
2526
private $injector: IInjector,
2627
private $logger: ILogger,
2728
private $options: IOptions,
2829
private $projectData: IProjectData,
2930
private $projectDataService: IProjectDataService,
3031
private $propertiesParser: IPropertiesParser,
31-
private $sysInfo: ISysInfo,
32-
$fs: IFileSystem) {
32+
private $sysInfo: ISysInfo) {
3333
super($fs);
3434
this._androidProjectPropertiesManagers = Object.create(null);
3535
}
@@ -152,11 +152,20 @@ class AndroidProjectService extends projectServiceBaseLib.PlatformProjectService
152152
}
153153

154154
public canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
155-
return Future.fromResult<boolean>(true);
155+
return Future.fromResult(true);
156156
}
157157

158-
public updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
159-
return Future.fromResult();
158+
public updatePlatform(currentVersion: string, newVersion: string, canUpdate: boolean, addPlatform?: Function, removePlatforms?: (platforms: string[]) => IFuture<void>): IFuture<boolean> {
159+
return (() => {
160+
if(semver.eq(newVersion, AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE)) {
161+
let platformLowercase = this.platformData.normalizedPlatformName.toLowerCase();
162+
removePlatforms([platformLowercase.split("@")[0]]).wait();
163+
addPlatform(platformLowercase).wait();
164+
return false;
165+
}
166+
167+
return true;
168+
}).future<boolean>()();
160169
}
161170

162171
public buildProject(projectRoot: string, buildConfig?: IBuildConfig): IFuture<void> {

lib/services/ios-project-service.ts

+29-19
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as constants from "../constants";
1111
import * as helpers from "../common/helpers";
1212
import * as projectServiceBaseLib from "./platform-project-service-base";
1313

14-
export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServiceBase implements IPlatformProjectService {
14+
export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServiceBase implements IPlatformProjectService {
1515
private static XCODE_PROJECT_EXT_NAME = ".xcodeproj";
1616
private static XCODEBUILD_MIN_VERSION = "6.0";
1717
private static IOS_PROJECT_NAME_PLACEHOLDER = "__PROJECT_NAME__";
@@ -30,7 +30,8 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
3030
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices,
3131
private $options: IOptions,
3232
private $injector: IInjector,
33-
private $projectDataService: IProjectDataService) {
33+
private $projectDataService: IProjectDataService,
34+
private $prompter: IPrompter) {
3435
super($fs);
3536
}
3637

@@ -240,24 +241,33 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
240241
}).future<boolean>()();
241242
}
242243

243-
public updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
244+
public updatePlatform(currentVersion: string, newVersion: string, canUpdate: boolean): IFuture<boolean> {
244245
return (() => {
245-
// Copy old file to options["profile-dir"]
246-
let sourceFile = path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName));
247-
let destinationFile = path.join(this.$options.profileDir, "xcodeproj");
248-
this.$fs.deleteDirectory(destinationFile).wait();
249-
shell.cp("-R", path.join(sourceFile, "*"), destinationFile);
250-
this.$logger.info("Backup file %s at location %s", sourceFile, destinationFile);
251-
this.$fs.deleteDirectory(path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName))).wait();
252-
253-
// Copy xcodeProject file
254-
let cachedPackagePath = path.join(this.$npmInstallationManager.getCachedPackagePath(this.platformData.frameworkPackageName, newVersion), constants.PROJECT_FRAMEWORK_FOLDER_NAME, util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
255-
shell.cp("-R", path.join(cachedPackagePath, "*"), path.join(this.platformData.projectRoot, util.format("%s.xcodeproj", this.$projectData.projectName)));
256-
this.$logger.info("Copied from %s at %s.", cachedPackagePath, this.platformData.projectRoot);
257-
258-
let pbxprojFilePath = path.join(this.platformData.projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
259-
this.replaceFileContent(pbxprojFilePath).wait();
260-
}).future<void>()();
246+
if(!canUpdate) {
247+
let isUpdateConfirmed = this.$prompter.confirm(`We need to override xcodeproj file. The old one will be saved at ${this.$options.profileDir}. Are you sure?`, () => true).wait();
248+
if(isUpdateConfirmed) {
249+
// Copy old file to options["profile-dir"]
250+
let sourceDir = path.join(this.platformData.projectRoot, `${this.$projectData.projectName}.xcodeproj`);
251+
let destinationDir = path.join(this.$options.profileDir, "xcodeproj");
252+
this.$fs.deleteDirectory(destinationDir).wait();
253+
shell.cp("-R", path.join(sourceDir, "*"), destinationDir);
254+
this.$logger.info(`Backup file ${sourceDir} at location ${destinationDir}`);
255+
this.$fs.deleteDirectory(sourceDir).wait();
256+
257+
// Copy xcodeProject file
258+
let cachedPackagePath = path.join(this.$npmInstallationManager.getCachedPackagePath(this.platformData.frameworkPackageName, newVersion), constants.PROJECT_FRAMEWORK_FOLDER_NAME, util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
259+
shell.cp("-R", path.join(cachedPackagePath, "*"), sourceDir);
260+
this.$logger.info(`Copied from ${cachedPackagePath} at ${this.platformData.projectRoot}.`);
261+
262+
let pbxprojFilePath = path.join(this.platformData.projectRoot, this.$projectData.projectName + IOSProjectService.XCODE_PROJECT_EXT_NAME, "project.pbxproj");
263+
this.replaceFileContent(pbxprojFilePath).wait();
264+
}
265+
266+
return isUpdateConfirmed;
267+
}
268+
269+
return true;
270+
}).future<boolean>()();
261271
}
262272

263273
public prepareProject(): IFuture<void> {

lib/services/platform-service.ts

+42-44
Original file line numberDiff line numberDiff line change
@@ -425,74 +425,72 @@ export class PlatformService implements IPlatformService {
425425

426426
this.ensurePackageIsCached(platformData.frameworkPackageName, newVersion).wait();
427427

428-
if(platformData.platformProjectService.canUpdatePlatform(currentVersion, newVersion).wait()) {
429-
428+
let canUpdate = platformData.platformProjectService.canUpdatePlatform(currentVersion, newVersion).wait();
429+
if(canUpdate) {
430430
if(!semver.valid(newVersion)) {
431431
this.$errors.fail("The version %s is not valid. The version should consists from 3 parts separated by dot.", newVersion);
432432
}
433433

434434
if(semver.gt(currentVersion, newVersion)) { // Downgrade
435-
let isUpdateConfirmed = this.$prompter.confirm(`You are going to downgrade to android runtime v.${newVersion}. Are you sure?`, () => false).wait();
435+
let isUpdateConfirmed = this.$prompter.confirm(`You are going to downgrade to runtime v.${newVersion}. Are you sure?`, () => false).wait();
436436
if(isUpdateConfirmed) {
437-
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
437+
this.updatePlatformCore(platformData, currentVersion, newVersion, canUpdate).wait();
438438
}
439439
} else if(semver.eq(currentVersion, newVersion)) {
440440
this.$errors.fail("Current and new version are the same.");
441441
} else {
442-
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
442+
this.updatePlatformCore(platformData, currentVersion, newVersion, canUpdate).wait();
443443
}
444444
} else {
445-
let isUpdateConfirmed = this.$prompter.confirm(`We need to override xcodeproj file. The old one will be saved at ${this.$options.profileDir}. Are you sure?`, () => true).wait();
446-
if(isUpdateConfirmed) {
447-
platformData.platformProjectService.updatePlatform(currentVersion, newVersion).wait();
448-
this.updatePlatformCore(platformData, currentVersion, newVersion).wait();
449-
}
445+
this.updatePlatformCore(platformData, currentVersion, newVersion, canUpdate).wait();
450446
}
451447

452448
}).future<void>()();
453449
}
454450

455-
private updatePlatformCore(platformData: IPlatformData, currentVersion: string, newVersion: string): IFuture<void> {
451+
private updatePlatformCore(platformData: IPlatformData, currentVersion: string, newVersion: string, canUpdate: boolean): IFuture<void> {
456452
return (() => {
457-
// Remove old framework files
458-
let oldFrameworkData = this.getFrameworkFiles(platformData, currentVersion).wait();
459-
460-
_.each(oldFrameworkData.frameworkFiles, file => {
461-
let fileToDelete = path.join(platformData.projectRoot, file);
462-
this.$logger.trace("Deleting %s", fileToDelete);
463-
this.$fs.deleteFile(fileToDelete).wait();
464-
});
465-
466-
_.each(oldFrameworkData.frameworkDirectories, dir => {
467-
let dirToDelete = path.join(platformData.projectRoot, dir);
468-
this.$logger.trace("Deleting %s", dirToDelete);
469-
this.$fs.deleteDirectory(dirToDelete).wait();
470-
});
453+
let update = platformData.platformProjectService.updatePlatform(currentVersion, newVersion, canUpdate, this.addPlatform.bind(this), this.removePlatforms.bind(this)).wait();
454+
if(update) {
455+
// Remove old framework files
456+
let oldFrameworkData = this.getFrameworkFiles(platformData, currentVersion).wait();
457+
458+
_.each(oldFrameworkData.frameworkFiles, file => {
459+
let fileToDelete = path.join(platformData.projectRoot, file);
460+
this.$logger.trace("Deleting %s", fileToDelete);
461+
this.$fs.deleteFile(fileToDelete).wait();
462+
});
471463

472-
// Add new framework files
473-
let newFrameworkData = this.getFrameworkFiles(platformData, newVersion).wait();
474-
let cacheDirectoryPath = this.$npmInstallationManager.getCachedPackagePath(platformData.frameworkPackageName, newVersion);
464+
_.each(oldFrameworkData.frameworkDirectories, dir => {
465+
let dirToDelete = path.join(platformData.projectRoot, dir);
466+
this.$logger.trace("Deleting %s", dirToDelete);
467+
this.$fs.deleteDirectory(dirToDelete).wait();
468+
});
475469

476-
_.each(newFrameworkData.frameworkFiles, file => {
477-
let sourceFile = path.join(cacheDirectoryPath, constants.PROJECT_FRAMEWORK_FOLDER_NAME, file);
478-
let destinationFile = path.join(platformData.projectRoot, file);
479-
this.$logger.trace("Replacing %s with %s", sourceFile, destinationFile);
480-
shell.cp("-f", sourceFile, destinationFile);
481-
});
470+
// Add new framework files
471+
let newFrameworkData = this.getFrameworkFiles(platformData, newVersion).wait();
472+
let cacheDirectoryPath = this.$npmInstallationManager.getCachedPackagePath(platformData.frameworkPackageName, newVersion);
482473

483-
_.each(newFrameworkData.frameworkDirectories, dir => {
484-
let sourceDirectory = path.join(cacheDirectoryPath, constants.PROJECT_FRAMEWORK_FOLDER_NAME, dir);
485-
let destinationDirectory = path.join(platformData.projectRoot, dir);
486-
this.$logger.trace("Copying %s to %s", sourceDirectory, destinationDirectory);
487-
shell.cp("-fR", path.join(sourceDirectory, "*"), destinationDirectory);
488-
});
474+
_.each(newFrameworkData.frameworkFiles, file => {
475+
let sourceFile = path.join(cacheDirectoryPath, constants.PROJECT_FRAMEWORK_FOLDER_NAME, file);
476+
let destinationFile = path.join(platformData.projectRoot, file);
477+
this.$logger.trace("Replacing %s with %s", sourceFile, destinationFile);
478+
shell.cp("-f", sourceFile, destinationFile);
479+
});
489480

490-
// Update .tnsproject file
491-
this.$projectDataService.initialize(this.$projectData.projectDir);
492-
this.$projectDataService.setValue(platformData.frameworkPackageName, {version: newVersion}).wait();
481+
_.each(newFrameworkData.frameworkDirectories, dir => {
482+
let sourceDirectory = path.join(cacheDirectoryPath, constants.PROJECT_FRAMEWORK_FOLDER_NAME, dir);
483+
let destinationDirectory = path.join(platformData.projectRoot, dir);
484+
this.$logger.trace("Copying %s to %s", sourceDirectory, destinationDirectory);
485+
shell.cp("-fR", path.join(sourceDirectory, "*"), destinationDirectory);
486+
});
493487

494-
this.$logger.out("Successfully updated to version ", newVersion);
488+
// Update .tnsproject file
489+
this.$projectDataService.initialize(this.$projectData.projectDir);
490+
this.$projectDataService.setValue(platformData.frameworkPackageName, {version: newVersion}).wait();
495491

492+
this.$logger.out("Successfully updated to version ", newVersion);
493+
}
496494
}).future<void>()();
497495
}
498496

test/stubs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ export class PlatformProjectServiceStub implements IPlatformProjectService {
307307
canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
308308
return Future.fromResult(false);
309309
}
310-
updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
311-
return Future.fromResult();
310+
updatePlatform(currentVersion: string, newVersion: string, canUpdate: boolean): IFuture<boolean> {
311+
return Future.fromResult(true);
312312
}
313313
prepareAppResources(appResourcesDirectoryPath: string): IFuture<void> {
314314
return Future.fromResult();

0 commit comments

Comments
 (0)