Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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