Class: BranchIOCLI::Commands::ReportCommand

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

Instance Attribute Summary

Attributes inherited from Command

#options

Instance Method Summary collapse

Methods inherited from Command

#config_helper, #helper

Constructor Details

#initialize(options) ⇒ ReportCommand

Returns a new instance of ReportCommand.



6
7
8
9
# File 'lib/branch_io_cli/commands/report_command.rb', line 6

def initialize(options)
  super
  config_helper.validate_report_options options
end

Instance Method Details

#base_xcodebuild_cmdObject



60
61
62
63
64
65
66
67
68
# File 'lib/branch_io_cli/commands/report_command.rb', line 60

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

#branch_versionObject



70
71
72
73
74
75
# File 'lib/branch_io_cli/commands/report_command.rb', line 70

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

#report_configurationObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/branch_io_cli/commands/report_command.rb', line 144

def report_configuration
  <<EOF
Configuration:

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

#report_headerObject

rubocop: disable Metrics/PerceivedComplexity



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
# File 'lib/branch_io_cli/commands/report_command.rb', line 161

def report_header
  header = `xcodebuild -version`

  header += "\nTarget #{config_helper.target.name} deploy target: #{config_helper.target.deployment_target}\n"

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

    if File.exist?("#{config_helper.podfile_path}.lock")
      podfile_lock = Pod::Lockfile.from_file Pathname.new "#{config_helper.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

    # Already verified existence.
    podfile = Pod::Podfile.from_file Pathname.new config_helper.podfile_path
    target_definition = podfile.target_definition_list.find { |t| t.name == config_helper.target.name }
    if target_definition
      branch_dep = target_definition.dependencies.find { |p| p.name == "Branch" }
      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}"
      header += "\n pod 'Branch', '#{branch_dep.requirement}'" if branch_dep
      header += ", #{branch_dep.external_source}" if branch_dep && branch_dep.external_source
      header += "\n"
    else
      header += "Target #{config_helper.target.name.inspect} not found in Podfile.\n"
    end

  end

  if config_helper.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
end

#requirement_from_cartfileObject



84
85
86
87
88
89
# File 'lib/branch_io_cli/commands/report_command.rb', line 84

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



77
78
79
80
81
82
# File 'lib/branch_io_cli/commands/report_command.rb', line 77

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

#run!Object



11
12
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
# File 'lib/branch_io_cli/commands/report_command.rb', line 11

def run!
  say "\n"

  if config_helper.header_only
    say report_header
    exit 0
  end

  File.open config_helper.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.report_command "#{base_xcodebuild_cmd} -list"

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

    base_cmd = base_xcodebuild_cmd
    # Add -scheme option for the rest of the commands if using a workspace
    base_cmd = "#{base_cmd} -scheme #{config_helper.scheme}" if config_helper.workspace_path

    # xcodebuild -showBuildSettings
    report.report_command "#{base_cmd} -showBuildSettings"

    # Add more options for the rest of the commands
    base_cmd = "#{base_cmd} -configuration #{config_helper.configuration} -sdk #{config_helper.sdk}"
    base_cmd = "#{base_cmd} -target #{config_helper.target}" unless config_helper.workspace_path

    if config_helper.clean
      say "Cleaning"
      report.report_command "#{base_cmd} clean"
    end

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

    say "Done ✅"
  end

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

#version_from_bnc_config_mObject



133
134
135
136
137
138
139
140
141
142
# File 'lib/branch_io_cli/commands/report_command.rb', line 133

def version_from_bnc_config_m
  # Look for BNCConfig.m in embedded source
  bnc_config_m_ref = config_helper.xcodeproj.files.find { |f| f.path =~ /BNCConfig\.m$/ }
  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]"
end

#version_from_branch_frameworkObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/branch_io_cli/commands/report_command.rb', line 118

def version_from_branch_framework
  framework = config_helper.target.frameworks_build_phase.files.find { |f| f.file_ref.path =~ /Branch.framework$/ }
  return nil unless framework
  framework_path = framework.file_ref.real_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"]
  version ? "#{version} [Branch.framework/Info.plist]" : nil
end

#version_from_cartfile_resolvedObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/branch_io_cli/commands/report_command.rb', line 99

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



91
92
93
94
95
96
97
# File 'lib/branch_io_cli/commands/report_command.rb', line 91

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

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