Skip to content

Commit 6522178

Browse files
authored
Plamen5kov/as compatible cli (#3193)
* cli is compatible with android studio templates * made lint happy * change way to check template * made lint happy * use right syntax * update after review
1 parent ec6d540 commit 6522178

File tree

2 files changed

+98
-28
lines changed

2 files changed

+98
-28
lines changed

lib/constants.ts

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ export const XML_FILE_EXTENSION = ".xml";
1818
export const PLATFORMS_DIR_NAME = "platforms";
1919
export const CODE_SIGN_ENTITLEMENTS = "CODE_SIGN_ENTITLEMENTS";
2020
export const AWAIT_NOTIFICATION_TIMEOUT_SECONDS = 9;
21+
export const SRC_DIR = "src";
22+
export const MAIN_DIR = "main";
23+
export const ASSETS_DIR = "assets";
24+
export const MANIFEST_FILE_NAME = "AndroidManifest.xml";
25+
export const BUILD_DIR = "build";
26+
export const OUTPUTS_DIR = "outputs";
27+
export const APK_DIR = "apk";
28+
export const RESOURCES_DIR = "res";
2129

2230
export class PackageVersion {
2331
static NEXT = "next";

lib/services/android-project-service.ts

+90-28
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
2222
];
2323

2424
private _androidProjectPropertiesManagers: IDictionary<IAndroidProjectPropertiesManager>;
25+
private isAndroidStudioTemplate: boolean;
2526

2627
constructor(private $androidEmulatorServices: Mobile.IEmulatorPlatformServices,
2728
private $androidToolsInfo: IAndroidToolsInfo,
@@ -38,6 +39,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
3839
private $npm: INodePackageManager) {
3940
super($fs, $projectDataService);
4041
this._androidProjectPropertiesManagers = Object.create(null);
42+
this.isAndroidStudioTemplate = false;
4143
}
4244

4345
private _platformsDirCache: string = null;
@@ -46,19 +48,41 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
4648
if (!projectData && !this._platformData) {
4749
throw new Error("First call of getPlatformData without providing projectData.");
4850
}
51+
if (projectData && projectData.platformsDir) {
52+
const projectRoot = path.join(projectData.platformsDir, AndroidProjectService.ANDROID_PLATFORM_NAME);
53+
if (this.isAndroidStudioCompatibleTemplate(projectData)) {
54+
this.isAndroidStudioTemplate = true;
55+
}
56+
57+
const appDestinationDirectoryArr = [projectRoot];
58+
if (this.isAndroidStudioTemplate) {
59+
appDestinationDirectoryArr.push(constants.APP_FOLDER_NAME);
60+
}
61+
appDestinationDirectoryArr.push(constants.SRC_DIR, constants.MAIN_DIR, constants.ASSETS_DIR);
62+
63+
const configurationsDirectoryArr = [projectRoot];
64+
if (this.isAndroidStudioTemplate) {
65+
configurationsDirectoryArr.push(constants.APP_FOLDER_NAME);
66+
}
67+
configurationsDirectoryArr.push(constants.SRC_DIR, constants.MAIN_DIR, constants.MANIFEST_FILE_NAME);
68+
69+
const deviceBuildOutputArr = [projectRoot];
70+
if (this.isAndroidStudioTemplate) {
71+
deviceBuildOutputArr.push(constants.APP_FOLDER_NAME);
72+
}
73+
deviceBuildOutputArr.push(constants.BUILD_DIR, constants.OUTPUTS_DIR, constants.APK_DIR);
4974

50-
if (projectData && projectData.platformsDir && this._platformsDirCache !== projectData.platformsDir) {
5175
this._platformsDirCache = projectData.platformsDir;
52-
const projectRoot = path.join(projectData.platformsDir, AndroidProjectService.ANDROID_PLATFORM_NAME);
5376
const packageName = this.getProjectNameFromId(projectData);
77+
5478
this._platformData = {
55-
frameworkPackageName: "tns-android",
79+
frameworkPackageName: constants.TNS_ANDROID_RUNTIME_NAME,
5680
normalizedPlatformName: "Android",
57-
appDestinationDirectoryPath: path.join(projectRoot, "src", "main", "assets"),
81+
appDestinationDirectoryPath: path.join(...appDestinationDirectoryArr),
5882
platformProjectService: this,
5983
emulatorServices: this.$androidEmulatorServices,
6084
projectRoot: projectRoot,
61-
deviceBuildOutputPath: path.join(projectRoot, "build", "outputs", "apk"),
85+
deviceBuildOutputPath: path.join(...deviceBuildOutputArr),
6286
getValidPackageNames: (buildOptions: { isReleaseBuild?: boolean, isForDevice?: boolean }): string[] => {
6387
const buildMode = buildOptions.isReleaseBuild ? Configurations.Release.toLowerCase() : Configurations.Debug.toLowerCase();
6488

@@ -69,11 +93,12 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
6993
];
7094
},
7195
frameworkFilesExtensions: [".jar", ".dat", ".so"],
72-
configurationFileName: "AndroidManifest.xml",
73-
configurationFilePath: path.join(projectRoot, "src", "main", "AndroidManifest.xml"),
74-
relativeToFrameworkConfigurationFilePath: path.join("src", "main", "AndroidManifest.xml"),
96+
configurationFileName: constants.MANIFEST_FILE_NAME,
97+
configurationFilePath: path.join(...configurationsDirectoryArr),
98+
relativeToFrameworkConfigurationFilePath: path.join(constants.SRC_DIR, constants.MAIN_DIR, constants.MANIFEST_FILE_NAME),
7599
fastLivesyncFileExtensions: [".jpg", ".gif", ".png", ".bmp", ".webp"] // http://developer.android.com/guide/appendix/media-formats.html
76100
};
101+
77102
}
78103

79104
return this._platformData;
@@ -93,10 +118,16 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
93118

94119
public getAppResourcesDestinationDirectoryPath(projectData: IProjectData, frameworkVersion?: string): string {
95120
if (this.canUseGradle(projectData, frameworkVersion)) {
96-
return path.join(this.getPlatformData(projectData).projectRoot, "src", "main", "res");
121+
const resourcePath: string[] = [constants.SRC_DIR, constants.MAIN_DIR, constants.RESOURCES_DIR];
122+
if (this.isAndroidStudioTemplate) {
123+
resourcePath.unshift(constants.APP_FOLDER_NAME);
124+
}
125+
126+
return path.join(this.getPlatformData(projectData).projectRoot, ...resourcePath);
127+
97128
}
98129

99-
return path.join(this.getPlatformData(projectData).projectRoot, "res");
130+
return path.join(this.getPlatformData(projectData).projectRoot, constants.RESOURCES_DIR);
100131
}
101132

102133
public async validate(projectData: IProjectData): Promise<void> {
@@ -125,25 +156,31 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
125156
const androidToolsInfo = this.$androidToolsInfo.getToolsInfo();
126157
const targetSdkVersion = androidToolsInfo && androidToolsInfo.targetSdkVersion;
127158
this.$logger.trace(`Using Android SDK '${targetSdkVersion}'.`);
128-
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "libs", "-R");
129159

130-
if (config.pathToTemplate) {
131-
const mainPath = path.join(this.getPlatformData(projectData).projectRoot, "src", "main");
132-
this.$fs.createDirectory(mainPath);
133-
shell.cp("-R", path.join(path.resolve(config.pathToTemplate), "*"), mainPath);
160+
this.isAndroidStudioTemplate = this.isAndroidStudioCompatibleTemplate(projectData);
161+
if (this.isAndroidStudioTemplate) {
162+
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "*", "-R");
134163
} else {
135-
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "src", "-R");
136-
}
137-
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "build.gradle settings.gradle build-tools", "-Rf");
164+
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "libs", "-R");
138165

139-
try {
140-
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "gradle.properties", "-Rf");
141-
} catch (e) {
142-
this.$logger.warn(`\n${e}\nIt's possible, the final .apk file will contain all architectures instead of the ones described in the abiFilters!\nYou can fix this by using the latest android platform.`);
143-
}
166+
if (config.pathToTemplate) {
167+
const mainPath = path.join(this.getPlatformData(projectData).projectRoot, constants.SRC_DIR, constants.MAIN_DIR);
168+
this.$fs.createDirectory(mainPath);
169+
shell.cp("-R", path.join(path.resolve(config.pathToTemplate), "*"), mainPath);
170+
} else {
171+
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, constants.SRC_DIR, "-R");
172+
}
173+
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "build.gradle settings.gradle build-tools", "-Rf");
144174

145-
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "gradle", "-R");
146-
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "gradlew gradlew.bat", "-f");
175+
try {
176+
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "gradle.properties", "-Rf");
177+
} catch (e) {
178+
this.$logger.warn(`\n${e}\nIt's possible, the final .apk file will contain all architectures instead of the ones described in the abiFilters!\nYou can fix this by using the latest android platform.`);
179+
}
180+
181+
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "gradle", "-R");
182+
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "gradlew gradlew.bat", "-f");
183+
}
147184

148185
this.cleanResValues(targetSdkVersion, projectData, frameworkVersion);
149186

@@ -269,8 +306,11 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
269306
buildOptions.unshift("--stacktrace");
270307
buildOptions.unshift("--debug");
271308
}
272-
273-
buildOptions.unshift("buildapk");
309+
if (buildConfig.release) {
310+
buildOptions.unshift("assembleRelease");
311+
} else {
312+
buildOptions.unshift("assembleDebug");
313+
}
274314

275315
const handler = (data: any) => {
276316
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
@@ -380,7 +420,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
380420
const flattenedDependencyName = isScoped ? pluginData.name.replace("/", "_") : pluginData.name;
381421

382422
// Copy all resources from plugin
383-
const resourcesDestinationDirectoryPath = path.join(this.getPlatformData(projectData).projectRoot, "src", flattenedDependencyName);
423+
const resourcesDestinationDirectoryPath = path.join(this.getPlatformData(projectData).projectRoot, constants.SRC_DIR, flattenedDependencyName);
384424
this.$fs.ensureDirectoryExists(resourcesDestinationDirectoryPath);
385425
shell.cp("-Rf", path.join(pluginPlatformsFolderPath, "*"), resourcesDestinationDirectoryPath);
386426

@@ -593,6 +633,28 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
593633
const normalizedPlatformVersion = `${semver.major(platformVersion)}.${semver.minor(platformVersion)}.0`;
594634
return semver.gte(normalizedPlatformVersion, newRuntimeGradleRoutineVersion);
595635
}
636+
637+
private isAndroidStudioCompatibleTemplate(projectData: IProjectData): boolean {
638+
const currentPlatformData: IDictionary<any> = this.$projectDataService.getNSValue(projectData.projectDir, constants.TNS_ANDROID_RUNTIME_NAME);
639+
let platformVersion = currentPlatformData && currentPlatformData[constants.VERSION_STRING];
640+
641+
if (!platformVersion) {
642+
const tnsAndroidPackageJsonPath = path.join(projectData.projectDir, constants.NODE_MODULES_FOLDER_NAME, constants.TNS_ANDROID_RUNTIME_NAME, constants.PACKAGE_JSON_FILE_NAME);
643+
if (this.$fs.exists(tnsAndroidPackageJsonPath)) {
644+
const projectPackageJson: any = this.$fs.readJson(tnsAndroidPackageJsonPath);
645+
if (projectPackageJson && projectPackageJson.version) {
646+
platformVersion = projectPackageJson.version;
647+
}
648+
} else {
649+
return false;
650+
}
651+
}
652+
653+
const androidStudioCompatibleTemplate = "3.4.0";
654+
const normalizedPlatformVersion = `${semver.major(platformVersion)}.${semver.minor(platformVersion)}.0`;
655+
656+
return semver.gte(normalizedPlatformVersion, androidStudioCompatibleTemplate);
657+
}
596658
}
597659

598660
$injector.register("androidProjectService", AndroidProjectService);

0 commit comments

Comments
 (0)