Class: BranchIOCLI::Configuration::ReportConfiguration

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

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, available_options, #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.



13
14
15
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 13

def report_path
  @report_path
end

Class Method Details

.summaryObject



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

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

Instance Method Details

#all_schemesObject



184
185
186
187
188
189
190
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 184

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



341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 341

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



249
250
251
252
253
254
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 249

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

#configurationsObject



240
241
242
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 240

def configurations
  configuration ? [configuration] : configurations_from_scheme
end

#configurations_from_schemeObject



244
245
246
247
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 244

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 58

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

#report_configurationObject

TODO: Collapse the following methods with support for formatting.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 41

def report_configuration
  <<EOF
Configuration:

Xcode workspace: #{workspace_path || '(none)'}
Xcode project: #{xcodeproj_path || '(none)'}
Scheme: #{scheme || '(none)'}
Target: #{target || '(none)'}
Configuration: #{configuration || '(none)'}
SDK: #{sdk}
Podfile: #{relative_path(podfile_path) || '(none)'}
Cartfile: #{relative_path(cartfile_path) || '(none)'}
Pod repo update: #{pod_repo_update.inspect}
Clean: #{clean.inspect}
EOF
end

#requirement_from_cartfileObject



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

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



256
257
258
259
260
261
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 256

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 200

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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 223

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 15

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



155
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
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 155

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 76

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



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 321

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



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

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



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 278

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



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

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



192
193
194
195
196
197
198
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 192

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