Class: FastlaneCore::Project

Inherits:
Object
  • Object
show all
Defined in:
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.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fastlane_core/project.rb', line 76

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 or !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?



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

def is_workspace
  @is_workspace
end

#optionsObject

The config object containing the scheme, configuration, etc.



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

def options
  @options
end

#pathObject

Path to the project/workspace



61
62
63
# File 'lib/fastlane_core/project.rb', line 61

def path
  @path
end

#xcodebuild_list_silentObject

Should the output of xcodebuild commands be silenced?



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

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.



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

def xcodebuild_suppress_stderr
  @xcodebuild_suppress_stderr
end

Class Method Details

.detect_projects(config) ⇒ Object

Project discovery



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

def detect_projects(config)
  if config[:workspace].to_s.length > 0 and 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 and 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? and 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 Note: - currently affected by github.com/fastlane/fastlane/issues/1504

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


337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/fastlane_core/project.rb', line 337

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
  begin
    Timeout.timeout(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

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

    UI.important(message)

    raise if try_limit_reached

    try += 1
    retry
  end

  return result
end

.select_project(config) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fastlane_core/project.rb', line 40

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



327
328
329
# File 'lib/fastlane_core/project.rb', line 327

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

.xcode_build_settings_timeoutObject



322
323
324
# File 'lib/fastlane_core/project.rb', line 322

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

.xcode_list_retriesObject



317
318
319
# File 'lib/fastlane_core/project.rb', line 317

def self.xcode_list_retries
  (ENV['FASTLANE_XCODE_LIST_RETRIES'] || 3).to_i
end

.xcode_list_timeoutObject



312
313
314
# File 'lib/fastlane_core/project.rb', line 312

def self.xcode_list_timeout
  (ENV['FASTLANE_XCODE_LIST_TIMEOUT'] || 10).to_i
end

Instance Method Details

#app_nameObject



166
167
168
169
170
171
172
173
# File 'lib/fastlane_core/project.rb', line 166

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

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

Get the build settings for our project this is used to properly get the DerivedData folder

Parameters:

  • The (String)

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/fastlane_core/project.rb', line 219

def build_settings(key: nil, optional: true)
  unless @build_settings
    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)
    rescue Timeout::Error
      UI.crash!("xcodebuild -showBuildSettings timed-out after #{timeout} seconds and #{retries} retries." \
        " You can override the timeout value with the environment variable FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT," \
        " and the number of retries with the environment variable FASTLANE_XCODEBUILD_SETTINGS_RETRIES ")
    end
  end

  begin
    result = @build_settings.split("\n").find { |c| c.split(" = ").first.strip == key }
    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_list_commandObject



254
255
256
257
258
259
260
261
# File 'lib/fastlane_core/project.rb', line 254

def build_xcodebuild_list_command
  # Unfortunately since we pass the workspace we also get all the
  # schemes generated by CocoaPods
  options = xcodebuild_parameters.delete_if { |a| a.to_s.include? "scheme" }
  command = "xcodebuild -list #{options.join(' ')}"
  command += " 2> /dev/null" if xcodebuild_suppress_stderr
  command
end

#build_xcodebuild_showbuildsettings_commandObject



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

def build_xcodebuild_showbuildsettings_command
  # We also need to pass the workspace and scheme to this command.
  #
  # The 'clean' portion of this command is a workaround for an xcodebuild bug with Core Data projects.
  # See: https://github.com/fastlane/fastlane/pull/5626
  command = "xcodebuild clean -showBuildSettings #{xcodebuild_parameters.join(' ')}"
  command += " 2> /dev/null" if xcodebuild_suppress_stderr
  command
end

#configurationsObject

Get all available configurations in an array



148
149
150
# File 'lib/fastlane_core/project.rb', line 148

def configurations
  parsed_info.configurations
end

#default_app_identifierObject

Returns bundle_id and sets the scheme for xcrun



153
154
155
# File 'lib/fastlane_core/project.rb', line 153

def default_app_identifier
  default_build_settings(key: "PRODUCT_BUNDLE_IDENTIFIER")
end

#default_app_nameObject

Returns app name and sets the scheme for xcrun



158
159
160
161
162
163
164
# File 'lib/fastlane_core/project.rb', line 158

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



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

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

#ios?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/fastlane_core/project.rb', line 189

def ios?
  !mac? && !tvos?
end

#mac?Boolean

Returns:

  • (Boolean)


175
176
177
178
179
180
181
# File 'lib/fastlane_core/project.rb', line 175

def mac?
  # Some projects have different values... we have to look for all of them
  return true if build_settings(key: "PLATFORM_NAME") == "macosx"
  return true if build_settings(key: "PLATFORM_DISPLAY_NAME") == "macOS"
  return true if build_settings(key: "PLATFORM_DISPLAY_NAME") == "OS X"
  false
end

#project_nameObject



92
93
94
95
96
97
98
# File 'lib/fastlane_core/project.rb', line 92

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

#raw_info(silent: false) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/fastlane_core/project.rb', line 263

def raw_info(silent: false)
  # Examples:

  # Standard:
  #
  # Information about project "Example":
  #     Targets:
  #         Example
  #         ExampleUITests
  #
  #     Build Configurations:
  #         Debug
  #         Release
  #
  #     If no build configuration is specified and -scheme is not passed then "Release" is used.
  #
  #     Schemes:
  #         Example
  #         ExampleUITests

  # CococaPods
  #
  # Example.xcworkspace
  # Information about workspace "Example":
  #     Schemes:
  #         Example
  #         HexColors
  #         Pods-Example

  return @raw if @raw

  command = build_xcodebuild_list_command

  # xcode >= 6 might hang here if the user schemes are missing
  begin
    timeout = FastlaneCore::Project.xcode_list_timeout
    retries = FastlaneCore::Project.xcode_list_retries
    @raw = FastlaneCore::Project.run_command(command, timeout: timeout, retries: retries, print: !silent)
  rescue Timeout::Error
    UI.user_error!("xcodebuild -list timed-out after #{timeout * retries} seconds. You might need to recreate the user schemes." \
      " You can override the timeout value with the environment variable FASTLANE_XCODE_LIST_TIMEOUT")
  end

  UI.user_error!("Error parsing xcode file using `#{command}`") if @raw.length == 0

  return @raw
end

#schemesObject

Get all available schemes in an array



101
102
103
# File 'lib/fastlane_core/project.rb', line 101

def schemes
  parsed_info.schemes
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



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

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]}'.")
      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 and 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.is_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")
      UI.user_error!("Multiple schemes found")
    else
      puts "Select Scheme: "
      options[:scheme] = choose(*schemes)
    end
  else
    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.user_error!("No Schemes found")
  end
end

#tvos?Boolean

Returns:

  • (Boolean)


183
184
185
186
187
# File 'lib/fastlane_core/project.rb', line 183

def tvos?
  return true if build_settings(key: "PLATFORM_NAME").to_s.include? "appletv"
  return true if build_settings(key: "PLATFORM_DISPLAY_NAME").to_s.include? "tvOS"
  false
end

#workspace?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/fastlane_core/project.rb', line 88

def workspace?
  self.is_workspace
end

#xcodebuild_parametersObject



193
194
195
196
197
198
199
200
# File 'lib/fastlane_core/project.rb', line 193

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]

  return proj
end