Skip to content

Commit

Permalink
fix(firebase-analytics): consistent handling of Podfile variable $NSF…
Browse files Browse the repository at this point in the history
…irebaseAnalyticsWithoutAdIdSupport (#167)
  • Loading branch information
edusperoni committed Dec 27, 2022
1 parent 7a1618d commit 7ca3c04
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
34 changes: 24 additions & 10 deletions packages/firebase-analytics/hooks/before-checkForChanges.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
const fs = require('fs');
const path = require('path');
import * as fs from 'fs';
import * as path from 'path';

export = function ($logger, $projectData, hookArgs) {
const platformName = ((hookArgs.prepareData && hookArgs.prepareData.platform) || (hookArgs.platformData && hookArgs.platformData.normalizedPlatformName) || '').toLowerCase();

if (platformName === 'ios') {
const rootPath = $projectData.projectDir;

const Podfile = path.join(rootPath, 'platforms', 'ios', 'Podfile');
const ResourcePodfile = $projectData.podfilePath;
const PluginPodfile = path.join(__dirname, '..', 'platforms', 'ios', 'Podfile');

if (fs.existsSync(Podfile) && fs.existsSync(ResourcePodfile)) {
const podData = fs.readFileSync(Podfile, 'utf8') || '';
if (fs.existsSync(PluginPodfile) && fs.existsSync(ResourcePodfile)) {
const pluginPodData = fs.readFileSync(PluginPodfile, 'utf8') || '';
const resourcePodData = fs.readFileSync(ResourcePodfile, 'utf8');

// set variable early in the Podfile
// set variable in our Podfile to ensure it's picked up correctly
if (pluginPodData) {
const placeholderRegex = /^(\s*)(.*?)### PLACEHOLDER_FOR_HOOK: \$NSFirebaseAnalyticsWithoutAdIdSupport$/m;
const enabledRegex = /^(\s*)\$NSFirebaseAnalyticsWithoutAdIdSupport=true\b/m;
const isEnabled = enabledRegex.test(resourcePodData);

if (podData && resourcePodData && resourcePodData.indexOf('$NSFirebaseAnalyticsWithoutAdIdSupport=true') > -1 && podData.indexOf('$NSFirebaseAnalyticsWithoutAdIdSupport=true') !== 0) {
fs.writeFileSync(Podfile, '$NSFirebaseAnalyticsWithoutAdIdSupport=true \n' + podData);
const pluginMatch = pluginPodData.match(placeholderRegex);
if (pluginMatch) {
// this will always be:
// ` ### PLACEHOLDER_FOR_HOOK: $NSFirebaseAnalyticsWithoutAdIdSupport`
// or ` $NSFirebaseAnalyticsWithoutAdIdSupport=true ### PLACEHOLDER_FOR_HOOK: $NSFirebaseAnalyticsWithoutAdIdSupport`
// indentation is always preserved, and an extra space is added after "true"
const replacement = isEnabled ? '$NSFirebaseAnalyticsWithoutAdIdSupport=true ' : '';
const replacementLine = `${pluginMatch[1]}${replacement}### PLACEHOLDER_FOR_HOOK: $NSFirebaseAnalyticsWithoutAdIdSupport`;
if (replacementLine !== pluginMatch[0]) {
fs.writeFileSync(PluginPodfile, pluginPodData.replace(placeholderRegex, replacementLine));
}
} else {
$logger.warn(`Could not find NSFirebaseAnalyticsWithoutAdIdSupport placeholder in ${PluginPodfile}`);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/firebase-analytics/platforms/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
platform :ios, '10.0'
# Firebase dependencies
### PLACEHOLDER_FOR_HOOK: $NSFirebaseAnalyticsWithoutAdIdSupport
if defined?($NSFirebaseAnalyticsWithoutAdIdSupport)
Pod::UI.puts "Using Firebase/AnalyticsWithoutAdIdSupport pod in place of default Firebase/Analytics"

Expand Down

0 comments on commit 7ca3c04

Please sign in to comment.