|
1 |
| -const fs = require('fs'); |
2 |
| -const path = require('path'); |
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
3 | 3 |
|
4 | 4 | export = function ($logger, $projectData, hookArgs) {
|
5 | 5 | const platformName = ((hookArgs.prepareData && hookArgs.prepareData.platform) || (hookArgs.platformData && hookArgs.platformData.normalizedPlatformName) || '').toLowerCase();
|
6 | 6 |
|
7 | 7 | if (platformName === 'ios') {
|
8 |
| - const rootPath = $projectData.projectDir; |
9 |
| - |
10 |
| - const Podfile = path.join(rootPath, 'platforms', 'ios', 'Podfile'); |
11 | 8 | const ResourcePodfile = $projectData.podfilePath;
|
| 9 | + const PluginPodfile = path.join(__dirname, '..', 'platforms', 'ios', 'Podfile'); |
12 | 10 |
|
13 |
| - if (fs.existsSync(Podfile) && fs.existsSync(ResourcePodfile)) { |
14 |
| - const podData = fs.readFileSync(Podfile, 'utf8') || ''; |
| 11 | + if (fs.existsSync(PluginPodfile) && fs.existsSync(ResourcePodfile)) { |
| 12 | + const pluginPodData = fs.readFileSync(PluginPodfile, 'utf8') || ''; |
15 | 13 | const resourcePodData = fs.readFileSync(ResourcePodfile, 'utf8');
|
16 | 14 |
|
17 |
| - // set variable early in the Podfile |
| 15 | + // set variable in our Podfile to ensure it's picked up correctly |
| 16 | + if (pluginPodData) { |
| 17 | + const placeholderRegex = /^(\s*)(.*?)### PLACEHOLDER_FOR_HOOK: \$NSFirebaseAnalyticsWithoutAdIdSupport$/m; |
| 18 | + const enabledRegex = /^(\s*)\$NSFirebaseAnalyticsWithoutAdIdSupport=true\b/m; |
| 19 | + const isEnabled = enabledRegex.test(resourcePodData); |
18 | 20 |
|
19 |
| - if (podData && resourcePodData && resourcePodData.indexOf('$NSFirebaseAnalyticsWithoutAdIdSupport=true') > -1 && podData.indexOf('$NSFirebaseAnalyticsWithoutAdIdSupport=true') !== 0) { |
20 |
| - fs.writeFileSync(Podfile, '$NSFirebaseAnalyticsWithoutAdIdSupport=true \n' + podData); |
| 21 | + const pluginMatch = pluginPodData.match(placeholderRegex); |
| 22 | + if (pluginMatch) { |
| 23 | + // this will always be: |
| 24 | + // ` ### PLACEHOLDER_FOR_HOOK: $NSFirebaseAnalyticsWithoutAdIdSupport` |
| 25 | + // or ` $NSFirebaseAnalyticsWithoutAdIdSupport=true ### PLACEHOLDER_FOR_HOOK: $NSFirebaseAnalyticsWithoutAdIdSupport` |
| 26 | + // indentation is always preserved, and an extra space is added after "true" |
| 27 | + const replacement = isEnabled ? '$NSFirebaseAnalyticsWithoutAdIdSupport=true ' : ''; |
| 28 | + const replacementLine = `${pluginMatch[1]}${replacement}### PLACEHOLDER_FOR_HOOK: $NSFirebaseAnalyticsWithoutAdIdSupport`; |
| 29 | + if (replacementLine !== pluginMatch[0]) { |
| 30 | + fs.writeFileSync(PluginPodfile, pluginPodData.replace(placeholderRegex, replacementLine)); |
| 31 | + } |
| 32 | + } else { |
| 33 | + $logger.warn(`Could not find NSFirebaseAnalyticsWithoutAdIdSupport placeholder in ${PluginPodfile}`); |
| 34 | + } |
21 | 35 | }
|
22 | 36 | }
|
23 | 37 | }
|
|
0 commit comments