Class: Xcodeproj::XCScheme

Inherits:
Object
  • Object
show all
Defined in:
lib/xcodeproj/scheme.rb,
lib/xcodeproj/scheme/test_action.rb,
lib/xcodeproj/scheme/build_action.rb,
lib/xcodeproj/scheme/launch_action.rb,
lib/xcodeproj/scheme/analyze_action.rb,
lib/xcodeproj/scheme/archive_action.rb,
lib/xcodeproj/scheme/profile_action.rb,
lib/xcodeproj/scheme/macro_expansion.rb,
lib/xcodeproj/scheme/remote_runnable.rb,
lib/xcodeproj/scheme/buildable_reference.rb,
lib/xcodeproj/scheme/xml_element_wrapper.rb,
lib/xcodeproj/scheme/environment_variables.rb,
lib/xcodeproj/scheme/abstract_scheme_action.rb,
lib/xcodeproj/scheme/command_line_arguments.rb,
lib/xcodeproj/scheme/buildable_product_runnable.rb

Overview

This class represents a Scheme document represented by a “.xcscheme” file usually stored in a xcuserdata or xcshareddata (for a shared scheme) folder.

Defined Under Namespace

Classes: AbstractSchemeAction, AnalyzeAction, ArchiveAction, BuildAction, BuildableProductRunnable, BuildableReference, CommandLineArgument, CommandLineArguments, EnvironmentVariable, EnvironmentVariables, LaunchAction, MacroExpansion, ProfileAction, RemoteRunnable, TestAction, XMLElementWrapper, XMLFormatter

Constant Summary collapse

VARIABLES_NODE =
'EnvironmentVariables'
VARIABLE_NODE =
'EnvironmentVariable'
COMMAND_LINE_ARGS_NODE =
'CommandLineArguments'.freeze
COMMAND_LINE_ARG_NODE =
'CommandLineArgument'.freeze

Instance Attribute Summary collapse

Access Action nodes collapse

Target methods collapse

Class methods collapse

Serialization collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = nil) ⇒ XCScheme

Create a XCScheme either from scratch or using an existing file

Parameters:

  • file_path (String) (defaults to: nil)

    The path of the existing .xcscheme file. If nil will create an empty scheme



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/xcodeproj/scheme.rb', line 31

def initialize(file_path = nil)
  if file_path
    @file_path = file_path
    @doc = File.open(file_path, 'r') do |f|
      REXML::Document.new(f)
    end
    @doc.context[:attribute_quote] = :quote

    @scheme = @doc.elements['Scheme']
  else
    @doc = REXML::Document.new
    @doc.context[:attribute_quote] = :quote
    @doc << REXML::XMLDecl.new(REXML::XMLDecl::DEFAULT_VERSION, 'UTF-8')

    @scheme = @doc.add_element 'Scheme'
    @scheme.attributes['LastUpgradeVersion'] = Constants::LAST_UPGRADE_CHECK
    @scheme.attributes['version'] = Xcodeproj::Constants::XCSCHEME_FORMAT_VERSION

    self.build_action   = BuildAction.new
    self.test_action    = TestAction.new
    self.launch_action  = LaunchAction.new
    self.profile_action = ProfileAction.new
    self.analyze_action = AnalyzeAction.new
    self.archive_action = ArchiveAction.new
  end
end

Instance Attribute Details

#docREXML::Document (readonly)

Returns the XML object that will be manipulated to save the scheme file after.

Returns:

  • (REXML::Document)

    the XML object that will be manipulated to save the scheme file after.



24
25
26
# File 'lib/xcodeproj/scheme.rb', line 24

def doc
  @doc
end

Class Method Details

.share_scheme(project_path, scheme_name, user = nil) ⇒ Object

Share a User Scheme. Basically this method move the xcscheme file from the xcuserdata folder to xcshareddata folder.

Parameters:

  • project_path (String)

    Path of the .xcodeproj folder.

  • scheme_name (String)

    The name of scheme that will be shared.

  • user (String) (defaults to: nil)

    The user name that have the scheme.



247
248
249
250
251
252
253
# File 'lib/xcodeproj/scheme.rb', line 247

def self.share_scheme(project_path, scheme_name, user = nil)
  to_folder = shared_data_dir(project_path)
  to_folder.mkpath
  to = to_folder + "#{scheme_name}.xcscheme"
  from = user_data_dir(project_path, user) + "#{scheme_name}.xcscheme"
  FileUtils.mv(from, to)
end

.shared_data_dir(project_path) ⇒ Pathname

Returns:

  • (Pathname)


257
258
259
260
# File 'lib/xcodeproj/scheme.rb', line 257

def self.shared_data_dir(project_path)
  project_path = Pathname.new(project_path)
  project_path + 'xcshareddata/xcschemes'
end

.user_data_dir(project_path, user = nil) ⇒ Pathname

Returns:

  • (Pathname)


264
265
266
267
268
# File 'lib/xcodeproj/scheme.rb', line 264

def self.user_data_dir(project_path, user = nil)
  project_path = Pathname.new(project_path)
  user ||= ENV['USER']
  project_path + "xcuserdata/#{user}.xcuserdatad/xcschemes"
end

Instance Method Details

#add_build_target(build_target, build_for_running = true) ⇒ Object

Add a target to the list of targets to build in the build action.

Parameters:

  • build_target (Xcodeproj::Project::Object::AbstractTarget)

    A target used by scheme in the build step.

  • build_for_running (Bool) (defaults to: true)

    Whether to build this target in the launch action. Often false for test targets.



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/xcodeproj/scheme.rb', line 193

def add_build_target(build_target, build_for_running = true)
  entry = BuildAction::Entry.new(build_target)

  entry.build_for_testing   = true
  entry.build_for_running   = build_for_running
  entry.build_for_profiling = build_for_running
  entry.build_for_archiving = build_for_running
  entry.build_for_analyzing = build_for_running

  build_action.add_entry(entry)
end

#add_test_target(test_target) ⇒ Object

Add a target to the list of targets to build in the build action.

Parameters:



210
211
212
213
# File 'lib/xcodeproj/scheme.rb', line 210

def add_test_target(test_target)
  testable = TestAction::TestableReference.new(test_target)
  test_action.add_testable(testable)
end

#analyze_actionXCScheme::AnalyzeAction

Returns The Analyze Action associated with this scheme.

Returns:



154
155
156
# File 'lib/xcodeproj/scheme.rb', line 154

def analyze_action
  @analyze_action ||= AnalyzeAction.new(@scheme.elements['AnalyzeAction'])
end

#analyze_action=(action) ⇒ Object

Parameters:



161
162
163
164
165
# File 'lib/xcodeproj/scheme.rb', line 161

def analyze_action=(action)
  @scheme.delete_element('AnalyzeAction')
  @scheme.add_element(action.xml_element)
  @analyze_action = action
end

#archive_actionXCScheme::ArchiveAction

Returns The Archive Action associated with this scheme.

Returns:



170
171
172
# File 'lib/xcodeproj/scheme.rb', line 170

def archive_action
  @archive_action ||= ArchiveAction.new(@scheme.elements['ArchiveAction'])
end

#archive_action=(action) ⇒ Object

Parameters:



177
178
179
180
181
# File 'lib/xcodeproj/scheme.rb', line 177

def archive_action=(action)
  @scheme.delete_element('ArchiveAction')
  @scheme.add_element(action.xml_element)
  @archive_action = action
end

#build_actionXCScheme::BuildAction

Returns The Build Action associated with this scheme.

Returns:



90
91
92
# File 'lib/xcodeproj/scheme.rb', line 90

def build_action
  @build_action ||= BuildAction.new(@scheme.elements['BuildAction'])
end

#build_action=(action) ⇒ Object

Parameters:



97
98
99
100
101
# File 'lib/xcodeproj/scheme.rb', line 97

def build_action=(action)
  @scheme.delete_element('BuildAction')
  @scheme.add_element(action.xml_element)
  @build_action = action
end

#configure_with_targets(runnable_target, test_target, launch_target: false) ⇒ Object

Convenience method to quickly add app and test targets to a new scheme.

It will add the runnable_target to the Build, Launch and Profile actions and the test_target to the Build and Test actions

Parameters:

  • runnable_target (Xcodeproj::Project::Object::PBXAbstractTarget)

    The target to use for the ‘Run’, ‘Profile’ and ‘Analyze’ actions

  • test_target (Xcodeproj::Project::Object::PBXAbstractTarget)

    The target to use for the ‘Test’ action

  • launch_target (Boolean) (defaults to: false)

    Determines if the runnable_target is launchable.



72
73
74
75
76
77
78
79
80
81
# File 'lib/xcodeproj/scheme.rb', line 72

def configure_with_targets(runnable_target, test_target, launch_target: false)
  if runnable_target
    add_build_target(runnable_target)
    set_launch_target(runnable_target) if launch_target
  end
  if test_target
    add_build_target(test_target, false) if test_target != runnable_target
    add_test_target(test_target)
  end
end

#launch_actionXCScheme::LaunchAction

Returns The Launch Action associated with this scheme.

Returns:



122
123
124
# File 'lib/xcodeproj/scheme.rb', line 122

def launch_action
  @launch_action ||= LaunchAction.new(@scheme.elements['LaunchAction'])
end

#launch_action=(action) ⇒ Object

Parameters:



129
130
131
132
133
# File 'lib/xcodeproj/scheme.rb', line 129

def launch_action=(action)
  @scheme.delete_element('LaunchAction')
  @scheme.add_element(action.xml_element)
  @launch_action = action
end

#profile_actionXCScheme::ProfileAction

Returns The Profile Action associated with this scheme.

Returns:



138
139
140
# File 'lib/xcodeproj/scheme.rb', line 138

def profile_action
  @profile_action ||= ProfileAction.new(@scheme.elements['ProfileAction'])
end

#profile_action=(action) ⇒ Object

Parameters:



145
146
147
148
149
# File 'lib/xcodeproj/scheme.rb', line 145

def profile_action=(action)
  @scheme.delete_element('ProfileAction')
  @scheme.add_element(action.xml_element)
  @profile_action = action
end

#save!Object

Serializes the current state of the object to the original “.xcscheme” file this XCScheme was created from, overriding the original file.

Requires that the XCScheme object was initialized using a file path.

Raises:



329
330
331
332
333
334
335
# File 'lib/xcodeproj/scheme.rb', line 329

def save!
  raise Informative, 'This XCScheme object was not initialized ' \
    'using a file path. Use save_as instead.' unless @file_path
  File.open(@file_path, 'w') do |f|
    f.write(to_s)
  end
end

#save_as(project_path, name, shared = true) ⇒ void

This method returns an undefined value.

Serializes the current state of the object to a “.xcscheme” file.

Examples:

Saving a scheme

scheme.save_as('path/to/Project.xcodeproj') #=> true

Parameters:

  • project_path (String, Pathname)

    The path where the “.xcscheme” file should be stored.

  • name (String)

    The name of the scheme, to have “.xcscheme” appended.

  • shared (Boolean) (defaults to: true)

    true => if the scheme must be a shared scheme (default value) false => if the scheme must be a user scheme



310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/xcodeproj/scheme.rb', line 310

def save_as(project_path, name, shared = true)
  scheme_folder_path = if shared
                         self.class.shared_data_dir(project_path)
                       else
                         self.class.user_data_dir(project_path)
                       end
  scheme_folder_path.mkpath
  scheme_path = scheme_folder_path + "#{name}.xcscheme"
  @file_path = scheme_path
  File.open(scheme_path, 'w') do |f|
    f.write(to_s)
  end
end

#set_launch_target(build_target) ⇒ Object

Sets a runnable target to be the target of the launch action of the scheme.

Parameters:



220
221
222
223
224
225
226
227
228
229
# File 'lib/xcodeproj/scheme.rb', line 220

def set_launch_target(build_target)
  launch_runnable = BuildableProductRunnable.new(build_target, 0)
  launch_action.buildable_product_runnable = launch_runnable

  profile_runnable = BuildableProductRunnable.new(build_target)
  profile_action.buildable_product_runnable = profile_runnable

  macro_exp = MacroExpansion.new(build_target)
  test_action.add_macro_expansion(macro_exp)
end

#test_actionXCScheme::TestAction

Returns The Test Action associated with this scheme.

Returns:



106
107
108
# File 'lib/xcodeproj/scheme.rb', line 106

def test_action
  @test_action ||= TestAction.new(@scheme.elements['TestAction'])
end

#test_action=(action) ⇒ Object

Parameters:



113
114
115
116
117
# File 'lib/xcodeproj/scheme.rb', line 113

def test_action=(action)
  @scheme.delete_element('TestAction')
  @scheme.add_element(action.xml_element)
  @test_action = action
end

#to_sString

Note:

The goal of the string representation is to match Xcode output as close as possible to aide comparison.

Serializes the current state of the object to a String.

Returns:

  • (String)

    the XML string value of the current state of the object.



283
284
285
286
287
288
289
290
291
# File 'lib/xcodeproj/scheme.rb', line 283

def to_s
  formatter = XMLFormatter.new(2)
  formatter.compact = false
  out = ''
  formatter.write(@doc, out)
  out.gsub!("<?xml version='1.0' encoding='UTF-8'?>", '<?xml version="1.0" encoding="UTF-8"?>')
  out << "\n"
  out
end