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) ⇒ Project

Returns a new instance of Project.



69
70
71
72
73
74
75
76
77
# File 'lib/fastlane_core/project.rb', line 69

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

  if !path or !File.directory?(path)
    raise "Could not find project at path '#{path}'".red
  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

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
    raise "You can only pass either a workspace or a project path, not both".red
  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))
    else
      config[:workspace] = workspace.first # this will result in nil if no files were found
    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))
    else
      config[:project] = project.first # this will result in nil if no files were found
    end
  end

  if config[:workspace].nil? and config[:project].nil?
    select_project(config)
  end
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 = ask("Couldn't automatically detect the project file, please provide a path: ".yellow).strip
    if File.directory? path
      if path.end_with? ".xcworkspace"
        config[:workspace] = path
        break
      elsif path.end_with? ".xcodeproj"
        config[:project] = path
        break
      else
        Helper.log.error "Path must end with either .xcworkspace or .xcodeproj"
      end
    else
      Helper.log.error "Couldn't find project at path '#{File.expand_path(path)}'".red
    end
  end
end

Instance Method Details

#app_nameObject



138
139
140
141
142
143
144
145
# File 'lib/fastlane_core/project.rb', line 138

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”)



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

def build_settings(key: nil, optional: true)
  unless @build_settings
    # We also need to pass the workspace and scheme to this command
    command = "xcrun xcodebuild -showBuildSettings #{xcodebuild_parameters.join(' ')}"
    @build_settings = Helper.backticks(command, print: false)
  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

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

  nil
end

#configurationsObject

Get all available configurations in an array



120
121
122
# File 'lib/fastlane_core/project.rb', line 120

def configurations
  parsed_info.configurations
end

#default_app_identifierObject

Returns bundle_id and sets the scheme for xcrun



125
126
127
# File 'lib/fastlane_core/project.rb', line 125

def default_app_identifier
  default_build_settings(key: "PRODUCT_BUNDLE_IDENTIFIER")
end

#default_app_nameObject

Returns app name and sets the scheme for xcrun



130
131
132
133
134
135
136
# File 'lib/fastlane_core/project.rb', line 130

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, silent: false) ⇒ Object

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



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

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

#ios?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/fastlane_core/project.rb', line 160

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

#mac?Boolean

Returns:

  • (Boolean)


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

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") == "OS X"
  false
end

#raw_info(silent: false) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
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
247
248
249
250
251
252
253
254
255
# File 'lib/fastlane_core/project.rb', line 206

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

  # 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 = "xcrun xcodebuild -list #{options.join(' ')}"
  Helper.log.info command.yellow unless silent

  # xcode >= 6 might hang here if the user schemes are missing
  begin
    require 'timeout'
    @raw = Timeout.timeout(10) { `#{command}`.to_s }
  rescue Timeout::Error
    raise "xcodebuild -list timed-out. You might need to recreate the user schemes. See https://github.com/fastlane/gym/issues/143".red
  end

  raise "Error parsing xcode file using `#{command}`".red if @raw.length == 0

  return @raw
end

#schemesObject

Get all available schemes in an array



84
85
86
# File 'lib/fastlane_core/project.rb', line 84

def schemes
  parsed_info.schemes
end

#select_schemeObject

Let the user select a scheme



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

def select_scheme
  if options[:scheme].to_s.length > 0
    # Verify the scheme is available
    unless schemes.include?(options[:scheme].to_s)
      Helper.log.error "Couldn't find specified scheme '#{options[:scheme]}'.".red
      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
    if Helper.is_ci?
      Helper.log.error "Multiple schemes found but you haven't specified one.".red
      Helper.log.error "Since this is a CI, please pass one using the `scheme` option".red
      raise "Multiple schemes found".red
    else
      puts "Select Scheme: "
      options[:scheme] = choose(*(schemes))
    end
  else
    Helper.log.error "Couldn't find any schemes in this project, make sure that the scheme is shared if you are using a workspace".red
    Helper.log.error "Open Xcode, click on `Manage Schemes` and check the `Shared` box for the schemes you want to use".red

    raise "No Schemes found".red
  end
end

#tvos?Boolean

Returns:

  • (Boolean)


154
155
156
157
158
# File 'lib/fastlane_core/project.rb', line 154

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)


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

def workspace?
  self.is_workspace
end

#xcodebuild_parametersObject



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

def xcodebuild_parameters
  proj = []
  proj << "-workspace '#{options[:workspace]}'" if options[:workspace]
  proj << "-scheme '#{options[:scheme]}'" if options[:scheme]
  proj << "-project '#{options[:project]}'" if options[:project]

  return proj
end