appcenter-link-scripts
Advanced tools
Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "appcenter-link-scripts", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Node module that contains common functionality needed to link appcenter-* modules", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -75,3 +75,3 @@ const fs = require('fs'); | ||
addPodDeps(pods) { | ||
addPodDeps(pods, minimumTarget = null) { | ||
if (!PodFile.isCocoaPodsInstalled()) { | ||
@@ -86,2 +86,5 @@ return Promise.reject(new Error('Could not find "pod" command. Is CocoaPods installed?')); | ||
podFile.eraseOldLines(); | ||
if (minimumTarget) { | ||
podFile.addMinimumDeploymentTarget(minimumTarget.platform, minimumTarget.version); | ||
} | ||
podFile.save(); | ||
@@ -88,0 +91,0 @@ return Promise.resolve(podFile.install()); |
@@ -40,2 +40,72 @@ const fs = require('fs'); | ||
} | ||
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 .*/); | ||
@@ -49,7 +119,113 @@ if (patterns === null) { | ||
} | ||
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 () { | ||
@@ -56,0 +232,0 @@ fs.writeFileSync(this.file, this.fileContents); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26506
590