Class: BranchIOCLI::Helper::PatchHelper
- Inherits:
-
Object
- Object
- BranchIOCLI::Helper::PatchHelper
- Defined in:
- lib/branch_io_cli/helper/patch_helper.rb
Class Method Summary collapse
- .add_change(change) ⇒ Object
- .config ⇒ Object
- .helper ⇒ Object
- .load_patch(name) ⇒ Object
- .patch_app_delegate_objc(project) ⇒ Object
- .patch_app_delegate_swift(project) ⇒ Object
- .patch_bridging_header ⇒ 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
- .use_conditional_test_key? ⇒ Boolean
Class Method Details
.add_change(change) ⇒ Object
20 21 22 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 20 def add_change(change) helper.add_change change end |
.config ⇒ Object
12 13 14 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 12 def config Configuration::Configuration.current end |
.helper ⇒ Object
16 17 18 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 16 def helper BranchHelper end |
.load_patch(name) ⇒ Object
7 8 9 10 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 7 def load_patch(name) path = File.(File.join('..', '..', '..', 'assets', 'patches', "#{name}.yml"), __FILE__) PatternPatch::Patch.from_yaml path end |
.patch_app_delegate_objc(project) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 78 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\s*;} say "Patching #{app_delegate_objc_path}" load_patch(:objc_import).apply app_delegate_objc_path 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 51 def patch_app_delegate_swift(project) return false unless config.swift_version 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 # Can't check for the import here, since there may be a bridging header. return false if app_delegate =~ /Branch\.initSession/ unless config.bridging_header_required? load_patch(:swift_import).apply app_delegate_swift_path end say "Patching #{app_delegate_swift_path}" 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_bridging_header ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 28 def patch_bridging_header unless config.bridging_header_path say "Modules not available and bridging header not found. Cannot import Branch." say "Please add use_frameworks! to your Podfile and/or enable modules in your project or use --no-patch-source." exit(-1) end begin bridging_header = File.read config.bridging_header_path return false if bridging_header =~ %r{^\s+#import\s+<Branch/Branch.h>|^\s+@import\s+Branch\s*;} rescue RuntimeError => e say e. say "Cannot read #{config.bridging_header_path}." say "Please correct this setting or use --no-patch-source." exit(-1) end say "Patching #{config.bridging_header_path}" load_patch(:objc_import).apply config.bridging_header_path helper.add_change config.bridging_header_path end |
.patch_cartfile(cartfile_path) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 236 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}" load_patch(:cartfile).apply cartfile_path true end |
.patch_continue_user_activity_method_objc(app_delegate_objc_path) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 191 def patch_continue_user_activity_method_objc(app_delegate_objc_path) app_delegate = File.read app_delegate_objc_path patch_name = "continue_user_activity_" if app_delegate =~ /application:.*continueUserActivity:.*restorationHandler:/ patch_name += "objc" patch = load_patch patch_name patch.regexp = /application:.*continueUserActivity:.*restorationHandler:.*?\{.*?\n/m else # Add the application:continueUserActivity:restorationHandler method if it does not exist patch_name += "new_objc" patch = load_patch patch_name patch.regexp = /\n\s*@end[^@]*\Z/m end patch.apply app_delegate_objc_path end |
.patch_continue_user_activity_method_swift(app_delegate_swift_path) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 152 def patch_continue_user_activity_method_swift(app_delegate_swift_path) app_delegate = File.read app_delegate_swift_path patch_name = "continue_user_activity_" if app_delegate =~ /application:.*continue userActivity:.*restorationHandler:/ # Add something to the top of the method patch_name += "swift" patch = load_patch patch_name patch.regexp = /application:.*continue userActivity:.*restorationHandler:.*?\{.*?\n/m else # Add the application:continueUserActivity:restorationHandler method if it does not exist patch_name += "new_swift" patch = load_patch patch_name patch.regexp = /\n\s*\}[^{}]*\Z/m end patch.apply app_delegate_swift_path end |
.patch_did_finish_launching_method_objc(app_delegate_objc_path) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 114 def patch_did_finish_launching_method_objc(app_delegate_objc_path) app_delegate_objc = File.read app_delegate_objc_path patch = load_patch(:did_finish_launching_objc) is_new_method = app_delegate_objc !~ /didFinishLaunchingWithOptions/m if is_new_method # method does not exist. add it. patch.regexp = /^@implementation.*?\n/m else # method exists. patch it. patch.regexp = /didFinishLaunchingWithOptions.*?\{[^\n]*\n/m end patch.apply app_delegate_objc_path, binding: binding end |
.patch_did_finish_launching_method_swift(app_delegate_swift_path) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 99 def patch_did_finish_launching_method_swift(app_delegate_swift_path) app_delegate_swift = File.read app_delegate_swift_path patch = load_patch(:did_finish_launching_swift) is_new_method = app_delegate_swift !~ /didFinishLaunching[^\n]+?\{/m if is_new_method # method not present. add entire method patch.regexp = /var\s+window\s?:\s?UIWindow\?.*?\n/m else # method already present patch.regexp = /didFinishLaunchingWithOptions.*?\{[^\n]*\n/m end patch.apply app_delegate_swift_path, binding: binding end |
.patch_open_url_method_objc(app_delegate_objc_path) ⇒ Object
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/patch_helper.rb', line 169 def patch_open_url_method_objc(app_delegate_objc_path) app_delegate_objc = File.read app_delegate_objc_path patch_name = "open_url_" if app_delegate_objc =~ /application:.*openURL:.*options/ # Has application:openURL:options: patch_name += "objc" patch = load_patch patch_name patch.regexp = /application:.*openURL:.*options:.*?\{.*?\n/m elsif app_delegate_objc =~ /application:.*openURL:.*sourceApplication/ # Has application:openURL:sourceApplication:annotation: patch_name += "source_application_objc" patch = load_patch patch_name patch.regexp = /application:.*openURL:.*sourceApplication:.*?\{.*?\n/m else # Has neither patch_name += "new_objc" patch = load_patch patch_name patch.regexp = /\n\s*@end[^@]*\Z/m end patch.apply app_delegate_objc_path end |
.patch_open_url_method_swift(app_delegate_swift_path) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 129 def patch_open_url_method_swift(app_delegate_swift_path) app_delegate_swift = File.read app_delegate_swift_path patch_name = "open_url_" if app_delegate_swift =~ /application.*open\s+url.*options/ # Has application:openURL:options: patch_name += "swift" patch = load_patch patch_name patch.regexp = /application.*open\s+url.*options:.*?\{.*?\n/m elsif app_delegate_swift =~ /application.*open\s+url.*sourceApplication/ # Has application:openURL:sourceApplication:annotation: # TODO: This method is deprecated. patch_name += "source_application_swift" patch = load_patch patch_name patch.regexp = /application.*open\s+url.*sourceApplication:.*?\{.*?\n/m else # Has neither patch_name += "new_swift" patch = load_patch patch_name patch.regexp = /\n\s*\}[^{}]*\Z/m end patch.apply app_delegate_swift_path end |
.patch_podfile(podfile_path) ⇒ Object
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 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 207 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+(["'])#{config.target.name}\1\s+do.*?\n/m # if there is a target block for this target: patch = PatternPatch::Patch.new( regexp: /\n(\s*)target\s+(["'])#{config.target.name}\2\s+do.*?\n/m, text: "\\1 pod \"Branch\"\n", mode: :append ) else # add to the abstract_target for this target patch = PatternPatch::Patch.new( regexp: /^(\s*)target\s+["']#{config.target.name}/, text: "\\1pod \"Branch\"\n", mode: :prepend ) end patch.apply podfile_path true end |
.patch_source(xcodeproj) ⇒ Object
249 250 251 252 253 254 255 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 249 def patch_source(xcodeproj) # Patch the bridging header any time Swift imports are not available, # to make Branch available throughout the app, whether the AppDelegate # is in Swift or Objective-C. patch_bridging_header if config.bridging_header_required? patch_app_delegate_swift(xcodeproj) || patch_app_delegate_objc(xcodeproj) end |
.use_conditional_test_key? ⇒ Boolean
24 25 26 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 24 def use_conditional_test_key? config.keys.count > 1 && !helper.has_multiple_info_plists? end |