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

Support Cocoapods 1.4 #237

Merged
merged 10 commits into from
Feb 9, 2018
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Visual Studio App Center Plugin for React Native
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to add this file because it's automatically copied as part of the SDK release process at here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with modifying build script, but without this file react-native link outputs annoying error when building our TestApp and with cocoapods 1.4 installed.


Copyright (c) Microsoft Corporation

All rights reserved.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 7 additions & 4 deletions appcenter-analytics/scripts/postlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ return rnpmlink.ios.checkIfAppDelegateExists()
})
.then((file) => {
console.log(`Added code to initialize iOS Analytics SDK in ${file}`);
return rnpmlink.ios.addPodDeps([
{ pod: 'AppCenter/Analytics', version: '1.3.0' },
{ pod: 'AppCenterReactNativeShared', version: '1.2.0' } // in case people don't link appcenter (core)
]).catch((e) => {
return rnpmlink.ios.addPodDeps(
[
{ pod: 'AppCenter/Analytics', version: '1.3.0' },
{ pod: 'AppCenterReactNativeShared', version: '1.2.0' } // in case people don't link appcenter (core)
],
{ platform: 'ios', version: '9.0' }
).catch((e) => {
console.log(`
Could not install dependencies using CocoaPods.
Please refer to the documentation to install dependencies manually.
Expand Down
11 changes: 7 additions & 4 deletions appcenter-crashes/scripts/postlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ return rnpmlink.ios.checkIfAppDelegateExists()
})
.then((file) => {
console.log(`Added code to initialize iOS Crashes SDK in ${file}`);
return rnpmlink.ios.addPodDeps([
{ pod: 'AppCenter/Crashes', version: '1.3.0' },
{ pod: 'AppCenterReactNativeShared', version: '1.2.0' } // in case people don't link appcenter (core)
]).catch((e) => {
return rnpmlink.ios.addPodDeps(
[
{ pod: 'AppCenter/Crashes', version: '1.3.0' },
{ pod: 'AppCenterReactNativeShared', version: '1.2.0' } // in case people don't link appcenter (core)
],
{ platform: 'ios', version: '9.0' }
).catch((e) => {
console.log(`
Could not install dependencies using CocoaPods.
Please refer to the documentation to install dependencies manually.
Expand Down
182 changes: 179 additions & 3 deletions appcenter-link-scripts/src/ios/PodFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,76 @@ Podfile.prototype.addPodLine = function (pod, podspec, version) {
debug(`Replace ${existingLine} by ${line}`);
return;
}
const pattern = this.getTargetSectionPattern()[0];
this.fileContents = this.fileContents.replace(pattern, `${pattern}\n ${line}`);
debug(`${line} ===> ${this.file}`);
};

Podfile.prototype.addMinimumDeploymentTarget = function (platform, version) {
try {
const isGlobalPlatformDefined = this.isGlobalPlatformDefined(platform);
if (!isGlobalPlatformDefined) {
const isTargetPlatformDefined = this.isTargetPlatformDefined(platform);
if (!isTargetPlatformDefined) {
this.addPlatformToTarget(platform, version);
}
}
} catch (e) {
const line = `platform :${platform}, '${version}'`;
console.log(`Could not automatically add line: ${line} to your Podfile. Please make sure that this line is present.`, e);
}
};

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 part1 = this.fileContents.slice(0, newLineIndex);
const part2 = this.fileContents.slice(newLineIndex);
const indent = ' ';
this.fileContents = `${part1}\n${indent}${line}${part2}`;
};

Podfile.prototype.isGlobalPlatformDefined = function (platform) {
const platformPattern = new RegExp(`platform\\s+:${platform}`, 'g');
const platformMatches = this.fileContents.match(platformPattern);
if (!platformMatches) {
return false;
}
const globalRanges = this.scopeRanges(this.fileContents);
let platformIndex = 0;
for (let match = 0; match < platformMatches.length; match++) {
platformIndex = this.fileContents.indexOf(platformMatches[match], platformIndex + 1);
if (this.isInScope(platformIndex, globalRanges) && this.isLineActive(this.fileContents, platformIndex)) {
return true;
}
}
return false;
};

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 sectionContent = this.fileContents.substring(startSection, endSection);
const scopeRanges = this.scopeRanges(sectionContent);
const platformMatches = sectionContent.match(platformPattern);
if (!platformMatches) {
return false;
}
let platformIndex = 0;
for (let match = 0; match < platformMatches.length; match++) {
platformIndex = sectionContent.indexOf(platformMatches[match], platformIndex + 1);
if (this.isInScope(platformIndex, scopeRanges) && this.isLineActive(sectionContent, platformIndex)) {
return true;
}
}
return false;
};

Podfile.prototype.getTargetSectionPattern = function () {
const patterns = this.fileContents.match(/# Pods for .*/);
if (patterns === null) {
throw new Error(`
Expand All @@ -45,9 +115,115 @@ Podfile.prototype.addPodLine = function (pod, podspec, version) {
the "target" section, then rerun the react-native link. AppCenter pods will be added below the comment.
`);
}
const pattern = patterns[0];
this.fileContents = this.fileContents.replace(pattern, `${pattern}\n ${line}`);
debug(`${line} ===> ${this.file}`);
return patterns;
};

Podfile.prototype.isInScope = function (index, scopeRanges) {
for (let scope = 0; scope < scopeRanges.length; scope++) {
if (index > scopeRanges[scope].start && index < scopeRanges[scope].end) {
return true;
}
}
return false;
};

Podfile.prototype.isLineActive = function (content, index) {
if (index < 0) {
return false;
}
const commentSymbol = '#';
const newlineSymbol = '\n';
let diff = 1;
let previousCharacter = content.charAt(index - (diff++));
while (previousCharacter !== commentSymbol && previousCharacter !== newlineSymbol) {
previousCharacter = content.charAt(index - (diff++));
}
return previousCharacter !== commentSymbol;
};

Podfile.prototype.scopeRanges = function (content) {
const targetKeyword = 'target';

const result = [];
const targetsStack = [];
const currentRange = { start: 0, end: 0 };
let currentOffset = 0;

let keywordMatch = this.nextKeyword(content, currentOffset);
while (keywordMatch.index >= 0) {
if (keywordMatch.keyword === targetKeyword) {
currentRange.end = keywordMatch.index - 1;
if (targetsStack.length === 0) {
result.push(Object.assign({}, currentRange));
}
targetsStack.push(keywordMatch.index);
} else {
targetsStack.pop();
if (targetsStack.length === 0) {
currentRange.start = keywordMatch.index + keywordMatch.keyword.length;
}
}
currentOffset = keywordMatch.index + keywordMatch.keyword.length;
keywordMatch = this.nextKeyword(content, currentOffset);
}
currentRange.end = content.length;
result.push(Object.assign({}, currentRange));
return result;
};

Podfile.prototype.sectionBoundary = function (content, position, keyword) {
const targetKeyword = 'target';
let reverse = false;
let direction = 1;
if (keyword === targetKeyword) {
reverse = true;
direction = -1;
}
const keywordStack = [];
let keywordMatch = this.nextKeyword(content, position, reverse);
while (keywordMatch.index >= 0) {
if (keywordMatch.keyword !== keyword) {
keywordStack.push(keywordMatch.keyword);
} else if (keywordStack.length === 0) {
break;
} else {
keywordStack.pop();
}
keywordMatch = this.nextKeyword(content, keywordMatch.index + (keywordMatch.keyword.length * direction), reverse);
}
return keywordMatch.index;
};

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

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

Podfile.prototype.nextKeyword = function (content, offset, reverse = false) {
const targetKeyword = 'target';
const endKeyword = 'end';

const targetIndex = reverse ?
content.lastIndexOf(targetKeyword, offset) :
content.indexOf(targetKeyword, offset);
const endIndex = reverse ?
content.lastIndexOf(endKeyword, offset) :
content.indexOf(endKeyword, offset);

if (reverse) {
return targetIndex < endIndex ?
{ keyword: endKeyword, index: endIndex } :
{ keyword: targetKeyword, index: targetIndex };
}
if (targetIndex < 0) {
return { keyword: endKeyword, index: endIndex };
}
return targetIndex < endIndex ?
{ keyword: targetKeyword, index: targetIndex } :
{ keyword: endKeyword, index: endIndex };
};

Podfile.prototype.save = function () {
Expand Down
5 changes: 4 additions & 1 deletion appcenter-link-scripts/src/ios/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module.exports = {
}
},

addPodDeps(pods) {
addPodDeps(pods, minimumTarget = null) {
if (!PodFile.isCocoaPodsInstalled()) {
return Promise.reject(new Error('Could not find "pod" command. Is CocoaPods installed?'));
}
Expand All @@ -82,6 +82,9 @@ module.exports = {
podFile.addPodLine(pod.pod, pod.podspec, pod.version);
});
podFile.eraseOldLines();
if (minimumTarget) {
podFile.addMinimumDeploymentTarget(minimumTarget.platform, minimumTarget.version);
}
podFile.save();
return Promise.resolve(podFile.install());
} catch (e) {
Expand Down
11 changes: 7 additions & 4 deletions appcenter-push/scripts/postlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ return rnpmlink.ios.checkIfAppDelegateExists()
})
.then((file) => {
console.log(`Added code to initialize iOS Push SDK in ${file}`);
return rnpmlink.ios.addPodDeps([
{ pod: 'AppCenter/Push', version: '1.3.0' },
{ pod: 'AppCenterReactNativeShared', version: '1.2.0' } // in case people don't link appcenter (core)
]).catch((e) => {
return rnpmlink.ios.addPodDeps(
[
{ pod: 'AppCenter/Push', version: '1.3.0' },
{ pod: 'AppCenterReactNativeShared', version: '1.2.0' } // in case people don't link appcenter (core)
],
{ platform: 'ios', version: '9.0' }
).catch((e) => {
console.log(`
Could not install dependencies using CocoaPods.
Please refer to the documentation to install dependencies manually.
Expand Down
7 changes: 4 additions & 3 deletions appcenter/scripts/postlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ return rnpmlink.ios.checkIfAppDelegateExists()
})
.then((file) => {
console.log(`Added code to initialize iOS AppCenter SDK in ${file}`);
return rnpmlink.ios.addPodDeps([
{ pod: 'AppCenterReactNativeShared', version: '1.2.0' }
]).catch((e) => {
return rnpmlink.ios.addPodDeps(
[{ pod: 'AppCenterReactNativeShared', version: '1.2.0' }],
{ platform: 'ios', version: '9.0' }
).catch((e) => {
console.log(`
Could not install dependencies using CocoaPods.
Please refer to the documentation to install dependencies manually.
Expand Down