class Xcodeproj::Workspace


documents.
Provides support for generating, reading and serializing Xcode Workspace

def self.from_s(xml, workspace_path = '')

Returns:
  • (Workspace) - the generated workspace.

Parameters:
  • xml (String) --
def self.from_s(xml, workspace_path = '')
  document = REXML::Document.new(xml)
  instance = new(document)
  instance.load_schemes(workspace_path)
  instance
end

def self.new_from_xcworkspace(path)

Returns:
  • (Workspace) - the generated workspace.

Parameters:
  • path (String) --
def self.new_from_xcworkspace(path)
  from_s(File.read(File.join(path, 'contents.xcworkspacedata')),
         File.expand_path(path))
rescue Errno::ENOENT
  new(nil)
end

def <<(path_or_reference)

Returns:
  • (void) -

Raises:
  • (ArgumentError) - Raised if the input is neither a String nor a FileReference

Parameters:
  • path_or_reference (String, Xcodeproj::Workspace::FileReference) --
def <<(path_or_reference)
  return unless @document && @document.respond_to?(:root)
  case
  when path_or_reference.is_a?(String)
    project_file_reference = Xcodeproj::Workspace::FileReference.new(path_or_reference)
  when path_or_reference.is_a?(Xcodeproj::Workspace::FileReference)
    project_file_reference = path_or_reference
    projpath = nil
  else
    raise ArgumentError, 'Input to the << operator must be a file path or FileReference'
  end
  @document.root.add_element(project_file_reference.to_node)
  load_schemes_from_project File.expand_path(projpath || project_file_reference.path)
end

def add_group(name)

Returns:
  • (Xcodeproj::Workspace::GroupReference) - The added group reference

Other tags:
    Yield: -

Parameters:
  • name (String) -- The name of the group
def add_group(name)
  return nil unless @document
  group = Xcodeproj::Workspace::GroupReference.new(name)
  elem = @document.root.add_element(group.to_node)
  yield group, elem if block_given?
  group
end

def file_references

Returns:
  • (Array) - the paths of the projects contained in the
def file_references
  return [] unless @document
  @document.get_elements('/Workspace//FileRef').map do |node|
    FileReference.from_node(node)
  end
end

def group_references

Returns:
  • (Array) - the groups contained in the workspace
def group_references
  return [] unless @document
  @document.get_elements('/Workspace//Group').map do |node|
    GroupReference.from_node(node)
  end
end

def include?(file_reference)

Returns:
  • (Boolean) - whether the project is contained in the workspace.

Parameters:
  • file_reference (FileReference) --
def include?(file_reference)
  file_references.include?(file_reference)
end

def initialize(document, *file_references)

Other tags:
    Note: - The document parameter is passed to the << operator if it is not a

Parameters:
  • file_references (Array) -- additional projects to add
  • document (REXML::Document) -- @see document
def initialize(document, *file_references)
  @schemes = {}
  if document.nil?
    @document = REXML::Document.new(root_xml(''))
  elsif document.is_a?(REXML::Document)
    @document = document
  else
    @document = REXML::Document.new(root_xml(''))
    self << document
  end
  file_references.each { |ref| self << ref }
end

def load_schemes(workspace_dir_path)

Returns:
  • (void) -

Parameters:
  • workspace_dir_path (String) --
def load_schemes(workspace_dir_path)
  # Normalizes path to directory of workspace needed for file_reference.absolute_path
  workspaces_dir = workspace_dir_path
  if File.extname(workspace_dir_path) == '.xcworkspace'
    workspaces_dir = File.expand_path('..', workspaces_dir)
  end
  file_references.each do |file_reference|
    project_full_path = file_reference.absolute_path(workspaces_dir)
    load_schemes_from_project(project_full_path)
  end
  # Load schemes that are in the workspace container.
  workspace_abs_path = File.absolute_path(workspace_dir_path)
  Dir[File.join(workspace_dir_path, 'xcshareddata', 'xcschemes', '*.xcscheme')].each do |scheme|
    scheme_name = File.basename(scheme, '.xcscheme')
    @schemes[scheme_name] = workspace_abs_path
  end
end

def load_schemes_from_project(project_full_path)

Returns:
  • (void) -

Parameters:
  • project_full_path (String) --
def load_schemes_from_project(project_full_path)
  schemes = Xcodeproj::Project.schemes project_full_path
  schemes.each do |scheme_name|
    @schemes[scheme_name] = project_full_path
  end
end

def root_xml(contents)

Parameters:
  • contents (String) -- The XML contents of the workspace.

Returns:
  • (String) - The template of the workspace XML as formated by Xcode.
def root_xml(contents)
  <<-DOC
l version="1.0" encoding="UTF-8"?>
kspace
ersion = "1.0">
ntents.rstrip}
rkspace>
end

def save_as(path)

Returns:
  • (void) -

Parameters:
  • path (String) --
def save_as(path)
  FileUtils.mkdir_p(path)
  File.open(File.join(path, 'contents.xcworkspacedata'), 'w') do |out|
    out << to_s
  end
end

def to_s

Returns:
  • (String) - the XML representation of the workspace.
def to_s
  contents = ''
  stack = []
  @document.root.each_recursive do |elem|
    until stack.empty?
      last = stack.last
      break if last == elem.parent
      contents += xcworkspace_element_end_xml(stack.length, last)
      stack.pop
    end
    stack << elem
    contents += xcworkspace_element_start_xml(stack.length, elem)
  end
  until stack.empty?
    contents += xcworkspace_element_end_xml(stack.length, stack.last)
    stack.pop
  end
  root_xml(contents)
end

def xcworkspace_element_end_xml(depth, elem)

Returns:
  • (String) - The Xcode-specific XML formatting of an element end

Parameters:
  • elem (REXML::Document::Element) -- The XML element to format.
  • depth (Integer) -- The depth of the element in the tree
def xcworkspace_element_end_xml(depth, elem)
  "#{'   ' * depth}</#{elem.name}>\n"
end

def xcworkspace_element_start_xml(depth, elem)

Returns:
  • (String) - The Xcode-specific XML formatting of an element start

Parameters:
  • elem (REXML::Document::Element) -- The XML element to format.
  • depth (Integer) -- The depth of the element in the tree
def xcworkspace_element_start_xml(depth, elem)
  attributes = case elem.name
               when 'Group'
                 %w(location name)
               when 'FileRef'
                 %w(location)
               end
  contents = "<#{elem.name}"
  indent = '   ' * depth
  attributes.each { |name| contents += "\n   #{name} = \"#{elem.attribute(name)}\"" }
  contents.split("\n").map { |line| "#{indent}#{line}" }.join("\n") + ">\n"
end