Skip to content

Add support for Custom entitlements files #2753

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 20 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ node_js:
git:
submodules: true
before_script:
- gem install xcodeproj
- gem install cocoapods
- npm install grunt
- node_modules/.bin/grunt enableScripts:false
- grunt rebuild
Expand Down
2 changes: 2 additions & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ $injector.require("projectData", "./project-data");
$injector.require("projectDataService", "./services/project-data-service");
$injector.requirePublic("projectService", "./services/project-service");
$injector.require("androidProjectService", "./services/android-project-service");
$injector.require("iOSEntitlementsService", "./services/ios-entitlements-service");
$injector.require("iOSProjectService", "./services/ios-project-service");
$injector.require("iOSProvisionService", "./services/ios-provision-service");
$injector.require("xCConfigService", "./services/xcconfig-service");

$injector.require("cocoapodsService", "./services/cocoapods-service");

Expand Down
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const TEST_RUNNER_NAME = "nativescript-unit-test-runner";
export const LIVESYNC_EXCLUDED_FILE_PATTERNS = ["**/*.js.map", "**/*.ts"];
export const XML_FILE_EXTENSION = ".xml";
export const PLATFORMS_DIR_NAME = "platforms";
export const CODE_SIGN_ENTITLEMENTS = "CODE_SIGN_ENTITLEMENTS";

export class PackageVersion {
static NEXT = "next";
Expand Down
72 changes: 72 additions & 0 deletions lib/services/ios-entitlements-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as path from "path";
import * as constants from "../constants";
import { PlistSession } from "plist-merge-patch";

export class IOSEntitlementsService {
constructor(private $fs: IFileSystem,
private $logger: ILogger,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $mobileHelper: Mobile.IMobileHelper,
private $pluginsService: IPluginsService) {
}

public static readonly DefaultEntitlementsName: string = "app.entitlements";

private getDefaultAppEntitlementsPath(projectData: IProjectData) : string {
const entitlementsName = IOSEntitlementsService.DefaultEntitlementsName;
const entitlementsPath = path.join(projectData.projectDir,
constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME,
this.$mobileHelper.normalizePlatformName(this.$devicePlatformsConstants.iOS),
entitlementsName);
return entitlementsPath;
}

public getPlatformsEntitlementsPath(projectData: IProjectData) : string {
return path.join(projectData.platformsDir, this.$devicePlatformsConstants.iOS,
projectData.projectName, projectData.projectName + ".entitlements");
}
public getPlatformsEntitlementsRelativePath(projectData: IProjectData): string {
return path.join(projectData.projectName, projectData.projectName + ".entitlements");
}

public async merge(projectData: IProjectData): Promise<void> {
let session = new PlistSession({ log: (txt: string) => this.$logger.trace("App.entitlements: " + txt) });

let projectDir = projectData.projectDir;
let makePatch = (plistPath: string) => {
if (!this.$fs.exists(plistPath)) {
this.$logger.trace("No plist found at: " + plistPath);
return;
}

this.$logger.trace("Schedule merge plist at: " + plistPath);
session.patch({
name: path.relative(projectDir, plistPath),
read: () => this.$fs.readText(plistPath)
});
};

let allPlugins = await this.getAllInstalledPlugins(projectData);
for (let plugin of allPlugins) {
let pluginInfoPlistPath = path.join(plugin.pluginPlatformsFolderPath(this.$devicePlatformsConstants.iOS),
IOSEntitlementsService.DefaultEntitlementsName);
makePatch(pluginInfoPlistPath);
}

let appEntitlementsPath = this.getDefaultAppEntitlementsPath(projectData);
if (this.$fs.exists(appEntitlementsPath)) {
makePatch(appEntitlementsPath);
}

let plistContent = session.build();
this.$logger.trace("App.entitlements: Write to: " + this.getPlatformsEntitlementsPath(projectData));
this.$fs.writeFile(this.getPlatformsEntitlementsPath(projectData), plistContent);
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you do not need this return

}

private getAllInstalledPlugins(projectData: IProjectData): Promise<IPluginData[]> {
return this.$pluginsService.getAllInstalledPlugins(projectData);
}
}

$injector.register("iOSEntitlementsService", IOSEntitlementsService);
70 changes: 37 additions & 33 deletions lib/services/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { EOL } from "os";
import * as temp from "temp";
import * as plist from "plist";
import { IOSProvisionService } from "./ios-provision-service";
import { IOSEntitlementsService } from "./ios-entitlements-service";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the practice is to ad interface for the service and use the interface in the constructor instead of importing the Type from another file. However I do not insist on using this approach strictly.

import { XCConfigService } from "./xcconfig-service";

export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServiceBase implements IPlatformProjectService {
private static XCODE_PROJECT_EXT_NAME = ".xcodeproj";
Expand Down Expand Up @@ -40,9 +42,11 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
private $pluginVariablesService: IPluginVariablesService,
private $xcprojService: IXcprojService,
private $iOSProvisionService: IOSProvisionService,
private $sysInfo: ISysInfo,
private $pbxprojDomXcode: IPbxprojDomXcode,
private $xcode: IXcode) {
private $xcode: IXcode,
private $iOSEntitlementsService: IOSEntitlementsService,
private $sysInfo: ISysInfo,
private $xCConfigService: XCConfigService) {
super($fs, $projectDataService);
}

Expand Down Expand Up @@ -668,6 +672,7 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f

public async processConfigurationFilesFromAppResources(release: boolean, projectData: IProjectData): Promise<void> {
await this.mergeInfoPlists(projectData);
await this.$iOSEntitlementsService.merge(projectData);
await this.mergeProjectXcconfigFiles(release, projectData);
for (let pluginData of await this.getAllInstalledPlugins(projectData)) {
await this.$pluginVariablesService.interpolatePluginVariables(pluginData, this.getPlatformData(projectData).configurationFilePath, projectData);
Expand Down Expand Up @@ -1085,20 +1090,33 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
}

private async mergeProjectXcconfigFiles(release: boolean, projectData: IProjectData): Promise<void> {
this.$fs.deleteFile(release ? this.getPluginsReleaseXcconfigFilePath(projectData) : this.getPluginsDebugXcconfigFilePath(projectData));
let pluginsXcconfigFilePath = release ? this.getPluginsReleaseXcconfigFilePath(projectData) : this.getPluginsDebugXcconfigFilePath(projectData);
this.$fs.deleteFile(pluginsXcconfigFilePath);

let allPlugins: IPluginData[] = await (<IPluginsService>this.$injector.resolve("pluginsService")).getAllInstalledPlugins(projectData);
for (let plugin of allPlugins) {
let pluginPlatformsFolderPath = plugin.pluginPlatformsFolderPath(IOSProjectService.IOS_PLATFORM_NAME);
let pluginXcconfigFilePath = path.join(pluginPlatformsFolderPath, "build.xcconfig");
if (this.$fs.exists(pluginXcconfigFilePath)) {
await this.mergeXcconfigFiles(pluginXcconfigFilePath, release ? this.getPluginsReleaseXcconfigFilePath(projectData) : this.getPluginsDebugXcconfigFilePath(projectData));
await this.mergeXcconfigFiles(pluginXcconfigFilePath, pluginsXcconfigFilePath);
}
}

let appResourcesXcconfigPath = path.join(projectData.projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME, this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
if (this.$fs.exists(appResourcesXcconfigPath)) {
await this.mergeXcconfigFiles(appResourcesXcconfigPath, release ? this.getPluginsReleaseXcconfigFilePath(projectData) : this.getPluginsDebugXcconfigFilePath(projectData));
await this.mergeXcconfigFiles(appResourcesXcconfigPath, pluginsXcconfigFilePath);
}

// Set Entitlements Property to point to default file if not set explicitly by the user.
let entitlementsPropertyValue = this.$xCConfigService.readPropertyValue(pluginsXcconfigFilePath, constants.CODE_SIGN_ENTITLEMENTS);
if (entitlementsPropertyValue === null) {
temp.track();
const tempEntitlementsDir = temp.mkdirSync("entitlements");
const tempEntitlementsFilePath = path.join(tempEntitlementsDir, "set-entitlements.xcconfig");
const entitlementsRelativePath = this.$iOSEntitlementsService.getPlatformsEntitlementsRelativePath(projectData);
this.$fs.writeFile(tempEntitlementsFilePath, `CODE_SIGN_ENTITLEMENTS = ${entitlementsRelativePath}${EOL}`);

await this.mergeXcconfigFiles(tempEntitlementsFilePath, pluginsXcconfigFilePath);
}

let podFilesRootDirName = path.join("Pods", "Target Support Files", `Pods-${projectData.projectName}`);
Expand Down Expand Up @@ -1178,51 +1196,37 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
return null;
}

private readXCConfig(flag: string, projectData: IProjectData): string {
let xcconfigFile = path.join(projectData.appResourcesDirectoryPath, this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
if (this.$fs.exists(xcconfigFile)) {
let text = this.$fs.readText(xcconfigFile);
let teamId: string;
text.split(/\r?\n/).forEach((line) => {
line = line.replace(/\/(\/)[^\n]*$/, "");
if (line.indexOf(flag) >= 0) {
teamId = line.split("=")[1].trim();
if (teamId[teamId.length - 1] === ';') {
teamId = teamId.slice(0, -1);
}
}
});
if (teamId) {
return teamId;
}
}
private getBuildXCConfigFilePath(projectData: IProjectData): string {
let buildXCConfig = path.join(projectData.appResourcesDirectoryPath,
this.getPlatformData(projectData).normalizedPlatformName, "build.xcconfig");
return buildXCConfig;
}

private readTeamId(projectData: IProjectData): string {
let teamId = this.$xCConfigService.readPropertyValue(this.getBuildXCConfigFilePath(projectData), "DEVELOPMENT_TEAM");

let fileName = path.join(this.getPlatformData(projectData).projectRoot, "teamid");
if (this.$fs.exists(fileName)) {
return this.$fs.readText(fileName);
teamId = this.$fs.readText(fileName);
}

return null;
}

private readTeamId(projectData: IProjectData): string {
return this.readXCConfig("DEVELOPMENT_TEAM", projectData);
return teamId;
}

private readXCConfigProvisioningProfile(projectData: IProjectData): string {
return this.readXCConfig("PROVISIONING_PROFILE", projectData);
return this.$xCConfigService.readPropertyValue(this.getBuildXCConfigFilePath(projectData), "PROVISIONING_PROFILE");
}

private readXCConfigProvisioningProfileForIPhoneOs(projectData: IProjectData): string {
return this.readXCConfig("PROVISIONING_PROFILE[sdk=iphoneos*]", projectData);
return this.$xCConfigService.readPropertyValue(this.getBuildXCConfigFilePath(projectData), "PROVISIONING_PROFILE[sdk=iphoneos*]");
}

private readXCConfigProvisioningProfileSpecifier(projectData: IProjectData): string {
return this.readXCConfig("PROVISIONING_PROFILE_SPECIFIER", projectData);
return this.$xCConfigService.readPropertyValue(this.getBuildXCConfigFilePath(projectData), "PROVISIONING_PROFILE_SPECIFIER");
}

private readXCConfigProvisioningProfileSpecifierForIPhoneOs(projectData: IProjectData): string {
return this.readXCConfig("PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]", projectData);
return this.$xCConfigService.readPropertyValue(this.getBuildXCConfigFilePath(projectData), "PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]");
}

private async getDevelopmentTeam(projectData: IProjectData, teamId?: string): Promise<string> {
Expand Down
40 changes: 40 additions & 0 deletions lib/services/xcconfig-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export class XCConfigService {
constructor(private $fs: IFileSystem) {
}

/**
* Returns the Value of a Property from a XC Config file.
* @param xcconfigFilePath
* @param propertyName
*/
public readPropertyValue(xcconfigFilePath: string, propertyName: string): string {
if (this.$fs.exists(xcconfigFilePath)) {
let text = this.$fs.readText(xcconfigFilePath);

let property: string;
let isPropertyParsed: boolean = false;
text.split(/\r?\n/).forEach((line) => {
line = line.replace(/\/(\/)[^\n]*$/, "");
if (line.indexOf(propertyName) >= 0) {
let parts = line.split("=");
if (parts.length > 1 && parts[1]) {
property = parts[1].trim();
isPropertyParsed = true;
if (property[property.length - 1] === ';') {
property = property.slice(0, -1);
}
}
}
});

if (isPropertyParsed) {
// property can be an empty string, so we don't check for that.
return property;
}
}

return null;
}
}

$injector.register("xCConfigService", XCConfigService);
Loading