Skip to content

Commit 1142680

Browse files
petekanevKristianDD
authored andcommitted
refactor(project-data): read app_resources dir from nsconfig.json
Refactor the project-data service to read a config.json for the 'app_resources' property entry, which points relatively to the App_Resources directory. All references to app/App_Resources now retrieve the path from said service. To keep the service backwards compatible, if no config.json is present, or the app_resources entry isn't available, location defaults to projectDir/app/App_Resources.
1 parent 61f4b6c commit 1142680

18 files changed

+97
-47
lines changed

lib/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export const BUILD_DIR = "build";
2828
export const OUTPUTS_DIR = "outputs";
2929
export const APK_DIR = "apk";
3030
export const RESOURCES_DIR = "res";
31+
export const CONFIG_NS_FILE_NAME = "nsconfig.json";
32+
export const CONFIG_NS_APP_RESOURCES_ENTRY = "app_resources";
3133

3234
export class PackageVersion {
3335
static NEXT = "next";

lib/definitions/project.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ interface IProjectData extends IProjectDir {
6161
dependencies: any;
6262
devDependencies: IStringDictionary;
6363
appDirectoryPath: string;
64-
appResourcesDirectoryPath: string;
6564
projectType: string;
6665
/**
6766
* Initializes project data with the given project directory. If none supplied defaults to --path option or cwd.
6867
* @param {string} projectDir Project root directory.
6968
* @returns {void}
7069
*/
7170
initializeProjectData(projectDir?: string): void;
71+
getAppResourcesDirectoryPath(projectDir?: string): string;
7272
}
7373

7474
interface IProjectDataService {

lib/project-data.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export class ProjectData implements IProjectData {
6969
this.platformsDir = path.join(projectDir, constants.PLATFORMS_DIR_NAME);
7070
this.projectFilePath = projectFilePath;
7171
this.appDirectoryPath = path.join(projectDir, constants.APP_FOLDER_NAME);
72-
this.appResourcesDirectoryPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
7372
this.projectId = data.id;
7473
this.dependencies = fileContent.dependencies;
7574
this.devDependencies = fileContent.devDependencies;
@@ -87,6 +86,27 @@ export class ProjectData implements IProjectData {
8786
this.$errors.fail("No project found at or above '%s' and neither was a --path specified.", projectDir || this.$options.path || currentDir);
8887
}
8988

89+
public getAppResourcesDirectoryPath(projectDir?: string): string {
90+
if (!projectDir) {
91+
projectDir = this.projectDir;
92+
}
93+
94+
const configNSFilePath = path.join(projectDir, constants.CONFIG_NS_FILE_NAME);
95+
let absoluteAppResourcesDirPath: string;
96+
97+
if (this.$fs.exists(configNSFilePath)) {
98+
const configNS = this.$fs.readJson(configNSFilePath);
99+
100+
if (configNS && configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY]) {
101+
const appResourcesDirPath = configNS[constants.CONFIG_NS_APP_RESOURCES_ENTRY];
102+
103+
absoluteAppResourcesDirPath = path.resolve(projectDir, appResourcesDirPath);
104+
}
105+
}
106+
107+
return absoluteAppResourcesDirPath || path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
108+
}
109+
90110
private getProjectType(): string {
91111
let detectedProjectType = _.find(ProjectData.PROJECT_TYPES, (projectType) => projectType.isDefaultProjectType).type;
92112

lib/providers/project-files-provider.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { ProjectFilesProviderBase } from "../common/services/project-files-provi
66
export class ProjectFilesProvider extends ProjectFilesProviderBase {
77
constructor(private $platformsData: IPlatformsData,
88
$mobileHelper: Mobile.IMobileHelper,
9-
$options:IOptions) {
10-
super($mobileHelper, $options);
9+
$options: IOptions) {
10+
super($mobileHelper, $options);
1111
}
1212

13-
private static INTERNAL_NONPROJECT_FILES = [ "**/*.ts" ];
13+
private static INTERNAL_NONPROJECT_FILES = ["**/*.ts"];
1414

1515
public mapFilePath(filePath: string, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): string {
1616
const platformData = this.$platformsData.getPlatformData(platform.toLowerCase(), projectData);
@@ -23,14 +23,14 @@ export class ProjectFilesProvider extends ProjectFilesProviderBase {
2323
mappedFilePath = path.join(platformData.appDestinationDirectoryPath, path.relative(projectData.projectDir, parsedFilePath));
2424
}
2525

26-
const appResourcesDirectoryPath = path.join(constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
26+
const appResourcesDirectoryPath = projectData.getAppResourcesDirectoryPath();
2727
const platformSpecificAppResourcesDirectoryPath = path.join(appResourcesDirectoryPath, platformData.normalizedPlatformName);
2828
if (parsedFilePath.indexOf(appResourcesDirectoryPath) > -1 && parsedFilePath.indexOf(platformSpecificAppResourcesDirectoryPath) === -1) {
2929
return null;
3030
}
3131

3232
if (parsedFilePath.indexOf(platformSpecificAppResourcesDirectoryPath) > -1) {
33-
const appResourcesRelativePath = path.relative(path.join(projectData.projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME,
33+
const appResourcesRelativePath = path.relative(path.join(projectData.getAppResourcesDirectoryPath(),
3434
platformData.normalizedPlatformName), parsedFilePath);
3535
mappedFilePath = path.join(platformData.platformProjectService.getAppResourcesDestinationDirectoryPath(projectData), appResourcesRelativePath);
3636
}

lib/services/android-project-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
269269
shell.sed('-i', /__PROJECT_NAME__/, this.getProjectNameFromId(projectData), gradleSettingsFilePath);
270270

271271
// will replace applicationId in app/App_Resources/Android/app.gradle if it has not been edited by the user
272-
const userAppGradleFilePath = path.join(projectData.appResourcesDirectoryPath, this.$devicePlatformsConstants.Android, "app.gradle");
272+
const userAppGradleFilePath = path.join(projectData.getAppResourcesDirectoryPath(), this.$devicePlatformsConstants.Android, "app.gradle");
273273

274274
try {
275275
shell.sed('-i', /__PACKAGE__/, projectData.projectId, userAppGradleFilePath);
@@ -391,7 +391,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
391391
}
392392

393393
public ensureConfigurationFileInAppResources(projectData: IProjectData): void {
394-
const originalAndroidManifestFilePath = path.join(projectData.appResourcesDirectoryPath, this.$devicePlatformsConstants.Android, this.getPlatformData(projectData).configurationFileName);
394+
const originalAndroidManifestFilePath = path.join(projectData.getAppResourcesDirectoryPath(), this.$devicePlatformsConstants.Android, this.getPlatformData(projectData).configurationFileName);
395395

396396
const manifestExists = this.$fs.exists(originalAndroidManifestFilePath);
397397

lib/services/app-files-updater.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ export class AppFilesUpdater {
5454

5555
protected readSourceDir(): string[] {
5656
const tnsDir = path.join(this.appSourceDirectoryPath, constants.TNS_MODULES_FOLDER_NAME);
57-
return this.fs.enumerateFilesInDirectorySync(this.appSourceDirectoryPath, null, { includeEmptyDirectories: true }).filter(dirName => dirName !== tnsDir);
57+
const defaultAppResourcesDir = path.join(this.appSourceDirectoryPath, constants.APP_RESOURCES_FOLDER_NAME);
58+
return this.fs.enumerateFilesInDirectorySync(this.appSourceDirectoryPath, null, { includeEmptyDirectories: true }).filter(dirName => dirName !== tnsDir).filter(dirName => !dirName.startsWith(defaultAppResourcesDir));
5859
}
5960

6061
protected resolveAppSourceFiles(): string[] {
61-
// Copy all files from app dir, but make sure to exclude tns_modules
62+
// Copy all files from app dir, but make sure to exclude tns_modules and App_Resources
6263
let sourceFiles = this.readSourceDir();
6364

6465
if (this.options.release) {

lib/services/ios-entitlements-service.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as path from "path";
2-
import * as constants from "../constants";
32
import { PlistSession } from "plist-merge-patch";
43

54
export class IOSEntitlementsService {
@@ -14,8 +13,7 @@ export class IOSEntitlementsService {
1413

1514
private getDefaultAppEntitlementsPath(projectData: IProjectData) : string {
1615
const entitlementsName = IOSEntitlementsService.DefaultEntitlementsName;
17-
const entitlementsPath = path.join(projectData.projectDir,
18-
constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME,
16+
const entitlementsPath = path.join(projectData.getAppResourcesDirectoryPath(),
1917
this.$mobileHelper.normalizePlatformName(this.$devicePlatformsConstants.iOS),
2018
entitlementsName);
2119
return entitlementsPath;

lib/services/ios-project-service.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
765765

766766
private getInfoPlistPath(projectData: IProjectData): string {
767767
return path.join(
768-
projectData.projectDir,
769-
constants.APP_FOLDER_NAME,
770-
constants.APP_RESOURCES_FOLDER_NAME,
768+
projectData.getAppResourcesDirectoryPath(),
771769
this.getPlatformData(projectData).normalizedPlatformName,
772770
this.getPlatformData(projectData).configurationFileName
773771
);
@@ -787,7 +785,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
787785

788786
private async mergeInfoPlists(buildOptions: IRelease, projectData: IProjectData): Promise<void> {
789787
const projectDir = projectData.projectDir;
790-
const infoPlistPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME, this.getPlatformData(projectData).normalizedPlatformName, this.getPlatformData(projectData).configurationFileName);
788+
const infoPlistPath = path.join(projectData.getAppResourcesDirectoryPath(), this.getPlatformData(projectData).normalizedPlatformName, this.getPlatformData(projectData).configurationFileName);
791789
this.ensureConfigurationFileInAppResources();
792790

793791
if (!this.$fs.exists(infoPlistPath)) {
@@ -1217,7 +1215,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
12171215
}
12181216
}
12191217

1220-
const appResourcesXcconfigPath = path.join(projectData.projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME, this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
1218+
const appResourcesXcconfigPath = path.join(projectData.getAppResourcesDirectoryPath(), this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
12211219
if (this.$fs.exists(appResourcesXcconfigPath)) {
12221220
await this.mergeXcconfigFiles(appResourcesXcconfigPath, pluginsXcconfigFilePath);
12231221
}
@@ -1272,7 +1270,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
12721270
}
12731271

12741272
private getBuildXCConfigFilePath(projectData: IProjectData): string {
1275-
const buildXCConfig = path.join(projectData.appResourcesDirectoryPath,
1273+
const buildXCConfig = path.join(projectData.getAppResourcesDirectoryPath(),
12761274
this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
12771275
return buildXCConfig;
12781276
}
@@ -1334,7 +1332,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
13341332
const choicePersist = await this.$prompter.promptForChoice("Do you want to make teamId: " + teamId + " a persistent choice for your app?", choicesPersist);
13351333
switch (choicesPersist.indexOf(choicePersist)) {
13361334
case 0:
1337-
const xcconfigFile = path.join(projectData.appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
1335+
const xcconfigFile = path.join(projectData.getAppResourcesDirectoryPath(), this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
13381336
this.$fs.appendFile(xcconfigFile, "\nDEVELOPMENT_TEAM = " + teamId + "\n");
13391337
break;
13401338
case 1:
@@ -1352,8 +1350,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
13521350
}
13531351

13541352
private validateApplicationIdentifier(projectData: IProjectData): void {
1355-
const projectDir = projectData.projectDir;
1356-
const infoPlistPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME, this.getPlatformData(projectData).normalizedPlatformName, this.getPlatformData(projectData).configurationFileName);
1353+
const infoPlistPath = path.join(projectData.getAppResourcesDirectoryPath(), this.getPlatformData(projectData).normalizedPlatformName, this.getPlatformData(projectData).configurationFileName);
13571354
const mergedPlistPath = this.getPlatformData(projectData).configurationFilePath;
13581355

13591356
if (!this.$fs.exists(infoPlistPath) || !this.$fs.exists(mergedPlistPath)) {

lib/services/livesync/livesync-service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
537537
}
538538
}
539539

540+
pattern.push(projectData.getAppResourcesDirectoryPath());
541+
540542
const currentWatcherInfo = this.liveSyncProcessesInfo[liveSyncData.projectDir].watcherInfo;
541543
const areWatcherPatternsDifferent = () => _.xor(currentWatcherInfo.patterns, patterns).length;
542544
if (!currentWatcherInfo || areWatcherPatternsDifferent()) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class PreparePlatformJSService extends PreparePlatformService implements
3636
public async preparePlatform(config: IPreparePlatformJSInfo): Promise<void> {
3737
if (!config.changesInfo || config.changesInfo.appFilesChanged || config.changesInfo.changesRequirePrepare) {
3838
await this.copyAppFiles(config);
39+
this.copyAppResourcesFiles(config);
3940
}
4041

4142
if (config.changesInfo && !config.changesInfo.changesRequirePrepare) {
@@ -101,6 +102,13 @@ export class PreparePlatformJSService extends PreparePlatformService implements
101102
this.$errors.failWithoutHelp(`Processing node_modules failed. ${error}`);
102103
}
103104
}
105+
106+
private copyAppResourcesFiles(config: IPreparePlatformJSInfo) {
107+
const appDestinationDirectoryPath = path.join(config.platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
108+
const appResourcesSourcePath = config.projectData.getAppResourcesDirectoryPath();
109+
110+
shell.cp("-Rf", appResourcesSourcePath, appDestinationDirectoryPath);
111+
}
104112
}
105113

106114
$injector.register("preparePlatformJSService", PreparePlatformJSService);

0 commit comments

Comments
 (0)