Skip to content

feat: Expose paths to project configuration files in projectData #3478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export const SRC_DIR = "src";
export const MAIN_DIR = "main";
export const ASSETS_DIR = "assets";
export const MANIFEST_FILE_NAME = "AndroidManifest.xml";
export const APP_GRADLE_FILE_NAME = "app.gradle";
export const INFO_PLIST_FILE_NAME = "Info.plist";
export const INCLUDE_GRADLE_NAME = "include.gradle";
export const BUILD_XCCONFIG_FILE_NAME = "build.xcconfig";
export const BUILD_DIR = "build";
export const OUTPUTS_DIR = "outputs";
export const APK_DIR = "apk";
Expand Down
6 changes: 6 additions & 0 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ interface IProjectData extends IProjectDir {
appResourcesDirectoryPath: string;
projectType: string;
nsConfig: INsConfig;
androidManifestPath: string;
appGradlePath: string;
gradleFilesDirectoryPath: string;
infoPlistPath: string;
buildXcconfigPath: string;

/**
* Initializes project data with the given project directory. If none supplied defaults to --path option or cwd.
* @param {string} projectDir Project root directory.
Expand Down
23 changes: 22 additions & 1 deletion lib/project-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ export class ProjectData implements IProjectData {
public dependencies: any;
public devDependencies: IStringDictionary;
public projectType: string;
public androidManifestPath: string;
public infoPlistPath: string;
public appGradlePath: string;
public gradleFilesDirectoryPath: string;
public buildXcconfigPath: string;

constructor(private $fs: IFileSystem,
private $errors: IErrors,
private $projectHelper: IProjectHelper,
private $staticConfig: IStaticConfig,
private $options: IOptions,
private $logger: ILogger) { }
private $logger: ILogger,
private $androidResourcesMigrationService: IAndroidResourcesMigrationService,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants) { }

public initializeProjectData(projectDir?: string): void {
projectDir = projectDir || this.$projectHelper.projectDir;
Expand Down Expand Up @@ -108,13 +115,27 @@ export class ProjectData implements IProjectData {
this.nsConfig = nsConfig;
this.appDirectoryPath = this.getAppDirectoryPath();
this.appResourcesDirectoryPath = this.getAppResourcesDirectoryPath();
this.androidManifestPath = this.getPathToAndroidManifest(this.appResourcesDirectoryPath);
this.gradleFilesDirectoryPath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.Android);
this.appGradlePath = path.join(this.gradleFilesDirectoryPath, constants.APP_GRADLE_FILE_NAME);
this.infoPlistPath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.INFO_PLIST_FILE_NAME);
this.buildXcconfigPath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.BUILD_XCCONFIG_FILE_NAME);

return;
}

this.errorInvalidProject(projectDir);
}

private getPathToAndroidManifest(appResourcesDir: string): string {
const androidDirPath = path.join(appResourcesDir, this.$devicePlatformsConstants.Android);
const androidManifestDir = this.$androidResourcesMigrationService.hasMigrated(appResourcesDir) ?
path.join(androidDirPath, constants.SRC_DIR, constants.MAIN_DIR) :
androidDirPath;

return path.join(androidManifestDir, constants.MANIFEST_FILE_NAME);
}

private errorInvalidProject(projectDir: string): void {
const currentDir = path.resolve(".");
this.$logger.trace(`Unable to find project. projectDir: ${projectDir}, options.path: ${this.$options.path}, ${currentDir}`);
Expand Down
8 changes: 3 additions & 5 deletions lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,9 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
const gradleSettingsFilePath = path.join(this.getPlatformData(projectData).projectRoot, "settings.gradle");
shell.sed('-i', /__PROJECT_NAME__/, this.getProjectNameFromId(projectData), gradleSettingsFilePath);

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

try {
shell.sed('-i', /__PACKAGE__/, projectData.projectId, userAppGradleFilePath);
// will replace applicationId in app/App_Resources/Android/app.gradle if it has not been edited by the user
shell.sed('-i', /__PACKAGE__/, projectData.projectId, projectData.appGradlePath);
} catch (e) {
this.$logger.warn(`\n${e}.\nCheck if you're using an outdated template and update it.`);
}
Expand Down Expand Up @@ -520,7 +518,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
}

// Copy include.gradle file
const includeGradleFilePath = path.join(pluginPlatformsFolderPath, "include.gradle");
const includeGradleFilePath = path.join(pluginPlatformsFolderPath, constants.INCLUDE_GRADLE_NAME);
if (this.$fs.exists(includeGradleFilePath)) {
shell.cp("-f", includeGradleFilePath, pluginConfigurationDirectoryPath);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/services/android-resources-migration-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class AndroidResourcesMigrationService implements IAndroidResourcesMigrat
const getDirectories = (files: string[]) => files.filter(isDirectory);
const getFiles = (files: string[]) => files.filter((file: string) => !isDirectory(file));

this.$fs.copyFile(path.join(originalAppResources, "app.gradle"), path.join(appResourcesDestination, "app.gradle"));
this.$fs.copyFile(path.join(originalAppResources, constants.APP_GRADLE_FILE_NAME), path.join(appResourcesDestination, constants.APP_GRADLE_FILE_NAME));

const appResourcesFiles = getAllFiles(originalAppResources);
const resourceDirectories = getDirectories(appResourcesFiles);
Expand Down
17 changes: 9 additions & 8 deletions lib/services/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { XCConfigService } from "./xcconfig-service";
import * as simplePlist from "simple-plist";
import * as mobileprovision from "ios-mobileprovision-finder";
import { SpawnOptions } from "child_process";
import { BUILD_XCCONFIG_FILE_NAME } from "../constants";

export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServiceBase implements IPlatformProjectService {
private static XCODE_PROJECT_EXT_NAME = ".xcodeproj";
Expand Down Expand Up @@ -89,8 +90,8 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
frameworkDirectoriesExtensions: [".framework"],
frameworkDirectoriesNames: ["Metadata", "metadataGenerator", "NativeScript", "internal"],
targetedOS: ['darwin'],
configurationFileName: "Info.plist",
configurationFilePath: path.join(projectRoot, projectData.projectName, projectData.projectName + "-Info.plist"),
configurationFileName: constants.INFO_PLIST_FILE_NAME,
configurationFilePath: path.join(projectRoot, projectData.projectName, projectData.projectName + `-${constants.INFO_PLIST_FILE_NAME}`),
relativeToFrameworkConfigurationFilePath: path.join("__PROJECT_NAME__", "__PROJECT_NAME__-Info.plist"),
fastLivesyncFileExtensions: [".tiff", ".tif", ".jpg", "jpeg", "gif", ".png", ".bmp", ".BMPf", ".ico", ".cur", ".xbm"] // https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/
};
Expand Down Expand Up @@ -338,7 +339,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
// Starting from tns-ios 1.4 the xcconfig file is referenced in the project template
const frameworkVersion = this.getFrameworkVersion(this.getPlatformData(projectData).frameworkPackageName, projectData.projectDir);
if (semver.lt(frameworkVersion, "1.4.0")) {
basicArgs.push("-xcconfig", path.join(projectRoot, projectData.projectName, "build.xcconfig"));
basicArgs.push("-xcconfig", path.join(projectRoot, projectData.projectName, BUILD_XCCONFIG_FILE_NAME));
}

// if (this.$logger.getLevel() === "INFO") {
Expand Down Expand Up @@ -1039,7 +1040,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
}

private validateFramework(libraryPath: string): void {
const infoPlistPath = path.join(libraryPath, "Info.plist");
const infoPlistPath = path.join(libraryPath, constants.INFO_PLIST_FILE_NAME);
if (!this.$fs.exists(infoPlistPath)) {
this.$errors.failWithoutHelp("The bundle at %s does not contain an Info.plist file.", libraryPath);
}
Expand Down Expand Up @@ -1227,13 +1228,13 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
const allPlugins: IPluginData[] = await (<IPluginsService>this.$injector.resolve("pluginsService")).getAllInstalledPlugins(projectData);
for (const plugin of allPlugins) {
const pluginPlatformsFolderPath = plugin.pluginPlatformsFolderPath(IOSProjectService.IOS_PLATFORM_NAME);
const pluginXcconfigFilePath = path.join(pluginPlatformsFolderPath, "build.xcconfig");
const pluginXcconfigFilePath = path.join(pluginPlatformsFolderPath, BUILD_XCCONFIG_FILE_NAME);
if (this.$fs.exists(pluginXcconfigFilePath)) {
await this.mergeXcconfigFiles(pluginXcconfigFilePath, pluginsXcconfigFilePath);
}
}

const appResourcesXcconfigPath = path.join(projectData.appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
const appResourcesXcconfigPath = path.join(projectData.appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, BUILD_XCCONFIG_FILE_NAME);
if (this.$fs.exists(appResourcesXcconfigPath)) {
await this.mergeXcconfigFiles(appResourcesXcconfigPath, pluginsXcconfigFilePath);
}
Expand Down Expand Up @@ -1289,7 +1290,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f

private getBuildXCConfigFilePath(projectData: IProjectData): string {
const buildXCConfig = path.join(projectData.appResourcesDirectoryPath,
this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
this.getPlatformData(projectData).normalizedPlatformName, BUILD_XCCONFIG_FILE_NAME);
return buildXCConfig;
}

Expand Down Expand Up @@ -1350,7 +1351,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
const choicePersist = await this.$prompter.promptForChoice("Do you want to make teamId: " + teamId + " a persistent choice for your app?", choicesPersist);
switch (choicesPersist.indexOf(choicePersist)) {
case 0:
const xcconfigFile = path.join(projectData.appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
const xcconfigFile = path.join(projectData.appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, BUILD_XCCONFIG_FILE_NAME);
this.$fs.appendFile(xcconfigFile, "\nDEVELOPMENT_TEAM = " + teamId + "\n");
break;
case 1:
Expand Down
4 changes: 2 additions & 2 deletions lib/services/itmstransporter-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from "path";
import * as temp from "temp";
import { EOL } from "os";
import { ITMSConstants } from "../constants";
import { ITMSConstants, INFO_PLIST_FILE_NAME } from "../constants";
import { ItunesConnectApplicationTypes } from "../constants";
import { quoteString, versionCompare } from "../common/helpers";

Expand Down Expand Up @@ -135,7 +135,7 @@ export class ITMSTransporterService implements IITMSTransporterService {
}
const appFile = path.join(payloadDir, allApps[0]);

const plistObject = await this.$bplistParser.parseFile(path.join(appFile, "Info.plist"));
const plistObject = await this.$bplistParser.parseFile(path.join(appFile, INFO_PLIST_FILE_NAME));
const bundleId = plistObject && plistObject[0] && plistObject[0].CFBundleIdentifier;
if (!bundleId) {
this.$errors.failWithoutHelp(`Unable to determine bundle identifier from ${ipaFileFullPath}.`);
Expand Down
6 changes: 3 additions & 3 deletions lib/services/project-changes-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from "path";
import { NODE_MODULES_FOLDER_NAME, NativePlatformStatus, PACKAGE_JSON_FILE_NAME } from "../constants";
import { NODE_MODULES_FOLDER_NAME, NativePlatformStatus, PACKAGE_JSON_FILE_NAME, APP_GRADLE_FILE_NAME, BUILD_XCCONFIG_FILE_NAME } from "../constants";
import { getHash } from "../common/helpers";

const prepareInfoFileName = ".nsprepareinfo";
Expand Down Expand Up @@ -77,12 +77,12 @@ export class ProjectChangesService implements IProjectChangesService {
if (platform === this.$devicePlatformsConstants.iOS.toLowerCase()) {
this._changesInfo.configChanged = this.filesChanged([path.join(platformResourcesDir, platformData.configurationFileName),
path.join(platformResourcesDir, "LaunchScreen.storyboard"),
path.join(platformResourcesDir, "build.xcconfig")
path.join(platformResourcesDir, BUILD_XCCONFIG_FILE_NAME)
]);
} else {
this._changesInfo.configChanged = this.filesChanged([
path.join(platformResourcesDir, platformData.configurationFileName),
path.join(platformResourcesDir, "app.gradle")
path.join(platformResourcesDir, APP_GRADLE_FILE_NAME)
]);
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { NodePackageManager } from "../lib/node-package-manager";
import { assert } from "chai";
import { IOSProvisionService } from "../lib/services/ios-provision-service";
import { SettingsService } from "../lib/common/test/unit-tests/stubs";
import { BUILD_XCCONFIG_FILE_NAME } from "../lib/constants";
import { ProjectDataStub } from "./stubs";
import temp = require("temp");

Expand Down Expand Up @@ -800,7 +801,7 @@ describe("Merge Project XCConfig files", () => {

iOSEntitlementsService = testInjector.resolve("iOSEntitlementsService");

appResourcesXcconfigPath = path.join(projectData.appResourcesDirectoryPath, "iOS", "build.xcconfig");
appResourcesXcconfigPath = path.join(projectData.appResourcesDirectoryPath, "iOS", BUILD_XCCONFIG_FILE_NAME);
appResourceXCConfigContent = `CODE_SIGN_IDENTITY = iPhone Distribution
// 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
// DEVELOPMENT_TEAM = YOUR_TEAM_ID;
Expand Down
3 changes: 2 additions & 1 deletion test/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as ChildProcessLib from "../lib/common/child-process";
import ProjectChangesLib = require("../lib/services/project-changes-service");
import { Messages } from "../lib/common/messages/messages";
import { SettingsService } from "../lib/common/test/unit-tests/stubs";
import { INFO_PLIST_FILE_NAME, MANIFEST_FILE_NAME } from "../lib/constants";

require("should");
const temp = require("temp");
Expand Down Expand Up @@ -425,7 +426,7 @@ describe('Platform Service Tests', () => {
appDestinationDirectoryPath: testDirData.appDestFolderPath,
appResourcesDestinationDirectoryPath: testDirData.appResourcesFolderPath,
normalizedPlatformName: platformToTest,
configurationFileName: platformToTest === "ios" ? "Info.plist" : "AndroidManifest.xml",
configurationFileName: platformToTest === "ios" ? INFO_PLIST_FILE_NAME : MANIFEST_FILE_NAME,
projectRoot: testDirData.tempFolder,
platformProjectService: {
prepareProject: (): any => null,
Expand Down
5 changes: 5 additions & 0 deletions test/plugin-variables-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { StaticConfig } from "../lib/config";
import { MessagesService } from "../lib/common/services/messages-service";
import { Yok } from '../lib/common/yok';
import { SettingsService } from "../lib/common/test/unit-tests/stubs";
import { DevicePlatformsConstants } from "../lib/common/mobile/device-platforms-constants";
import * as stubs from './stubs';
import * as path from "path";
import * as temp from "temp";
Expand Down Expand Up @@ -39,6 +40,10 @@ function createTestInjector(): IInjector {
});
testInjector.register("staticConfig", StaticConfig);
testInjector.register("settingsService", SettingsService);
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
testInjector.register("androidResourcesMigrationService", {
hasMigrated: () => true
});

return testInjector;
}
Expand Down
7 changes: 7 additions & 0 deletions test/project-data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ProjectData } from "../lib/project-data";
import { Yok } from "../lib/common/yok";
import { DevicePlatformsConstants } from "../lib/common/mobile/device-platforms-constants";
import { assert } from "chai";
import * as stubs from "./stubs";
import * as path from "path";
Expand Down Expand Up @@ -30,6 +31,12 @@ describe("projectData", () => {

testInjector.register("options", {});

testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);

testInjector.register("androidResourcesMigrationService", {
hasMigrated: () => true
});

testInjector.register("projectData", ProjectData);

return testInjector;
Expand Down
5 changes: 3 additions & 2 deletions test/services/android-plugin-build-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HostInfo } from "../../lib/common/host-info";
import { Logger } from "../../lib/common/logger";
import * as ErrorsLib from "../../lib/common/errors";
import temp = require("temp");
import { INCLUDE_GRADLE_NAME } from "../../lib/constants";
temp.track();

describe('androiPluginBuildService', () => {
Expand Down Expand Up @@ -65,7 +66,7 @@ dependencies {
compile "com.android.support:design:$supportVersion"
}`;

fs.writeFile(path.join(pluginFolder, "include.gradle"), validIncludeGradleContent);
fs.writeFile(path.join(pluginFolder, INCLUDE_GRADLE_NAME), validIncludeGradleContent);
}

function setUpPluginNativeFolder(manifestFile: boolean, resFolder: boolean, assetsFolder: boolean) {
Expand Down Expand Up @@ -196,7 +197,7 @@ dependencies {
tempPluginDirPath: pluginFolder
};

const includeGradleName = "include.gradle";
const includeGradleName = INCLUDE_GRADLE_NAME;
await androidBuildPluginService.migrateIncludeGradle(config);
const includeGradleContent = fs.readText(path.join(pluginFolder, includeGradleName).toString());
const productFlavorsAreRemoved = includeGradleContent.indexOf("productFlavors") === -1;
Expand Down
6 changes: 6 additions & 0 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ export class ProjectDataStub implements IProjectData {
devDependencies: IStringDictionary;
projectType: string;
appResourcesDirectoryPath: string;
public androidManifestPath: string;
public infoPlistPath: string;
public appGradlePath: string;
public gradleFilesDirectoryPath: string;
public buildXcconfigPath: string;

public initializeProjectData(projectDir?: string): void {
this.projectDir = this.projectDir || projectDir;
}
Expand Down