Class: BranchIOCLI::Helper::PatchHelper
- Inherits:
-
Object
- Object
- BranchIOCLI::Helper::PatchHelper
- Extended by:
- PatternPatch::Methods
- Defined in:
- lib/branch_io_cli/helper/patch_helper.rb
Class Method Summary collapse
- .add_change(change) ⇒ Object
- .config ⇒ Object
- .helper ⇒ 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_messages_view_controller ⇒ 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
- .swift_file_includes_branch?(path) ⇒ Boolean
- .use_conditional_test_key? ⇒ Boolean
Class Method Details
.add_change(change) ⇒ Object
21 22 23 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 21 def add_change(change) helper.add_change change end |
.config ⇒ Object
13 14 15 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 13 def config Configuration::Configuration.current end |
.helper ⇒ Object
17 18 19 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 17 def helper BranchHelper end |
.patch_app_delegate_objc(project) ⇒ Object
89 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/patch_helper.rb', line 89 def patch_app_delegate_objc(project) return false unless config.patch_source app_delegate_objc_path = config.app_delegate_objc_path return false unless app_delegate_objc_path 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}" 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 68 def patch_app_delegate_swift(project) return false unless config.patch_source app_delegate_swift_path = config.app_delegate_swift_path return false if app_delegate_swift_path.nil? || swift_file_includes_branch?(app_delegate_swift_path) say "Patching #{app_delegate_swift_path}" unless config.bridging_header_required? patch(:swift_import).apply app_delegate_swift_path end 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
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 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 36 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}" if /^\s*(#import|#include|@import)/.match_file config.bridging_header_path # Add among other imports patch(:objc_import).apply config.bridging_header_path elsif /\n\s*#ifndef\s+(\w+).*\n\s*#define\s+\1.*?\n/m.match_file config.bridging_header_path # Has an include guard. Add inside. patch(:objc_import_include_guard).apply config.bridging_header_path else # No imports, no include guard. Add at the end. patch(:objc_import_at_end).apply config.bridging_header_path end helper.add_change config.bridging_header_path end |
.patch_cartfile(cartfile_path) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 257 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}" patch(:cartfile).apply cartfile_path true end |
.patch_continue_user_activity_method_objc(app_delegate_objc_path) ⇒ Object
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 179 def patch_continue_user_activity_method_objc(app_delegate_objc_path) app_delegate_swift = File.read app_delegate_objc_path if app_delegate_swift =~ /application:.*continueUserActivity:.*restorationHandler:/ patch_name = :continue_user_activity_objc else patch_name = :continue_user_activity_new_objc end patch(patch_name).apply app_delegate_objc_path end |
.patch_continue_user_activity_method_swift(app_delegate_swift_path) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 151 def patch_continue_user_activity_method_swift(app_delegate_swift_path) app_delegate_swift = File.read app_delegate_swift_path if app_delegate_swift =~ /application:.*continue userActivity:.*restorationHandler:/ patch_name = :continue_user_activity_swift else patch_name = :continue_user_activity_new_swift end patch(patch_name).apply app_delegate_swift_path end |
.patch_did_finish_launching_method_objc(app_delegate_objc_path) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 122 def patch_did_finish_launching_method_objc(app_delegate_objc_path) app_delegate_objc = File.read app_delegate_objc_path is_new_method = app_delegate_objc !~ /didFinishLaunchingWithOptions/m if is_new_method patch_name = :did_finish_launching_new_objc else patch_name = :did_finish_launching_objc end patch(patch_name).apply app_delegate_objc_path, binding: binding end |
.patch_did_finish_launching_method_swift(app_delegate_swift_path) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 110 def patch_did_finish_launching_method_swift(app_delegate_swift_path) app_delegate_swift = File.read app_delegate_swift_path is_new_method = app_delegate_swift !~ /didFinishLaunching[^\n]+?\{/m if is_new_method patch_name = :did_finish_launching_new_swift else patch_name = :did_finish_launching_swift end patch(patch_name).apply app_delegate_swift_path, binding: binding end |
.patch_messages_view_controller ⇒ Object
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 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 190 def path = config. patch_name = "messages_did_become_active_" case path when nil return false when /\.swift$/ return false if swift_file_includes_branch?(path) unless config.bridging_header_required? patch(:swift_import).apply path end is_new_method = !/didBecomeActive\(with.*?\{[^\n]*\n/m.match_file(path) patch_name += "#{is_new_method ? 'new_' : ''}swift" else return false if %r{^\s+#import\s+<Branch/Branch.h>|^\s+@import\s+Branch\s*;}.match_file(path) patch(:objc_import).apply path is_new_method = !/didBecomeActiveWithConversation.*?\{[^\n]*\n/m.match_file(path) patch_name += "#{is_new_method ? 'new_' : ''}objc" end say "Patching #{path}" patch(patch_name).apply path, binding: binding helper.add_change(path) true end |
.patch_open_url_method_objc(app_delegate_objc_path) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 162 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: patch_name = :open_url_objc elsif app_delegate_objc =~ /application:.*openURL:.*sourceApplication/ # Has application:openURL:sourceApplication:annotation: patch_name = :open_url_source_annotation_objc # TODO: This method is deprecated. else # Has neither patch_name = :open_url_new_objc end patch(patch_name).apply app_delegate_objc_path end |
.patch_open_url_method_swift(app_delegate_swift_path) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 134 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: patch_name = :open_url_swift elsif app_delegate_swift =~ /application.*open\s+url.*sourceApplication/ # Has application:openURL:sourceApplication:annotation: # TODO: This method is deprecated. patch_name = :open_url_source_application_swift else # Has neither patch_name = :open_url_new_swift end patch(patch_name).apply app_delegate_swift_path end |
.patch_podfile(podfile_path) ⇒ Object
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/branch_io_cli/helper/patch_helper.rb', line 223 def patch_podfile(podfile_path) target_definition = config.podfile.target_definitions[config.target.name] raise "Target #{config.target.name} not found in Podfile" unless target_definition # Podfile already contains the Branch pod, possibly just a subspec return false if target_definition.dependencies.any? { |d| d.name =~ %r{^(Branch|Branch-SDK)(/.*)?$} } say "Adding pod \"Branch\" to #{podfile_path}" # It may not be clear from the Pod::Podfile whether the target has a do block. # It doesn't seem to be possible to update the Podfile object and write it out. # So we patch. podfile = File.read config.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
270 271 272 273 274 275 276 277 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 270 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 |
.swift_file_includes_branch?(path) ⇒ Boolean
29 30 31 32 33 34 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 29 def swift_file_includes_branch?(path) # Can't just check for the import here, since there may be a bridging header. # This may match branch.initSession (if the Branch instance is stored) or # Branch.getInstance().initSession, etc. !/branch.*initsession|^\s*import\s+branch/i.match_file(path).nil? end |
.use_conditional_test_key? ⇒ Boolean
25 26 27 |
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 25 def use_conditional_test_key? config.keys.count > 1 && config.setting.nil? && !helper.has_multiple_info_plists? end |