Class: Fastlane::Actions::XcodebuildAction

Inherits:
Fastlane::Action show all
Defined in:
lib/fastlane/actions/xcodebuild.rb

Overview

Constant Summary collapse

ARGS_MAP =
{
  # actions
  analyze: "analyze",
  archive: "archive",
  build: "build",
  clean: "clean",
  install: "install",
  installsrc: "installsrc",
  test: "test",

  # parameters
  alltargets: "-alltargets",
  arch: "-arch",
  archive_path: "-archivePath",
  configuration: "-configuration",
  derivedDataPath: "-derivedDataPath",
  destination_timeout: "-destination-timeout",
  export_archive: "-exportArchive",
  export_format: "-exportFormat",
  export_installer_identity: "-exportInstallerIdentity",
  export_path: "-exportPath",
  export_profile: "-exportProvisioningProfile",
  export_signing_identity: "-exportSigningIdentity",
  export_with_original_signing_identity: "-exportWithOriginalSigningIdentity",
  project: "-project",
  result_bundle_path: "-resultBundlePath",
  scheme: "-scheme",
  sdk: "-sdk",
  skip_unavailable_actions: "-skipUnavailableActions",
  target: "-target",
  workspace: "-workspace",
  xcconfig: "-xcconfig"
}

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, authors, output, sh, step_text

Class Method Details

.authorObject



260
261
262
# File 'lib/fastlane/actions/xcodebuild.rb', line 260

def self.author
  "dtrenz"
end

.available_optionsObject



245
246
247
248
249
250
251
252
253
254
# File 'lib/fastlane/actions/xcodebuild.rb', line 245

def self.available_options
  [
    ['archive', 'Set to true to build archive'],
    ['archive_path', 'The path to archive the to. Must contain `.xcarchive`'],
    ['workspace', 'The workspace to use'],
    ['scheme', 'The scheme to build'],
    ['build_settings', 'Hash of additional build information'],
    ['output_style', 'Set the output format to one of: :standard (Colored UTF8 output, default), :basic (black & white ASCII outout)']
  ]
end

.descriptionObject



241
242
243
# File 'lib/fastlane/actions/xcodebuild.rb', line 241

def self.description
  "Use the `xcodebuild` command to build and sign your app"
end

.detailsObject



256
257
258
# File 'lib/fastlane/actions/xcodebuild.rb', line 256

def self.details
  "More infomration on GitHub: https://github.com/KrauseFx/fastlane/blob/master/docs/Actions.md#xcodebuild"
end

.detect_workspaceObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/fastlane/actions/xcodebuild.rb', line 225

def self.detect_workspace
  workspace = nil
  workspaces = Dir.glob("*.xcworkspace")

  if workspaces.length > 1
    Helper.log.warn "Multiple workspaces detected."
  end

  if !workspaces.empty?
    workspace = workspaces.first
    Helper.log.warn "Using workspace \"#{workspace}\""
  end

  return workspace
end

.hash_to_args(hash) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/fastlane/actions/xcodebuild.rb', line 204

def self.hash_to_args(hash)
  # Remove nil value params
  hash = hash.delete_if { |_, v| v.nil? }

  # Maps nice developer param names to CLI arguments
  hash.map do |k, v|
    v ||= ""
    if arg = ARGS_MAP[k]
      value = (v != true && v.to_s.length > 0 ? "\"#{v}\"" : "")
      "#{arg} #{value}".strip
    elsif k == :build_settings
      v.map{|setting,value| "#{setting}=\"#{value}\""}.join(' ')
    elsif k == :destination
      [*v].collect { |dst| "-destination \"#{dst}\"" }.join(' ')
    elsif k == :keychain && v.to_s.length > 0
      # If keychain is specified, append as OTHER_CODE_SIGN_FLAGS
      "OTHER_CODE_SIGN_FLAGS=\"--keychain #{v}\""
    end
  end.compact.sort
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/fastlane/actions/xcodebuild.rb', line 45

def self.is_supported?(platform)
  [:ios, :mac].include?platform
end

.run(params) ⇒ Object



49
50
51
52
53
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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/fastlane/actions/xcodebuild.rb', line 49

def self.run(params)
  unless Helper.test?
    raise "xcodebuild not installed".red if `which xcodebuild`.length == 0
  end

  # The args we will build with
  xcodebuild_args = Array[]

  # Supported ENV vars
  build_path = ENV["XCODE_BUILD_PATH"] || nil
  scheme     = ENV["XCODE_SCHEME"]
  workspace  = ENV["XCODE_WORKSPACE"]
  project    = ENV["XCODE_PROJECT"]

  # Append slash to build path, if needed
  if build_path && !build_path.end_with?("/")
    build_path += "/"
  end


  if params
    # Operation bools
    archiving = params.key? :archive
    exporting = params.key? :export_archive
    testing   = params.key? :test

    if exporting
      # If not passed, retrieve path from previous xcodebuild call
      params[:archive_path] ||= Actions.lane_context[SharedValues::XCODEBUILD_ARCHIVE]

      # Default to ipa as export format
      params[:export_format] ||= "ipa"

      # If not passed, construct export path from env vars
      if params[:export_path] == nil
        ipa_filename = scheme ? scheme : File.basename(params[:archive_path], ".*")
        params[:export_path] = "#{build_path}#{ipa_filename}"
      end

      # Store IPA path for later deploy steps (i.e. Crashlytics)
      Actions.lane_context[SharedValues::IPA_OUTPUT_PATH] = params[:export_path] + "." + params[:export_format].downcase
    else
      # If not passed, check for archive scheme & workspace/project env vars
      params[:scheme] ||= scheme
      params[:workspace] ||= workspace
      params[:project] ||= project

      # If no project or workspace was passed in or set as an environment
      # variable, attempt to autodetect the workspace.
      if params[:project].to_s.empty? && params[:workspace].to_s.empty?
        params[:workspace] = detect_workspace
      end
    end

    if archiving
      # If not passed, construct archive path from env vars
      params[:archive_path] ||= "#{build_path}#{params[:scheme]}.xcarchive"

      # Cache path for later xcodebuild calls
      Actions.lane_context[SharedValues::XCODEBUILD_ARCHIVE] = params[:archive_path]
    end

    # Maps parameter hash to CLI args
    if hash_args = hash_to_args(params)
      xcodebuild_args += hash_args
    end
  end

  # Joins args into space delimited string
  xcodebuild_args = xcodebuild_args.join(" ")

  # Default args
  xcpretty_args = []

  # Formatting style
  if params && params[:output_style]
    output_style = params[:output_style]
    raise "Invalid output_style #{output_style}".red unless [:standard, :basic].include?(output_style)
  else
    output_style = :standard
  end

  case output_style
  when :standard
    xcpretty_args << '--color'
  when :basic
    xcpretty_args << '--no-utf'
  end

  if testing
    if params[:reports]
      # New report options format
      reports = params[:reports].reduce("") do |arguments, report|

        report_string = "--report #{report[:report]}"

        if report[:output]
          report_string << " --output \"#{report[:output]}\""
        elsif report[:report] == 'junit'
          report_string << " --output \"#{build_path}report/report.xml\""
        elsif report[:report] == 'html'
          report_string << " --output \"#{build_path}report/report.html\""
        elsif report[:report] == 'json-compilation-database'
          report_string << " --output \"#{build_path}report/report.json\""
        end

        if report[:screenshots]
          report_string << " --screenshots"
        end

        unless arguments == ""
          arguments << " "
        end

        arguments << report_string
      end

      xcpretty_args.push reports

    elsif params[:report_formats]
      # Test report file format
      report_formats = params[:report_formats].map do |format|
        "--report #{format}"
      end.sort().join(" ")

      xcpretty_args.push report_formats

      # Save screenshots flag
      if params[:report_formats].include?("html") && params[:report_screenshots]
        xcpretty_args.push "--screenshots"
      end

      xcpretty_args.sort!

      # Test report file path
      if params[:report_path]
        xcpretty_args.push "--output \"#{params[:report_path]}\""
      elsif build_path
        xcpretty_args.push "--output \"#{build_path}report\""
      end
    end
  end

  # Stdout format
  if testing && !archiving
    xcpretty_args.push "--test"
  else
    xcpretty_args.push "--simple"
  end

  xcpretty_args = xcpretty_args.join(" ")

  Actions.sh "set -o pipefail && xcodebuild #{xcodebuild_args} | xcpretty #{xcpretty_args}"
end