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_integration_mode, #target, #workspace, #workspace_path, #xcodeproj, #xcodeproj_path

Instance Method Summary collapse

Methods inherited from Configuration

#app_delegate_objc_path, #app_delegate_swift_path, #branch_imports, #branch_imports_from_file, #bridging_header_path, #bridging_header_required?, #helper, #initialize, #modules_enabled?, #open_podfile, #print_identification, #relative_path, #swift_version, #uses_frameworks?, #validate_buildfile_at_path, #validate_buildfile_path, #validate_target, #validate_xcodeproj_path

Constructor Details

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

Instance Attribute Details

#cleanObject (readonly)

Returns the value of attribute clean.



4
5
6
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 4

def clean
  @clean
end

#configurationObject (readonly)

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#header_onlyObject (readonly)

Returns the value of attribute header_only.



5
6
7
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 5

def header_only
  @header_only
end

#report_pathObject (readonly)

Returns the value of attribute report_path.



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

def report_path
  @report_path
end

#schemeObject (readonly)

Returns the value of attribute scheme.



6
7
8
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 6

def scheme
  @scheme
end

#sdkObject (readonly)

Returns the value of attribute sdk.



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

def sdk
  @sdk
end

Instance Method Details

#all_schemesObject



163
164
165
166
167
168
169
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 163

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

#logObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 36

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}\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

#scheme_with_name(scheme_name) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 179

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 = @xcode_settings["USER"] if @xcode_settings
    user ||= ENV["USER"] || ENV["LOGNAME"]
    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



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

def validate_configuration(options)
  all_configs = xcodeproj.build_configurations.map(&:name)

  if options.configuration && all_configs.include?(options.configuration)
    @configuration = options.configuration
  elsif options.configuration
    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

  return if @configuration

  @configuration = "Debug" # Usual default for the launch action

  return unless xcscheme

  @configuration = xcscheme.launch_action.build_configuration
end

#validate_optionsObject



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

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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 133

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.
  # This will be picked up in #validate_target
  entry = xcscheme.build_action.entries.select(&:build_for_running?).first
  options.target = entry.buildable_references.first.target_name
end

#validate_xcodeproj_and_workspace(options) ⇒ Object

rubocop: disable Metrics/PerceivedComplexity



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

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

#xcschemeObject



171
172
173
174
175
176
177
# File 'lib/branch_io_cli/configuration/report_configuration.rb', line 171

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