Class: RXCode::Workspace

Inherits:
Object
  • Object
show all
Defined in:
lib/rxcode/workspace.rb

Constant Summary collapse

WORKSPACE_EXTENSION =
WORKSPACE DISCOVERY ========================================================================================
'.xcworkspace'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace_path) ⇒ Workspace

Returns a new instance of Workspace.



5
6
7
8
9
10
11
# File 'lib/rxcode/workspace.rb', line 5

def initialize(workspace_path)
  unless self.class.is_workspace_at_path?(workspace_path)
    raise "#{workspace_path.inspect} is not a valid XCode workspace path" 
  end
  
  @path = workspace_path
end

Instance Attribute Details

#pathObject (readonly)

PATH =======================================================================================================


23
24
25
# File 'lib/rxcode/workspace.rb', line 23

def path
  @path
end

Class Method Details

.is_workspace_at_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/rxcode/workspace.rb', line 17

def self.is_workspace_at_path?(path)
  File.extname(path) == WORKSPACE_EXTENSION && File.directory?(path)
end

Instance Method Details

#build_locationObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rxcode/workspace.rb', line 108

def build_location
  Dir[File.join(derived_data_location, '*/info.plist')].each do |info_plist_path|
    build_location_data = Plist::parse_xml(info_plist_path)
    workspace_path = build_location_data['WorkspacePath']
    
    if workspace_path == self.path || (project_dependent? && workspace_path == enclosing_product_path)
      return File.dirname(info_plist_path)
    end
  end
  
  nil
end

#built_products_dirObject



121
122
123
124
125
# File 'lib/rxcode/workspace.rb', line 121

def built_products_dir
  if build_root = build_location
    File.join(build_root, "Build/Products")
  end
end

#contents_pathObject

CONTENTS ===================================================================================================


84
85
86
# File 'lib/rxcode/workspace.rb', line 84

def contents_path
  @contents_path ||= File.join(path, 'contents.xcworkspacedata').freeze
end

#derived_data_locationObject

BUILD LOCATION =============================================================================================


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rxcode/workspace.rb', line 90

def derived_data_location
  derived_data_style = value_for_setting('IDEWorkspaceUserSettings_DerivedDataLocationStyle')
  custom_derived_data_location = value_for_setting('IDEWorkspaceUserSettings_DerivedDataCustomLocation')
  
  if derived_data_style == 2 # 2 is the code for 'Workspace-relative path'
    File.expand_path(custom_derived_data_location, self.root)
  else
    
    prefs = RXCode.xcode_preferences
    if prefs.derived_data_location_is_relative_to_workspace?
      File.expand_path(prefs.derived_data_location, self.root)
    else
      prefs.derived_data_location
    end
    
  end
end

#enclosing_product_pathObject



51
52
53
# File 'lib/rxcode/workspace.rb', line 51

def enclosing_product_path
  File.dirname(path) if project_dependent?
end

#nameObject



41
42
43
# File 'lib/rxcode/workspace.rb', line 41

def name
  File.basename(path, '.xcworkspace')
end

#project_dependent?Boolean

PROJECTS ===================================================================================================

Returns:

  • (Boolean)


47
48
49
# File 'lib/rxcode/workspace.rb', line 47

def project_dependent?
  name == 'project' && RXCode::Project.is_project_at_path?(File.dirname(path))
end

#project_independent?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rxcode/workspace.rb', line 55

def project_independent?
  !project_dependent?
end

#project_pathsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rxcode/workspace.rb', line 63

def project_paths
  require 'nokogiri'
  
  if project_dependent?
    
    [ File.dirname(path) ]
    
  elsif File.exist?(contents_path)
    document = Nokogiri::XML(File.read(contents_path))
    
    document.xpath('/Workspace/FileRef').collect { |element|
      resolve_file_reference(element['location']) if element['location'] =~ /\.xcodeproj$/
    }.compact
    
  else
    []
  end
end

#projectsObject



59
60
61
# File 'lib/rxcode/workspace.rb', line 59

def projects
  @projects ||= project_paths.map { |project_path| Project.new(project_path, :workspace => self) }
end

#resolve_file_reference(location) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rxcode/workspace.rb', line 33

def resolve_file_reference(location)
  if location =~ /^(?:container|group):(.*)$/
    File.expand_path("../#{$1}", path)
  else
    location
  end
end

#rootObject



25
26
27
28
29
30
31
# File 'lib/rxcode/workspace.rb', line 25

def root
  if project_dependent?
    File.expand_path('../..', path)
  else
    File.dirname(path)
  end
end

#settings_file_for_user(user_name) ⇒ Object



150
151
152
# File 'lib/rxcode/workspace.rb', line 150

def settings_file_for_user(user_name)
  File.join(user_data_directory_for_user(user_name), "WorkspaceSettings.xcsettings")
end

#settings_for_user(user_name) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/rxcode/workspace.rb', line 154

def settings_for_user(user_name)
  settings_file = settings_file_for_user(user_name)
  if File.file?(settings_file)
    Plist::parse_xml(settings_file)
  else
    {}
  end
end

#shared_data_directoryObject

SETTINGS ===================================================================================================


129
130
131
# File 'lib/rxcode/workspace.rb', line 129

def shared_data_directory
  File.join(self.path, "xcshareddata")
end

#shared_settingsObject



137
138
139
140
141
142
143
144
# File 'lib/rxcode/workspace.rb', line 137

def shared_settings
  settings_file = shared_settings_file
  if File.file?(settings_file)
    Plist::parse_xml(settings_file)
  else
    {}
  end
end

#shared_settings_fileObject



133
134
135
# File 'lib/rxcode/workspace.rb', line 133

def shared_settings_file
  File.join(shared_data_directory, "WorkspaceSettings.xcsettings")
end

#user_data_directory_for_user(user_name) ⇒ Object



146
147
148
# File 'lib/rxcode/workspace.rb', line 146

def user_data_directory_for_user(user_name)
  File.join(path, "xcuserdata", "#{user_name}.xcuserdatad")
end

#user_settingsObject



163
164
165
# File 'lib/rxcode/workspace.rb', line 163

def 
  settings_for_user(ENV['USER'])
end

#value_for_setting(setting_name) ⇒ Object



167
168
169
# File 'lib/rxcode/workspace.rb', line 167

def value_for_setting(setting_name)
  [setting_name] || shared_settings[setting_name]
end