Class: FastlaneCore::Project

Inherits:
Object
  • Object
show all
Defined in:
fastlane_core/lib/fastlane_core/project.rb

Overview

Represents an Xcode project

Instance Attribute Summary collapse

Raw Access collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, xcodebuild_list_silent: false, xcodebuild_suppress_stderr: false) ⇒ Project

Returns a new instance of Project.



80
81
82
83
84
85
86
87
88
89
90
# File 'fastlane_core/lib/fastlane_core/project.rb', line 80

def initialize(options, xcodebuild_list_silent: false, xcodebuild_suppress_stderr: false)
  self.options = options
  self.path = File.expand_path(options[:workspace] || options[:project])
  self.is_workspace = (options[:workspace].to_s.length > 0)
  self.xcodebuild_list_silent = xcodebuild_list_silent
  self.xcodebuild_suppress_stderr = xcodebuild_suppress_stderr

  if !path || !File.directory?(path)
    UI.user_error!("Could not find project at path '#{path}'")
  end
end

Instance Attribute Details

#is_workspaceObject

Is this project a workspace?



68
69
70
# File 'fastlane_core/lib/fastlane_core/project.rb', line 68

def is_workspace
  @is_workspace
end

#optionsObject

The config object containing the scheme, configuration, etc.



71
72
73
# File 'fastlane_core/lib/fastlane_core/project.rb', line 71

def options
  @options
end

#pathObject

Path to the project/workspace



65
66
67
# File 'fastlane_core/lib/fastlane_core/project.rb', line 65

def path
  @path
end

#xcodebuild_list_silentObject

Should the output of xcodebuild commands be silenced?



74
75
76
# File 'fastlane_core/lib/fastlane_core/project.rb', line 74

def xcodebuild_list_silent
  @xcodebuild_list_silent
end

#xcodebuild_suppress_stderrObject

Should we redirect stderr to /dev/null for xcodebuild commands? Gets rid of annoying plugin info warnings.



78
79
80
# File 'fastlane_core/lib/fastlane_core/project.rb', line 78

def xcodebuild_suppress_stderr
  @xcodebuild_suppress_stderr
end

Class Method Details

.detect_projects(config) ⇒ Object

Project discovery



10
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
# File 'fastlane_core/lib/fastlane_core/project.rb', line 10

def detect_projects(config)
  if config[:workspace].to_s.length > 0 && config[:project].to_s.length > 0
    UI.user_error!("You can only pass either a workspace or a project path, not both")
  end

  return if config[:project].to_s.length > 0

  if config[:workspace].to_s.length == 0
    workspace = Dir["./*.xcworkspace"]
    if workspace.count > 1
      puts("Select Workspace: ")
      config[:workspace] = choose(*workspace)
    elsif !workspace.first.nil?
      config[:workspace] = workspace.first
    end
  end

  return if config[:workspace].to_s.length > 0

  if config[:workspace].to_s.length == 0 && config[:project].to_s.length == 0
    project = Dir["./*.xcodeproj"]
    if project.count > 1
      puts("Select Project: ")
      config[:project] = choose(*project)
    elsif !project.first.nil?
      config[:project] = project.first
    end
  end

  if config[:workspace].nil? && config[:project].nil?
    select_project(config)
  end
end

.run_command(command, timeout: 0, retries: 0, print: true) ⇒ Object

runs the specified command with the specified number of retries, killing each run if it times out. the first run times out after specified timeout elapses, and each successive run times out after a doubling of the previous timeout has elapsed. Note: - currently affected by github.com/fastlane/fastlane/issues/1504

- retry feature added to solve https://github.com/fastlane/fastlane/issues/4059


435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'fastlane_core/lib/fastlane_core/project.rb', line 435

def self.run_command(command, timeout: 0, retries: 0, print: true)
  require 'timeout'

  UI.command(command) if print

  result = ''

  total_tries = retries + 1
  try = 1
  try_timeout = timeout
  begin
    Timeout.timeout(try_timeout) do
      # Using Helper.backticks didn't work here. `Timeout` doesn't time out, and the command hangs forever
      result = `#{command}`.to_s
    end
  rescue Timeout::Error
    try_limit_reached = try >= total_tries

    # Try harder on each iteration
    next_timeout = try_timeout * 2

    message = "Command timed out after #{try_timeout} seconds on try #{try} of #{total_tries}"
    message += ", trying again with a #{next_timeout} second timeout..." unless try_limit_reached

    UI.important(message)

    raise if try_limit_reached

    try += 1
    try_timeout = next_timeout
    retry
  end

  return result
end

.select_project(config) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'fastlane_core/lib/fastlane_core/project.rb', line 44

def select_project(config)
  loop do
    path = UI.input("Couldn't automatically detect the project file, please provide a path: ")
    if File.directory?(path)
      if path.end_with?(".xcworkspace")
        config[:workspace] = path
        break
      elsif path.end_with?(".xcodeproj")
        config[:project] = path
        break
      else
        UI.error("Path must end with either .xcworkspace or .xcodeproj")
      end
    else
      UI.error("Couldn't find project at path '#{File.expand_path(path)}'")
    end
  end
end

.xcode_build_settings_retriesObject



423
424
425
# File 'fastlane_core/lib/fastlane_core/project.rb', line 423

def self.xcode_build_settings_retries
  (ENV['FASTLANE_XCODEBUILD_SETTINGS_RETRIES'] || 3).to_i
end

.xcode_build_settings_timeoutObject



418
419
420
# File 'fastlane_core/lib/fastlane_core/project.rb', line 418

def self.xcode_build_settings_timeout
  (ENV['FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT'] || 3).to_i
end

Instance Method Details

#app_nameObject



216
217
218
219
220
221
222
223
# File 'fastlane_core/lib/fastlane_core/project.rb', line 216

def app_name
  # WRAPPER_NAME: Example.app
  # WRAPPER_SUFFIX: .app
  name = build_settings(key: "WRAPPER_NAME")

  return name.gsub(build_settings(key: "WRAPPER_SUFFIX"), "") if name
  return "App" # default value
end

#application?Boolean

Returns:



241
242
243
# File 'fastlane_core/lib/fastlane_core/project.rb', line 241

def application?
  (build_settings(key: "PRODUCT_TYPE") == "com.apple.product-type.application")
end

#build_settings(key: nil, optional: true) ⇒ Object

Get the build settings for our project e.g. to properly get the DerivedData folder

Parameters:

  • The (String)

    key of which we want the value for (e.g. “PRODUCT_NAME”)



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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'fastlane_core/lib/fastlane_core/project.rb', line 360

def build_settings(key: nil, optional: true)
  unless @build_settings
    if is_workspace
      if schemes.count == 0
        UI.user_error!("Could not find any schemes for Xcode workspace at path '#{self.path}'. Please make sure that the schemes you want to use are marked as `Shared` from Xcode.")
      end
      options[:scheme] ||= schemes.first
    end

    # SwiftPM support
    if FastlaneCore::Helper.xcode_at_least?('11.0')
      UI.important("Resolving Swift Package Manager dependencies...")
      FastlaneCore::CommandExecutor.execute(command: build_xcodebuild_resolvepackagedependencies_command,
                                            print_all: true,
                                            print_command: !self.xcodebuild_list_silent)
    end

    command = build_xcodebuild_showbuildsettings_command

    # Xcode might hang here and retrying fixes the problem, see fastlane#4059
    begin
      timeout = FastlaneCore::Project.xcode_build_settings_timeout
      retries = FastlaneCore::Project.xcode_build_settings_retries
      @build_settings = FastlaneCore::Project.run_command(command, timeout: timeout, retries: retries, print: !self.xcodebuild_list_silent)
      if @build_settings.empty?
        UI.error("Could not read build settings. Make sure that the scheme \"#{options[:scheme]}\" is configured for running by going to Product → Scheme → Edit Scheme…, selecting the \"Build\" section, checking the \"Run\" checkbox and closing the scheme window.")
      end
    rescue Timeout::Error
      raise FastlaneCore::Interface::FastlaneDependencyCausedException.new, "xcodebuild -showBuildSettings timed out after #{retries + 1} retries with a base timeout of #{timeout}." \
        " You can override the base timeout value with the environment variable FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT," \
        " and the number of retries with the environment variable FASTLANE_XCODEBUILD_SETTINGS_RETRIES ".red
    end
  end

  begin
    result = @build_settings.split("\n").find do |c|
      sp = c.split(" = ")
      next if sp.length == 0
      sp.first.strip == key
    end
    return result.split(" = ").last
  rescue => ex
    return nil if optional # an optional value, we really don't care if something goes wrong

    UI.error(caller.join("\n\t"))
    UI.error("Could not fetch #{key} from project file: #{ex}")
  end

  nil
end

#build_xcodebuild_resolvepackagedependencies_commandObject



351
352
353
354
355
# File 'fastlane_core/lib/fastlane_core/project.rb', line 351

def build_xcodebuild_resolvepackagedependencies_command
  command = "xcodebuild -resolvePackageDependencies #{xcodebuild_parameters.join(' ')}"
  command += " 2> /dev/null" if xcodebuild_suppress_stderr
  command
end

#build_xcodebuild_showbuildsettings_commandObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'fastlane_core/lib/fastlane_core/project.rb', line 336

def build_xcodebuild_showbuildsettings_command
  # We also need to pass the workspace and scheme to this command.
  #
  # The 'clean' portion of this command was a workaround for an xcodebuild bug with Core Data projects.
  # This xcodebuild bug is fixed in Xcode 8.3 so 'clean' it's not necessary anymore
  # See: https://github.com/fastlane/fastlane/pull/5626
  if FastlaneCore::Helper.xcode_at_least?('8.3')
    command = "xcodebuild -showBuildSettings #{xcodebuild_parameters.join(' ')}"
  else
    command = "xcodebuild clean -showBuildSettings #{xcodebuild_parameters.join(' ')}"
  end
  command += " 2> /dev/null" if xcodebuild_suppress_stderr
  command
end

#command_line_tool?Boolean

Returns:



281
282
283
# File 'fastlane_core/lib/fastlane_core/project.rb', line 281

def command_line_tool?
  (build_settings(key: "PRODUCT_TYPE") == "com.apple.product-type.tool")
end

#configurationsObject

Get all available configurations in an array



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

def configurations
  @configurations ||= if workspace?
                        workspace
                          .file_references
                          .map(&:path)
                          .reject { |p| p.include?("Pods/Pods.xcodeproj") }
                          .map do |p|
                            # To maintain backwards compatibility, we
                            # silently ignore non-existent projects from
                            # workspaces.
                            begin
                              Xcodeproj::Project.open(p).build_configurations
                            rescue
                              []
                            end
                          end
                          .flatten
                          .compact
                          .map(&:name)
                      else
                        project.build_configurations.map(&:name)
                      end
end

#default_app_identifierObject

Returns bundle_id and sets the scheme for xcrun



203
204
205
# File 'fastlane_core/lib/fastlane_core/project.rb', line 203

def default_app_identifier
  default_build_settings(key: "PRODUCT_BUNDLE_IDENTIFIER")
end

#default_app_nameObject

Returns app name and sets the scheme for xcrun



208
209
210
211
212
213
214
# File 'fastlane_core/lib/fastlane_core/project.rb', line 208

def default_app_name
  if is_workspace
    return default_build_settings(key: "PRODUCT_NAME")
  else
    return app_name
  end
end

#default_build_settings(key: nil, optional: true) ⇒ Object

Returns the build settings and sets the default scheme to the options hash



412
413
414
415
# File 'fastlane_core/lib/fastlane_core/project.rb', line 412

def default_build_settings(key: nil, optional: true)
  options[:scheme] ||= schemes.first if is_workspace
  build_settings(key: key, optional: optional)
end

#dynamic_library?Boolean

Returns:



225
226
227
# File 'fastlane_core/lib/fastlane_core/project.rb', line 225

def dynamic_library?
  (build_settings(key: "PRODUCT_TYPE") == "com.apple.product-type.library.dynamic")
end

#framework?Boolean

Returns:



237
238
239
# File 'fastlane_core/lib/fastlane_core/project.rb', line 237

def framework?
  (build_settings(key: "PRODUCT_TYPE") == "com.apple.product-type.framework")
end

#ios?Boolean

Returns:



293
294
295
# File 'fastlane_core/lib/fastlane_core/project.rb', line 293

def ios?
  supported_platforms.include?(:iOS)
end

#ios_app?Boolean

Returns:



257
258
259
# File 'fastlane_core/lib/fastlane_core/project.rb', line 257

def ios_app?
  (application? && build_settings(key: "PLATFORM_NAME") == "iphoneos")
end

#ios_framework?Boolean

Returns:



253
254
255
# File 'fastlane_core/lib/fastlane_core/project.rb', line 253

def ios_framework?
  (framework? && build_settings(key: "PLATFORM_NAME") == "iphoneos")
end

#ios_library?Boolean

Returns:



245
246
247
# File 'fastlane_core/lib/fastlane_core/project.rb', line 245

def ios_library?
  ((static_library? or dynamic_library?) && build_settings(key: "PLATFORM_NAME") == "iphoneos")
end

#ios_tvos_app?Boolean

Returns:



249
250
251
# File 'fastlane_core/lib/fastlane_core/project.rb', line 249

def ios_tvos_app?
  (ios? || tvos?)
end

#library?Boolean

Returns:



233
234
235
# File 'fastlane_core/lib/fastlane_core/project.rb', line 233

def library?
  (static_library? || dynamic_library?)
end

#mac?Boolean

Returns:



285
286
287
# File 'fastlane_core/lib/fastlane_core/project.rb', line 285

def mac?
  supported_platforms.include?(:macOS)
end

#mac_app?Boolean

Returns:



265
266
267
# File 'fastlane_core/lib/fastlane_core/project.rb', line 265

def mac_app?
  (application? && build_settings(key: "PLATFORM_NAME") == "macosx")
end

#mac_framework?Boolean

Returns:



273
274
275
# File 'fastlane_core/lib/fastlane_core/project.rb', line 273

def mac_framework?
  (framework? && build_settings(key: "PLATFORM_NAME") == "macosx")
end

#mac_library?Boolean

Returns:



269
270
271
# File 'fastlane_core/lib/fastlane_core/project.rb', line 269

def mac_library?
  ((dynamic_library? or static_library?) && build_settings(key: "PLATFORM_NAME") == "macosx")
end

#produces_archive?Boolean

Returns:



261
262
263
# File 'fastlane_core/lib/fastlane_core/project.rb', line 261

def produces_archive?
  !(framework? || static_library? || dynamic_library?)
end

#projectObject

returns the Xcodeproj::Project or nil if it is a workspace



113
114
115
116
# File 'fastlane_core/lib/fastlane_core/project.rb', line 113

def project
  return nil if workspace?
  @project ||= Xcodeproj::Project.open(path)
end

#project_nameObject



96
97
98
99
100
101
102
# File 'fastlane_core/lib/fastlane_core/project.rb', line 96

def project_name
  if is_workspace
    return File.basename(options[:workspace], ".xcworkspace")
  else
    return File.basename(options[:project], ".xcodeproj")
  end
end

#project_pathsObject

Array of paths to all project files (might be multiple, because of workspaces)



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'fastlane_core/lib/fastlane_core/project.rb', line 473

def project_paths
  return @_project_paths if @_project_paths
  if self.workspace?
    # Find the xcodeproj file, as the information isn't included in the workspace file
    # We have a reference to the workspace, let's find the xcodeproj file
    # Use Xcodeproj gem here to
    # * parse the contents.xcworkspacedata XML file
    # * handle different types (group:, container: etc.) of file references and their paths
    # for details see https://github.com/CocoaPods/Xcodeproj/blob/e0287156d426ba588c9234bb2a4c824149889860/lib/xcodeproj/workspace/file_reference.rb```

    workspace_dir_path = File.expand_path("..", self.path)
    file_references_paths = workspace.file_references.map { |fr| fr.absolute_path(workspace_dir_path) }
    @_project_paths = file_references_paths.select do |current_match|
      # Xcode workspaces can contain loose files now, so let's filter non-xcodeproj files.
      current_match.end_with?(".xcodeproj")
    end.reject do |current_match|
      # We're not interested in a `Pods` project, as it doesn't contain any relevant information about code signing
      current_match.end_with?("Pods/Pods.xcodeproj")
    end

    return @_project_paths
  else
    # Return the path as an array
    return @_project_paths = [path]
  end
end

#schemesObject

Get all available schemes in an array



119
120
121
122
123
124
125
126
127
# File 'fastlane_core/lib/fastlane_core/project.rb', line 119

def schemes
  @schemes ||= if workspace?
                 workspace.schemes.reject do |k, v|
                   v.include?("Pods/Pods.xcodeproj")
                 end.keys
               else
                 Xcodeproj::Project.schemes(path)
               end
end

#select_scheme(preferred_to_include: nil) ⇒ Object

Let the user select a scheme Use a scheme containing the preferred_to_include string when multiple schemes were found



131
132
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
162
163
164
165
166
167
168
169
# File 'fastlane_core/lib/fastlane_core/project.rb', line 131

def select_scheme(preferred_to_include: nil)
  if options[:scheme].to_s.length > 0
    # Verify the scheme is available
    unless schemes.include?(options[:scheme].to_s)
      UI.error("Couldn't find specified scheme '#{options[:scheme]}'. Please make sure that the scheme is shared, see https://developer.apple.com/library/content/documentation/IDEs/Conceptual/xcode_guide-continuous_integration/ConfigureBots.html#//apple_ref/doc/uid/TP40013292-CH9-SW3")
      options[:scheme] = nil
    end
  end

  return if options[:scheme].to_s.length > 0

  if schemes.count == 1
    options[:scheme] = schemes.last
  elsif schemes.count > 1
    preferred = nil
    if preferred_to_include
      preferred = schemes.find_all { |a| a.downcase.include?(preferred_to_include.downcase) }
    end

    if preferred_to_include && preferred.count == 1
      options[:scheme] = preferred.last
    elsif automated_scheme_selection? && schemes.include?(project_name)
      UI.important("Using scheme matching project name (#{project_name}).")
      options[:scheme] = project_name
    elsif Helper.ci?
      UI.error("Multiple schemes found but you haven't specified one.")
      UI.error("Since this is a CI, please pass one using the `scheme` option")
      show_scheme_shared_information
      UI.user_error!("Multiple schemes found")
    else
      puts("Select Scheme: ")
      options[:scheme] = choose(*schemes)
    end
  else
    show_scheme_shared_information

    UI.user_error!("No Schemes found")
  end
end

#show_scheme_shared_informationObject



171
172
173
174
175
# File 'fastlane_core/lib/fastlane_core/project.rb', line 171

def show_scheme_shared_information
  UI.error("Couldn't find any schemes in this project, make sure that the scheme is shared if you are using a workspace")
  UI.error("Open Xcode, click on `Manage Schemes` and check the `Shared` box for the schemes you want to use")
  UI.error("Afterwards make sure to commit the changes into version control")
end

#static_library?Boolean

Returns:



229
230
231
# File 'fastlane_core/lib/fastlane_core/project.rb', line 229

def static_library?
  (build_settings(key: "PRODUCT_TYPE") == "com.apple.product-type.library.static")
end

#supported_platformsObject



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'fastlane_core/lib/fastlane_core/project.rb', line 301

def supported_platforms
  supported_platforms = build_settings(key: "SUPPORTED_PLATFORMS")
  if supported_platforms.nil?
    UI.important("Could not read the \"SUPPORTED_PLATFORMS\" build setting, assuming that the project supports iOS only.")
    return [:iOS]
  end
  supported_platforms.split.map do |platform|
    case platform
    when "macosx" then :macOS
    when "iphonesimulator", "iphoneos" then :iOS
    when "watchsimulator", "watchos" then :watchOS
    when "appletvsimulator", "appletvos" then :tvOS
    end
  end.uniq.compact
end

#supports_mac_catalyst?Boolean

Returns:



277
278
279
# File 'fastlane_core/lib/fastlane_core/project.rb', line 277

def supports_mac_catalyst?
  build_settings(key: "SUPPORTS_MACCATALYST") == "YES"
end

#tvos?Boolean

Returns:



289
290
291
# File 'fastlane_core/lib/fastlane_core/project.rb', line 289

def tvos?
  supported_platforms.include?(:tvOS)
end

#watchos?Boolean

Returns:



297
298
299
# File 'fastlane_core/lib/fastlane_core/project.rb', line 297

def watchos?
  supported_platforms.include?(:watchOS)
end

#workspaceObject

returns the Xcodeproj::Workspace or nil if it is a project



105
106
107
108
109
110
# File 'fastlane_core/lib/fastlane_core/project.rb', line 105

def workspace
  return nil unless workspace?

  @workspace ||= Xcodeproj::Workspace.new_from_xcworkspace(path)
  @workspace
end

#workspace?Boolean

Returns:



92
93
94
# File 'fastlane_core/lib/fastlane_core/project.rb', line 92

def workspace?
  self.is_workspace
end

#xcodebuild_parametersObject



317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'fastlane_core/lib/fastlane_core/project.rb', line 317

def xcodebuild_parameters
  proj = []
  proj << "-workspace #{options[:workspace].shellescape}" if options[:workspace]
  proj << "-scheme #{options[:scheme].shellescape}" if options[:scheme]
  proj << "-project #{options[:project].shellescape}" if options[:project]
  proj << "-configuration #{options[:configuration].shellescape}" if options[:configuration]
  proj << "-xcconfig #{options[:xcconfig].shellescape}" if options[:xcconfig]

  if FastlaneCore::Helper.xcode_at_least?('11.0') && options[:cloned_source_packages_path]
    proj << "-clonedSourcePackagesDirPath #{options[:cloned_source_packages_path].shellescape}"
  end

  return proj
end