Class: FastlaneFlutterFlavor::AnnaiLanes

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb

Overview


AnnaiLanes (Main Wrapper Class)


Instance Method Summary collapse

Constructor Details

#initialize(lane:, platform:, spec_file: nil) ⇒ AnnaiLanes

Refactored initialize to use a single ‘platform’ symbol and accept an optional ‘spec_file’



20
21
22
23
24
25
26
27
28
29
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
66
67
68
69
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb', line 20

def initialize(lane:, platform:, spec_file: nil)
  @lane = lane
  @statusManager = StatusManager.new(lane: lane)

  # Validate the platform input: now includes :web
  unless [:android, :ios, :web].include?(platform)
    raise "Invalid Fastfile configuration: 'platform' must be :android, :ios, or :web"
  end

  # Set internal flags based on the single parameter
  @platform = platform
  @isAndroid = (platform == :android)
  @isIos = (platform == :ios)
  @isWeb = (platform == :web) # New flag for web platform

  # 1. Find the project root folder using ProjectUtil helper
  @root_folder = FastlaneFlutterFlavor::ProjectUtil.find_flutter_root

  # 2. Resolve the main spec file path
  if spec_file
    # If a spec_file path is explicitly provided, resolve it relative to the root folder.
    full_spec_file = File.expand_path(spec_file, @root_folder)
  else
    # Otherwise, use ProjectUtil's logic which checks for ANN_SPEC_FILE env var and defaults.
    # The method handles checking the ANN_SPEC_FILE environment variable and applying defaults.
    full_spec_file = FastlaneFlutterFlavor::ProjectUtil.find_annai_spec_path(nil)
  end

  # full_spec_file contains the absolute path if found/resolved.
  unless File.exist?(full_spec_file)
    error_msg = "Configuration Error: The required specification file was not found. Path checked: #{full_spec_file}. "
    unless spec_file
      error_msg += "Please ensure 'annspec.yaml' exists or set the ANN_SPEC_FILE environment variable correctly."
    end
    raise error_msg
  end

  # Determine the root folder for resolving relative paths used in the spec file
  @spec_root_folder = File.dirname(full_spec_file)

  # Initialize the YamlSpecLoader using the determined root folder and file path
  # We pass the root folder for context, and the full file path for loading
  @specLoader = YamlSpecLoader.new(@root_folder, full_spec_file)

  # Helper classes are only initialized if they are mobile platforms
  @androidLanes = AndroidLanes.new(lane: lane, root_folder:@root_folder) if @isAndroid
  @iosLanes = IosLanes.new(lane: lane, root_folder:@root_folder) if @isIos

  @setup_lanes = SetupLanes.new(root_folder: @root_folder,status_manager: @statusManager)
end

Instance Method Details

#clean_buildObject



497
498
499
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb', line 497

def clean_build
    @setup_lanes.cleanup
end

#compile_build(flavor: "", sub_command: "appbundle", build_config: "release", main_file: "lib/main.dart", skip_sound_null_safety: false, skip_code_sign: false, export_options_plist: "", additional_parameter: "", platform:) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb', line 81

def compile_build(flavor: "", sub_command: "appbundle", build_config: "release",
                  main_file: "lib/main.dart", skip_sound_null_safety: false,
                  skip_code_sign: false, export_options_plist: "", additional_parameter: "", platform:
)

  begin
    # nullSafety is the argument string or empty
    nullSafety = skip_sound_null_safety ? "--no-sound-null-safety" : ""

    # Determine command-line flavor parameters. The original 'flavor' parameter is preserved for logging.
    cli_flavor = flavor
    cli_flavor_option = cli_flavor.empty? ? "" : "--flavor"

    # Initialize platform-specific options as empty
    codeSign = ""
    export_option = ""

    if @isIos
      # Only iOS uses codesign and export options plist
      codeSign = skip_code_sign ? "--no-codesign" : ""
      export_option = export_options_plist.empty? ? "" : "--export-options-plist=#{export_options_plist}"

    elsif @isWeb
      # Web compilation does not support --flavor. Override CLI parameters.
      Fastlane::UI.message("Web compilation detected. Omitting '--flavor' from Flutter build command, but using '#{flavor}' for logging.")
      cli_flavor = ""
      cli_flavor_option = ""
    end

    # Base command parts
    command_parts = [
      "flutter", "build",
      sub_command,
      "--#{build_config}",
    ]

    # Conditionally add nullSafety parameter
    command_parts << nullSafety unless nullSafety.empty?

    # Conditionally add flavor parts (only for non-web, non-empty flavor scenarios)
    if !cli_flavor_option.empty?
      command_parts << cli_flavor_option
      command_parts << cli_flavor
    end

    command_parts << "-t"
    command_parts << main_file
    command_parts << additional_parameter

    # Conditionally add iOS-specific parts
    if @isIos
      command_parts << codeSign unless codeSign.empty?
      command_parts << export_option unless export_option.empty?
    end

    # Ensure the command runs from the Flutter root
    flutter_root = @root_folder

    # Clean up empty strings and execute
    command_to_execute = command_parts.flatten.compact.map(&:to_s).reject(&:empty?)

    Fastlane::UI.message("Executing command: #{command_to_execute.join(' ')}")

    Dir.chdir flutter_root do
      Fastlane::Actions::sh(*command_to_execute)
    end

    # Logging uses the original 'flavor' parameter
    @statusManager.logSuccess flavor, "compile_build", platform
    return true

  rescue => e
    Fastlane::UI.error "Error while compiling flavor #{flavor}"
    Fastlane::UI.error e

    # Logging uses the original 'flavor' parameter
    @statusManager.logError flavor, "compile_build", platform, e
    return false
  end
end

#download_from_store(package_name:, api_key_path:, flavor: "", platform:, use_live_version:, metadata_path:, screenshots_path:, distribution_platform:) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb', line 257

def download_from_store(package_name:, api_key_path:, flavor: "", platform:, use_live_version:, metadata_path:, screenshots_path:, distribution_platform:)
  begin
    if @isWeb
      raise "Invalid call: Web compilation does not support downloading from mobile app stores."
    end

    # API key path is relative to the spec file root
    api_key_absolute_path = File.join(@spec_root_folder, api_key_path)

    if @isAndroid
      @androidLanes.download_from_store(
        package_name: package_name,
        api_key_path: api_key_absolute_path,
        metadata_path: @androidLanes.(metadata_path: , flavor: flavor),
        platform: platform,
        )
    end
    if @isIos
      @iosLanes.download_from_store(
        package_name: package_name,
        api_key_path: api_key_absolute_path,
        metadata_path: @iosLanes.(metadata_path: , flavor: flavor, distribution_platform: distribution_platform),
        screenshots_path: @iosLanes.get_screenshots_path(screenshots_path: screenshots_path, flavor: flavor, distribution_platform: distribution_platform),
        deliver_file: @iosLanes.get_deliver_file(),
        use_live_version: use_live_version,
        )
    end
    @statusManager.logSuccess flavor, "download_from_store", platform
    return true
  rescue => e
    Fastlane::UI.error "Error while downloading from store for flavor #{flavor}"
    Fastlane::UI.error e
    @statusManager.logError flavor, "download_from_store", platform, e
    return false
  end
end

#finalizeObject



71
72
73
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb', line 71

def finalize()
  @statusManager.displayStatus
end

#onError(exception) ⇒ Object

Handles unexpected exceptions during lane execution



76
77
78
79
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb', line 76

def onError(exception)
  @statusManager.logError "", "Unknown Error", exception.message
  @statusManager.displayStatus
end

#upload_to_app_store(bundle_identifier:, api_key_path:, flavor: "", main_file: "lib/main.dart", display_name:, export_option_file:, team_id:, signing_certificate:, app_version:, build_number:, skip_sound_null_safety: false, skip_compile: false, skip_upload_binary: false, skip_upload_prod: false, skip_upload_metadata: false, skip_upload_screenshots: false, export_compliance_uses_encryption: nil, add_id_info_uses_idfa: nil, platform:, metadata_path: nil, screenshots_path: nil, distribution_platform: nil) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb', line 367

def upload_to_app_store(bundle_identifier:, api_key_path:, flavor: "",
                        main_file: "lib/main.dart", display_name:, export_option_file:,
                        team_id:, signing_certificate:, app_version:, build_number:,
                        skip_sound_null_safety: false, skip_compile: false, skip_upload_binary: false,
                        skip_upload_prod: false,
                        skip_upload_metadata: false, skip_upload_screenshots: false,
                        export_compliance_uses_encryption: nil, add_id_info_uses_idfa: nil, platform:,
                        metadata_path: nil, screenshots_path: nil, distribution_platform: nil
)

  begin

    unless @isIos
      # This handles both general invalid calls and the new :web case
      raise "Invalid call to upload_to_app_store. This action is only supported for the :ios platform. Error during uploading of flavor #{flavor}"
    end

    build_config = "release"
    sub_command = "ipa"
    build_config_xcode = "Release"

    # Paths are now relative to the root folder, which is @root_folder
    root_folder = @root_folder
    ipa_folder = File.join(root_folder, "build/ios/ipa/") # Source folder for built IPA
    archive_folder = File.join(root_folder, "build/ios/archive/")
    ipa_flavor_folder = File.join(ipa_folder, flavor, platform) # Destination folder
    ios_project_path = File.join(root_folder, "ios")

    # The path the upload process will use
    ipa_file = File.join(ipa_flavor_folder, display_name + ".ipa")

     = @iosLanes.(metadata_path: , flavor: flavor, distribution_platform: distribution_platform)
    final_screenshots_path = @iosLanes.get_screenshots_path(screenshots_path: screenshots_path, flavor: flavor, distribution_platform: distribution_platform)
    api_key_absolute_path = File.join(@spec_root_folder, api_key_path)

    if flavor != ""
      build_config_xcode = "Release-" + flavor
    end

    # 1. Ensure provisioning profile is correct
    Dir.chdir ios_project_path do
      @iosLanes.sigh(
        app_identifier: bundle_identifier,
        api_key_path: api_key_absolute_path,
        team_id: team_id,
      )
    end

    unless skip_compile

      # 2. Update Xcode code signing settings
      Dir.chdir ios_project_path do
        @iosLanes.update_code_signing_settings(
          team_id: team_id,
          code_sign_identity: signing_certificate,
          profile_name: ENV["SIGH_NAME"],
          build_configurations: build_config_xcode,
        )
      end

      # 3. Compile the build (creates an IPA in ipa_folder)
      unless(
        compile_build(
          flavor: flavor,
          sub_command: sub_command,
          build_config: build_config,
          main_file: main_file,
          skip_sound_null_safety: skip_sound_null_safety,
          skip_code_sign: false,
          export_options_plist: export_option_file,
          platform: platform,
        )
      )
        raise "Error during compilation of flavor #{flavor}"
      end

      # 4. Find the generated IPA, move it to the flavor folder, and rename it.
      ipa_candidates = Dir.glob(File.join(ipa_folder, "*.ipa"))
      if ipa_candidates.empty?
        raise "Compilation Error: No IPA file found in expected directory: #{ipa_folder}"
      end

      original_ipa_path = ipa_candidates.first

      # Create the destination directory
      FileUtils.mkdir_p ipa_flavor_folder

      # Move and rename the IPA file to the expected display_name.ipa
      FileUtils.mv(original_ipa_path, ipa_file)
      Fastlane::UI.message "Moved and renamed IPA from #{File.basename(original_ipa_path)} to #{File.expand_path(ipa_file)}"

      # 5. Backup the XCArchive
      @iosLanes.backup_xcarchive(
        xcarchive: File.join(archive_folder, 'Runner.xcarchive'),
        destination: ipa_flavor_folder,
        zip_filename: 'Runner',
      )
    end

    # 6. Upload to App Store Connect
    unless skip_upload_prod && skip_upload_binary &&  && skip_upload_screenshots
      @iosLanes.upload_to_store(
        ipa_file: ipa_file,
        bundle_identifier: bundle_identifier,
        api_key_path: api_key_absolute_path,
        metadata_path: ,
        screenshots_path: final_screenshots_path,
        app_version: app_version,
        build_number: build_number,
        skip_upload_prod: skip_upload_prod,
        skip_upload_binary: skip_upload_binary,
        skip_upload_metadata: ,
        skip_upload_screenshots: skip_upload_screenshots,
        export_compliance_uses_encryption: export_compliance_uses_encryption,
        add_id_info_uses_idfa: add_id_info_uses_idfa,
        platform: platform,
      )
    end

    @statusManager.logSuccess flavor, "upload_to_app_store", platform
    return true

  rescue => e
    Fastlane::UI.error "Error while uploading to store for flavor #{flavor}"
    Fastlane::UI.error e
    @statusManager.logError flavor, "upload_to_app_store", platform, e
    return false
  end
end

#upload_to_firebase(flavor: "", main_file: "lib/main.dart", build_config: "release", skip_compile: false, skip_sound_null_safety: false, firebase_project:, firebase_token:) ⇒ Object

New method to compile web build and deploy to Firebase Hosting



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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb', line 163

def upload_to_firebase(flavor: "", main_file: "lib/main.dart", build_config: "release",
                       skip_compile: false, skip_sound_null_safety: false,
                       firebase_project:, firebase_token:) # firebase_project is the user override

  begin
    unless @isWeb
      raise "Invalid call to upload_to_firebase. This action is only supported for the :web platform. Error during uploading of flavor #{flavor}"
    end

    # --- 1. Determine the Firebase Project ID (Prioritization Logic) ---
    project_id = firebase_project.to_s.strip # Check user-provided argument first

    if project_id.empty?
      # If user argument is empty, fallback to spec file configuration
      project_id = @specLoader.get_firebase_project_id(@platform, flavor, "release").to_s.strip
    end

    if project_id.empty?
      raise "Missing Firebase Project ID. Please provide it via the 'firebase_project' parameter or define it under 'project_id' in your annai spec file for flavor '#{flavor}' and platform '#{@platform}'."
    end

    final_project_id = project_id
    Fastlane::UI.message("Using Firebase Project ID: '#{final_project_id}'")

    # 2. Compile the web build unless skipped
    unless skip_compile
      unless(
        compile_build(
          flavor: flavor,
          sub_command: "web", # Use 'web' as sub_command for 'flutter build web'
          build_config: build_config,
          main_file: main_file,
          skip_sound_null_safety: skip_sound_null_safety,
          skip_code_sign: true, # Not applicable for web
          export_options_plist: "", # Not applicable for web
          platform: @platform,
        )
      )
        raise "Error during compilation of web flavor #{flavor}"
      end
    end

    # 3. Read the existing firebase.json
    firebase_config_path = File.join(@root_folder, "firebase.json")
    config = JSON.parse(File.read(firebase_config_path))

    # 4. Update the hosting block
    config['hosting'] = {
        "public" => "build/web",
        "ignore" => ["firebase.json", "**/.*", "**/node_modules/**"],
        "rewrites" => [{"source" => "**", "destination" => "/index.html"}]
    }

    # 5. Write the updated firebase.json back to disk
    File.write(firebase_config_path, JSON.pretty_generate(config))

    token = firebase_token.to_s.strip
    if token.empty?
      token = @specLoader.get_firebase_token_from_properties.to_s.strip
      unless token.empty?
        Fastlane::UI.message("Successfully retrieved Firebase token from external properties file.")
      end
    end

    # 6. Deploy to Firebase Hosting using the determined ID
    command_parts = [
      "firebase", "deploy",
      "--only", "hosting",
      "--project", final_project_id,
    ]
    # Only add the token flags if token is present
    if !token.empty?
      command_parts.push("--token", token)
    end

    firebase_root = @root_folder

    Fastlane::UI.message("Executing Firebase deploy command: #{command_parts.join(' ')}")

    Dir.chdir firebase_root do
      Fastlane::Actions::sh(*command_parts)
    end

    @statusManager.logSuccess flavor, "upload_to_firebase", @platform
    return true

  rescue => e
    Fastlane::UI.error "Error while uploading to Firebase for web flavor #{flavor}"
    Fastlane::UI.error e
    @statusManager.logError flavor, "upload_to_firebase", @platform, e
    return false
  end
end

#upload_to_play_store(package_name:, api_key_path:, in_app_update_priority: 1, flavor: "", main_file: "lib/main.dart", track: "beta", track_promote_to: "production", skip_sound_null_safety: false, skip_compile: false, skip_upload_prod: false, skip_upload_binary: false, skip_upload_changelogs: false, skip_upload_metadata: false, skip_upload_images: false, skip_upload_screenshots: false, platform:, metadata_path: nil) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_annai.rb', line 294

def upload_to_play_store(package_name:, api_key_path:, in_app_update_priority: 1, flavor: "",
                         main_file: "lib/main.dart", track: "beta", track_promote_to: "production", skip_sound_null_safety: false, skip_compile: false,
                         skip_upload_prod: false, skip_upload_binary: false, skip_upload_changelogs: false,
                         skip_upload_metadata: false, skip_upload_images: false, skip_upload_screenshots: false, platform:,
                         metadata_path: nil
)

  begin

    unless @isAndroid
      # This handles both general invalid calls and the new :web case
      raise "Invalid call to upload_to_play_store. This action is only supported for the :android platform. Error during uploading of flavor #{flavor}"
    end

    build_config = "release"
    sub_command = "appbundle"
    # Adjusted aabFile path to be relative from the root
    aabFile = "build/app/outputs/bundle/release/app-release.aab"
     = @androidLanes.(metadata_path: , flavor: flavor)

    if flavor != ""
      aabFile = "build/app/outputs/bundle/" + flavor + "Release/app-" + flavor + "-release.aab"
    end

    unless skip_compile
      unless(
        compile_build(
          flavor: flavor,
          sub_command: sub_command,
          build_config: build_config,
          main_file: main_file,
          skip_sound_null_safety: skip_sound_null_safety,
          skip_code_sign: false,
          platform: platform,
        )
      )
        raise "Error during compilation of flavor #{flavor}"
      end
    end

    # Prepend the root folder to the aabFile path since Fastlane actions require absolute paths
    aab_absolute_path = File.join(@root_folder, aabFile)
    # API key path is relative to the spec file root
    api_key_absolute_path = File.join(@spec_root_folder, api_key_path)

    @androidLanes.upload_to_store(
      aab: aab_absolute_path,
      package_name: package_name,
      in_app_update_priority: in_app_update_priority,
      track: track,
      track_promote_to: track_promote_to,
      metadata_path: ,
      api_key_path: api_key_absolute_path,
      skip_upload_prod: skip_upload_prod,
      skip_upload_binary: skip_upload_binary,
      skip_upload_changelogs: skip_upload_changelogs,
      skip_upload_metadata: ,
      skip_upload_images: skip_upload_images,
      skip_upload_screenshots: skip_upload_screenshots,
      platform: platform,
      )

    @statusManager.logSuccess flavor, "upload_to_play_store", platform
    return true

  rescue => e
    Fastlane::UI.error "Error while uploading to store for flavor #{flavor}"
    Fastlane::UI.error e
    @statusManager.logError flavor, "upload_to_play_store", platform, e
    return false
  end
end