Skip to content

Commit 2f92c16

Browse files
committed
fix(resources-update-command): make prepare and run backward-compatible
1 parent 651bd90 commit 2f92c16

File tree

3 files changed

+89
-68
lines changed

3 files changed

+89
-68
lines changed

lib/services/android-project-service.ts

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as semver from "semver";
55
import * as projectServiceBaseLib from "./platform-project-service-base";
66
import { DeviceAndroidDebugBridge } from "../common/mobile/android/device-android-debug-bridge";
77
import { attachAwaitDetach } from "../common/helpers";
8-
import { EOL } from "os";
98
import { Configurations } from "../common/constants";
109
import { SpawnOptions } from "child_process";
1110

@@ -36,7 +35,8 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
3635
private $injector: IInjector,
3736
private $pluginVariablesService: IPluginVariablesService,
3837
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
39-
private $npm: INodePackageManager) {
38+
private $npm: INodePackageManager,
39+
private $projectV4MigrationService: IProjectV4MigrationService) {
4040
super($fs, $projectDataService);
4141
this._androidProjectPropertiesManagers = Object.create(null);
4242
this.isAndroidStudioTemplate = false;
@@ -133,18 +133,32 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
133133
return Promise.resolve(true);
134134
}
135135

136-
public getAppResourcesDestinationDirectoryPath(projectData: IProjectData, frameworkVersion?: string): string {
137-
if (this.canUseGradle(projectData, frameworkVersion)) {
138-
const resourcePath: string[] = [constants.SRC_DIR, constants.MAIN_DIR, constants.RESOURCES_DIR];
139-
if (this.isAndroidStudioTemplate) {
140-
resourcePath.unshift(constants.APP_FOLDER_NAME);
141-
}
136+
public getAppResourcesDestinationDirectoryPath(projectData: IProjectData): string {
137+
const appResourcesDirStructureHasMigrated = this.$projectV4MigrationService.hasMigrated(projectData.getAppResourcesDirectoryPath());
138+
139+
if (appResourcesDirStructureHasMigrated) {
140+
return this.getAppResourcesDestinationDirectoryPathUpdatedAppResourcesDirStructure(projectData);
141+
} else {
142+
return this.getAppResourcesDestinationDirectoryPathOldAppResourcesDirStructure(projectData);
143+
}
144+
}
145+
146+
private getAppResourcesDestinationDirectoryPathOldAppResourcesDirStructure(projectData: IProjectData): string {
147+
const resourcePath: string[] = [constants.SRC_DIR, constants.MAIN_DIR, constants.RESOURCES_DIR];
148+
if (this.isAndroidStudioTemplate) {
149+
resourcePath.unshift(constants.APP_FOLDER_NAME);
150+
}
142151

143-
return path.join(this.getPlatformData(projectData).projectRoot, ...resourcePath);
152+
return path.join(this.getPlatformData(projectData).projectRoot, ...resourcePath);
153+
}
144154

155+
private getAppResourcesDestinationDirectoryPathUpdatedAppResourcesDirStructure(projectData: IProjectData): string {
156+
const resourcePath: string[] = [constants.SRC_DIR];
157+
if (this.isAndroidStudioTemplate) {
158+
resourcePath.unshift(constants.APP_FOLDER_NAME);
145159
}
146160

147-
return path.join(this.getPlatformData(projectData).projectRoot, constants.RESOURCES_DIR);
161+
return path.join(this.getPlatformData(projectData).projectRoot, ...resourcePath);
148162
}
149163

150164
public async validate(projectData: IProjectData): Promise<void> {
@@ -199,7 +213,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
199213
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "gradlew gradlew.bat", "-f");
200214
}
201215

202-
this.cleanResValues(targetSdkVersion, projectData, frameworkVersion);
216+
this.cleanResValues(targetSdkVersion, projectData);
203217

204218
const npmConfig: INodePackageManagerInstallOptions = {
205219
save: true,
@@ -235,8 +249,8 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
235249
}
236250
}
237251

238-
private cleanResValues(targetSdkVersion: number, projectData: IProjectData, frameworkVersion: string): void {
239-
const resDestinationDir = this.getAppResourcesDestinationDirectoryPath(projectData, frameworkVersion);
252+
private cleanResValues(targetSdkVersion: number, projectData: IProjectData): void {
253+
const resDestinationDir = this.getAppResourcesDestinationDirectoryPath(projectData);
240254
const directoriesInResFolder = this.$fs.readDirectory(resDestinationDir);
241255
const directoriesToClean = directoriesInResFolder
242256
.map(dir => {
@@ -260,8 +274,16 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
260274
public async interpolateData(projectData: IProjectData, platformSpecificData: IPlatformSpecificData): Promise<void> {
261275
// Interpolate the apilevel and package
262276
this.interpolateConfigurationFile(projectData, platformSpecificData);
277+
const appResourcesDirectoryPath = projectData.getAppResourcesDirectoryPath();
278+
279+
let stringsFilePath: string;
280+
281+
if (!this.$projectV4MigrationService.hasMigrated(appResourcesDirectoryPath)) {
282+
stringsFilePath = path.join(this.getAppResourcesDestinationDirectoryPath(projectData), 'values', 'strings.xml');
283+
} else {
284+
stringsFilePath = path.join(this.getAppResourcesDestinationDirectoryPath(projectData), "main", "res", 'values', 'strings.xml');
285+
}
263286

264-
const stringsFilePath = path.join(this.getAppResourcesDestinationDirectoryPath(projectData), 'values', 'strings.xml');
265287
shell.sed('-i', /__NAME__/, projectData.projectName, stringsFilePath);
266288
shell.sed('-i', /__TITLE_ACTIVITY__/, projectData.projectName, stringsFilePath);
267289

@@ -317,33 +339,28 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
317339
}
318340

319341
public async buildProject(projectRoot: string, projectData: IProjectData, buildConfig: IBuildConfig): Promise<void> {
320-
if (this.canUseGradle(projectData)) {
321-
const buildOptions = this.getBuildOptions(buildConfig, projectData);
322-
if (this.$logger.getLevel() === "TRACE") {
323-
buildOptions.unshift("--stacktrace");
324-
buildOptions.unshift("--debug");
325-
}
326-
if (buildConfig.release) {
327-
buildOptions.unshift("assembleRelease");
328-
} else {
329-
buildOptions.unshift("assembleDebug");
330-
}
331-
332-
const handler = (data: any) => {
333-
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
334-
};
335-
336-
await attachAwaitDetach(constants.BUILD_OUTPUT_EVENT_NAME,
337-
this.$childProcess,
338-
handler,
339-
this.executeGradleCommand(this.getPlatformData(projectData).projectRoot,
340-
buildOptions,
341-
{ stdio: buildConfig.buildOutputStdio || "inherit" },
342-
{ emitOptions: { eventName: constants.BUILD_OUTPUT_EVENT_NAME }, throwError: true }));
342+
const buildOptions = this.getBuildOptions(buildConfig, projectData);
343+
if (this.$logger.getLevel() === "TRACE") {
344+
buildOptions.unshift("--stacktrace");
345+
buildOptions.unshift("--debug");
346+
}
347+
if (buildConfig.release) {
348+
buildOptions.unshift("assembleRelease");
343349
} else {
344-
this.$errors.failWithoutHelp("Cannot complete build because this project is ANT-based." + EOL +
345-
"Run `tns platform remove android && tns platform add android` to switch to Gradle and try again.");
350+
buildOptions.unshift("assembleDebug");
346351
}
352+
353+
const handler = (data: any) => {
354+
this.emit(constants.BUILD_OUTPUT_EVENT_NAME, data);
355+
};
356+
357+
await attachAwaitDetach(constants.BUILD_OUTPUT_EVENT_NAME,
358+
this.$childProcess,
359+
handler,
360+
this.executeGradleCommand(this.getPlatformData(projectData).projectRoot,
361+
buildOptions,
362+
{ stdio: buildConfig.buildOutputStdio || "inherit" },
363+
{ emitOptions: { eventName: constants.BUILD_OUTPUT_EVENT_NAME }, throwError: true }));
347364
}
348365

349366
private getBuildOptions(settings: IAndroidBuildOptionsSettings, projectData: IProjectData): Array<string> {
@@ -391,7 +408,15 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
391408
}
392409

393410
public ensureConfigurationFileInAppResources(projectData: IProjectData): void {
394-
const originalAndroidManifestFilePath = path.join(projectData.appResourcesDirectoryPath, this.$devicePlatformsConstants.Android, this.getPlatformData(projectData).configurationFileName);
411+
const appResourcesDirectoryPath = projectData.appResourcesDirectoryPath;
412+
const appResourcesDirStructureHasMigrated = this.$projectV4MigrationService.hasMigrated(appResourcesDirectoryPath);
413+
let originalAndroidManifestFilePath;
414+
415+
if (appResourcesDirStructureHasMigrated) {
416+
originalAndroidManifestFilePath = path.join(appResourcesDirectoryPath, this.$devicePlatformsConstants.Android, "src", "main", this.getPlatformData(projectData).configurationFileName);
417+
} else {
418+
originalAndroidManifestFilePath = path.join(appResourcesDirectoryPath, this.$devicePlatformsConstants.Android, this.getPlatformData(projectData).configurationFileName);
419+
}
395420

396421
const manifestExists = this.$fs.exists(originalAndroidManifestFilePath);
397422

@@ -400,16 +425,13 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
400425
return;
401426
}
402427
// Overwrite the AndroidManifest from runtime.
403-
this.$fs.copyFile(originalAndroidManifestFilePath, this.getPlatformData(projectData).configurationFilePath);
428+
if (!appResourcesDirStructureHasMigrated) {
429+
this.$fs.copyFile(originalAndroidManifestFilePath, this.getPlatformData(projectData).configurationFilePath);
430+
}
404431
}
405432

406433
public prepareAppResources(appResourcesDirectoryPath: string, projectData: IProjectData): void {
407-
const resourcesDirPath = path.join(appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName);
408-
const valuesDirRegExp = /^values/;
409-
const resourcesDirs = this.$fs.readDirectory(resourcesDirPath).filter(resDir => !resDir.match(valuesDirRegExp));
410-
_.each(resourcesDirs, resourceDir => {
411-
this.$fs.deleteDirectory(path.join(this.getAppResourcesDestinationDirectoryPath(projectData), resourceDir));
412-
});
434+
// Intentionally left empty
413435
}
414436

415437
public async preparePluginNativeCode(pluginData: IPluginData, projectData: IProjectData): Promise<void> {
@@ -559,20 +581,6 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
559581
// Nothing android specific to check yet.
560582
}
561583

562-
private _canUseGradle: boolean;
563-
private canUseGradle(projectData: IProjectData, frameworkVersion?: string): boolean {
564-
if (!this._canUseGradle) {
565-
if (!frameworkVersion) {
566-
const frameworkInfoInProjectFile = this.$projectDataService.getNSValue(projectData.projectDir, this.getPlatformData(projectData).frameworkPackageName);
567-
frameworkVersion = frameworkInfoInProjectFile && frameworkInfoInProjectFile.version;
568-
}
569-
570-
this._canUseGradle = !frameworkVersion || semver.gte(frameworkVersion, AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE);
571-
}
572-
573-
return this._canUseGradle;
574-
}
575-
576584
private copy(projectRoot: string, frameworkDir: string, files: string, cpArg: string): void {
577585
const paths = files.split(' ').map(p => path.join(frameworkDir, p));
578586
shell.cp(cpArg, paths, projectRoot);

lib/services/prepare-platform-native-service.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export class PreparePlatformNativeService extends PreparePlatformService impleme
1010
$hooksService: IHooksService,
1111
private $nodeModulesBuilder: INodeModulesBuilder,
1212
private $pluginsService: IPluginsService,
13-
private $projectChangesService: IProjectChangesService) {
13+
private $projectChangesService: IProjectChangesService,
14+
private $projectV4MigrationService: IProjectV4MigrationService) {
1415
super($fs, $hooksService, $xmlValidator);
1516
}
1617

@@ -62,13 +63,26 @@ export class PreparePlatformNativeService extends PreparePlatformService impleme
6263

6364
private copyAppResources(platformData: IPlatformData, projectData: IProjectData): void {
6465
const appDestinationDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
65-
const appResourcesDirectoryPath = path.join(appDestinationDirectoryPath, constants.APP_RESOURCES_FOLDER_NAME);
66-
if (this.$fs.exists(appResourcesDirectoryPath)) {
67-
platformData.platformProjectService.prepareAppResources(appResourcesDirectoryPath, projectData);
66+
const appResourcesDestinationDirectoryPath = path.join(appDestinationDirectoryPath, constants.APP_RESOURCES_FOLDER_NAME);
67+
if (this.$fs.exists(appResourcesDestinationDirectoryPath)) {
68+
platformData.platformProjectService.prepareAppResources(appResourcesDestinationDirectoryPath, projectData);
6869
const appResourcesDestination = platformData.platformProjectService.getAppResourcesDestinationDirectoryPath(projectData);
6970
this.$fs.ensureDirectoryExists(appResourcesDestination);
70-
shell.cp("-Rf", path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName, "*"), appResourcesDestination);
71-
this.$fs.deleteDirectory(appResourcesDirectoryPath);
71+
72+
if (platformData.normalizedPlatformName.toLowerCase() === "android") {
73+
const appResourcesDirectoryPath = projectData.getAppResourcesDirectoryPath();
74+
const appResourcesDirStructureHasMigrated = this.$projectV4MigrationService.hasMigrated(appResourcesDirectoryPath);
75+
76+
if (appResourcesDirStructureHasMigrated) {
77+
shell.cp("-Rf", path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName, "src", "main"), appResourcesDestination);
78+
79+
return;
80+
}
81+
}
82+
83+
shell.cp("-Rf", path.join(appResourcesDestinationDirectoryPath, platformData.normalizedPlatformName, "*"), appResourcesDestination);
84+
85+
this.$fs.deleteDirectory(appResourcesDestinationDirectoryPath);
7286
}
7387
}
7488

lib/services/project-v4-migration-service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export class ProjectV4MigrationService implements IProjectV4MigrationService {
5555

5656
this.$logger.out(`Successfully updated your project's App_Resources/Android directory structure.\nThe previous version of App_Resources/Android has been renamed to App_Resources/${ProjectV4MigrationService.ANDROID_DIR_OLD}`);
5757
}
58-
5958
}
6059

6160
$injector.register("projectV4MigrationService", ProjectV4MigrationService);

0 commit comments

Comments
 (0)