Skip to content
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

Enhanced pod file integration #369

Merged
merged 2 commits into from
Jul 30, 2018
Merged
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
43 changes: 28 additions & 15 deletions appcenter-link-scripts/src/ios/PodFile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const os = require('os');
const path = require('path');
const childProcess = require('child_process');

Expand Down Expand Up @@ -37,7 +38,7 @@ Podfile.prototype.addPodLine = function (pod, podspec, version) {
debug(`Replace ${existingLine} by ${line}`);
return;
}
const pattern = this.getTargetSectionPattern()[0];
const pattern = this.getTargetSectionPattern().match;
this.fileContents = this.fileContents.replace(pattern, `${pattern}\n ${line}`);
debug(`${line} ===> ${this.file}`);
};
Expand All @@ -60,8 +61,8 @@ Podfile.prototype.addMinimumDeploymentTarget = function (platform, version) {
Podfile.prototype.addPlatformToTarget = function (platform, version) {
const line = `platform :${platform}, '${version}'`;
const sectionStart = this.getTargetSectionPattern();
const keywordMatch = this.nextKeyword(this.fileContents, sectionStart.index);
const newLineIndex = this.fileContents.lastIndexOf('\n', keywordMatch.index);
const keywordMatch = this.nextKeyword(this.fileContents, sectionStart.index + 1);
const newLineIndex = this.fileContents.lastIndexOf(os.EOL, keywordMatch.index);
const part1 = this.fileContents.slice(0, newLineIndex);
const part2 = this.fileContents.slice(newLineIndex);
const indent = ' ';
Expand All @@ -88,8 +89,8 @@ Podfile.prototype.isGlobalPlatformDefined = function (platform) {
Podfile.prototype.isTargetPlatformDefined = function (platform) {
const platformPattern = new RegExp(`platform\\s+:${platform}`, 'g');
const patternIndex = this.getTargetSectionPattern().index;
const startSection = this.startOfSection(patternIndex) + 'target'.length;
const endSection = this.endOfSection(patternIndex);
const startSection = this.startOfSection(patternIndex);
const endSection = this.endOfSection(patternIndex + 'target'.length);
const sectionContent = this.fileContents.substring(startSection, endSection);
const scopeRanges = this.scopeRanges(sectionContent);
const platformMatches = sectionContent.match(platformPattern);
Expand All @@ -107,15 +108,25 @@ Podfile.prototype.isTargetPlatformDefined = function (platform) {
};

Podfile.prototype.getTargetSectionPattern = function () {
const patterns = this.fileContents.match(/# Pods for .*/);
if (patterns === null) {
throw new Error(`
Error: Could not find a "# Pods for" comment in your Podfile. Please add a "# Pods for AppCenter" line
in ${this.file}, inside
the "target" section, then rerun the react-native link. AppCenter pods will be added below the comment.
`);
const projectDirectory = path.resolve(__dirname, '..', '..', '..', '..');
Copy link
Member

@dhei dhei Jul 30, 2018

Choose a reason for hiding this comment

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

Concat four '..' to find the project directory assumes "appcenter-link-scripts" npm module is located at "/node_modules/appcenter-link-scripts" rather than multiple level down under the "/node_modules" folder like "/node_modules/appcenter/appcenter-link-scripts". This shouldn't be a problem for anyone using npm v3 or later, so it's good. (I tested this pr works with npm v3.10.10, which is the version of npm on App Center Build VMs.)

const projectName = projectDirectory.substr(projectDirectory.lastIndexOf(path.sep) + 1);
const reg = RegExp(`target\\s+'${projectName}'\\s+do`);
const targetPatterns = this.fileContents.match(reg);
if (targetPatterns === null) {
debug(`Could not find target ${reg}`);
const patterns = this.fileContents.match(/# Pods for .*/);
if (patterns === null) {
throw new Error(`
Error: Could not find a "# Pods for" comment in your Podfile. Please add a "# Pods for AppCenter" line
in ${this.file}, inside
the "target" section, then rerun the react-native link. AppCenter pods will be added below the comment.
`);
}
const sectionStart = this.startOfSection(patterns.index);
const targetSection = this.fileContents.substring(sectionStart, this.fileContents.indexOf(os.EOL, sectionStart));
return { match: targetSection, index: sectionStart };
}
return patterns;
return { match: targetPatterns[0], index: targetPatterns.index };
};

Podfile.prototype.isInScope = function (index, scopeRanges) {
Expand Down Expand Up @@ -195,11 +206,13 @@ Podfile.prototype.sectionBoundary = function (content, position, keyword) {
};

Podfile.prototype.endOfSection = function (position) {
return this.sectionBoundary(this.fileContents, position, 'end');
const endKeyword = 'end';
return this.sectionBoundary(this.fileContents, position - endKeyword.length, endKeyword);
};

Podfile.prototype.startOfSection = function (position) {
return this.sectionBoundary(this.fileContents, position, 'target');
const targetKeyword = 'target';
return this.sectionBoundary(this.fileContents, position + targetKeyword.length, targetKeyword);
};

Podfile.prototype.nextKeyword = function (content, offset, reverse = false) {
Expand Down