Module: BranchIOCLI::Helper::IOSHelper

Included in:
BranchHelper
Defined in:
lib/branch_io_cli/helper/ios_helper.rb

Constant Summary collapse

"applinks"
ASSOCIATED_DOMAINS =
"com.apple.developer.associated-domains"
CODE_SIGN_ENTITLEMENTS =
"CODE_SIGN_ENTITLEMENTS"
DEVELOPMENT_TEAM =
"DEVELOPMENT_TEAM"
PRODUCT_BUNDLE_IDENTIFIER =
"PRODUCT_BUNDLE_IDENTIFIER"
RELEASE_CONFIGURATION =
"Release"

Instance Method Summary collapse

Instance Method Details



29
30
31
32
33
34
35
36
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 29

def add_branch_universal_link_domains_to_info_plist(project, target_name, domains, configuration = RELEASE_CONFIGURATION)
  # Add all supplied domains unless all are app.link domains.
  return if domains.all? { |d| d =~ /app\.link$/ }

  update_info_plist_setting project, target_name, configuration do |info_plist|
    info_plist["branch_universal_link_domains"] = domains
  end
end

#add_keys_to_info_plist(project, target_name, keys, configuration = RELEASE_CONFIGURATION) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 16

def add_keys_to_info_plist(project, target_name, keys, configuration = RELEASE_CONFIGURATION)
  update_info_plist_setting project, target_name, configuration do |info_plist|
    # add/overwrite Branch key(s)
    if keys.count > 1
      info_plist["branch_key"] = keys
    elsif keys[:live]
      info_plist["branch_key"] = keys[:live]
    else # no need to validate here, which was done by the action
      info_plist["branch_key"] = keys[:test]
    end
  end
end

#add_system_frameworks(project, target_name, frameworks) ⇒ Object



373
374
375
376
377
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 373

def add_system_frameworks(project, target_name, frameworks)
  target = target_from_project project, target_name

  target.add_system_framework frameworks
end


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
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 98

def add_universal_links_to_project(project, target_name, domains, remove_existing, configuration = RELEASE_CONFIGURATION)
  # raises
  target = target_from_project project, target_name

  relative_entitlements_path = expanded_build_setting target, CODE_SIGN_ENTITLEMENTS, configuration
  project_parent = File.dirname project.path

  if relative_entitlements_path.nil?
    relative_entitlements_path = File.join target.name, "#{target.name}.entitlements"
    entitlements_path = File.expand_path relative_entitlements_path, project_parent

    # Add CODE_SIGN_ENTITLEMENTS setting to each configuration
    target.build_configuration_list.set_setting CODE_SIGN_ENTITLEMENTS, relative_entitlements_path

    # Add the file to the project
    project.new_file relative_entitlements_path

    entitlements = {}
    current_domains = []

    add_change project.path
    new_path = entitlements_path
  else
    entitlements_path = File.expand_path relative_entitlements_path, project_parent
    # Raises
    entitlements = File.open(entitlements_path) { |f| Plist.parse_xml f }
    raise "Failed to parse entitlements file #{entitlements_path}" if entitlements.nil?

    if remove_existing
      current_domains = []
    else
      current_domains = entitlements[ASSOCIATED_DOMAINS]
    end
  end

  current_domains += domains.map { |d| "#{APPLINKS}:#{d}" }
  all_domains = current_domains.uniq

  entitlements[ASSOCIATED_DOMAINS] = all_domains

  Plist::Emit.save_plist entitlements, entitlements_path
  add_change entitlements_path

  new_path
end

#app_ids_from_aasa_file(domain) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 191

def app_ids_from_aasa_file(domain)
  data = contents_of_aasa_file domain
  # errors reported in the method above
  return nil if data.nil?

  # raises
  file = JSON.parse data

  applinks = file[APPLINKS]
  @errors << "[#{domain}] No #{APPLINKS} found in AASA file" and return if applinks.nil?

  details = applinks["details"]
  @errors << "[#{domain}] No details found for #{APPLINKS} in AASA file" and return if details.nil?

  identifiers = details.map { |d| d["appID"] }.uniq
  @errors << "[#{domain}] No appID found in AASA file" and return if identifiers.count <= 0
  identifiers
rescue JSON::ParserError => e
  @errors << "[#{domain}] Failed to parse AASA file: #{e.message}"
  nil
end

#contents_of_aasa_file(domain) ⇒ Object



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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 213

def contents_of_aasa_file(domain)
  uris = [
    URI("https://#{domain}/.well-known/apple-app-site-association"),
    URI("https://#{domain}/apple-app-site-association")
    # URI("http://#{domain}/.well-known/apple-app-site-association"),
    # URI("http://#{domain}/apple-app-site-association")
  ]

  data = nil

  uris.each do |uri|
    break unless data.nil?

    Net::HTTP.start uri.host, uri.port, use_ssl: uri.scheme == "https" do |http|
      request = Net::HTTP::Get.new uri
      response = http.request request

      # Better to use Net::HTTPRedirection and Net::HTTPSuccess here, but
      # having difficulty with the unit tests.
      if (300..399).cover?(response.code.to_i)
        say "#{uri} cannot result in a redirect. Ignoring."
        next
      elsif response.code.to_i != 200
        # Try the next URI.
        say "Could not retrieve #{uri}: #{response.code} #{response.message}. Ignoring."
        next
      end

      content_type = response["Content-type"]
      @errors << "[#{domain}] AASA Response does not contain a Content-type header" and next if content_type.nil?

      case content_type
      when %r{application/pkcs7-mime}
        # Verify/decrypt PKCS7 (non-Branch domains)
        cert_store = OpenSSL::X509::Store.new
        signature = OpenSSL::PKCS7.new response.body
        # raises
        signature.verify nil, cert_store, nil, OpenSSL::PKCS7::NOVERIFY
        data = signature.data
      else
        @error << "[#{domain}] Unsigned AASA files must be served via HTTPS" and next if uri.scheme == "http"
        data = response.body
      end

      say "GET #{uri}: #{response.code} #{response.message} (Content-type:#{content_type}) ✅"
    end
  end

  @errors << "[#{domain}] Failed to retrieve AASA file" and return nil if data.nil?

  data
rescue IOError, SocketError => e
  @errors << "[#{domain}] Socket error: #{e.message}"
  nil
rescue OpenSSL::PKCS7::PKCS7Error => e
  @errors << "[#{domain}] Failed to verify signed AASA file: #{e.message}"
  nil
end

#domains_from_project(project, target_name, configuration = RELEASE_CONFIGURATION) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 338

def domains_from_project(project, target_name, configuration = RELEASE_CONFIGURATION)
  # Raises. Does not return nil.
  target = target_from_project project, target_name

  relative_entitlements_path = expanded_build_setting target, CODE_SIGN_ENTITLEMENTS, configuration
  return [] if relative_entitlements_path.nil?

  project_parent = File.dirname project.path
  entitlements_path = File.expand_path relative_entitlements_path, project_parent

  # Raises
  entitlements = File.open(entitlements_path) { |f| Plist.parse_xml f }
  raise "Failed to parse entitlements file #{entitlements_path}" if entitlements.nil?

  entitlements[ASSOCIATED_DOMAINS].select { |d| d =~ /^applinks:/ }.map { |d| d.sub(/^applinks:/, "") }
end

#ensure_uri_scheme_in_info_plistObject



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
70
71
72
73
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 38

def ensure_uri_scheme_in_info_plist
  uri_scheme = ConfigurationHelper.uri_scheme

  # No URI scheme specified. Do nothing.
  return if uri_scheme.nil?

  update_info_plist_setting ConfigurationHelper.xcodeproj,
                            ConfigurationHelper.target.name,
                            RELEASE_CONFIGURATION do |info_plist|
    url_types = info_plist["CFBundleURLTypes"] || []
    uri_schemes = url_types.inject([]) { |schemes, t| schemes + t["CFBundleURLSchemes"] }

    if uri_schemes.empty?
      say "No URI scheme currently defined in project."
    else
      say "Existing URI schemes found in project:"
      uri_schemes.each do |scheme|
        say " #{scheme}"
      end
    end

    # Already present. Don't mess with the identifier.
    return if uri_schemes.include? uri_scheme

    # Not found. Add. Don't worry about the CFBundleURLName (reverse-DNS identifier)
    # TODO: Should we prompt here to add or let them change the Dashboard? If there's already
    # a URI scheme in the app, seems likely they'd want to use it. They may have just made
    # a typo at the CLI or in the Dashboard.
    url_types << {
      "CFBundleURLSchemes" => [uri_scheme]
    }
    info_plist["CFBundleURLTypes"] = url_types

    say "Added URI scheme #{uri_scheme} to project."
  end
end

#expanded_build_setting(target, setting_name, configuration) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 355

def expanded_build_setting(target, setting_name, configuration)
  setting_value = target.resolved_build_setting(setting_name)[configuration]
  return if setting_value.nil?

  search_position = 0
  while (matches = /\$\(([^(){}]*)\)|\$\{([^(){}]*)\}/.match(setting_value, search_position))
    macro_name = matches[1] || matches[2]
    search_position = setting_value.index(macro_name) - 2

    expanded_macro = macro_name == "SRCROOT" ? "." : expanded_build_setting(target, macro_name, configuration)
    search_position += macro_name.length + 3 and next if expanded_macro.nil?

    setting_value.gsub!(/\$\(#{macro_name}\)|\$\{#{macro_name}\}/, expanded_macro)
    search_position += expanded_macro.length
  end
  setting_value
end

#patch_app_delegate_objc(project) ⇒ Object



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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 459

def patch_app_delegate_objc(project)
  app_delegate_objc = project.files.find { |f| f.path =~ /AppDelegate.m$/ }
  return false if app_delegate_objc.nil?

  app_delegate_objc_path = app_delegate_objc.real_path.to_s

  app_delegate = File.open(app_delegate_objc_path, &:read)
  return false if app_delegate =~ %r{^\s+#import\s+<Branch/Branch.h>|^\s+@import\s+Branch;}

  say "Patching #{app_delegate_objc_path}"

  apply_patch(
    files: app_delegate_objc_path,
    regexp: /^\s+@import|^\s+#import.*$/,
    text: "\n#import <Branch/Branch.h>",
    mode: :prepend
  )

  init_session_text = ConfigurationHelper.keys.count <= 1 ? "" : "#ifdef DEBUG\n    [Branch setUseTestBranchKey:YES];\n#endif // DEBUG\n\n"

  init_session_text += "    [[Branch getInstance] initSessionWithLaunchOptions:launchOptions\n  andRegisterDeepLinkHandlerUsingBranchUniversalObject:^(BranchUniversalObject *universalObject, BranchLinkProperties *linkProperties, NSError *error){\n  // TODO: Route Branch links\n    }];\n  EOF\n\n  apply_patch(\n    files: app_delegate_objc_path,\n    regexp: /didFinishLaunchingWithOptions.*?\\{[^\\n]*\\n/m,\n    text: init_session_text,\n    mode: :append\n  )\n\n  if app_delegate =~ /application:.*continueUserActivity:.*restorationHandler:/\n    continue_user_activity_text = <<-EOF\n    // TODO: Adjust your method as you see fit.\n    if ([[Branch getInstance] continueUserActivity:userActivity]) {\n  return YES;\n    }\n\n"

    apply_patch(
      files: app_delegate_objc_path,
      regexp: /application:.*continueUserActivity:.*restorationHandler:.*?\{.*?\n/m,
      text: continue_user_activity_text,
      mode: :append
    )
  else
    # Add the application:continueUserActivity:restorationHandler method if it does not exist
    continue_user_activity_text = "\n\n- (BOOL)application:(UIApplication *)app continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * _Nullable))restorationHandler\n{\n    return [[Branch getInstance] continueUserActivity:userActivity];\n}\n    EOF\n\n    apply_patch(\n      files: app_delegate_objc_path,\n      regexp: /\\n\\s*@end[^@]*\\Z/m,\n      text: continue_user_activity_text,\n      mode: :prepend\n    )\n  end\n\n  patch_open_url_method_objc app_delegate_objc_path\n\n  add_change app_delegate_objc_path\n  true\nend\n"

#patch_app_delegate_swift(project) ⇒ Object



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
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 379

def patch_app_delegate_swift(project)
  app_delegate_swift = project.files.find { |f| f.path =~ /AppDelegate.swift$/ }
  return false if app_delegate_swift.nil?

  app_delegate_swift_path = app_delegate_swift.real_path.to_s

  app_delegate = File.open(app_delegate_swift_path, &:read)
  return false if app_delegate =~ /import\s+Branch/

  say "Patching #{app_delegate_swift_path}"

  apply_patch(
    files: app_delegate_swift_path,
    regexp: /^\s*import .*$/,
    text: "\nimport Branch",
    mode: :prepend
  )

  init_session_text = ConfigurationHelper.keys.count <= 1 ? "" : "  #if DEBUG\n      Branch.setUseTestBranchKey(true)\n  #endif\n\n"

  init_session_text += "  Branch.getInstance().initSession(launchOptions: launchOptions) {\n      universalObject, linkProperties, error in\n\n      // TODO: Route Branch links\n  }\n  EOF\n\n  apply_patch(\n    files: app_delegate_swift_path,\n    regexp: /didFinishLaunchingWithOptions.*?\\{[^\\n]*\\n/m,\n    text: init_session_text,\n    mode: :append\n  )\n\n  if app_delegate =~ /application:.*continue userActivity:.*restorationHandler:/\n    # Add something to the top of the method\n    continue_user_activity_text = <<-EOF\n  // TODO: Adjust your method as you see fit.\n  if Branch.getInstance.continue(userActivity) {\n      return true\n  }\n\n    EOF\n\n    apply_patch(\n      files: app_delegate_swift_path,\n      regexp: /application:.*continue userActivity:.*restorationHandler:.*?\\{.*?\\n/m,\n      text: continue_user_activity_text,\n      mode: :append\n    )\n  else\n    # Add the application:continueUserActivity:restorationHandler method if it does not exist\n    continue_user_activity_text = <<-EOF\n\n\n    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {\n  return Branch.getInstance().continue(userActivity)\n    }\n    EOF\n\n    apply_patch(\n      files: app_delegate_swift_path,\n      regexp: /\\n\\s*\\}[^{}]*\\Z/m,\n      text: continue_user_activity_text,\n      mode: :prepend\n    )\n  end\n\n  patch_open_url_method_swift app_delegate_swift_path\n\n  add_change app_delegate_swift_path\n  true\nend\n"

#patch_cartfile(cartfile_path) ⇒ Object



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 665

def patch_cartfile(cartfile_path)
  cartfile = File.open(cartfile_path, &:read)

  # Cartfile already contains the Branch framework
  return false if cartfile =~ /git.+Branch/

  say "Adding \"Branch\" to #{cartfile_path}"

  apply_patch(
    files: cartfile_path,
    regexp: /\z/,
    text: "git \"https://github.com/BranchMetrics/ios-branch-deep-linking\"\n",
    mode: :append
  )

  true
end

#patch_open_url_method_objc(app_delegate_objc_path) ⇒ Object



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 592

def patch_open_url_method_objc(app_delegate_objc_path)
  app_delegate_objc = File.open app_delegate_objc_path, &:read
  if app_delegate_objc =~ /application:.*openURL:.*options/
    # Has application:openURL:options:
    open_url_text = "    // TODO: Adjust your method as you see fit.\n    if ([[Branch getInstance] application:app openURL:url options:options]) {\n  return YES;\n    }\n\n"

    apply_patch(
      files: app_delegate_objc_path,
      regexp: /application:.*openURL:.*options:.*?\{.*?\n/m,
      text: open_url_text,
      mode: :append
    )
  elsif app_delegate_objc =~ /application:.*openURL:.*sourceApplication/
    # Has application:openURL:sourceApplication:annotation:
    open_url_text = "    // TODO: Adjust your method as you see fit.\n    if ([[Branch getInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation]) {\n  return YES;\n    }\n\n"

    apply_patch(
      files: app_delegate_objc_path,
      regexp: /application:.*openURL:.*sourceApplication:.*?\{.*?\n/m,
      text: open_url_text,
      mode: :append
    )
  else
    # Has neither
    open_url_text = "\n\n- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options\n{\n    return [[Branch getInstance] application:app openURL:url options:options];\n}\n    EOF\n\n    apply_patch(\n      files: app_delegate_objc_path,\n      regexp: /\\n\\s*@end[^@]*\\Z/m,\n      text: open_url_text,\n      mode: :prepend\n    )\n  end\nend\n"

#patch_open_url_method_swift(app_delegate_swift_path) ⇒ Object



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 538

def patch_open_url_method_swift(app_delegate_swift_path)
  app_delegate_swift = File.open app_delegate_swift_path, &:read
  if app_delegate_swift =~ /application.*open\s+url.*options/
    # Has application:openURL:options:
    open_url_text = "  // TODO: Adjust your method as you see fit.\n  if Branch.getInstance().application(app, open: url, options: options) {\n      return true\n  }\n\n    EOF\n\n    apply_patch(\n      files: app_delegate_swift_path,\n      regexp: /application.*open\\s+url.*options:.*?\\{.*?\\n/m,\n      text: open_url_text,\n      mode: :append\n    )\n  elsif app_delegate_swift =~ /application.*open\\s+url.*sourceApplication/\n    # Has application:openURL:sourceApplication:annotation:\n    # TODO: This method is deprecated.\n    open_url_text = <<-EOF\n  // TODO: Adjust your method as you see fit.\n  if Branch.getInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) {\n      return true\n  }\n\n    EOF\n\n    apply_patch(\n      files: app_delegate_swift_path,\n      regexp: /application.*open\\s+url.*sourceApplication:.*?\\{.*?\\n/m,\n      text: open_url_text,\n      mode: :append\n    )\n  else\n    # Has neither\n    open_url_text = <<EOF\n\n\n    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {\n  return Branch.getInstance().application(app, open: url, options: options)\n    }\n"

    apply_patch(
      files: app_delegate_swift_path,
      regexp: /\n\s*\}[^{}]*\Z/m,
      text: open_url_text,
      mode: :prepend
    )
  end
end

#patch_podfile(podfile_path) ⇒ Object



646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 646

def patch_podfile(podfile_path)
  podfile = File.open(podfile_path, &:read)

  # Podfile already contains the Branch pod
  return false if podfile =~ /pod\s+('Branch'|"Branch")/

  say "Adding pod \"Branch\" to #{podfile_path}"

  # TODO: Improve this patch. Should work in the majority of cases for now.
  apply_patch(
    files: podfile_path,
    regexp: /^(\s*)pod\s*/,
    text: "\n\\1pod \"Branch\"\n",
    mode: :prepend
  )

  true
end

#target_from_project(project, target_name) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 326

def target_from_project(project, target_name)
  if target_name
    target = project.targets.find { |t| t.name == target_name }
    raise "Target #{target} not found" if target.nil?
  else
    # find the first application target
    target = project.targets.find { |t| !t.extension_target_type? && !t.test_target_type? }
    raise "No application target found" if target.nil?
  end
  target
end

#team_and_bundle_from_app_id(identifier) ⇒ Object



144
145
146
147
148
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 144

def team_and_bundle_from_app_id(identifier)
  team = identifier.sub(/\..+$/, "")
  bundle = identifier.sub(/^[^.]+\./, "")
  [team, bundle]
end

#update_info_plist_setting(project, target_name, configuration = RELEASE_CONFIGURATION) {|info_plist| ... } ⇒ Object

Yields:

  • (info_plist)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 75

def update_info_plist_setting(project, target_name, configuration = RELEASE_CONFIGURATION, &b)
  # raises
  target = target_from_project project, target_name

  # find the Info.plist paths for this configuration
  info_plist_path = expanded_build_setting target, "INFOPLIST_FILE", configuration

  raise "Info.plist not found for configuration #{configuration}" if info_plist_path.nil?

  project_parent = File.dirname project.path

  info_plist_path = File.expand_path info_plist_path, project_parent

  # try to open and parse the Info.plist (raises)
  info_plist = File.open(info_plist_path) { |f| Plist.parse_xml f }
  raise "Failed to parse #{info_plist_path}" if info_plist.nil?

  yield info_plist

  Plist::Emit.save_plist info_plist, info_plist_path
  add_change info_plist_path
end

#update_team_and_bundle_ids(project, target_name, team, bundle) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 312

def update_team_and_bundle_ids(project, target_name, team, bundle)
  # raises
  target = target_from_project project, target_name

  target.build_configuration_list.set_setting PRODUCT_BUNDLE_IDENTIFIER, bundle
  target.build_configuration_list.set_setting DEVELOPMENT_TEAM, team

  # also update the team in the first test target
  target = project.targets.find(&:test_target_type?)
  return if target.nil?

  target.build_configuration_list.set_setting DEVELOPMENT_TEAM, team
end

#update_team_and_bundle_ids_from_aasa_file(project, target_name, domain) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 150

def update_team_and_bundle_ids_from_aasa_file(project, target_name, domain)
  # raises
  identifiers = app_ids_from_aasa_file domain
  raise "Multiple appIDs found in AASA file" if identifiers.count > 1

  identifier = identifiers[0]
  team, bundle = team_and_bundle_from_app_id identifier

  update_team_and_bundle_ids project, target_name, team, bundle
  add_change project.path.expand_path
end

#validate_project_domains(expected, project, target, configuration = RELEASE_CONFIGURATION) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 292

def validate_project_domains(expected, project, target, configuration = RELEASE_CONFIGURATION)
  @errors = []
  project_domains = domains_from_project project, target, configuration
  valid = expected.count == project_domains.count
  if valid
    sorted = expected.sort
    project_domains.sort.each_with_index do |domain, index|
      valid = false and break unless sorted[index] == domain
    end
  end

  unless valid
    @errors << "Project domains do not match :domains parameter"
    @errors << "Project domains: #{project_domains}"
    @errors << ":domains parameter: #{expected}"
  end

  valid
end

#validate_team_and_bundle_ids(project, target_name, domain, configuration) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 272

def validate_team_and_bundle_ids(project, target_name, domain, configuration)
  # raises
  target = target_from_project project, target_name

  product_bundle_identifier = expanded_build_setting target, PRODUCT_BUNDLE_IDENTIFIER, configuration
  development_team = expanded_build_setting target, DEVELOPMENT_TEAM, configuration

  identifiers = app_ids_from_aasa_file domain
  return false if identifiers.nil?

  app_id = "#{development_team}.#{product_bundle_identifier}"
  match_found = identifiers.include? app_id

  unless match_found
    @errors << "[#{domain}] appID mismatch. Project: #{app_id}. AASA: #{identifiers}"
  end

  match_found
end

#validate_team_and_bundle_ids_from_aasa_files(project, target_name, domains = [], remove_existing = false, configuration = RELEASE_CONFIGURATION) ⇒ Object



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
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 162

def validate_team_and_bundle_ids_from_aasa_files(project, target_name, domains = [], remove_existing = false, configuration = RELEASE_CONFIGURATION)
  @errors = []
  valid = true

  # Include any domains already in the project.
  # Raises. Returns a non-nil array of strings.
  if remove_existing
    # Don't validate domains to be removed (#16)
    all_domains = domains
  else
    all_domains = (domains + domains_from_project(project, target_name, configuration)).uniq
  end

  if all_domains.empty?
    # Cannot get here from SetupBranchAction, since the domains passed in will never be empty.
    # If called from ValidateUniversalLinksAction, this is a failure, possibly caused by
    # failure to add applinks:.
    @errors << "No Universal Link domains in project. Be sure each Universal Link domain is prefixed with applinks:."
    return false
  end

  all_domains.each do |domain|
    domain_valid = validate_team_and_bundle_ids project, target_name, domain, configuration
    valid &&= domain_valid
    say "Valid Universal Link configuration for #{domain} ✅" if domain_valid
  end
  valid
end