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



42
43
44
45
46
47
48
49
50
# File 'lib/branch_io_cli/commands/report_command.rb', line 42

def base_xcodebuild_cmd
  cmd = "xcodebuild -sdk iphonesimulator"
  cmd = "#{cmd} -scheme #{config_helper.scheme}" if config_helper.scheme
  cmd = "#{cmd} -workspace #{config_helper.workspace_path}" if config_helper.workspace_path
  cmd = "#{cmd} -project #{config_helper.xcodeproj_path}" if config_helper.xcodeproj_path && !config_helper.workspace_path
  cmd = "#{cmd} -target #{config_helper.target}" if config_helper.target
  cmd = "#{cmd} -configuration #{config_helper.configuration}" if config_helper.configuration
  cmd
end

#branch_versionObject



52
53
54
55
56
57
# File 'lib/branch_io_cli/commands/report_command.rb', line 52

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

#report_headerObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/branch_io_cli/commands/report_command.rb', line 126

def report_header
  header = `xcodebuild -version`

  if config_helper.podfile_path && File.exist?("#{config_helper.podfile_path}.lock")
    podfile_lock = Pod::Lockfile.from_file Pathname.new "#{config_helper.podfile_path}.lock"
    header = "#{header}\nUsing CocoaPods v. #{podfile_lock.cocoapods_version}\n"
  end

  podfile_requirement = requirement_from_podfile
  header = "#{header}\nFrom Podfile:\n#{podfile_requirement}\n" if podfile_requirement

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

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

  header
end

#requirement_from_cartfileObject



66
67
68
69
70
71
# File 'lib/branch_io_cli/commands/report_command.rb', line 66

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

#requirement_from_podfileObject



59
60
61
62
63
64
# File 'lib/branch_io_cli/commands/report_command.rb', line 59

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
# 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"
    # TODO: Write out command-line options or configuration from helper
    report.write "#{report_header}\n"

    report.report_command "#{base_xcodebuild_cmd} -list"
    report.report_command "#{base_xcodebuild_cmd} -showBuildSettings"

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

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

    say "Done ✅"
  end
  @report = nil

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

#version_from_bnc_config_mObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/branch_io_cli/commands/report_command.rb', line 115

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



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

def version_from_branch_framework
  framework = config_helper.xcodeproj.frameworks_group.files.find { |f| f.path =~ /Branch.framework$/ }
  return nil unless framework
  framework_path = framework.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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/branch_io_cli/commands/report_command.rb', line 81

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



73
74
75
76
77
78
79
# File 'lib/branch_io_cli/commands/report_command.rb', line 73

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