Module: AnnFlavorCocoapods::PodfileHelper

Defined in:
lib/ann_flavor_cocoapods/podfile_helper.rb

Class Method Summary collapse

Class Method Details

.bundle_id_for_build_type(flavor_key, build_type, project_root: nil) ⇒ Object

Returns the bundle ID for a given flavor key and build type (e.g. "debug"), applying build_types.id_suffix from either flavor or default. An unknown flavor key falls back to the unsuffixed default id (matches pre-delegation behavior — this method never raises for a missing flavor).



94
95
96
97
98
99
100
101
102
# File 'lib/ann_flavor_cocoapods/podfile_helper.rb', line 94

def self.bundle_id_for_build_type(flavor_key, build_type, project_root: nil)
  project_root ||= File.dirname(SpecLoader.find_spec_file(Dir.pwd) ||
    raise("annspec.yaml not found (searched upward from #{Dir.pwd})"))
  spec = load_core_spec(project_root)
  ios  = spec.app&.ios
  return '' if ios.nil?

  AnnFlavorCore::IosResolution.bundle_id(ios, flavor_key, build_type)
end

.configure_firebase(installer, project_root: nil) ⇒ Object

Call inside your Podfile's post_integrate hook to wire per-flavor Firebase GoogleService-Info.plist into the Xcode build. Must be post_integrate, not post_install (DEF-061, #61): post_install fires before CocoaPods has integrated/saved the user's app project, so build phases injected there never reach the real app target's build graph — only post_integrate runs after the app project has been integrated and saved.

Example Podfile usage:

require 'ann_flavor_cocoapods'
post_integrate do |installer|
AnnFlavorCocoapods::PodfileHelper.configure_firebase(installer)
end

For each iOS flavor that has config_file set, this injects two build phases into the app target (identified by product type, not a hardcoded name) in the real user Xcode project (Runner.xcodeproj, not CocoaPods' generated Pods.xcodeproj):

- A pre-build shell script that copies the correct plist to
${SRCROOT}/Runner/GoogleService-Info.plist at build time.
- A post-build shell script that removes the copied file so the workspace
stays clean and a mis-matched plist cannot accidentally survive.

Gate: only runs when integrations.firebase is true in annspec.yaml.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ann_flavor_cocoapods/podfile_helper.rb', line 30

def self.configure_firebase(installer, project_root: nil)
  project_root ||= File.dirname(SpecLoader.find_spec_file(Dir.pwd) ||
    raise("annspec.yaml not found (searched upward from #{Dir.pwd})"))
  spec = load_core_spec(project_root)

  # integrations.firebase must be explicitly enabled (REQ-FIRE-00010).
  return unless spec.app&.integrations&.firebase == true

  ios = spec.app&.ios
  return if ios.nil?

  layout = detect_project_layout(project_root)

  # Build a map: configuration_name → config_file path.
  # Xcode configuration names are capitalized: Release-<flavor>, Debug-<flavor>.
  config_map = {}
  ios.flavors.each_key do |flavor_key|
    AnnFlavorCore::Resolver::STANDARD_BUILD_TYPES.each do |bt|
      fb_file = AnnFlavorCore::IosResolution.firebase_config_path(
        ios, flavor_key, bt, generated_dir: layout[:generated_dir]
      )
      next unless fb_file

      # Map Xcode config name (e.g. "Release-ledger_in") → plist path
      xcode_config = "#{bt.capitalize}-#{flavor_key}"
      config_map[xcode_config] = fb_file

      warn_if_static_plist(project_root, xcode_config, fb_file)
    end
  end

  return if config_map.empty?
  return if installer.nil?

  inject_build_phases(installer, project_root, config_map)
end

.detect_project_layout(project_root) ⇒ Object

Detects whether project_root is a Flutter project (has pubspec.yaml, so generated Firebase files live under lib/generated/firebase/) or a plain iOS-only Xcode project with no Flutter wrapper (no pubspec.yaml — ios/ itself is the effective root, so generated Firebase files live under ios/generated/firebase/ instead, since there is no lib/ to nest under). Either way, the returned generated_dir is relative to project_root, and for the real Runner target $SRCROOT is always <project_root>/ios — one level below project_root — so inject_build_phases always needs exactly one ".." hop to reach it, regardless of layout.

Resolved once here, at pod install time, rather than probed for at build time inside the generated shell script — a runtime "try here, then try one level up" fallback would mask a genuinely wrong layout instead of surfacing it, exactly the failure mode already fixed once in this file (DEF-061, #61 and the 0.1.22/0.1.23 stale-path bugs).



82
83
84
85
86
87
88
# File 'lib/ann_flavor_cocoapods/podfile_helper.rb', line 82

def self.detect_project_layout(project_root)
  if File.exist?(File.join(project_root, 'pubspec.yaml'))
    { generated_dir: 'lib/generated/firebase' }
  else
    { generated_dir: 'ios/generated/firebase' }
  end
end

.inject_build_phases(installer, project_root, config_map) ⇒ Object

Injects pre-build and post-build shell script phases into the real app target (identified by product type) inside the user's own Xcode project — NOT installer.pods_project (CocoaPods' generated Pods.xcodeproj, whose Pods-Runner aggregate target only builds pod dependencies and can never satisfy the app target's own build graph — DEF-061, #61).



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ann_flavor_cocoapods/podfile_helper.rb', line 130

def self.inject_build_phases(installer, project_root, config_map)
  # Build the per-config shell that selects the right source file.
  # Uses a case statement keyed on $CONFIGURATION (Xcode build setting).
  # plist_path is always relative to project_root (either
  # lib/generated/firebase/... for a Flutter project, or
  # ios/generated/firebase/... for an iOS-only project — see
  # detect_project_layout); for the real Runner target (fixed by
  # DEF-061, #61) $SRCROOT is <project_root>/ios, one level below
  # project_root regardless of layout — a single ".." reaches it in both
  # cases. (The old ".." x2 was seemingly sized for Pods-Runner's
  # $SRCROOT, one level deeper still inside CocoaPods' generated
  # Pods.xcodeproj, but that build phase never actually ran there either,
  # so this was never previously exercised.)
  case_body = config_map.map do |xcode_config, plist_path|
    "  #{xcode_config}) SRC_PLIST=\"${SRCROOT}/../#{plist_path}\" ;;"
  end.join("\n")

  pre_build_script = <<~SHELL
    # Injected by ann-flavor-cocoapods: copy per-flavor GoogleService-Info.plist
    case "$CONFIGURATION" in
    #{case_body}
      *) echo "ann-flavor-cocoapods: no Firebase plist for $CONFIGURATION"; exit 0 ;;
    esac
    DEST="${SRCROOT}/Runner/GoogleService-Info.plist"
    if [ ! -f "$SRC_PLIST" ]; then
      echo "ann-flavor-cocoapods ERROR: Firebase plist not found: $SRC_PLIST"
      exit 1
    fi
    cp "$SRC_PLIST" "$DEST"
    echo "ann-flavor-cocoapods: copied $SRC_PLIST -> $DEST"
  SHELL

  post_build_script = <<~SHELL
    # Injected by ann-flavor-cocoapods: remove copied GoogleService-Info.plist after build
    DEST="${SRCROOT}/Runner/GoogleService-Info.plist"
    rm -f "$DEST"
    echo "ann-flavor-cocoapods: removed $DEST"
  SHELL

  # aggregate_targets are the Podfile's own `target 'Runner' do ... end`
  # definitions; each carries a user_project pointing at the real
  # Runner.xcodeproj (not installer.pods_project).
  touched_projects = []

  installer.aggregate_targets.each do |aggregate_target|
    user_project = aggregate_target.user_project
    next if user_project.nil?

    user_project.native_targets.each do |target|
      # Robust across differently-named projects/targets — an app target's
      # product type, not a hardcoded "Runner"/"Pods-Runner" name.
      next unless target.symbol_type == :application

      # Remove any previously-injected phases before re-adding fresh ones.
      # A presence-only check here (skip if a phase with our marker already
      # exists) would leave a stale script baked into project.pbxproj forever
      # once injected once — pod install does not re-read the gem's current
      # script content into an existing phase, so upgrading the gem alone
      # would never fix an already-injected, now-outdated shell_script.
      target.build_phases.reject! do |p|
        p.respond_to?(:name) && ['[ann-flavor] Copy Firebase plist', '[ann-flavor] Remove Firebase plist'].include?(p.name)
      end

      pre  = target.project.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
      pre.name             = '[ann-flavor] Copy Firebase plist'
      pre.shell_path       = '/bin/sh'
      pre.shell_script     = pre_build_script
      pre.show_env_vars_in_log = '0'
      # Declaring the destination as an output file tells Xcode's build system
      # that this script produces the plist — suppresses the "input file not found"
      # error that fires when the plist is referenced in Copy Bundle Resources.
      pre.output_paths     = ['$(SRCROOT)/Runner/GoogleService-Info.plist']
      target.build_phases.unshift(pre)

      post = target.project.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
      post.name            = '[ann-flavor] Remove Firebase plist'
      post.shell_path      = '/bin/sh'
      post.shell_script    = post_build_script
      post.show_env_vars_in_log = '0'
      post.input_paths     = ['$(SRCROOT)/Runner/GoogleService-Info.plist']
      target.build_phases << post

      touched_projects << user_project unless touched_projects.include?(user_project)

      puts "ann-flavor-cocoapods: injected Firebase build phases into #{target.name} " \
           "(#{config_map.size} configuration(s))"
    end
  end

  # post_integrate runs after CocoaPods' own save pass for the user project
  # (UserProjectIntegrator#integrate! already saved it before this hook
  # fires) — no later save happens automatically, so these changes are
  # silently lost unless saved explicitly here.
  touched_projects.each(&:save)
end

.load_core_spec(project_root) ⇒ Object

Parses annspec.yaml through the shared core (ADR-008) — the single source of truth for parsing, cascade merging, and resolution.



106
107
108
109
# File 'lib/ann_flavor_cocoapods/podfile_helper.rb', line 106

def self.load_core_spec(project_root)
  path = File.join(project_root, SpecLoader::SPEC_FILE)
  AnnFlavorCore::Parser.parse_file(path)
end

.warn_if_static_plist(project_root, xcode_config, config_file) ⇒ Object

Warn if a static GoogleService-Info.plist already exists in the project; a higher-priority static file would shadow the one we copy at build time.



115
116
117
118
119
120
121
122
123
# File 'lib/ann_flavor_cocoapods/podfile_helper.rb', line 115

def self.warn_if_static_plist(project_root, xcode_config, config_file)
  static_path = File.join(project_root, 'ios', 'Runner', 'GoogleService-Info.plist')
  if File.exist?(static_path)
    puts "ann-flavor-cocoapods WARNING: A static GoogleService-Info.plist exists at " \
         "ios/Runner/GoogleService-Info.plist. It will be overwritten at build time " \
         "for configuration '#{xcode_config}' (source: #{config_file}). " \
         "Remove the static file to silence this warning."
  end
end