Skip to content

Commit ec308c4

Browse files
FatmeFatme
Fatme
authored and
Fatme
committed
Merge pull request #881 from NativeScript/fatme/fix-broken-platform-add-android
Fix broken platform add android command
2 parents 81321b5 + 3790ca5 commit ec308c4

File tree

5 files changed

+38
-31
lines changed

5 files changed

+38
-31
lines changed

lib/common

lib/definitions/project.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface IBuildConfig {
3333
interface IPlatformProjectService {
3434
platformData: IPlatformData;
3535
validate(): IFuture<void>;
36-
createProject(projectRoot: string, frameworkDir: string): IFuture<void>;
36+
createProject(frameworkDir: string, frameworkVersion: string): IFuture<void>;
3737
interpolateData(projectRoot: string): IFuture<void>;
3838
afterCreateProject(projectRoot: string): IFuture<void>;
3939
buildProject(projectRoot: string, buildConfig?: IBuildConfig): IFuture<void>;

lib/services/android-project-service.ts

+29-22
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ class AndroidProjectService extends projectServiceBaseLib.PlatformProjectService
6161
return this._platformData;
6262
}
6363

64-
public getAppResourcesDestinationDirectoryPath(): IFuture<string> {
64+
public getAppResourcesDestinationDirectoryPath(frameworkVersion?: string): IFuture<string> {
6565
return (() => {
66-
if(this.canUseGradle().wait()) {
66+
if(this.canUseGradle(frameworkVersion).wait()) {
6767
return path.join(this.platformData.projectRoot, "src", "main", "res");
6868
}
6969

@@ -81,40 +81,39 @@ class AndroidProjectService extends projectServiceBaseLib.PlatformProjectService
8181
}).future<void>()();
8282
}
8383

84-
public createProject(projectRoot: string, frameworkDir: string): IFuture<void> {
84+
public createProject(frameworkDir: string, frameworkVersion: string): IFuture<void> {
8585
return (() => {
86-
let frameworkVersion = this.$fs.readJson(path.join(frameworkDir, "../", "package.json")).wait().version;
8786
if(semver.lt(frameworkVersion, AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE)) {
8887
this.$errors.fail(`The NativeScript CLI requires Android runtime ${AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE} or later to work properly.`);
8988
}
9089

9190
// TODO: Move these check to validate method once we do not support ant.
9291
this.checkGradle().wait();
9392

94-
this.$fs.ensureDirectoryExists(projectRoot).wait();
93+
this.$fs.ensureDirectoryExists(this.platformData.projectRoot).wait();
9594
let androidToolsInfo = this.$androidToolsInfo.getToolsInfo().wait();
96-
let newTarget = androidToolsInfo.targetSdkVersion;
97-
this.$logger.trace(`Using Android SDK '${newTarget}'.`);
95+
let targetSdkVersion = androidToolsInfo.targetSdkVersion;
96+
this.$logger.trace(`Using Android SDK '${targetSdkVersion}'.`);
9897
if(this.$options.symlink) {
99-
this.symlinkDirectory("build-tools", projectRoot, frameworkDir).wait();
100-
this.symlinkDirectory("libs", projectRoot, frameworkDir).wait();
101-
this.symlinkDirectory("src", projectRoot, frameworkDir).wait();
98+
this.symlinkDirectory("build-tools", this.platformData.projectRoot, frameworkDir).wait();
99+
this.symlinkDirectory("libs", this.platformData.projectRoot, frameworkDir).wait();
100+
this.symlinkDirectory("src", this.platformData.projectRoot, frameworkDir).wait();
102101

103-
this.$fs.symlink(path.join(frameworkDir, "build.gradle"), path.join(projectRoot, "build.gradle")).wait();
104-
this.$fs.symlink(path.join(frameworkDir, "settings.gradle"), path.join(projectRoot, "settings.gradle")).wait();
102+
this.$fs.symlink(path.join(frameworkDir, "build.gradle"), path.join(this.platformData.projectRoot, "build.gradle")).wait();
103+
this.$fs.symlink(path.join(frameworkDir, "settings.gradle"), path.join(this.platformData.projectRoot, "settings.gradle")).wait();
105104
} else {
106-
this.copy(projectRoot, frameworkDir, "build-tools libs src", "-R");
107-
this.copy(projectRoot, frameworkDir, "build.gradle settings.gradle", "-f");
105+
this.copy(this.platformData.projectRoot, frameworkDir, "build-tools libs src", "-R");
106+
this.copy(this.platformData.projectRoot, frameworkDir, "build.gradle settings.gradle", "-f");
108107
}
109108

110-
this.cleanResValues(newTarget).wait();
109+
this.cleanResValues(targetSdkVersion, frameworkVersion).wait();
111110

112111
}).future<any>()();
113112
}
114113

115-
private cleanResValues(versionNumber: number): IFuture<void> {
114+
private cleanResValues(targetSdkVersion: number, frameworkVersion: string): IFuture<void> {
116115
return (() => {
117-
let resDestinationDir = this.getAppResourcesDestinationDirectoryPath().wait();
116+
let resDestinationDir = this.getAppResourcesDestinationDirectoryPath(frameworkVersion).wait();
118117
let directoriesInResFolder = this.$fs.readDirectory(resDestinationDir).wait();
119118
let directoriesToClean = directoriesInResFolder
120119
.map(dir => { return {
@@ -124,7 +123,7 @@ class AndroidProjectService extends projectServiceBaseLib.PlatformProjectService
124123
})
125124
.filter(dir => dir.dirName.match(AndroidProjectService.VALUES_VERSION_DIRNAME_PREFIX)
126125
&& dir.sdkNum
127-
&& (!versionNumber || (versionNumber < dir.sdkNum)))
126+
&& (!targetSdkVersion || (targetSdkVersion < dir.sdkNum)))
128127
.map(dir => path.join(resDestinationDir, dir.dirName));
129128
this.$logger.trace("Directories to clean:");
130129
this.$logger.trace(directoriesToClean);
@@ -270,11 +269,19 @@ class AndroidProjectService extends projectServiceBaseLib.PlatformProjectService
270269
return Future.fromResult();
271270
}
272271

273-
private canUseGradle(): IFuture<boolean> {
272+
private _canUseGradle: boolean;
273+
private canUseGradle(frameworkVersion?: string): IFuture<boolean> {
274274
return (() => {
275-
this.$projectDataService.initialize(this.$projectData.projectDir);
276-
let frameworkVersion = this.$projectDataService.getValue(this.platformData.frameworkPackageName).wait().version;
277-
return semver.gte(frameworkVersion, AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE);
275+
if(!this._canUseGradle) {
276+
if(!frameworkVersion) {
277+
this.$projectDataService.initialize(this.$projectData.projectDir);
278+
frameworkVersion = this.$projectDataService.getValue(this.platformData.frameworkPackageName).wait().version;
279+
}
280+
281+
this._canUseGradle = semver.gte(frameworkVersion, AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE);
282+
}
283+
284+
return this._canUseGradle;
278285
}).future<boolean>()();
279286
}
280287

lib/services/ios-project-service.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,23 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
9696
}).future<void>()();
9797
}
9898

99-
public createProject(projectRoot: string, frameworkDir: string): IFuture<void> {
99+
public createProject(frameworkDir: string, frameworkVersion: string): IFuture<void> {
100100
return (() => {
101-
this.$fs.ensureDirectoryExists(path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER)).wait();
101+
this.$fs.ensureDirectoryExists(path.join(this.platformData.projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER)).wait();
102102
if(this.$options.symlink) {
103103
let xcodeProjectName = util.format("%s.xcodeproj", IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER);
104104

105-
shell.cp("-R", path.join(frameworkDir, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, "*"), path.join(projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
106-
shell.cp("-R", path.join(frameworkDir, xcodeProjectName), projectRoot);
105+
shell.cp("-R", path.join(frameworkDir, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, "*"), path.join(this.platformData.projectRoot, IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER));
106+
shell.cp("-R", path.join(frameworkDir, xcodeProjectName), this.platformData.projectRoot);
107107

108108
let directoryContent = this.$fs.readDirectory(frameworkDir).wait();
109109
let frameworkFiles = _.difference(directoryContent, [IOSProjectService.IOS_PROJECT_NAME_PLACEHOLDER, xcodeProjectName]);
110110
_.each(frameworkFiles, (file: string) => {
111-
this.$fs.symlink(path.join(frameworkDir, file), path.join(projectRoot, file)).wait();
111+
this.$fs.symlink(path.join(frameworkDir, file), path.join(this.platformData.projectRoot, file)).wait();
112112
});
113113

114114
} else {
115-
shell.cp("-R", path.join(frameworkDir, "*"), projectRoot);
115+
shell.cp("-R", path.join(frameworkDir, "*"), this.platformData.projectRoot);
116116
}
117117
}).future<void>()();
118118
}

lib/services/platform-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ export class PlatformService implements IPlatformService {
9393

9494
private addPlatformCore(platformData: IPlatformData, frameworkDir: string): IFuture<void> {
9595
return (() => {
96-
platformData.platformProjectService.createProject(platformData.projectRoot, frameworkDir).wait();
9796
let installedVersion = this.$fs.readJson(path.join(frameworkDir, "../", "package.json")).wait().version;
97+
platformData.platformProjectService.createProject(frameworkDir, installedVersion).wait();
9898

9999
if(this.$options.frameworkPath && this.$fs.getFsStats(this.$options.frameworkPath).wait().isFile() && !this.$options.symlink) {
100100
// Need to remove unneeded node_modules folder

0 commit comments

Comments
 (0)