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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 80

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
77
78
# 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 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.
  return false if app_delegate =~ /(import\s+branch|branch\.*initsession)/i

  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



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



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 215

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



170
171
172
173
174
175
176
177
178
179
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 170

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 = load_patch :continue_user_activity_objc
  else
    patch = load_patch :continue_user_activity_new_objc
  end
  patch.apply app_delegate_objc_path
end

.patch_continue_user_activity_method_swift(app_delegate_swift_path) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 142

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 = load_patch :continue_user_activity_swift
  else
    patch = load_patch :continue_user_activity_new_swift
  end
  patch.apply app_delegate_swift_path
end

.patch_did_finish_launching_method_objc(app_delegate_objc_path) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 113

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 = load_patch :did_finish_launching_new_objc
  else
    patch = load_patch :did_finish_launching_objc
  end
  patch.apply app_delegate_objc_path, binding: binding
end

.patch_did_finish_launching_method_swift(app_delegate_swift_path) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 101

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 = load_patch :did_finish_launching_new_swift
  else
    patch = load_patch :did_finish_launching_swift
  end
  patch.apply app_delegate_swift_path, binding: binding
end

.patch_open_url_method_objc(app_delegate_objc_path) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 153

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 = load_patch :open_url_objc
  elsif app_delegate_objc =~ /application:.*openURL:.*sourceApplication/
    # Has application:openURL:sourceApplication:annotation:
    patch = load_patch :open_url_source_annotation_objc
    # TODO: This method is deprecated.
  else
    # Has neither
    patch = load_patch :open_url_new_objc
  end
  patch.apply app_delegate_objc_path
end

.patch_open_url_method_swift(app_delegate_swift_path) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 125

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 = load_patch :open_url_swift
  elsif app_delegate_swift =~ /application.*open\s+url.*sourceApplication/
    # Has application:openURL:sourceApplication:annotation:
    # TODO: This method is deprecated.
    patch = load_patch :open_url_source_application_swift
  else
    # Has neither
    patch = load_patch :open_url_new_swift
  end
  patch.apply app_delegate_swift_path
end

.patch_podfile(podfile_path) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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 181

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



228
229
230
231
232
233
234
# File 'lib/branch_io_cli/helper/patch_helper.rb', line 228

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

Returns:

  • (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