Class: Xcodeproj::Workspace

Inherits:
Object
  • Object
show all
Defined in:
lib/xcodeproj/workspace.rb,
lib/xcodeproj/workspace/file_reference.rb

Overview

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

Defined Under Namespace

Classes: FileReference

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*file_references) ⇒ Workspace

Returns a new instance of Workspace.

Parameters:

  • file_references (Array)

    @see file_references



19
20
21
22
# File 'lib/xcodeproj/workspace.rb', line 19

def initialize(*file_references)
  @file_references = file_references.flatten
  @schemes = {}
end

Instance Attribute Details

#file_referencesArray<String>, Array<FileReference> (readonly)

Returns:

  • (Array<String>)

    the paths of the projects contained in the

  • (Array<FileReference>)

    the paths of the projects contained in the workspace.



14
15
16
# File 'lib/xcodeproj/workspace.rb', line 14

def file_references
  @file_references
end

#schemesObject (readonly)

Returns the value of attribute schemes.



15
16
17
# File 'lib/xcodeproj/workspace.rb', line 15

def schemes
  @schemes
end

Class Method Details

.from_s(xml, workspace_path = '') ⇒ Workspace

Returns a workspace generated by reading the contents of the given XML representation.

Parameters:

  • xml (String)

    the XML representation of the workspace.

Returns:



49
50
51
52
53
54
55
56
57
# File 'lib/xcodeproj/workspace.rb', line 49

def self.from_s(xml, workspace_path = '')
  document = REXML::Document.new(xml)
  file_references = document.get_elements('/Workspace/FileRef').map do |node|
    FileReference.from_node(node)
  end
  instance = new(file_references)
  instance.load_schemes(workspace_path)
  instance
end

.new_from_xcworkspace(path) ⇒ Workspace

Returns a workspace generated by reading the contents of the given path.

Parameters:

  • path (String)

    the path of the ‘xcworkspace` file.

Returns:



33
34
35
36
37
# File 'lib/xcodeproj/workspace.rb', line 33

def self.new_from_xcworkspace(path)
  from_s(File.read(File.join(path, 'contents.xcworkspacedata')), File.expand_path(File.dirname(path)))
rescue Errno::ENOENT
  new
end

Instance Method Details

#<<(projpath) ⇒ void

This method returns an undefined value.

Adds a new path to the list of the of projects contained in the workspace.

Parameters:

  • projpath (String)

    The path of the project to add.



69
70
71
72
73
# File 'lib/xcodeproj/workspace.rb', line 69

def <<(projpath)
  project_file_reference = Xcodeproj::Workspace::FileReference.new(projpath)
  @file_references << project_file_reference
  load_schemes_from_project File.expand_path(projpath)
end

#include?(file_reference) ⇒ Boolean

Checks if the workspace contains the project with the given file reference.

Parameters:

  • file_reference (FileReference)

    The file_reference to the project.

Returns:

  • (Boolean)

    whether the project is contained in the workspace.



83
84
85
# File 'lib/xcodeproj/workspace.rb', line 83

def include?(file_reference)
  @file_references.include?(file_reference)
end

#load_schemes(workspace_dir_path) ⇒ void

This method returns an undefined value.

Load all schemes from all projects in workspace

Parameters:

  • workspace_dir_path (String)

    path of workspaces dir



117
118
119
120
121
122
# File 'lib/xcodeproj/workspace.rb', line 117

def load_schemes(workspace_dir_path)
  @file_references.each do |file_reference|
    project_full_path = file_reference.absolute_path(workspace_dir_path)
    load_schemes_from_project(project_full_path)
  end
end

#save_as(path) ⇒ void

This method returns an undefined value.

Saves the workspace at the given ‘xcworkspace` path.

Parameters:

  • path (String)

    the path where to save the project.



101
102
103
104
105
106
# File 'lib/xcodeproj/workspace.rb', line 101

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

#to_sString

Returns the XML representation of the workspace.

Returns:

  • (String)

    the XML representation of the workspace.



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

def to_s
  contents = file_references.map { |reference| file_reference_xml(reference) }
  root_xml(contents.join(''))
end