Skip to content

Commit 422835c

Browse files
committed
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 the 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 8125d4b commit 422835c

13 files changed

+49
-28
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: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as constants from "./constants";
22
import * as path from "path";
3+
import { cache } from "./common/decorators";
34
import { EOL } from "os";
45

56
interface IProjectType {
@@ -69,7 +70,6 @@ export class ProjectData implements IProjectData {
6970
this.platformsDir = path.join(projectDir, constants.PLATFORMS_DIR_NAME);
7071
this.projectFilePath = projectFilePath;
7172
this.appDirectoryPath = path.join(projectDir, constants.APP_FOLDER_NAME);
72-
this.appResourcesDirectoryPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
7373
this.projectId = data.id;
7474
this.dependencies = fileContent.dependencies;
7575
this.devDependencies = fileContent.devDependencies;
@@ -87,6 +87,28 @@ export class ProjectData implements IProjectData {
8787
this.$errors.fail("No project found at or above '%s' and neither was a --path specified.", projectDir || this.$options.path || currentDir);
8888
}
8989

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

lib/providers/project-files-provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.appDirectoryPath;
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(appResourcesDirectoryPath,
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
@@ -252,7 +252,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
252252
shell.sed('-i', /__PROJECT_NAME__/, this.getProjectNameFromId(projectData), gradleSettingsFilePath);
253253

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

257257
try {
258258
shell.sed('-i', /__PACKAGE__/, projectData.projectId, userAppGradleFilePath);
@@ -374,7 +374,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
374374
}
375375

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

379379
const manifestExists = this.$fs.exists(originalAndroidManifestFilePath);
380380

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
@@ -759,9 +759,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
759759

760760
private getInfoPlistPath(projectData: IProjectData): string {
761761
return path.join(
762-
projectData.projectDir,
763-
constants.APP_FOLDER_NAME,
764-
constants.APP_RESOURCES_FOLDER_NAME,
762+
projectData.getAppResourcesDirectoryPath(),
765763
this.getPlatformData(projectData).normalizedPlatformName,
766764
this.getPlatformData(projectData).configurationFileName
767765
);
@@ -781,7 +779,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
781779

782780
private async mergeInfoPlists(buildOptions: IRelease, projectData: IProjectData): Promise<void> {
783781
const projectDir = projectData.projectDir;
784-
const infoPlistPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME, this.getPlatformData(projectData).normalizedPlatformName, this.getPlatformData(projectData).configurationFileName);
782+
const infoPlistPath = path.join(projectData.getAppResourcesDirectoryPath(), this.getPlatformData(projectData).normalizedPlatformName, this.getPlatformData(projectData).configurationFileName);
785783
this.ensureConfigurationFileInAppResources();
786784

787785
if (!this.$fs.exists(infoPlistPath)) {
@@ -1211,7 +1209,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
12111209
}
12121210
}
12131211

1214-
const appResourcesXcconfigPath = path.join(projectData.projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME, this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
1212+
const appResourcesXcconfigPath = path.join(projectData.getAppResourcesDirectoryPath(), this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
12151213
if (this.$fs.exists(appResourcesXcconfigPath)) {
12161214
await this.mergeXcconfigFiles(appResourcesXcconfigPath, pluginsXcconfigFilePath);
12171215
}
@@ -1266,7 +1264,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
12661264
}
12671265

12681266
private getBuildXCConfigFilePath(projectData: IProjectData): string {
1269-
const buildXCConfig = path.join(projectData.appResourcesDirectoryPath,
1267+
const buildXCConfig = path.join(projectData.getAppResourcesDirectoryPath(),
12701268
this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
12711269
return buildXCConfig;
12721270
}
@@ -1328,7 +1326,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
13281326
const choicePersist = await this.$prompter.promptForChoice("Do you want to make teamId: " + teamId + " a persistent choice for your app?", choicesPersist);
13291327
switch (choicesPersist.indexOf(choicePersist)) {
13301328
case 0:
1331-
const xcconfigFile = path.join(projectData.appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
1329+
const xcconfigFile = path.join(projectData.getAppResourcesDirectoryPath(), this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
13321330
this.$fs.appendFile(xcconfigFile, "\nDEVELOPMENT_TEAM = " + teamId + "\n");
13331331
break;
13341332
case 1:
@@ -1346,8 +1344,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
13461344
}
13471345

13481346
private validateApplicationIdentifier(projectData: IProjectData): void {
1349-
const projectDir = projectData.projectDir;
1350-
const infoPlistPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME, this.getPlatformData(projectData).normalizedPlatformName, this.getPlatformData(projectData).configurationFileName);
1347+
const infoPlistPath = path.join(projectData.getAppResourcesDirectoryPath(), this.getPlatformData(projectData).normalizedPlatformName, this.getPlatformData(projectData).configurationFileName);
13511348
const mergedPlistPath = this.getPlatformData(projectData).configurationFilePath;
13521349

13531350
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
@@ -525,6 +525,8 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
525525
}
526526
}
527527

528+
pattern.push(projectData.getAppResourcesDirectoryPath());
529+
528530
const currentWatcherInfo = this.liveSyncProcessesInfo[liveSyncData.projectDir].watcherInfo;
529531

530532
if (!currentWatcherInfo || currentWatcherInfo.pattern !== pattern) {

lib/services/project-changes-service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export class ProjectChangesService implements IProjectChangesService {
6060
this._changesInfo = new ProjectChangesInfo();
6161
if (!this.ensurePrepareInfo(platform, projectData, projectChangesOptions)) {
6262
this._newFiles = 0;
63-
this._changesInfo.appFilesChanged = this.containsNewerFiles(projectData.appDirectoryPath, projectData.appResourcesDirectoryPath, projectData);
63+
this._changesInfo.appFilesChanged = this.containsNewerFiles(projectData.appDirectoryPath, projectData.getAppResourcesDirectoryPath(), projectData);
6464
this._changesInfo.packageChanged = this.isProjectFileChanged(projectData, platform);
65-
this._changesInfo.appResourcesChanged = this.containsNewerFiles(projectData.appResourcesDirectoryPath, null, projectData);
65+
this._changesInfo.appResourcesChanged = this.containsNewerFiles(projectData.getAppResourcesDirectoryPath(), null, projectData);
6666
/*done because currently all node_modules are traversed, a possible improvement could be traversing only the production dependencies*/
6767
this._changesInfo.nativeChanged = this.containsNewerFiles(
6868
path.join(projectData.projectDir, NODE_MODULES_FOLDER_NAME),
@@ -73,7 +73,7 @@ export class ProjectChangesService implements IProjectChangesService {
7373
if (this._newFiles > 0 || this._changesInfo.nativeChanged) {
7474
this._changesInfo.modulesChanged = true;
7575
}
76-
const platformResourcesDir = path.join(projectData.appResourcesDirectoryPath, platformData.normalizedPlatformName);
76+
const platformResourcesDir = path.join(projectData.getAppResourcesDirectoryPath(), platformData.normalizedPlatformName);
7777
if (platform === this.$devicePlatformsConstants.iOS.toLowerCase()) {
7878
this._changesInfo.configChanged = this.filesChanged([path.join(platformResourcesDir, platformData.configurationFileName),
7979
path.join(platformResourcesDir, "LaunchScreen.storyboard"),
@@ -282,7 +282,7 @@ export class ProjectChangesService implements IProjectChangesService {
282282
return true;
283283
}
284284
const projectDir = projectData.projectDir;
285-
if (_.startsWith(path.join(projectDir, file), projectData.appResourcesDirectoryPath)) {
285+
if (_.startsWith(path.join(projectDir, file), projectData.getAppResourcesDirectoryPath())) {
286286
return true;
287287
}
288288
if (_.startsWith(file, NODE_MODULES_FOLDER_NAME)) {

lib/services/project-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class ProjectService implements IProjectService {
112112

113113
private async ensureAppResourcesExist(projectDir: string): Promise<void> {
114114
const appPath = path.join(projectDir, constants.APP_FOLDER_NAME),
115-
appResourcesDestinationPath = path.join(appPath, constants.APP_RESOURCES_FOLDER_NAME);
115+
appResourcesDestinationPath = this.$projectData.getAppResourcesDirectoryPath(projectDir);
116116

117117
if (!this.$fs.exists(appResourcesDestinationPath)) {
118118
this.$fs.createDirectory(appResourcesDestinationPath);

test/ios-project-service.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import { Utils } from "../lib/common/utils";
2929
import { CocoaPodsService } from "../lib/services/cocoapods-service";
3030
import { NpmInstallationManager } from "../lib/npm-installation-manager";
3131
import { NodePackageManager } from "../lib/node-package-manager";
32-
import * as constants from "../lib/constants";
3332

3433
import { assert } from "chai";
3534
import { IOSProvisionService } from "../lib/services/ios-provision-service";
@@ -796,8 +795,7 @@ describe("Merge Project XCConfig files", () => {
796795

797796
iOSEntitlementsService = testInjector.resolve("iOSEntitlementsService");
798797

799-
appResourcesXcconfigPath = path.join(projectData.projectDir, constants.APP_FOLDER_NAME,
800-
constants.APP_RESOURCES_FOLDER_NAME, "iOS", "build.xcconfig");
798+
appResourcesXcconfigPath = path.join(projectData.getAppResourcesDirectoryPath(), "iOS", "build.xcconfig");
801799
appResourceXCConfigContent = `CODE_SIGN_IDENTITY = iPhone Distribution
802800
// To build for device with XCode 8 you need to specify your development team. More info: https://developer.apple.com/library/prerelease/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html
803801
// DEVELOPMENT_TEAM = YOUR_TEAM_ID;

test/platform-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ describe('Platform Service Tests', () => {
445445
const projectData = testInjector.resolve("projectData");
446446
projectData.projectDir = testDirData.tempFolder;
447447
projectData.appDirectoryPath = testDirData.appFolderPath;
448-
projectData.appResourcesDirectoryPath = path.join(testDirData.appFolderPath, "App_Resources");
448+
projectData.getAppResourcesDirectoryPath = () => path.join(testDirData.appFolderPath, "App_Resources");
449449
projectData.projectName = "app";
450450

451451
platformService = testInjector.resolve("platformService");

test/stubs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,14 @@ export class ProjectDataStub implements IProjectData {
248248
projectId: string;
249249
dependencies: any;
250250
appDirectoryPath: string;
251-
appResourcesDirectoryPath: string;
252251
devDependencies: IStringDictionary;
253252
projectType: string;
254253
initializeProjectData(projectDir?: string): void {
255254
this.projectDir = this.projectDir || projectDir;
256255
}
256+
getAppResourcesDirectoryPath(projectDir?: string): string {
257+
return this.projectDir + "/App_Resources";
258+
}
257259
}
258260

259261
export class PlatformProjectServiceStub extends EventEmitter implements IPlatformProjectService {

0 commit comments

Comments
 (0)