diff --git a/AppCenterReactNativeShared/Products/AppCenterReactNativeShared/LICENSE.md b/AppCenterReactNativeShared/Products/AppCenterReactNativeShared/LICENSE.md new file mode 100644 index 000000000..0188dbd82 --- /dev/null +++ b/AppCenterReactNativeShared/Products/AppCenterReactNativeShared/LICENSE.md @@ -0,0 +1,13 @@ +Visual Studio App Center Plugin for React Native + +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. \ No newline at end of file diff --git a/appcenter-analytics/scripts/postlink.js b/appcenter-analytics/scripts/postlink.js index 147f7df03..e783f6ee3 100644 --- a/appcenter-analytics/scripts/postlink.js +++ b/appcenter-analytics/scripts/postlink.js @@ -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. diff --git a/appcenter-crashes/scripts/postlink.js b/appcenter-crashes/scripts/postlink.js index 67d5e6ea9..591d0f5d0 100644 --- a/appcenter-crashes/scripts/postlink.js +++ b/appcenter-crashes/scripts/postlink.js @@ -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. diff --git a/appcenter-link-scripts/src/ios/PodFile.js b/appcenter-link-scripts/src/ios/PodFile.js index 0c0e5d476..cfd18974b 100644 --- a/appcenter-link-scripts/src/ios/PodFile.js +++ b/appcenter-link-scripts/src/ios/PodFile.js @@ -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(` @@ -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 () { diff --git a/appcenter-link-scripts/src/ios/index.js b/appcenter-link-scripts/src/ios/index.js index 804b9c3bd..c874c528a 100644 --- a/appcenter-link-scripts/src/ios/index.js +++ b/appcenter-link-scripts/src/ios/index.js @@ -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?')); } @@ -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) { diff --git a/appcenter-push/scripts/postlink.js b/appcenter-push/scripts/postlink.js index 684a9b8d5..ce7007ec3 100644 --- a/appcenter-push/scripts/postlink.js +++ b/appcenter-push/scripts/postlink.js @@ -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. diff --git a/appcenter/scripts/postlink.js b/appcenter/scripts/postlink.js index 4ada765c5..7cf8fa0e7 100644 --- a/appcenter/scripts/postlink.js +++ b/appcenter/scripts/postlink.js @@ -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.