Class: BranchIOCLI::Command::ReportCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/branch_io_cli/command/report_command.rb

Instance Attribute Summary

Attributes inherited from Command

#config, #options

Instance Method Summary collapse

Methods inherited from Command

#helper, #patch_helper

Constructor Details

#initialize(options) ⇒ ReportCommand

Returns a new instance of ReportCommand.



10
11
12
13
# File 'lib/branch_io_cli/command/report_command.rb', line 10

def initialize(options)
  super
  @config = Configuration::ReportConfiguration.new options
end

Instance Method Details

#base_xcodebuild_cmdObject



101
102
103
104
105
106
107
108
109
# File 'lib/branch_io_cli/command/report_command.rb', line 101

def base_xcodebuild_cmd
  cmd = "xcodebuild"
  if config.workspace_path
    cmd = "#{cmd} -workspace #{config.workspace_path}"
  else
    cmd = "#{cmd} -project #{config.xcodeproj_path}"
  end
  cmd
end

#branch_reportObject

String containing information relevant to Branch setup



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/branch_io_cli/command/report_command.rb', line 334

def branch_report
  infoplist_path = helper.expanded_build_setting config.target, "INFOPLIST_FILE", config.configuration
  infoplist_path = File.expand_path infoplist_path, File.dirname(config.xcodeproj_path)

  report = "Branch configuration:\n"

  begin
    info_plist = File.open(infoplist_path) { |f| Plist.parse_xml f }
    branch_key = info_plist["branch_key"]
    report += " Branch key(s) (Info.plist):\n"
    if branch_key.kind_of? Hash
      branch_key.each_key do |key|
        resolved_key = helper.expand_build_settings branch_key[key], config.target, config.configuration
        report += "  #{key.capitalize}: #{resolved_key}\n"
      end
    elsif branch_key
      resolved_key = helper.expand_build_settings branch_key, config.target, config.configuration
      report += "  #{resolved_key}\n"
    else
      report += "  (none found)\n"
    end

    branch_universal_link_domains = info_plist["branch_universal_link_domains"]
    if branch_universal_link_domains
      if branch_universal_link_domains.kind_of? Array
        report += " branch_universal_link_domains (Info.plist):\n"
        branch_universal_link_domains.each do |domain|
          report += "  #{domain}\n"
        end
      else
        report += " branch_universal_link_domains (Info.plist): #{branch_universal_link_domains}\n"
      end
    end
  rescue StandardError => e
    report += " (Failed to open Info.plist: #{e.message})\n"
  end

  unless config.target.extension_target_type?
    begin
      domains = helper.domains_from_project config.configuration
      report += " Universal Link domains (entitlements):\n"
      domains.each do |domain|
        report += "  #{domain}\n"
      end
    rescue StandardError => e
      report += " (Failed to get Universal Link domains from entitlements file: #{e.message})\n"
    end
  end

  report += report_helper.report_imports

  report
end

#branch_versionObject



111
112
113
114
115
116
# File 'lib/branch_io_cli/command/report_command.rb', line 111

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

#load_settings_from_xcodeObject



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/branch_io_cli/command/report_command.rb', line 388

def load_settings_from_xcode
  cmd = "#{base_xcodebuild_cmd} -scheme #{config.scheme}"
  cmd += " -configuration #{config.configuration} -sdk #{config.sdk} -showBuildSettings"
  @xcodebuild_showbuildsettings_output = ""
  @xcode_settings = {}
  Open3.popen2e(cmd) do |stdin, output, thread|
    while (line = output.gets)
      @xcodebuild_showbuildsettings_output += line
      line.strip!
      next unless (matches = /^(.+)\s+=\s+(.+)$/.match line)
      @xcode_settings[matches[1]] = matches[2]
    end
    @xcodebuild_showbuildsettings_status = thread.value
    return @xcodebuild_showbuildsettings_status.success?
  end
end

#pod_install_required?Boolean

Returns:

  • (Boolean)


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/command/report_command.rb', line 219

def pod_install_required?
  # If this is set, its existence has been verified.
  return false unless config.podfile_path

  lockfile_path = "#{config.podfile_path}.lock"
  manifest_path = File.expand_path "../Pods/Manifest.lock", config.podfile_path

  return true unless File.exist?(lockfile_path) && File.exist?(manifest_path)

  lockfile = Pod::Lockfile.from_file Pathname.new lockfile_path
  manifest = Pod::Lockfile.from_file Pathname.new manifest_path

  # diff the contents of Podfile.lock and Pods/Manifest.lock
  # This is just what is done in the "[CP] Check Pods Manifest.lock" script build phase
  # in a project using CocoaPods.
  return true unless lockfile == manifest

  # compare checksum of Podfile with checksum in Podfile.lock
  # This is a good sanity check, but perhaps unnecessary. It means pod install
  # has not been run since the Podfile was modified, which is probably an oversight.
  return true unless lockfile.to_hash["PODFILE CHECKSUM"] == config.podfile.checksum

  false
end

#report_configurationObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/branch_io_cli/command/report_command.rb', line 202

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

#report_headerObject

rubocop: disable Metrics/PerceivedComplexity



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
328
329
330
# File 'lib/branch_io_cli/command/report_command.rb', line 245

def report_header
  header = "cocoapods-core: #{Pod::CORE_VERSION}\n"

  header += `xcodebuild -version`
  header += "SDK: #{@xcode_settings['SDK_NAME']}\n" if @xcode_settings

  bundle_identifier = helper.expanded_build_setting config.target, "PRODUCT_BUNDLE_IDENTIFIER", config.configuration
  dev_team = helper.expanded_build_setting config.target, "DEVELOPMENT_TEAM", config.configuration
  infoplist_path = helper.expanded_build_setting config.target, "INFOPLIST_FILE", config.configuration
  entitlements_path = helper.expanded_build_setting config.target, "CODE_SIGN_ENTITLEMENTS", config.configuration

  header += "\nTarget #{config.target.name}:\n"
  header += " Bundle identifier: #{bundle_identifier || '(none)'}\n"
  header += " Development team: #{dev_team || '(none)'}\n"
  header += " Deployment target: #{config.target.deployment_target}\n"
  header += " Modules #{config.modules_enabled? ? '' : 'not '}enabled\n"
  header += " Swift #{config.swift_version}\n" if config.swift_version
  header += " Bridging header: #{config.relative_path(config.bridging_header_path)}\n" if config.bridging_header_path
  header += " Info.plist: #{config.relative_path(infoplist_path) || '(none)'}\n"
  header += " Entitlements file: #{config.relative_path(entitlements_path) || '(none)'}\n"

  if config.podfile_path
    begin
      cocoapods_version = `pod --version`.chomp
    rescue Errno::ENOENT
      header += "\n(pod command not found)\n"
    end

    if File.exist?("#{config.podfile_path}.lock")
      podfile_lock = Pod::Lockfile.from_file Pathname.new "#{config.podfile_path}.lock"
    end

    if cocoapods_version || podfile_lock
      header += "\nUsing CocoaPods v. "
      if cocoapods_version
        header += "#{cocoapods_version} (CLI) "
      end
      if podfile_lock
        header += "#{podfile_lock.cocoapods_version} (Podfile.lock)"
      end
      header += "\n"
    end

    target_definition = config.podfile.target_definitions[config.target.name]
    if target_definition
      branch_deps = target_definition.dependencies.select { |p| p.name =~ %r{^(Branch|Branch-SDK)(/.*)?$} }
      header += "Podfile target #{target_definition.name}:"
      header += "\n use_frameworks!" if target_definition.uses_frameworks?
      header += "\n platform: #{target_definition.platform}"
      header += "\n build configurations: #{target_definition.build_configurations}"
      header += "\n inheritance: #{target_definition.inheritance}"
      branch_deps.each do |dep|
        header += "\n pod '#{dep.name}', '#{dep.requirement}'"
        header += ", #{dep.external_source}" if dep.external_source
        header += "\n"
      end
    else
      header += "Target #{config.target.name.inspect} not found in Podfile.\n"
    end

    header += "\npod install #{pod_install_required? ? '' : 'not '}required.\n"
  end

  if config.cartfile_path
    begin
      carthage_version = `carthage version`.chomp
      header += "\nUsing Carthage v. #{carthage_version}\n"
    rescue Errno::ENOENT
      header += "\n(carthage command not found)\n"
    end
  end

  cartfile_requirement = requirement_from_cartfile
  header += "\nFrom Cartfile:\n#{cartfile_requirement}\n" if cartfile_requirement

  version = branch_version
  if version
    header += "\nBranch SDK v. #{version}\n"
  else
    header += "\nBranch SDK not found.\n"
  end

  header += "\n#{branch_report}"

  header
end

#report_helperObject



97
98
99
# File 'lib/branch_io_cli/command/report_command.rb', line 97

def report_helper
  Helper::ReportHelper
end

#requirement_from_cartfileObject



125
126
127
128
129
130
# File 'lib/branch_io_cli/command/report_command.rb', line 125

def requirement_from_cartfile
  return nil unless config.cartfile_path
  cartfile = File.read config.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



118
119
120
121
122
123
# File 'lib/branch_io_cli/command/report_command.rb', line 118

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

#run!Object



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
91
92
93
94
95
# File 'lib/branch_io_cli/command/report_command.rb', line 15

def run!
  say "\n"

  unless load_settings_from_xcode
    say "Failed to load settings from Xcode. Some information may be missing.\n"
  end

  if config.header_only
    say report_header
    exit 0
  end

  # Only if a Podfile is detected/supplied at the command line.
  if pod_install_required?
    say "pod install required in order to build."
    install = ask %{Run "pod install" now (Y/n)? }
    if install.downcase =~ /^n/
      say %{Please run "pod install" or "pod update" first in order to continue.}
      exit(-1)
    end

    helper.verify_cocoapods

    install_command = "pod install"

    if config.pod_repo_update
      install_command += " --repo-update"
    else
      say "You have disabled \"pod repo update\". This can cause \"pod install\" to fail in\nsome cases. If that happens, please rerun without --no-pod-repo-update or run\n\"pod install --repo-update\" manually.\n"
    end

    sh install_command
  end

  File.open config.report_path, "w" do |report|
    report.write "Branch.io Xcode build report v #{VERSION} #{DateTime.now}\n\n"
    report.write "#{report_configuration}\n"
    report.write "#{report_header}\n"

    # run xcodebuild -list
    report.log_command "#{base_xcodebuild_cmd} -list"

    # If using a workspace, -list all the projects as well
    if config.workspace_path
      config.workspace.file_references.map(&:path).each do |project_path|
        path = File.join File.dirname(config.workspace_path), project_path
        report.log_command "xcodebuild -list -project #{path}"
      end
    end

    base_cmd = base_xcodebuild_cmd
    # Add more options for the rest of the commands
    base_cmd = "#{base_cmd} -scheme #{config.scheme}"
    base_cmd = "#{base_cmd} -configuration #{config.configuration} -sdk #{config.sdk}"

    # xcodebuild -showBuildSettings
    report.write "$ #{base_cmd} -showBuildSettings\n\n"
    report.write @xcodebuild_showbuildsettings_output
    if @xcodebuild_showbuildsettings_status.success?
      report.write "Success.\n\n"
    else
      report.write "#{@xcodebuild_showbuildsettings_status}.\n\n"
    end

    if config.clean
      say "Cleaning"
      report.log_command "#{base_cmd} clean"
    end

    say "Building"
    report.log_command "#{base_cmd} -verbose"

    say "Done ✅"
  end

  say "Report generated in #{config.report_path}"
end

#version_from_bnc_config_m(project = @config.xcodeproj) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/branch_io_cli/command/report_command.rb', line 182

def version_from_bnc_config_m(project = @config.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:#{config.relative_path project.path}]"
end

#version_from_branch_frameworkObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/branch_io_cli/command/report_command.rb', line 159

def version_from_branch_framework
  framework = config.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 = config.relative_path(config.xcodeproj_path)
    framework_path = framework.file_ref.real_path
  elsif framework.file_ref.isa == "PBXReferenceProxy" && @xcode_settings
    project_path = config.relative_path framework.file_ref.remote_ref.proxied_object.project.path
    framework_path = File.expand_path framework.file_ref.path, @xcode_settings[framework.file_ref.source_tree]
  end
  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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/branch_io_cli/command/report_command.rb', line 140

def version_from_cartfile_resolved
  return nil unless config.cartfile_path && File.exist?("#{config.cartfile_path}.resolved")
  cartfile_resolved = File.read "#{config.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



132
133
134
135
136
137
138
# File 'lib/branch_io_cli/command/report_command.rb', line 132

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

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