Class: Xcodeproj::XCScheme

Inherits:
Object
  • Object
show all
Defined in:
lib/xcodeproj/scheme.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: XMLFormatter

Instance Attribute Summary collapse

Target methods collapse

Class methods collapse

Serialization collapse

Instance Method Summary collapse

Constructor Details

#initializeXCScheme

Create a new XCScheme instance


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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/xcodeproj/scheme.rb', line 16

def initialize
  @doc = REXML::Document.new
  @doc << REXML::XMLDecl.new(REXML::XMLDecl::DEFAULT_VERSION, 'UTF-8')
  @doc.context[:attribute_quote] = :quote

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

  @build_action = @scheme.add_element 'BuildAction'
  @build_action.attributes['parallelizeBuildables'] = 'YES'
  @build_action.attributes['buildImplicitDependencies'] = 'YES'
  @build_action_entries = nil

  @test_action = @scheme.add_element 'TestAction'
  @test_action.attributes['selectedDebuggerIdentifier'] = 'Xcode.DebuggerFoundation.Debugger.LLDB'
  @test_action.attributes['selectedLauncherIdentifier'] = 'Xcode.DebuggerFoundation.Launcher.LLDB'
  @test_action.attributes['shouldUseLaunchSchemeArgsEnv'] = 'YES'
  @test_action.attributes['buildConfiguration'] = 'Debug'

  @testables = @test_action.add_element 'Testables'

  @launch_action = @scheme.add_element 'LaunchAction'
  @launch_action.attributes['selectedDebuggerIdentifier'] = 'Xcode.DebuggerFoundation.Debugger.LLDB'
  @launch_action.attributes['selectedLauncherIdentifier'] = 'Xcode.DebuggerFoundation.Launcher.LLDB'
  @launch_action.attributes['launchStyle'] = '0'
  @launch_action.attributes['useCustomWorkingDirectory'] = 'NO'
  @launch_action.attributes['buildConfiguration'] = 'Debug'
  @launch_action.attributes['ignoresPersistentStateOnLaunch'] = 'NO'
  @launch_action.attributes['debugDocumentVersioning'] = 'YES'
  @launch_action.attributes['allowLocationSimulation'] = 'YES'
  @launch_action.add_element('AdditionalOptions')

  @profile_action = @scheme.add_element 'ProfileAction'
  @profile_action.attributes['shouldUseLaunchSchemeArgsEnv'] = 'YES'
  @profile_action.attributes['savedToolIdentifier'] = ''
  @profile_action.attributes['useCustomWorkingDirectory'] = 'NO'
  @profile_action.attributes['buildConfiguration'] = 'Release'
  @profile_action.attributes['debugDocumentVersioning'] = 'YES'

  analyze_action = @scheme.add_element 'AnalyzeAction'
  analyze_action.attributes['buildConfiguration'] = 'Debug'

  archive_action = @scheme.add_element 'ArchiveAction'
  archive_action.attributes['buildConfiguration'] = 'Release'
  archive_action.attributes['revealArchiveInOrganizer'] = 'YES'
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.


12
13
14
# File 'lib/xcodeproj/scheme.rb', line 12

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.


163
164
165
166
167
168
169
# File 'lib/xcodeproj/scheme.rb', line 163

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)

173
174
175
176
# File 'lib/xcodeproj/scheme.rb', line 173

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)

180
181
182
183
184
# File 'lib/xcodeproj/scheme.rb', line 180

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.


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/xcodeproj/scheme.rb', line 76

def add_build_target(build_target, build_for_running = true)
  unless @build_action_entries
    @build_action_entries = @build_action.add_element 'BuildActionEntries'
  end

  build_action_entry = @build_action_entries.add_element 'BuildActionEntry'
  build_action_entry.attributes['buildForTesting'] = 'YES'
  build_action_entry.attributes['buildForRunning'] = build_for_running ? 'YES' : 'NO'
  build_action_entry.attributes['buildForProfiling'] = 'YES'
  build_action_entry.attributes['buildForArchiving'] = 'YES'
  build_action_entry.attributes['buildForAnalyzing'] = 'YES'

  buildable_reference = build_action_entry.add_element 'BuildableReference'
  buildable_reference.attributes['BuildableIdentifier'] = 'primary'
  buildable_reference.attributes['BlueprintIdentifier'] = build_target.uuid
  buildable_reference.attributes['BuildableName'] = construct_buildable_name(build_target)
  buildable_reference.attributes['BlueprintName'] = build_target.name
  buildable_reference.attributes['ReferencedContainer'] = construct_referenced_container_uri(build_target)
end

#add_test_target(test_target) ⇒ Object

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

Parameters:


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/xcodeproj/scheme.rb', line 101

def add_test_target(test_target)
  testable_reference = @testables.add_element 'TestableReference'
  testable_reference.attributes['skipped'] = 'NO'

  buildable_reference = testable_reference.add_element 'BuildableReference'
  buildable_reference.attributes['BuildableIdentifier'] = 'primary'
  buildable_reference.attributes['BlueprintIdentifier'] = test_target.uuid
  buildable_reference.attributes['BuildableName'] = "#{test_target.name}.octest"
  buildable_reference.attributes['BlueprintName'] = test_target.name
  buildable_reference.attributes['ReferencedContainer'] = construct_referenced_container_uri(test_target)
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


226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/xcodeproj/scheme.rb', line 226

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

#set_launch_target(build_target) ⇒ Object

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

Parameters:


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/xcodeproj/scheme.rb', line 118

def set_launch_target(build_target)
  launch_product_runnable = @launch_action.add_element 'BuildableProductRunnable'

  launch_buildable_reference = launch_product_runnable.add_element 'BuildableReference'
  launch_buildable_reference.attributes['BuildableIdentifier'] = 'primary'
  launch_buildable_reference.attributes['BlueprintIdentifier'] = build_target.uuid
  launch_buildable_reference.attributes['BuildableName'] = "#{build_target.name}.app"
  launch_buildable_reference.attributes['BlueprintName'] = build_target.name
  launch_buildable_reference.attributes['ReferencedContainer'] = construct_referenced_container_uri(build_target)

  profile_product_runnable = @profile_action.add_element 'BuildableProductRunnable'

  profile_buildable_reference = profile_product_runnable.add_element 'BuildableReference'
  profile_buildable_reference.attributes['BuildableIdentifier'] = 'primary'
  profile_buildable_reference.attributes['BlueprintIdentifier'] = build_target.uuid
  profile_buildable_reference.attributes['BuildableName'] = "#{build_target.name}.app"
  profile_buildable_reference.attributes['BlueprintName'] = build_target.name
  profile_buildable_reference.attributes['ReferencedContainer'] = construct_referenced_container_uri(build_target)

  macro_expansion = @test_action.add_element 'MacroExpansion'

  buildable_reference = macro_expansion.add_element 'BuildableReference'
  buildable_reference.attributes['BuildableIdentifier'] = 'primary'
  buildable_reference.attributes['BlueprintIdentifier'] = build_target.uuid
  buildable_reference.attributes['BuildableName'] = File.basename(build_target.product_reference.path)
  buildable_reference.attributes['BlueprintName'] = build_target.name
  buildable_reference.attributes['ReferencedContainer'] = construct_referenced_container_uri(build_target)
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.


199
200
201
202
203
204
205
206
207
# File 'lib/xcodeproj/scheme.rb', line 199

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