Class: BranchIOCLI::Helper::ConfigurationHelper

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

Overview

Processes CLI options. Validates options. Prompts for input in a number of cases.

Constant Summary collapse

/\.app\.link$|\.test-app\.link$/
SDK_OPTIONS =
{
  "Set this project up to use CocoaPods and add the Branch SDK." => :cocoapods,
  "Set this project up to use Carthage and add the Branch SDK." => :carthage,
  "Add Branch.framework directly to the project's dependencies." => :direct,
  "Skip adding the framework to the project." => :skip
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.add_sdkObject (readonly)

Returns the value of attribute add_sdk.



30
31
32
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 30

def add_sdk
  @add_sdk
end

.all_domainsObject (readonly)

Returns the value of attribute all_domains.



23
24
25
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 23

def all_domains
  @all_domains
end

.cartfile_pathObject (readonly)

Returns the value of attribute cartfile_path.



25
26
27
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 25

def cartfile_path
  @cartfile_path
end

.commitObject (readonly)

Returns the value of attribute commit.



33
34
35
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 33

def commit
  @commit
end

.forceObject (readonly)

Returns the value of attribute force.



31
32
33
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 31

def force
  @force
end

.keysObject (readonly)

Returns the value of attribute keys.



22
23
24
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 22

def keys
  @keys
end

.patch_sourceObject (readonly)

Returns the value of attribute patch_source.



32
33
34
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 32

def patch_source
  @patch_source
end

.pod_repo_updateObject (readonly)

Returns the value of attribute pod_repo_update.



28
29
30
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 28

def pod_repo_update
  @pod_repo_update
end

.podfile_pathObject (readonly)

Returns the value of attribute podfile_path.



24
25
26
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 24

def podfile_path
  @podfile_path
end

.sdk_integration_modeObject (readonly)

Returns the value of attribute sdk_integration_mode.



34
35
36
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 34

def sdk_integration_mode
  @sdk_integration_mode
end

.targetObject (readonly)

Returns the value of attribute target.



26
27
28
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 26

def target
  @target
end

.uri_schemeObject (readonly)

Returns the value of attribute uri_scheme.



27
28
29
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 27

def uri_scheme
  @uri_scheme
end

.validateObject (readonly)

Returns the value of attribute validate.



29
30
31
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 29

def validate
  @validate
end

.xcodeprojObject (readonly)

Returns the value of attribute xcodeproj.



21
22
23
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 21

def xcodeproj
  @xcodeproj
end

.xcodeproj_pathObject (readonly)

Returns the value of attribute xcodeproj_path.



20
21
22
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 20

def xcodeproj_path
  @xcodeproj_path
end

Class Method Details

.all_domains_from_domains(domains) ⇒ Object



271
272
273
274
275
276
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 271

def all_domains_from_domains(domains)
  app_link_roots = app_link_roots_from_domains domains
  app_link_subdomains = app_link_subdomains_from_roots app_link_roots
  custom_domains = custom_domains_from_domains domains
  custom_domains + app_link_subdomains
end


231
232
233
234
235
236
237
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 231

def app_link_roots_from_domains(domains)
  return [] if domains.nil?

  domains.select { |d| d =~ APP_LINK_REGEXP }
         .map { |d| d.sub(APP_LINK_REGEXP, '').sub(/-alternate$/, '') }
         .uniq
end


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 244

def app_link_subdomains(root)
  app_link_subdomain = root
  return [] if app_link_subdomain.nil?

  live_key = @keys[:live]
  test_key = @keys[:test]

  domains = []
  unless live_key.nil?
    domains += [
      "#{app_link_subdomain}.app.link",
      "#{app_link_subdomain}-alternate.app.link"
    ]
  end
  unless test_key.nil?
    domains += [
      "#{app_link_subdomain}.test-app.link",
      "#{app_link_subdomain}-alternate.test-app.link"
    ]
  end
  domains
end


267
268
269
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 267

def app_link_subdomains_from_roots(roots)
  roots.inject([]) { |domains, root| domains + app_link_subdomains(root) }
end

.custom_domains_from_domains(domains) ⇒ Object



239
240
241
242
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 239

def custom_domains_from_domains(domains)
  return [] if domains.nil?
  domains.reject { |d| d =~ APP_LINK_REGEXP }.uniq
end


82
83
84
85
86
87
88
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 82

def print_identification(command)
  say <<EOF

<%= color("branch_io #{command} v. #{VERSION}", BOLD) %>

EOF
end


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 90

def print_setup_configuration
  say <<EOF
<%= color('Configuration:', BOLD) %>

<%= color('Xcode project:', BOLD) %> #{@xcodeproj_path}
<%= color('Target:', BOLD) %> #{@target.name}
<%= color('Live key:', BOLD) %> #{@keys[:live] || '(none)'}
<%= color('Test key:', BOLD) %> #{@keys[:test] || '(none)'}
<%= color('Domains:', BOLD) %> #{@all_domains}
<%= color('URI scheme:', BOLD) %> #{@uri_scheme || '(none)'}
<%= color('Podfile:', BOLD) %> #{@podfile_path || '(none)'}
<%= color('Cartfile:', BOLD) %> #{@cartfile_path || '(none)'}
<%= color('Pod repo update:', BOLD) %> #{@pod_repo_update.inspect}
<%= color('Validate:', BOLD) %> #{@validate.inspect}
<%= color('Force:', BOLD) %> #{@force.inspect}
<%= color('Add SDK:', BOLD) %> #{@add_sdk.inspect}
<%= color('Patch source:', BOLD) %> #{@patch_source.inspect}
<%= color('Commit:', BOLD) %> #{@commit.inspect}
<%= color('SDK integration mode:', BOLD) %> #{@sdk_integration_mode || '(none)'}

EOF
end


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

def print_validation_configuration
  say <<EOF
<%= color('Configuration:', BOLD) %>

<%= color('Xcode project:', BOLD) %> #{@xcodeproj_path}
<%= color('Target:', BOLD) %> #{@target.name}
<%= color('Domains:', BOLD) %> #{@all_domains || '(none)'}
EOF
end

.uri_scheme_without_suffix(scheme) ⇒ Object

Removes any trailing :// from the argument and returns a copy



279
280
281
282
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 279

def uri_scheme_without_suffix(scheme)
  return nil if scheme.nil?
  scheme.sub %r{://$}, ""
end

.validate_all_domains(options, required = true) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 140

def validate_all_domains(options, required = true)
  app_link_roots = app_link_roots_from_domains options.domains

  unless options.app_link_subdomain.nil? || app_link_roots.include?(options.app_link_subdomain)
    app_link_roots << options.app_link_subdomain
  end

  # app_link_roots now contains options.app_link_subdomain, if supplied, and the roots of any
  # .app.link or .test-app.link domains provided via options.domains.

  app_link_subdomains = app_link_subdomains_from_roots app_link_roots

  custom_domains = custom_domains_from_domains options.domains

  @all_domains = (app_link_subdomains + custom_domains).uniq

  while required && @all_domains.empty?
    domains = ask "Please enter domains as a comma-separated list: ", ->(str) { str.split "," }

    @all_domains = all_domains_from_domains domains
  end
end

.validate_buildfile_path(options, filename) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 284

def validate_buildfile_path(options, filename)
  # Disable Podfile/Cartfile update if --no-add-sdk is present
  return unless options.add_sdk && @sdk_integration_mode.nil?

  buildfile_path = options.send filename.downcase

  # Was --podfile/--cartfile used?
  if buildfile_path
    # Yes: Validate. Prompt if not valid.
    loop do
      valid = buildfile_path =~ %r{/?#{filename}$}
      say "#{filename} path must end in /#{filename}." unless valid

      if valid
        valid = File.exist? buildfile_path
        say "#{buildfile_path} not found." unless valid
      end

      if valid
        if filename == "Podfile"
          @podfile_path = buildfile_path
        else
          @cartfile_path = buildfile_path
        end
        return
      end

      buildfile_path = ask "Please enter the path to your #{filename}: "
    end
  end

  # No: Check for Podfile/Cartfile next to @xcodeproj_path
  buildfile_path = File.expand_path "../#{filename}", @xcodeproj_path
  return unless File.exist? buildfile_path

  # Exists: Use it (valid if found)
  if filename == "Podfile"
    @podfile_path = buildfile_path
  else
    @cartfile_path = buildfile_path
  end

  @sdk_integration_mode = filename == "Podfile" ? :cocoapods : :carthage
end

.validate_keys_from_setup_options(options) ⇒ Object



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

def validate_keys_from_setup_options(options)
  live_key = options.live_key
  test_key = options.test_key
  @keys = {}
  @keys[:live] = live_key unless live_key.nil?
  @keys[:test] = test_key unless test_key.nil?

  while @keys.empty?
    say "A live key, a test key or both is required."
    live_key = ask "Please enter your live Branch key or use --live_key [enter for none]: "
    test_key = ask "Please enter your test Branch key or use --test_key [enter for none]: "

    @keys[:live] = live_key unless live_key == ""
    @keys[:test] = test_key unless test_key == ""
  end
end

.validate_sdk_addition(options) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 329

def validate_sdk_addition(options)
  return if !options.add_sdk || @sdk_integration_mode

  # If no CocoaPods or Carthage, check to see if the framework is linked.
  target = BranchHelper.target_from_project @xcodeproj, options.target
  return if target.frameworks_build_phase.files.map(&:file_ref).map(&:path).any? { |p| p =~ /Branch.framework$/ }

  # --podfile, --cartfile not specified. No Podfile found. No Cartfile found. No Branch.framework in project.
  # Prompt the user:
  selected = choose do |menu|
    menu.header = "No Podfile or Cartfile specified or found. Here are your options"

    SDK_OPTIONS.each_key { |k| menu.choice k }

    menu.prompt = "What would you like to do?"
  end

  @sdk_integration_mode = SDK_OPTIONS[selected]

  case @sdk_integration_mode
  when :cocoapods
    @podfile_path = File.expand_path "../Podfile", @xcodeproj_path
  when :carthage
    @cartfile_path = File.expand_path "../Cartfile", @xcodeproj_path
  end
end

.validate_setup_options(options) ⇒ 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
67
68
69
70
71
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 36

def validate_setup_options(options)
  print_identification "setup"

  @pod_repo_update = options.pod_repo_update
  @validate = options.validate
  @patch_source = options.patch_source
  @add_sdk = options.add_sdk
  @force = options.force
  @commit = options.commit

  say "--force is ignored when --no-validate is used." if !options.validate && options.force
  if options.cartfile && options.podfile
    say "--cartfile and --podfile are mutually exclusive. Please specify the file to patch."
    exit 1
  end

  validate_xcodeproj_path options
  validate_target options
  validate_keys_from_setup_options options
  validate_all_domains options, !@target.extension_target_type?
  validate_uri_scheme options

  # If neither --podfile nor --cartfile is present, arbitrarily look for a Podfile
  # first.

  # If --cartfile is present, don't look for a Podfile. Just validate that
  # Cartfile.
  validate_buildfile_path options, "Podfile" if options.cartfile.nil?

  # If --podfile is present or a Podfile was found, don't look for a Cartfile.
  validate_buildfile_path options, "Cartfile" if @podfile.nil?

  validate_sdk_addition options

  print_setup_configuration
end

.validate_target(options, allow_extensions = true) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 203

def validate_target(options, allow_extensions = true)
  non_test_targets = @xcodeproj.targets.reject(&:test_target_type?)
  raise "No non-test target found in project" if non_test_targets.empty?

  valid_targets = non_test_targets.reject { |t| !allow_extensions && t.extension_target_type? }

  begin
    target = BranchHelper.target_from_project @xcodeproj, options.target

    # If a test target was explicitly specified.
    raise "Cannot use test targets" if target.test_target_type?

    # If an extension target was explicitly specified for validation.
    raise "Extension targets not allowed for this command" if !allow_extensions && target.extension_target_type?

    @target = target
  rescue StandardError => e
    say e.message

    choice = choose do |menu|
      valid_targets.each { |t| menu.choice t.name }
      menu.prompt = "Which target do you wish to use? "
    end

    @target = @xcodeproj.targets.find { |t| t.name = choice }
  end
end

.validate_uri_scheme(options) ⇒ Object



163
164
165
166
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 163

def validate_uri_scheme(options)
  # No validation at the moment. Just strips off any trailing ://
  @uri_scheme = uri_scheme_without_suffix options.uri_scheme
end

.validate_validation_options(options) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/branch_io_cli/helper/configuration_helper.rb', line 73

def validate_validation_options(options)
  print_identification "validate"

  validate_xcodeproj_path options
  validate_target options, false

  print_validation_configuration
end

.validate_xcodeproj_path(options) ⇒ Object

  1. Look for options.xcodeproj.

  2. If not specified, look for projects under . (excluding anything in Pods or Carthage folder).

  3. If none or more than one found, prompt the user.



171
172
173
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/configuration_helper.rb', line 171

def validate_xcodeproj_path(options)
  if options.xcodeproj
    path = options.xcodeproj
  else
    all_xcodeproj_paths = Dir[File.expand_path(File.join(".", "**/*.xcodeproj"))]
    # find an xcodeproj (ignoring the Pods and Carthage folders)
    # TODO: Improve this filter
    xcodeproj_paths = all_xcodeproj_paths.select do |p|
      valid = true
      Pathname.new(p).each_filename do |f|
        valid = false && break if f == "Carthage" || f == "Pods"
      end
      valid
    end

    path = xcodeproj_paths.first if xcodeproj_paths.count == 1
  end

  loop do
    path = ask "Please enter the path to your Xcode project or use --xcodeproj: " if path.nil?
    # TODO: Allow the user to choose if xcodeproj_paths.count > 0
    begin
      @xcodeproj = Xcodeproj::Project.open path
      @xcodeproj_path = path
      return
    rescue StandardError => e
      say e.message
      path = nil
    end
  end
end