|
| 1 | +import { EOL } from "os"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as semver from "semver"; |
| 4 | +import { PODFILE_NAME } from "../constants"; |
| 5 | + |
| 6 | +export class CocoaPodsPlatformManager implements ICocoaPodsPlatformManager { |
| 7 | + public addPlatformSection(projectData: IProjectData, podfilePlatformData: IPodfilePlatformData, projectPodfileContent: string): string { |
| 8 | + const platformSectionData = this.getPlatformSectionData(projectPodfileContent); |
| 9 | + if (platformSectionData && platformSectionData.podfilePlatformData) { |
| 10 | + const shouldReplacePlatformSection = this.shouldReplacePlatformSection(projectData, platformSectionData.podfilePlatformData, podfilePlatformData); |
| 11 | + if (shouldReplacePlatformSection) { |
| 12 | + const newSection = this.buildPlatformSection(podfilePlatformData); |
| 13 | + projectPodfileContent = projectPodfileContent.replace(platformSectionData.platformSectionContent, newSection); |
| 14 | + } |
| 15 | + } else { |
| 16 | + projectPodfileContent += this.buildPlatformSection(podfilePlatformData); |
| 17 | + } |
| 18 | + |
| 19 | + return projectPodfileContent; |
| 20 | + } |
| 21 | + |
| 22 | + public removePlatformSection(moduleName: string, projectPodfileContent: string, podfilePath: string): string { |
| 23 | + const platformSectionData = this.getPlatformSectionData(projectPodfileContent); |
| 24 | + if (platformSectionData && platformSectionData.podfilePlatformData && platformSectionData.podfilePlatformData.path === podfilePath) { |
| 25 | + const podfileContentRegExp = new RegExp(`# Begin Podfile - ([\\s\\S]*?)# End Podfile`, "mg"); |
| 26 | + const allPodfiles = projectPodfileContent.match(podfileContentRegExp) || []; |
| 27 | + const selectedPlatformData = this.selectPlatformDataFromProjectPodfile(allPodfiles); |
| 28 | + const newPlatformSection = selectedPlatformData ? this.buildPlatformSection(selectedPlatformData) : ""; |
| 29 | + const regExp = new RegExp(`\\r?\\n${platformSectionData.platformSectionContent}\\r?\\n`, "mg"); |
| 30 | + projectPodfileContent = projectPodfileContent.replace(regExp, newPlatformSection); |
| 31 | + } |
| 32 | + |
| 33 | + return projectPodfileContent; |
| 34 | + } |
| 35 | + |
| 36 | + public replacePlatformRow(podfileContent: string, podfilePath: string): { replacedContent: string, podfilePlatformData: IPodfilePlatformData } { |
| 37 | + let podfilePlatformData: IPodfilePlatformData = null; |
| 38 | + const platformRowRegExp = new RegExp(`^\\s*?(platform\\b\\s*?\\:\\s*?ios\\b(?:,\\s*?['"](.+)['"])?)`, "gm"); |
| 39 | + const replacedContent = podfileContent.replace(platformRowRegExp, (substring: string, firstGroup: string, secondGroup: string) => { |
| 40 | + podfilePlatformData = { content: firstGroup, version: secondGroup, path: podfilePath }; |
| 41 | + return `# ${substring.trim()}`; |
| 42 | + }); |
| 43 | + |
| 44 | + return { replacedContent, podfilePlatformData }; |
| 45 | + } |
| 46 | + |
| 47 | + private getPlatformSectionData(projectPodfileContent: string): { podfilePlatformData: IPodfilePlatformData, platformSectionContent: string } { |
| 48 | + const platformSectionRegExp = new RegExp(`${this.getPlatformSectionHeader()} ([\\s\\S]*?)with (.*)[\\s\\S]*?${this.getPlatformSectionFooter()}`, "m"); |
| 49 | + const match = platformSectionRegExp.exec(projectPodfileContent); |
| 50 | + let result = null; |
| 51 | + if (match && match[0]) { |
| 52 | + result = { |
| 53 | + platformSectionContent: match[0], |
| 54 | + podfilePlatformData: { |
| 55 | + path: match[1].trim(), |
| 56 | + content: "", |
| 57 | + version: match[2] |
| 58 | + } |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + private selectPlatformDataFromProjectPodfile(allPodfiles: string[]): IPodfilePlatformData { |
| 66 | + const platformRowRegExp = new RegExp(`^\\s*?#\\s*?(platform\\b\\s*?\\:\\s*?ios\\b(?:,\\s*?['"](.+)['"])?)`, "m"); |
| 67 | + const podfilePathRegExp = new RegExp(`# Begin Podfile - ([\\s\\S]*?)${EOL}`); |
| 68 | + let selectedPlatformData: IPodfilePlatformData = null; |
| 69 | + _.each(allPodfiles, podfileContent => { |
| 70 | + const platformMatch = platformRowRegExp.exec(podfileContent); |
| 71 | + const podfilePathMatch: any[] = podfilePathRegExp.exec(podfileContent) || []; |
| 72 | + // platform without version -> select it with highest priority |
| 73 | + if (platformMatch && platformMatch[0] && !platformMatch[2]) { |
| 74 | + selectedPlatformData = { |
| 75 | + version: null, |
| 76 | + content: platformMatch[1], |
| 77 | + path: podfilePathMatch[1] |
| 78 | + }; |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + // platform with version |
| 83 | + if (platformMatch && platformMatch[0] && platformMatch[2]) { |
| 84 | + if (!selectedPlatformData || semver.gt(semver.coerce(platformMatch[2]), semver.coerce(selectedPlatformData.version))) { |
| 85 | + selectedPlatformData = { |
| 86 | + version: platformMatch[2], |
| 87 | + content: platformMatch[1], |
| 88 | + path: podfilePathMatch[1] |
| 89 | + }; |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + return selectedPlatformData; |
| 95 | + } |
| 96 | + |
| 97 | + private shouldReplacePlatformSection(projectData: IProjectData, oldPodfilePlatformData: IPodfilePlatformData, currentPodfilePlatformData: IPodfilePlatformData): boolean { |
| 98 | + // The selected platform should be replaced in the following cases: |
| 99 | + // 1. When the pod file is from App_Resources and the selected platform is not from App_Resources |
| 100 | + // 2. When the pod file is from App_Resources, the selected platform is also from App_Resources |
| 101 | + // and the pod's version is greater than the selected one |
| 102 | + // 3. When the pod file doesn't have platform's version -> `platform :ios` |
| 103 | + // 4. When the pod file has a version greater than the selected platform's version |
| 104 | + const appResourcesPodfilePath = path.join(projectData.getAppResourcesDirectoryPath(), "iOS", PODFILE_NAME); |
| 105 | + const isFromAppResources = oldPodfilePlatformData.path !== appResourcesPodfilePath && currentPodfilePlatformData.path === appResourcesPodfilePath; |
| 106 | + const isFromAppResourcesWithGreaterPlatformVersion = oldPodfilePlatformData.path === appResourcesPodfilePath && currentPodfilePlatformData.path === appResourcesPodfilePath && semver.gt(semver.coerce(currentPodfilePlatformData.version), semver.coerce(oldPodfilePlatformData.version)); |
| 107 | + const isPodfileWithGreaterPlatformVersion = !currentPodfilePlatformData.version || semver.gt(semver.coerce(currentPodfilePlatformData.version), semver.coerce(oldPodfilePlatformData.version)); |
| 108 | + const result = isFromAppResources || isFromAppResourcesWithGreaterPlatformVersion || isPodfileWithGreaterPlatformVersion; |
| 109 | + return result; |
| 110 | + } |
| 111 | + |
| 112 | + private buildPlatformSection(podfilePlatformData: IPodfilePlatformData) { |
| 113 | + let result = `${this.getPlatformSectionHeader()} ${podfilePlatformData.path} with`; |
| 114 | + if (podfilePlatformData.version) { |
| 115 | + result += ` ${podfilePlatformData.version}`; |
| 116 | + } |
| 117 | + |
| 118 | + result += `${EOL}${podfilePlatformData.content}${EOL}${this.getPlatformSectionFooter()}`; |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + private getPlatformSectionHeader(): string { |
| 123 | + return '# NativeScriptPlatformSection'; |
| 124 | + } |
| 125 | + |
| 126 | + private getPlatformSectionFooter(): string { |
| 127 | + return '# End NativeScriptPlatformSection'; |
| 128 | + } |
| 129 | +} |
| 130 | +$injector.register("cocoaPodsPlatformManager", CocoaPodsPlatformManager); |
0 commit comments