|
| 1 | +import { |
| 2 | + withInfoPlist, |
| 3 | + withPodfile, |
| 4 | + withAppDelegate, |
| 5 | + type ConfigPlugin, |
| 6 | +} from '@expo/config-plugins'; |
| 7 | +import { mergeContents } from '@expo/config-plugins/build/utils/generateCode'; |
| 8 | +import type { RNGoogleMapsPlusExpoPluginProps } from '../types'; |
| 9 | + |
| 10 | +const withIosGoogleMapsPlus: ConfigPlugin<RNGoogleMapsPlusExpoPluginProps> = ( |
| 11 | + config, |
| 12 | + props |
| 13 | +) => { |
| 14 | + config = withInfoPlist(config, (conf) => { |
| 15 | + const apiKey = |
| 16 | + props.googleMapsIosApiKey ?? process.env.GOOGLE_MAPS_API_KEY_IOS ?? null; |
| 17 | + |
| 18 | + if (!apiKey) { |
| 19 | + console.warn( |
| 20 | + '[react-native-google-maps-plus] No iOS API key provided. Google Maps may fail to initialize.' |
| 21 | + ); |
| 22 | + } |
| 23 | + if (apiKey) { |
| 24 | + conf.modResults.MAPS_API_KEY = apiKey; |
| 25 | + } |
| 26 | + return conf; |
| 27 | + }); |
| 28 | + |
| 29 | + config = withPodfile(config, (conf) => { |
| 30 | + let src = conf.modResults.contents; |
| 31 | + if (!src.includes('use_modular_headers!')) { |
| 32 | + src = mergeContents({ |
| 33 | + tag: 'react-native-google-maps-modular-headers', |
| 34 | + src, |
| 35 | + newSrc: 'use_modular_headers!', |
| 36 | + anchor: /use_frameworks!|platform\s+:ios.*/, |
| 37 | + offset: 1, |
| 38 | + comment: '#', |
| 39 | + }).contents; |
| 40 | + } |
| 41 | + |
| 42 | + const patchSnippet = ` |
| 43 | + # Force iOS 16+ to avoid deployment target warnings |
| 44 | + installer.pods_project.targets.each do |target| |
| 45 | + target.build_configurations.each do |config| |
| 46 | + config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '16.0' |
| 47 | + end |
| 48 | + end |
| 49 | +
|
| 50 | + # --- SVGKit Patch --- |
| 51 | + require 'fileutils' |
| 52 | + svgkit_path = File.join(installer.sandbox.pod_dir('SVGKit'), 'Source') |
| 53 | +
|
| 54 | + # --- Patch Node.h imports --- |
| 55 | + Dir.glob(File.join(svgkit_path, '**', '*.{h,m}')).each do |file| |
| 56 | + FileUtils.chmod("u+w", file) |
| 57 | + text = File.read(file) |
| 58 | + new_contents = text.gsub('#import "Node.h"', '#import "SVGKit/Node.h"') |
| 59 | + File.open(file, 'w') { |f| f.write(new_contents) } |
| 60 | + end |
| 61 | +
|
| 62 | + # --- Patch CSSValue.h imports --- |
| 63 | + Dir.glob(File.join(svgkit_path, '**', '*.{h,m}')).each do |file| |
| 64 | + FileUtils.chmod("u+w", file) |
| 65 | + text = File.read(file) |
| 66 | + new_contents = text.gsub('#import "CSSValue.h"', '#import "SVGKit/CSSValue.h"') |
| 67 | + File.open(file, 'w') { |f| f.write(new_contents) } |
| 68 | + end |
| 69 | +
|
| 70 | + # --- Patch SVGLength.h imports --- |
| 71 | + Dir.glob(File.join(svgkit_path, '**', '*.{h,m}')).each do |file| |
| 72 | + FileUtils.chmod("u+w", file) |
| 73 | + text = File.read(file) |
| 74 | + new_contents = text.gsub('#import "SVGLength.h"', '#import "SVGKit/SVGLength.h"') |
| 75 | + File.open(file, 'w') { |f| f.write(new_contents) } |
| 76 | + end |
| 77 | + `; |
| 78 | + |
| 79 | + if (src.includes('post_install do |installer|')) { |
| 80 | + src = src.replace( |
| 81 | + /post_install do \|installer\|([\s\S]*?)end/, |
| 82 | + (match, inner) => { |
| 83 | + if (inner.includes('SVGKit Patch')) return match; // idempotent |
| 84 | + return `post_install do |installer|${inner}\n${patchSnippet}\nend`; |
| 85 | + } |
| 86 | + ); |
| 87 | + } else { |
| 88 | + src += `\npost_install do |installer|\n${patchSnippet}\nend\n`; |
| 89 | + } |
| 90 | + |
| 91 | + conf.modResults.contents = src; |
| 92 | + return conf; |
| 93 | + }); |
| 94 | + |
| 95 | + config = withAppDelegate(config, (conf) => { |
| 96 | + const { language } = conf.modResults; |
| 97 | + if (language !== 'swift') { |
| 98 | + console.warn( |
| 99 | + '[react-native-google-maps-plus] AppDelegate is not Swift; skipping GMSServices injection.' |
| 100 | + ); |
| 101 | + return conf; |
| 102 | + } |
| 103 | + |
| 104 | + let src = conf.modResults.contents; |
| 105 | + |
| 106 | + if (!src.includes('import GoogleMaps')) { |
| 107 | + src = mergeContents({ |
| 108 | + tag: 'react-native-google-maps-import', |
| 109 | + src, |
| 110 | + newSrc: 'import GoogleMaps', |
| 111 | + anchor: /^import React/m, |
| 112 | + offset: 1, |
| 113 | + comment: '//', |
| 114 | + }).contents; |
| 115 | + } |
| 116 | + |
| 117 | + const initSnippet = ` |
| 118 | + if let apiKey = Bundle.main.object(forInfoDictionaryKey: "MAPS_API_KEY") as? String { |
| 119 | + GMSServices.provideAPIKey(apiKey) |
| 120 | + }`; |
| 121 | + |
| 122 | + try { |
| 123 | + src = mergeContents({ |
| 124 | + tag: 'react-native-google-maps-init', |
| 125 | + src, |
| 126 | + newSrc: initSnippet, |
| 127 | + anchor: |
| 128 | + /return\s+super\.application\s*\(\s*application\s*,\s*didFinishLaunchingWithOptions:\s*launchOptions\s*\)/, |
| 129 | + offset: -1, |
| 130 | + comment: '//', |
| 131 | + }).contents; |
| 132 | + } catch (error: any) { |
| 133 | + if (error.code === 'ERR_NO_MATCH') { |
| 134 | + src = mergeContents({ |
| 135 | + tag: 'react-native-google-maps-init', |
| 136 | + src, |
| 137 | + newSrc: initSnippet, |
| 138 | + anchor: /didFinishLaunchingWithOptions[^{]*\{/, |
| 139 | + offset: 1, |
| 140 | + comment: '//', |
| 141 | + }).contents; |
| 142 | + } else { |
| 143 | + throw error; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + conf.modResults.contents = src; |
| 148 | + return conf; |
| 149 | + }); |
| 150 | + |
| 151 | + return config; |
| 152 | +}; |
| 153 | + |
| 154 | +export default withIosGoogleMapsPlus; |
0 commit comments