Class: BranchIOCLI::Helper::PatchHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/branch_io_cli/helper/patch_helper.rb

Class Method Summary collapse

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

.configObject



12
13
14
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 12

def config
  Configuration::Configuration.current
end

.helperObject



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.expand_path(File.join('..', '..', '..', 'assets', 'patches', "#{name}.yml"), __FILE__)
  PatternPatch::Patch.from_yaml path
end

.patch_app_delegate_objc(project) ⇒ Object



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

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



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

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_headerObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 24

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.message
    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



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 244

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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 199

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 160

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 116

def patch_did_finish_launching_method_objc(app_delegate_objc_path)
  app_delegate_objc = File.read app_delegate_objc_path

  patch_name = "did_finish_launching_"
  if app_delegate_objc =~ /didFinishLaunchingWithOptions/m
    # method exists. patch it.
    patch_name += "test_" unless config.keys.count <= 1 || has_multiple_info_plists?
    patch_name += "objc"
    patch = load_patch patch_name
    patch.regexp = /didFinishLaunchingWithOptions.*?\{[^\n]*\n/m
  else
    # method does not exist. add it.
    patch_name += "new_"
    patch_name += "test_" unless config.keys.count <= 1 || has_multiple_info_plists?
    patch_name += "objc"
    patch = load_patch patch_name
    patch.regexp = /^@implementation.*?\n/m
  end
  patch.apply app_delegate_objc_path
end

.patch_did_finish_launching_method_swift(app_delegate_swift_path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 95

def patch_did_finish_launching_method_swift(app_delegate_swift_path)
  app_delegate_swift = File.read app_delegate_swift_path

  patch_name = "did_finish_launching_"
  if app_delegate_swift =~ /didFinishLaunching[^\n]+?\{/m
    # method already present
    patch_name += "test_" unless config.keys.count <= 1 || has_multiple_info_plists?
    patch_name += "swift"
    patch = load_patch patch_name
    patch.regexp = /didFinishLaunchingWithOptions.*?\{[^\n]*\n/m
  else
    # method not present. add entire method
    patch_name += "new_"
    patch_name += "test_" unless config.keys.count <= 1 || has_multiple_info_plists?
    patch_name += "swift"
    patch = load_patch patch_name
    patch.regexp = /var\s+window\s?:\s?UIWindow\?.*?\n/m
  end
  patch.apply app_delegate_swift_path
end

.patch_open_url_method_objc(app_delegate_objc_path) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 177

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 137

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



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

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



257
258
259
260
261
262
263
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 257

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