Module: BranchIOCLI::Helper::IOSHelper
- Included in:
- BranchHelper
- Defined in:
- lib/branch_io_cli/helper/ios_helper.rb
Constant Summary collapse
- APPLINKS =
"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
- #add_branch_universal_link_domains_to_info_plist(domains) ⇒ Object
- #add_carthage(options) ⇒ Object
- #add_cocoapods(options) ⇒ Object
- #add_direct(options) ⇒ Object
- #add_keys_to_info_plist(keys) ⇒ Object
- #add_universal_links_to_project(domains, remove_existing, configuration = RELEASE_CONFIGURATION) ⇒ Object
- #app_ids_from_aasa_file(domain) ⇒ Object
- #contents_of_aasa_file(domain) ⇒ Object
- #domains_from_project(configuration = RELEASE_CONFIGURATION) ⇒ Object
- #ensure_uri_scheme_in_info_plist ⇒ Object
- #expanded_build_setting(target, setting_name, configuration) ⇒ Object
- #has_multiple_info_plists? ⇒ Boolean
- #patch_app_delegate_objc(project) ⇒ Object
- #patch_app_delegate_swift(project) ⇒ Object
- #patch_cartfile(cartfile_path) ⇒ Object
- #patch_continue_user_activity_method_objc(app_delegate_objc_path) ⇒ Object
- #patch_continue_user_activity_method_swift(app_delegate_swift_path) ⇒ Object
- #patch_did_finish_launching_method_objc(app_delegate_objc_path) ⇒ Object
- #patch_did_finish_launching_method_swift(app_delegate_swift_path) ⇒ Object
- #patch_open_url_method_objc(app_delegate_objc_path) ⇒ Object
- #patch_open_url_method_swift(app_delegate_swift_path) ⇒ Object
- #patch_podfile(podfile_path) ⇒ Object
- #patch_source(xcodeproj) ⇒ Object
- #target_from_project(project, target_name) ⇒ Object
- #team_and_bundle_from_app_id(identifier) ⇒ Object
- #update_cartfile(options, project) ⇒ Object
- #update_info_plist_setting(configuration = RELEASE_CONFIGURATION) {|info_plist| ... } ⇒ Object
- #update_podfile(options) ⇒ Object
- #update_team_and_bundle_ids(team, bundle) ⇒ Object
- #update_team_and_bundle_ids_from_aasa_file(domain) ⇒ Object
- #validate_project_domains(expected, configuration = RELEASE_CONFIGURATION) ⇒ Object
- #validate_team_and_bundle_ids(domain, configuration) ⇒ Object
- #validate_team_and_bundle_ids_from_aasa_files(domains = [], remove_existing = false, configuration = RELEASE_CONFIGURATION) ⇒ Object
- #verify_carthage ⇒ Object
- #verify_cocoapods ⇒ Object
- #verify_git ⇒ Object
Instance Method Details
#add_branch_universal_link_domains_to_info_plist(domains) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 53 def add_branch_universal_link_domains_to_info_plist(domains) # Add all supplied domains unless all are app.link domains. return if domains.all? { |d| d =~ /app\.link$/ } ConfigurationHelper.xcodeproj.build_configurations.each do |config| update_info_plist_setting config.name do |info_plist| info_plist["branch_universal_link_domains"] = domains end end end |
#add_carthage(options) ⇒ Object
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 818 def add_carthage() # TODO: Collapse this and Command::update_cartfile verify_carthage # 1. Generate Cartfile cartfile_path = ConfigurationHelper.cartfile_path File.open(cartfile_path, "w") do |file| file.write <<EOF github "BranchMetrics/ios-branch-deep-linking" EOF end # 2. carthage update Dir.chdir(File.dirname(cartfile_path)) do sh "carthage #{ConfigurationHelper.carthage_command}" end # 3. Add Cartfile and Cartfile.resolved to commit (in case :commit param specified) add_change cartfile_path add_change "#{cartfile_path}.resolved" add_change ConfigurationHelper.xcodeproj_path # 4. Add to target dependencies frameworks_group = ConfigurationHelper.xcodeproj.frameworks_group branch_framework = frameworks_group.new_file "Carthage/Build/iOS/Branch.framework" target = ConfigurationHelper.target target.frameworks_build_phase.add_file_reference branch_framework # 5. Create a copy-frameworks build phase carthage_build_phase = target.new_shell_script_build_phase "carthage copy-frameworks" carthage_build_phase.shell_script = "/usr/local/bin/carthage copy-frameworks" carthage_build_phase.input_paths << "$(SRCROOT)/Carthage/Build/iOS/Branch.framework" carthage_build_phase.output_paths << "$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/Branch.framework" ConfigurationHelper.xcodeproj.save # For now, add Carthage folder to SCM # 6. Add the Carthage folder to the commit (in case :commit param specified) carthage_folder_path = Pathname.new(File.("../Carthage", cartfile_path)).relative_path_from(Pathname.pwd) cartfile_pathname = Pathname.new(cartfile_path).relative_path_from Pathname.pwd add_change carthage_folder_path sh "git add #{cartfile_pathname} #{cartfile_pathname}.resolved #{carthage_folder_path}" if .commit end |
#add_cocoapods(options) ⇒ Object
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 787 def add_cocoapods() verify_cocoapods podfile_path = ConfigurationHelper.podfile_path install_command = "pod install" install_command += " --repo-update" if .pod_repo_update Dir.chdir(File.dirname(podfile_path)) do sh "pod init" apply_patch( files: podfile_path, regexp: /^(\s*)# Pods for #{ConfigurationHelper.target.name}$/, mode: :append, text: "\n\\1pod \"Branch\"", global: false ) sh install_command end add_change podfile_path add_change "#{podfile_path}.lock" # For now, add Pods folder to SCM. pods_folder_path = Pathname.new(File.("../Pods", podfile_path)).relative_path_from Pathname.pwd workspace_path = Pathname.new(File.(ConfigurationHelper.xcodeproj_path.sub(/.xcodeproj$/, ".xcworkspace"))).relative_path_from Pathname.pwd podfile_pathname = Pathname.new(podfile_path).relative_path_from Pathname.pwd add_change pods_folder_path add_change workspace_path sh "git add #{podfile_pathname} #{podfile_pathname}.lock #{pods_folder_path} #{workspace_path}" if .commit end |
#add_direct(options) ⇒ Object
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 864 def add_direct() # Put the framework in the path for any existing Frameworks group in the project. frameworks_group = ConfigurationHelper.xcodeproj.frameworks_group framework_path = File.join frameworks_group.real_path, "Branch.framework" raise "#{framework_path} exists." if File.exist? framework_path say "Finding current framework release" # Find the latest release from GitHub. releases = JSON.parse fetch "https://api.github.com/repos/BranchMetrics/ios-branch-deep-linking/releases" current_release = releases.first # Get the download URL for the framework. framework_asset = current_release["assets"][0] framework_url = framework_asset["browser_download_url"] say "Downloading Branch.framework v. #{current_release['tag_name']} (#{framework_asset['size']} bytes zipped)" Dir.mktmpdir do |download_folder| zip_path = File.join download_folder, "Branch.framework.zip" File.unlink zip_path if File.exist? zip_path # Download the framework zip download framework_url, zip_path say "Unzipping Branch.framework" # Unzip Zip::File.open zip_path do |zip_file| # Start with just the framework and add dSYM, etc., later zip_file.glob "Carthage/Build/iOS/Branch.framework/**/*" do |entry| filename = entry.name.sub %r{^Carthage/Build/iOS}, frameworks_group.real_path.to_s ensure_directory File.dirname filename entry.extract filename end end end # Now the current framework is in framework_path say "Adding to #{ConfigurationHelper.xcodeproj_path}" # Add as a dependency in the Frameworks group framework = frameworks_group.new_file "Branch.framework" # relative to frameworks_group.real_path ConfigurationHelper.target.frameworks_build_phase.add_file_reference framework, true # Make sure this is in the FRAMEWORK_SEARCH_PATHS if we just added it. if frameworks_group.files.count == 1 ConfigurationHelper.target.build_configurations.each do |config| paths = config.build_settings["FRAMEWORK_SEARCH_PATHS"] || [] next if paths.any? { |p| p == '$(SRCROOT)' || p == '$(SRCROOT)/**' } paths << '$(SRCROOT)' config.build_settings["FRAMEWORK_SEARCH_PATHS"] = paths end end # If it already existed, it's almost certainly already in FRAMEWORK_SEARCH_PATHS. ConfigurationHelper.xcodeproj.save add_change ConfigurationHelper.xcodeproj_path add_change framework_path sh "git add #{framework_path}" if .commit say "Done. ✅" end |
#add_keys_to_info_plist(keys) ⇒ Object
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 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 27 def add_keys_to_info_plist(keys) if has_multiple_info_plists? ConfigurationHelper.xcodeproj.build_configurations.each do |config| update_info_plist_setting config.name do |info_plist| if keys.count > 1 # Use test key in debug configs and live key in release configs info_plist["branch_key"] = config.debug? ? keys[:test] : keys[:live] else info_plist["branch_key"] = keys[:live] ? keys[:live] : keys[:test] end end end else update_info_plist_setting RELEASE_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 info_plist["branch_key"] = keys[:test] end end end end |
#add_universal_links_to_project(domains, remove_existing, configuration = RELEASE_CONFIGURATION) ⇒ Object
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 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 110 def add_universal_links_to_project(domains, remove_existing, configuration = RELEASE_CONFIGURATION) project = ConfigurationHelper.xcodeproj target = ConfigurationHelper.target relative_entitlements_path = 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. 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. 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
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 203 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.}" nil end |
#contents_of_aasa_file(domain) ⇒ Object
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 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 225 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.}. 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.} (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.}" nil rescue OpenSSL::PKCS7::PKCS7Error => e @errors << "[#{domain}] Failed to verify signed AASA file: #{e.}" nil end |
#domains_from_project(configuration = RELEASE_CONFIGURATION) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 349 def domains_from_project(configuration = RELEASE_CONFIGURATION) project = ConfigurationHelper.xcodeproj target = ConfigurationHelper.target relative_entitlements_path = target, CODE_SIGN_ENTITLEMENTS, configuration return [] if relative_entitlements_path.nil? project_parent = File.dirname project.path entitlements_path = File. 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_plist ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 64 def ensure_uri_scheme_in_info_plist uri_scheme = ConfigurationHelper.uri_scheme # No URI scheme specified. Do nothing. return if uri_scheme.nil? ConfigurationHelper.xcodeproj.build_configurations.each do |config| update_info_plist_setting config.name do |info_plist| url_types = info_plist["CFBundleURLTypes"] || [] uri_schemes = url_types.inject([]) { |schemes, t| schemes + t["CFBundleURLSchemes"] } # Already present. Don't mess with the identifier. next 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 end end end |
#expanded_build_setting(target, setting_name, configuration) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 366 def (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 = macro_name == "SRCROOT" ? "." : (target, macro_name, configuration) search_position += macro_name.length + 3 and next if .nil? setting_value.gsub!(/\$\(#{macro_name}\)|\$\{#{macro_name}\}/, ) search_position += .length end setting_value end |
#has_multiple_info_plists? ⇒ Boolean
21 22 23 24 25 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 21 def has_multiple_info_plists? ConfigurationHelper.xcodeproj.build_configurations.inject([]) do |files, config| files + [(ConfigurationHelper.target, "INFOPLIST_FILE", config.name)] end.uniq.count > 1 end |
#patch_app_delegate_objc(project) ⇒ Object
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 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 410 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.read app_delegate_objc_path 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 ) patch_did_finish_launching_method_objc app_delegate_objc_path patch_continue_user_activity_method_objc app_delegate_objc_path patch_open_url_method_objc app_delegate_objc_path add_change app_delegate_objc_path true end |
#patch_app_delegate_swift(project) ⇒ Object
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 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 384 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.read app_delegate_swift_path 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 ) patch_did_finish_launching_method_swift app_delegate_swift_path patch_continue_user_activity_method_swift app_delegate_swift_path patch_open_url_method_swift app_delegate_swift_path add_change app_delegate_swift_path true end |
#patch_cartfile(cartfile_path) ⇒ Object
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 769 def patch_cartfile(cartfile_path) cartfile = File.read cartfile_path # 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: "github \"BranchMetrics/ios-branch-deep-linking\"\n", mode: :append ) true end |
#patch_continue_user_activity_method_objc(app_delegate_objc_path) ⇒ Object
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 702 def patch_continue_user_activity_method_objc(app_delegate_objc_path) app_delegate = File.read app_delegate_objc_path if app_delegate =~ /application:.*continueUserActivity:.*restorationHandler:/ continue_user_activity_text = <<-EOF // TODO: Adjust your method as you see fit. if ([[Branch getInstance] continueUserActivity:userActivity]) { return YES; } EOF 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 = <<-EOF - (BOOL)application:(UIApplication *)app continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * _Nullable))restorationHandler { return [[Branch getInstance] continueUserActivity:userActivity]; } EOF apply_patch( files: app_delegate_objc_path, regexp: /\n\s*@end[^@]*\Z/m, text: continue_user_activity_text, mode: :prepend ) end end |
#patch_continue_user_activity_method_swift(app_delegate_swift_path) ⇒ Object
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 645 646 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 611 def patch_continue_user_activity_method_swift(app_delegate_swift_path) app_delegate = File.read app_delegate_swift_path if app_delegate =~ /application:.*continue userActivity:.*restorationHandler:/ # Add something to the top of the method continue_user_activity_text = <<-EOF // TODO: Adjust your method as you see fit. if Branch.getInstance.continue(userActivity) { return true } EOF apply_patch( files: app_delegate_swift_path, regexp: /application:.*continue userActivity:.*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 = <<-EOF func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { return Branch.getInstance().continue(userActivity) } EOF apply_patch( files: app_delegate_swift_path, regexp: /\n\s*\}[^{}]*\Z/m, text: continue_user_activity_text, mode: :prepend ) end end |
#patch_did_finish_launching_method_objc(app_delegate_objc_path) ⇒ Object
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 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 498 def patch_did_finish_launching_method_objc(app_delegate_objc_path) app_delegate_objc = File.read app_delegate_objc_path if app_delegate_objc =~ /didFinishLaunchingWithOptions/m # method exists. patch it. init_session_text = ConfigurationHelper.keys.count <= 1 || has_multiple_info_plists? ? "" : <<EOF #ifdef DEBUG [Branch setUseTestBranchKey:YES]; #endif // DEBUG EOF init_session_text += <<-EOF [[Branch getInstance] initSessionWithLaunchOptions:launchOptions andRegisterDeepLinkHandlerUsingBranchUniversalObject:^(BranchUniversalObject *universalObject, BranchLinkProperties *linkProperties, NSError *error){ // TODO: Route Branch links }]; EOF apply_patch( files: app_delegate_objc_path, regexp: /didFinishLaunchingWithOptions.*?\{[^\n]*\n/m, text: init_session_text, mode: :append ) else # method does not exist. add it. method_text = <<EOF - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { EOF if ConfigurationHelper.keys.count > 1 && !has_multiple_info_plists? method_text += <<EOF #ifdef DEBUG [Branch setUseTestBranchKey:YES]; #endif // DEBUG EOF end method_text += <<-EOF [[Branch getInstance] initSessionWithLaunchOptions:launchOptions andRegisterDeepLinkHandlerUsingBranchUniversalObject:^(BranchUniversalObject *universalObject, BranchLinkProperties *linkProperties, NSError *error){ // TODO: Route Branch links }]; return YES; } EOF apply_patch( files: app_delegate_objc_path, regexp: /^@implementation.*?\n/m, text: method_text, mode: :append ) end end |
#patch_did_finish_launching_method_swift(app_delegate_swift_path) ⇒ Object
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 496 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 436 def patch_did_finish_launching_method_swift(app_delegate_swift_path) app_delegate_swift = File.read app_delegate_swift_path if app_delegate_swift =~ /didFinishLaunching[^\n]+?\{/m # method already present init_session_text = ConfigurationHelper.keys.count <= 1 || has_multiple_info_plists? ? "" : <<EOF #if DEBUG Branch.setUseTestBranchKey(true) #endif EOF init_session_text += <<-EOF Branch.getInstance().initSession(launchOptions: launchOptions) { universalObject, linkProperties, error in // TODO: Route Branch links } EOF apply_patch( files: app_delegate_swift_path, regexp: /didFinishLaunchingWithOptions.*?\{[^\n]*\n/m, text: init_session_text, mode: :append ) else # method not present. add entire method method_text = <<EOF func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { EOF if ConfigurationHelper.keys.count > 1 && !has_multiple_info_plists? method_text += <<EOF #if DEBUG Branch.setUseTestBranchKey(true) #endif EOF end method_text += <<-EOF Branch.getInstance().initSession(launchOptions: launchOptions) { universalObject, linkProperties, error in // TODO: Route Branch links } return true } EOF apply_patch( files: app_delegate_swift_path, regexp: /var\s+window\s?:\s?UIWindow\?.*?\n/m, text: method_text, mode: :append ) end end |
#patch_open_url_method_objc(app_delegate_objc_path) ⇒ Object
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 648 def patch_open_url_method_objc(app_delegate_objc_path) app_delegate_objc = File.read app_delegate_objc_path if app_delegate_objc =~ /application:.*openURL:.*options/ # Has application:openURL:options: open_url_text = <<-EOF // TODO: Adjust your method as you see fit. if ([[Branch getInstance] application:app openURL:url options:options]) { return YES; } EOF 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 = <<-EOF // TODO: Adjust your method as you see fit. if ([[Branch getInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation]) { return YES; } EOF 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 = <<-EOF - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { return [[Branch getInstance] application:app openURL:url options:options]; } EOF apply_patch( files: app_delegate_objc_path, regexp: /\n\s*@end[^@]*\Z/m, text: open_url_text, mode: :prepend ) end end |
#patch_open_url_method_swift(app_delegate_swift_path) ⇒ Object
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 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 557 def patch_open_url_method_swift(app_delegate_swift_path) app_delegate_swift = File.read app_delegate_swift_path if app_delegate_swift =~ /application.*open\s+url.*options/ # Has application:openURL:options: open_url_text = <<-EOF // TODO: Adjust your method as you see fit. if Branch.getInstance().application(app, open: url, options: options) { return true } EOF apply_patch( files: app_delegate_swift_path, regexp: /application.*open\s+url.*options:.*?\{.*?\n/m, text: open_url_text, mode: :append ) elsif app_delegate_swift =~ /application.*open\s+url.*sourceApplication/ # Has application:openURL:sourceApplication:annotation: # TODO: This method is deprecated. open_url_text = <<-EOF // TODO: Adjust your method as you see fit. if Branch.getInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) { return true } EOF apply_patch( files: app_delegate_swift_path, regexp: /application.*open\s+url.*sourceApplication:.*?\{.*?\n/m, text: open_url_text, mode: :append ) else # Has neither open_url_text = <<EOF func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return Branch.getInstance().application(app, open: url, options: options) } EOF 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
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 739 def patch_podfile(podfile_path) podfile = File.read podfile_path # Podfile already contains the Branch pod # TODO: Allow for adding to multiple targets in the Podfile return false if podfile =~ /pod\s+('Branch'|"Branch")/ say "Adding pod \"Branch\" to #{podfile_path}" if podfile =~ /target\s+(["'])#{ConfigurationHelper.target.name}\1\s+do.*?\n/m # if there is a target block for this target: apply_patch( files: podfile_path, regexp: /\n(\s*)target\s+(["'])#{ConfigurationHelper.target.name}\2\s+do.*?\n/m, text: "\\1 pod \"Branch\"\n", mode: :append ) else # add to the abstract_target for this target apply_patch( files: podfile_path, regexp: /^(\s*)target\s+["']#{ConfigurationHelper.target.name}/, text: "\\1pod \"Branch\"\n", mode: :prepend ) end true end |
#patch_source(xcodeproj) ⇒ Object
1011 1012 1013 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 1011 def patch_source(xcodeproj) patch_app_delegate_swift(xcodeproj) || patch_app_delegate_objc(xcodeproj) end |
#target_from_project(project, target_name) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 336 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 targets = project.targets.select { |t| !t.extension_target_type? && !t.test_target_type? } target = targets.find { |t| t.name == File.basename(project.path).sub(/\.xcodeproj$/, "") } || targets.first raise "No application target found" if target.nil? end target end |
#team_and_bundle_from_app_id(identifier) ⇒ Object
156 157 158 159 160 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 156 def team_and_bundle_from_app_id(identifier) team = identifier.sub(/\..+$/, "") bundle = identifier.sub(/^[^.]+\./, "") [team, bundle] end |
#update_cartfile(options, project) ⇒ Object
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 964 def update_cartfile(, project) verify_carthage cartfile_path = ConfigurationHelper.cartfile_path return false if cartfile_path.nil? # 1. Patch Cartfile. Return if no change (Branch already present). return false unless patch_cartfile cartfile_path # 2. carthage update Dir.chdir(File.dirname(cartfile_path)) do sh "carthage #{ConfigurationHelper.carthage_command}" end # 3. Add Cartfile and Cartfile.resolved to commit (in case :commit param specified) add_change cartfile_path add_change "#{cartfile_path}.resolved" add_change ConfigurationHelper.xcodeproj_path # 4. Add to target dependencies frameworks_group = project.frameworks_group branch_framework = frameworks_group.new_file "Carthage/Build/iOS/Branch.framework" target = ConfigurationHelper.target target.frameworks_build_phase.add_file_reference branch_framework # 5. Add to copy-frameworks build phase carthage_build_phase = target.build_phases.find do |phase| phase.respond_to?(:shell_script) && phase.shell_script =~ /carthage\s+copy-frameworks/ end if carthage_build_phase carthage_build_phase.input_paths << "$(SRCROOT)/Carthage/Build/iOS/Branch.framework" carthage_build_phase.output_paths << "$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/Branch.framework" end # 6. Check if Carthage folder is under SCM carthage_folder_path = Pathname.new(File.("../Carthage", cartfile_path)).relative_path_from Pathname.pwd `git ls-files #{carthage_folder_path} --error-unmatch > /dev/null 2>&1` return true unless $?.exitstatus == 0 # 7. If so, add the Carthage folder to the commit (in case :commit param specified) add_change carthage_folder_path sh "git add #{carthage_folder_path}" if .commit true end |
#update_info_plist_setting(configuration = RELEASE_CONFIGURATION) {|info_plist| ... } ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 90 def update_info_plist_setting(configuration = RELEASE_CONFIGURATION, &b) # find the Info.plist paths for this configuration info_plist_path = ConfigurationHelper.target, "INFOPLIST_FILE", configuration raise "Info.plist not found for configuration #{configuration}" if info_plist_path.nil? project_parent = File.dirname ConfigurationHelper.xcodeproj_path info_plist_path = File. 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_podfile(options) ⇒ Object
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 930 def update_podfile() verify_cocoapods podfile_path = ConfigurationHelper.podfile_path return false if podfile_path.nil? # 1. Patch Podfile. Return if no change (Branch pod already present). return false unless patch_podfile podfile_path # 2. pod install # command = "PATH='#{ENV['PATH']}' pod install" command = 'pod install' command += ' --repo-update' if .pod_repo_update Dir.chdir(File.dirname(podfile_path)) do sh command end # 3. Add Podfile and Podfile.lock to commit (in case :commit param specified) add_change podfile_path add_change "#{podfile_path}.lock" # 4. Check if Pods folder is under SCM pods_folder_path = Pathname.new(File.("../Pods", podfile_path)).relative_path_from Pathname.pwd `git ls-files #{pods_folder_path} --error-unmatch > /dev/null 2>&1` return true unless $?.exitstatus == 0 # 5. If so, add the Pods folder to the commit (in case :commit param specified) add_change pods_folder_path sh "git add #{pods_folder_path}" if .commit true end |
#update_team_and_bundle_ids(team, bundle) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 323 def update_team_and_bundle_ids(team, bundle) target = ConfigurationHelper.target 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(domain) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 162 def update_team_and_bundle_ids_from_aasa_file(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 team, bundle add_change ConfigurationHelper.xcodeproj_path end |
#validate_project_domains(expected, configuration = RELEASE_CONFIGURATION) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 303 def validate_project_domains(expected, configuration = RELEASE_CONFIGURATION) @errors = [] project_domains = domains_from_project 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(domain, configuration) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 284 def validate_team_and_bundle_ids(domain, configuration) target = ConfigurationHelper.target product_bundle_identifier = target, PRODUCT_BUNDLE_IDENTIFIER, configuration development_team = 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(domains = [], remove_existing = false, configuration = RELEASE_CONFIGURATION) ⇒ Object
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 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 174 def validate_team_and_bundle_ids_from_aasa_files(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(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 domain, configuration valid &&= domain_valid say "Valid Universal Link configuration for #{domain} ✅" if domain_valid end valid end |
#verify_carthage ⇒ Object
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 1042 def verify_carthage carthage_cmd = `which carthage` return unless carthage_cmd.empty? brew_cmd = `which brew` if brew_cmd.empty? say "'carthage' command not available in PATH and 'brew' command not available in PATH to install 'carthage'." exit(-1) end install = ask "'carthage' command not available in PATH. Use Homebrew to install carthage (Y/n)? " if install.downcase =~ /^n/ say "Please install carthage or use --no-add-sdk to continue." exit(-1) end sh "brew install carthage" end |
#verify_cocoapods ⇒ Object
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 1015 def verify_cocoapods pod_cmd = `which pod` return unless pod_cmd.empty? gem_cmd = `which gem` if gem_cmd.empty? say "'pod' command not available in PATH and 'gem' command not available in PATH to install cocoapods." exit(-1) end install = ask "'pod' command not available in PATH. Install cocoapods (may require a sudo password) (Y/n)? " if install.downcase =~ /^n/ say "Please install cocoapods or use --no-add-sdk to continue." exit(-1) end gem_home = ENV["GEM_HOME"] if gem_home && File.writable?(gem_home) sh "gem install cocoapods" else sh "sudo gem install cocoapods" end # Ensure master podspec repo is set up (will update if it exists). sh "pod setup" end |
#verify_git ⇒ Object
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 |
# File 'lib/branch_io_cli/helper/ios_helper.rb', line 1061 def verify_git return unless ConfigurationHelper.commit git_cmd = `which git` return unless git_cmd.empty? xcode_select_path = `which xcode-select` if xcode_select_path.empty? say "'git' command not available in PATH and 'xcode-select' command not available in PATH to install 'git'." exit(-1) end install = ask "'git' command not available in PATH. Install Xcode command-line tools (requires password) (Y/n)? " if install.downcase =~ /^n/ say "Please install Xcode command tools or leave out the --commit option to continue." exit(-1) end sh "xcode-select --install" end |