Class: BranchIOCLI::Configuration::ReportConfiguration

Inherits:
Configuration
  • Object
show all
Defined in:
lib/branch_io_cli/configuration/report_configuration.rb

Overview

rubocop: disable Metrics/ClassLength

Instance Attribute Summary collapse

Attributes inherited from Configuration

#cartfile_path, #options, #pod_repo_update, #podfile, #podfile_path, #sdk, #sdk_integration_mode, #target, #workspace, #workspace_path, #xcodeproj, #xcodeproj_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Configuration

absolute_path, #absolute_path, #app_delegate_objc_path, #app_delegate_swift_path, #branch_imports, #branch_imports_from_file, #bridging_header_path, #bridging_header_required?, #confirm_with_user, defaults, #helper, #initialize, #messages_view_controller_path, #method_missing, #modules_enabled?, #open_podfile, open_podfile, #open_xcodeproj, open_xcodeproj, #pod_install_required?, #print_identification, #prompt_for_option, #relative_path, relative_path, #root, root, #swift_version, #target_name, #uses_frameworks?, #validate_buildfile_at_path, #validate_buildfile_path, #validate_target, #validate_xcodeproj_path, wrapper

Constructor Details

This class inherits a constructor from BranchIOCLI::Configuration::Configuration

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BranchIOCLI::Configuration::Configuration

Instance Attribute Details

#report_pathObject (readonly)

Returns the value of attribute report_path.



93
94
95
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 93

def report_path
  @report_path
end

Class Method Details

.available_optionsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 13

def available_options
  [
    Option.new(
      name: :workspace,
      description: "Path to an Xcode workspace",
      type: String,
      example: "MyProject.xcworkspace"
    ),
    Option.new(
      name: :xcodeproj,
      description: "Path to an Xcode project",
      type: String,
      example: "MyProject.xcodeproj"
    ),
    Option.new(
      name: :scheme,
      description: "A scheme from the project or workspace to build",
      type: String,
      example: "MyProjectScheme"
    ),
    Option.new(
      name: :target,
      description: "A target to build",
      type: String,
      example: "MyProjectTarget"
    ),
    Option.new(
      name: :configuration,
      description: "The build configuration to use (default: Scheme-dependent)",
      type: String,
      example: "Debug/Release/CustomConfigName"
    ),
    Option.new(
      name: :sdk,
      description: "Passed as -sdk to xcodebuild",
      type: String,
      example: "iphoneos",
      default_value: "iphonesimulator"
    ),
    Option.new(
      name: :podfile,
      description: "Path to the Podfile for the project",
      type: String,
      example: "/path/to/Podfile"
    ),
    Option.new(
      name: :cartfile,
      description: "Path to the Cartfile for the project",
      type: String,
      example: "/path/to/Cartfile"
    ),
    Option.new(
      name: :clean,
      description: "Clean before attempting to build",
      default_value: true
    ),
    Option.new(
      name: :header_only,
      description: "Write a report header to standard output and exit",
      default_value: false,
      aliases: "-H"
    ),
    Option.new(
      name: :pod_repo_update,
      description: "Update the local podspec repo before installing",
      default_value: true
    ),
    Option.new(
      name: :out,
      description: "Report output path",
      default_value: "./report.txt",
      aliases: "-o",
      example: "./report.txt",
      type: String,
      env_name: "BRANCH_REPORT_PATH"
    )
  ]
end

.summaryObject



9
10
11
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 9

def summary
  "Generate and optionally submit a build diagnostic report."
end

Instance Method Details

#all_schemesObject



264
265
266
267
268
269
270
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 264

def all_schemes
  if workspace_path
    workspace.schemes.keys.reject { |scheme| scheme == "Pods" }
  else
    Xcodeproj::Project.schemes xcodeproj_path
  end
end

#branch_key_setting_from_info_plist(config = configuration || "Release") ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 421

def branch_key_setting_from_info_plist(config = configuration || "Release")
  return @branch_key_setting_from_info_plist if @branch_key_setting_from_info_plist

  infoplist_path = target.expanded_build_setting "INFOPLIST_FILE", config
  infoplist_path = File.expand_path infoplist_path, File.dirname(xcodeproj_path)
  info_plist = File.open(infoplist_path) { |f| Plist.parse_xml f }
  branch_key = info_plist["branch_key"]
  regexp = /^\$\((\w+)\)$|^\$\{(\w+)\}$/
  return nil unless branch_key.kind_of?(String) && (matches = regexp.match branch_key)
  @branch_key_setting_from_info_plist = matches[1] || matches[2]
  @branch_key_setting_from_info_plist
end

#branch_versionObject



329
330
331
332
333
334
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 329

def branch_version
  version_from_podfile_lock ||
    version_from_cartfile_resolved ||
    version_from_branch_framework ||
    version_from_bnc_config_m
end

#configurationsObject



320
321
322
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 320

def configurations
  configuration ? [configuration] : configurations_from_scheme
end

#configurations_from_schemeObject



324
325
326
327
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 324

def configurations_from_scheme
  return ["Debug", "Release"] unless xcscheme
  i[test launch profile archive analyze].map { |pfx| xcscheme.send("#{pfx}_action").build_configuration }.uniq
end

#logObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 138

def log
  super
  say "<%= color('Xcode workspace:', BOLD) %> \#{workspace_path || '(none)'}\n<%= color('Xcode project:', BOLD) %> \#{xcodeproj_path || '(none)'}\n<%= color('Scheme:', BOLD) %> \#{scheme || '(none)'}\n<%= color('Target:', BOLD) %> \#{target || '(none)'}\n<%= color('Configuration:', BOLD) %> \#{configuration || '(none)'}\n<%= color('SDK:', BOLD) %> \#{sdk}\n<%= color('Podfile:', BOLD) %> \#{relative_path(podfile_path) || '(none)'}\n<%= color('Cartfile:', BOLD) %> \#{relative_path(cartfile_path) || '(none)'}\n<%= color('Pod repo update:', BOLD) %> \#{pod_repo_update.inspect}\n<%= color('Clean:', BOLD) %> \#{clean.inspect}\n<%= color('Report path:', BOLD) %> \#{report_path}\n"
end

#report_configurationObject

TODO: Collapse the following methods with support for formatting.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 121

def report_configuration
  "Configuration:\n\nXcode workspace: \#{workspace_path || '(none)'}\nXcode project: \#{xcodeproj_path || '(none)'}\nScheme: \#{scheme || '(none)'}\nTarget: \#{target || '(none)'}\nConfiguration: \#{configuration || '(none)'}\nSDK: \#{sdk}\nPodfile: \#{relative_path(podfile_path) || '(none)'}\nCartfile: \#{relative_path(cartfile_path) || '(none)'}\nPod repo update: \#{pod_repo_update.inspect}\nClean: \#{clean.inspect}\n"
end

#requirement_from_cartfileObject



343
344
345
346
347
348
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 343

def requirement_from_cartfile
  return nil unless cartfile_path
  cartfile = File.read cartfile_path
  matches = %r{^git(hub\s+"|\s+"https://github.com/)BranchMetrics/(ios-branch-deep-linking|iOS-Deferred-Deep-Linking-SDK.*?).*?\n}m.match cartfile
  matches ? matches[0].strip : nil
end

#requirement_from_podfileObject



336
337
338
339
340
341
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 336

def requirement_from_podfile
  return nil unless podfile_path
  podfile = File.read podfile_path
  matches = /\n?\s*pod\s+("Branch"|'Branch').*?\n/m.match podfile
  matches ? matches[0].strip : nil
end

#scheme_with_name(scheme_name) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 280

def scheme_with_name(scheme_name)
  if workspace_path
    project_path = workspace.schemes[@scheme]
  else
    project_path = xcodeproj_path
  end

  # Look for a shared scheme.
  xcshareddata_path = File.join project_path, "xcshareddata", "xcschemes", "#{@scheme}.xcscheme"
  scheme_path = xcshareddata_path if File.exist?(xcshareddata_path)

  unless scheme_path
    # Look for a local scheme
    user = ENV["USER"]
    xcuserdata_path = File.join project_path, "xcuserdata", "#{user}.xcuserdatad", "xcschemes", "#{@scheme}.xcscheme"
    scheme_path = xcuserdata_path if File.exist?(xcuserdata_path)
  end

  return nil unless scheme_path

  Xcodeproj::XCScheme.new(scheme_path)
end

#validate_configuration(options) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 303

def validate_configuration(options)
  return unless options.configuration

  all_configs = xcodeproj.build_configurations.map(&:name)

  if all_configs.include?(options.configuration)
    @configuration = options.configuration
  else
    say "Configuration #{options.configuration} not found."
    @configuration = choose do |menu|
      menu.header = "Configurations from project"
      all_configs.each { |c| menu.choice c }
      menu.prompt = "Please choose one of the above. "
    end
  end
end

#validate_optionsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 95

def validate_options
  @clean = options.clean
  @header_only = options.header_only
  @scheme = options.scheme
  @target = options.target
  @report_path = options.out
  @sdk = options.sdk
  @pod_repo_update = options.pod_repo_update

  validate_xcodeproj_and_workspace options
  validate_scheme options
  validate_target options
  validate_configuration 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, "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, "Cartfile") if sdk_integration_mode.nil?
end

#validate_scheme(options) ⇒ Object

rubocop: enable Metrics/PerceivedComplexity



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 235

def validate_scheme(options)
  schemes = all_schemes

  if options.scheme && schemes.include?(options.scheme)
    @scheme = options.scheme
  elsif schemes.count == 1
    @scheme = schemes.first
    say "Scheme #{options.scheme} not found. Using #{@scheme}." if options.scheme
  elsif !schemes.empty?
    # By default, take a scheme with the same name as the project name.
    return if !options.scheme && (@scheme = schemes.find { |s| s == File.basename(xcodeproj_path, '.xcodeproj') })

    @scheme = choose do |menu|
      menu.header = "Schemes from project"
      schemes.each { |s| menu.choice s }
      menu.prompt = "Please choose one of the schemes above. "
    end
  else
    say "No scheme defined in project."
    exit(-1)
  end

  return if options.target || xcscheme.nil?

  # Find the target used when running the scheme if the user didn't specify one.
  entry = xcscheme.build_action.entries.select(&:build_for_running?).first
  @target = xcodeproj.targets.find { |t| t.name == entry.buildable_references.first.target_name }
end

#validate_xcodeproj_and_workspace(options) ⇒ Object

rubocop: disable Metrics/PerceivedComplexity



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
202
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
230
231
232
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 156

def validate_xcodeproj_and_workspace(options)
  # 1. What was passed in?
  begin
    if options.workspace
      path = options.workspace
      @workspace = Xcodeproj::Workspace.new_from_xcworkspace options.workspace
      @workspace_path = File.expand_path options.workspace
    end
    if options.xcodeproj
      path = options.xcodeproj
      @xcodeproj = Xcodeproj::Project.open options.xcodeproj
      @xcodeproj_path = File.expand_path options.xcodeproj
    else
      # Pass --workspace and --xcodeproj to override this inference.
      if workspace && workspace.file_references.count > 0 && workspace.file_references.first.path =~ /\.xcodeproj$/
        @xcodeproj_path = File.expand_path "../#{@workspace.file_references.first.path}", workspace_path
        @xcodeproj = Xcodeproj::Project.open xcodeproj_path
      end
    end
    return if @workspace || @xcodeproj
  rescue StandardError => e
    say e.message
  end

  # Try to find first a workspace, then a project
  all_workspace_paths = Dir[File.expand_path(File.join(".", "**/*.xcworkspace"))]
                        .reject { |w| w =~ %r{/project.xcworkspace$} }
                        .select do |p|
    valid = true
    Pathname.new(p).each_filename do |f|
      valid = false && break if f == "Carthage" || f == "Pods"
    end
    valid
  end

  if all_workspace_paths.count == 1
    path = all_workspace_paths.first
  elsif all_workspace_paths.count == 0
    all_xcodeproj_paths = Dir[File.expand_path(File.join(".", "**/*.xcodeproj"))]
    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
  # If more than one workspace. Don't try to find a project. Just prompt.

  loop do
    path = ask "Please enter a path to your Xcode project or workspace: " if path.nil?
    begin
      if path =~ /\.xcworkspace$/
        @workspace = Xcodeproj::Workspace.new_from_xcworkspace path
        @workspace_path = File.expand_path path

        # Pass --workspace and --xcodeproj to override this inference.
        if workspace.file_references.count > 0 && workspace.file_references.first.path =~ /\.xcodeproj$/
          @xcodeproj_path = File.expand_path "../#{workspace.file_references.first.path}", workspace_path
          @xcodeproj = Xcodeproj::Project.open xcodeproj_path
        end

        return
      elsif path =~ /\.xcodeproj$/
        @xcodeproj = Xcodeproj::Project.open path
        @xcodeproj_path = File.expand_path path
        return
      else
        say "Path must end with .xcworkspace or .xcodeproj"
      end
    rescue StandardError => e
      say e.message
    end
  end
end

#version_from_bnc_config_m(project = xcodeproj) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 401

def version_from_bnc_config_m(project = xcodeproj)
  # Look for BNCConfig.m in embedded source
  bnc_config_m_ref = project.files.find { |f| f.path =~ /BNCConfig\.m$/ }
  unless bnc_config_m_ref
    subprojects = project.files.select { |f| f.path =~ /\.xcodeproj$/ }
    subprojects.each do |subproject|
      p = Xcodeproj::Project.open subproject.real_path
      version = version_from_bnc_config_m p
      return version if version
    end
  end

  return nil unless bnc_config_m_ref
  bnc_config_m = File.read bnc_config_m_ref.real_path
  matches = /BNC_SDK_VERSION\s+=\s+@"(\d+\.\d+\.\d+)"/m.match bnc_config_m
  return nil unless matches
  version = matches[1]
  "#{version} [BNCConfig.m:#{relative_path project.path}]"
end

#version_from_branch_framework(configuration = configurations.first) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 377

def version_from_branch_framework(configuration = configurations.first)
  framework = target.frameworks_build_phase.files.find { |f| f.file_ref.path =~ /Branch.framework$/ }
  return nil unless framework

  if framework.file_ref.isa == "PBXFileReference"
    project_path = relative_path(xcodeproj_path)
    framework_path = framework.file_ref.real_path
  elsif framework.file_ref.isa == "PBXReferenceProxy" && XcodeSettings[configuration].valid?
    project_path = relative_path framework.file_ref.remote_ref.proxied_object.project.path
    framework_path = File.expand_path framework.file_ref.path, XcodeSettings[configuration][framework.file_ref.source_tree]
  end
  return nil unless framework_path
  info_plist_path = File.join framework_path.to_s, "Info.plist"
  return nil unless File.exist? info_plist_path

  require "cfpropertylist"

  raw_info_plist = CFPropertyList::List.new file: info_plist_path
  info_plist = CFPropertyList.native_types raw_info_plist.value
  version = info_plist["CFBundleVersion"]
  return nil unless version
  "#{version} [Branch.framework/Info.plist:#{project_path}]"
end

#version_from_cartfile_resolvedObject



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 358

def version_from_cartfile_resolved
  return nil unless cartfile_path && File.exist?("#{cartfile_path}.resolved")
  cartfile_resolved = File.read "#{cartfile_path}.resolved"

  # Matches:
  # git "https://github.com/BranchMetrics/ios-branch-deep-linking"
  # git "https://github.com/BranchMetrics/ios-branch-deep-linking/"
  # git "https://github.com/BranchMetrics/iOS-Deferred-Deep-Linking-SDK"
  # git "https://github.com/BranchMetrics/iOS-Deferred-Deep-Linking-SDK/"
  # github "BranchMetrics/ios-branch-deep-linking"
  # github "BranchMetrics/ios-branch-deep-linking/"
  # github "BranchMetrics/iOS-Deferred-Deep-Linking-SDK"
  # github "BranchMetrics/iOS-Deferred-Deep-Linking-SDK/"
  matches = %r{(ios-branch-deep-linking|iOS-Deferred-Deep-Linking-SDK)/?" "(\d+\.\d+\.\d+)"}m.match cartfile_resolved
  return nil unless matches
  version = matches[2]
  "#{version} [Cartfile.resolved]"
end

#version_from_podfile_lockObject



350
351
352
353
354
355
356
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 350

def version_from_podfile_lock
  return nil unless podfile_path && File.exist?("#{podfile_path}.lock")
  podfile_lock = Pod::Lockfile.from_file Pathname.new "#{podfile_path}.lock"
  version = podfile_lock.version("Branch") || podfile_lock.version("Branch-SDK")

  version ? "#{version} [Podfile.lock]" : nil
end

#xcschemeObject



272
273
274
275
276
277
278
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 272

def xcscheme
  return @xcscheme if @xcscheme_checked
  # This may not exist. If it comes back nil once, don't keep checking.
  @xcscheme_checked = true
  @xcscheme = scheme_with_name @scheme
  @xcscheme
end