Class: Pod::Command::Jsource::XcodeManager

Inherits:
Pod::Command::Jsource show all
Defined in:
lib/cocoapods-jsource/command/xcode_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ XcodeManager

Returns a new instance of XcodeManager.



10
11
12
13
14
15
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 10

def initialize(argv)
  @debug_path = 'Pods/Debug.xcodeproj'
  @workspace_name = `ls | grep .xcworkspace`.strip
  @cache_dict = cache_object
  super
end

Instance Method Details

#add_component_to_debug(component_name, source_path_hash, debug_xcodeproj, spec) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 136

def add_component_to_debug(component_name, source_path_hash, debug_xcodeproj, spec)
  source_path_hash.each do |binary_name, source_path|
    # 将源码添加到Debug.xcodeproj 里。
    UI.puts "add #{binary_name} to Debug.xcodeproj"
    component_group = debug_xcodeproj.main_group.find_subpath(binary_name, true)
    component_group.set_source_tree('<absolute>')
    component_group.set_path(source_path)
    component_group.clear
    files = avaliable_file component_group, spec
    add_files_to_group(debug_xcodeproj, component_group, files)
  end
  debug_xcodeproj.save
end

#add_debug_to_workspaceObject



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 186

def add_debug_to_workspace()
  # 获取主工程的名字。
  if File.exist? @workspace_name
    workspace = Xcodeproj::Workspace.new_from_xcworkspace @workspace_name
  else
    UI.puts "找不到对应的workspace: #{@workspace_name},请检查。"
    exit 1
  end
  if !workspace.schemes.values.include? File.realdirpath @debug_path
    workspace << @debug_path
    workspace.save_as @workspace_name
  end
end

#add_files_to_group(debug_xcodeproj, group, files = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 43

def add_files_to_group(debug_xcodeproj, group, files=[])
  Dir.foreach(group.real_path).sort_by { |object| object.to_s }.each do |entry|
    filePath = File.join(group.real_path, entry)
    next unless files.length == 0 or files.include? filePath
    ext_name = File.extname(entry)
    # 过滤目录和.DS_Store文件
    if !File.directory?(filePath) && entry != ".DS_Store" then
      # 向group中增加文件引用
      group.new_reference(filePath)
      # 目录情况下, 递归添加
    elsif File.directory?(filePath) && entry != '.' && entry != '..' then
      hierarchy_path = group.hierarchy_path[1, group.hierarchy_path.length]
      subGroup = debug_xcodeproj.main_group.find_subpath(hierarchy_path + '/' + entry, true)
      subGroup.set_source_tree('<group>')
      subGroup.set_path(group.real_path + entry)
      add_files_to_group(debug_xcodeproj, subGroup, files)
    end
  end
end

#all_components_in_debugObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 72

def all_components_in_debug()
  component_info = {}
  debug_xcodeproj = get_debug_xcodeproj
  if debug_xcodeproj.nil?
    return component_info
  end
  debug_xcodeproj.groups.each do |component_group|
    detail_hash = {}
    source_path_hash = {}
    source_path_hash[component_group.display_name] = component_group.real_path.to_s
    detail_hash[:source_paths] = source_path_hash
    version = version_from_path component_group.display_name, component_group.real_path.to_s
    detail_hash[:version] = version if version
    component_info[component_group.display_name] = detail_hash if detail_hash.length > 0
  end
  component_info
end

#avaliable_dirs(file_path, dest_file_path) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 101

def avaliable_dirs(file_path, dest_file_path)
  dir_list = []
  if file_path.to_s == dest_file_path.to_s
    return dir_list
  else
    parent_dir = File.dirname file_path
    dir_list << parent_dir
    dir_list = dir_list | avaliable_dirs(parent_dir, dest_file_path)
  end
  dir_list
end

#avaliable_file(group, spec) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 113

def avaliable_file(group, spec)
  component_name = spec.name
  subspec = group.name.gsub(/#{component_name}/, "")
  files = []
  if subspec
    spec.subspecs.each do |subspec_spec|
      next unless subspec_spec.name == "#{component_name}/#{subspec}"
      source_files = "#{group.real_path}/#{subspec_spec.attributes_hash["source_files"]}"
      tmp_files = Dir.glob (source_files)
      # 找到所有的文件夹
      tmp_files.each do |file_path|
        files << file_path
        parent_path = File.dirname file_path
        if !files.include? parent_path
          files = files | avaliable_dirs(file_path, group.real_path)
        end
      end
      break
    end
  end
  files
end

#clean_debugObject



167
168
169
170
171
172
173
174
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 167

def clean_debug()
  debug_xcodeproj = get_debug_xcodeproj
  remove_debug_from_workspace
  if debug_xcodeproj
    FileUtils.rm_rf [@debug_path]
  end

end

#component_count_in_debug(debug_xcodeproj) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 176

def component_count_in_debug(debug_xcodeproj)
  count = debug_xcodeproj.groups.length
  if count >= 2
    return count - 2
  else
    UI.puts "获取component的个数可能发成错误"
  end
  return count
end

#component_in_debug(component_name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 90

def component_in_debug(component_name)
  all_component_hash = all_components_in_debug
  component_hash = {}
  all_component_hash.each do |binary_name, detail_hash|
    if binary_name.start_with? component_name
      component_hash[binary_name] = detail_hash
    end
  end
  component_hash
end

#generate_debug_xcodeprojObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 22

def generate_debug_xcodeproj()
  debug_xcodeproj = get_debug_xcodeproj
  if debug_xcodeproj.nil?
    debug_xcodeproj = Xcodeproj::Project.new(@debug_path)
    # 去掉Frameworks和Products
    #debug_xcodeproj.groups.each do |component_group|
    #  component_group.clear
    #  component_group.remove_from_project
    #end
  end
  debug_xcodeproj
end

#get_debug_xcodeprojObject



35
36
37
38
39
40
41
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 35

def get_debug_xcodeproj()
  debug_xcodeproj = nil
  if File.exist? @debug_path
    debug_xcodeproj = Xcodeproj::Project.open(@debug_path)
  end
  debug_xcodeproj
end

#include_component(component_name, source_path, debug_xcodeproj) ⇒ Object

感觉没啥必要



64
65
66
67
68
69
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 64

def include_component(component_name, source_path, debug_xcodeproj)
  debug_xcodeproj.groups.each do |group|
    return true if group.name == component_name and File.exist? group.path
  end
  return false
end

#remove_component_from_debug(component_name) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 150

def remove_component_from_debug(component_name)
  debug_xcodeproj = get_debug_xcodeproj
  if debug_xcodeproj.nil?
    return
  end
  debug_xcodeproj.groups.each do |component_group|
    if component_group.display_name.start_with? component_name
      UI.puts "removing #{component_group.display_name} from Debug.xcodeproj"
      if !component_group.empty?
        component_group.clear
        component_group.remove_from_project
      end
    end
  end
  debug_xcodeproj.save
end

#remove_debug_from_workspaceObject



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 200

def remove_debug_from_workspace()
  if File.exist? @workspace_name
    workspace = Xcodeproj::Workspace.new_from_xcworkspace @workspace_name
  else
    UI.puts "找不到对应的workspace: #{@workspace_name},请检查。"
    exit 1
  end
  if workspace.schemes.values.include? File.realdirpath @debug_path
    workspace >> @debug_path
    workspace.save_as @workspace_name
  end
end

#validate!Object



17
18
19
20
# File 'lib/cocoapods-jsource/command/xcode_manager.rb', line 17

def validate!
  super
  help! '请先pod install/update 之后,在运行这个命令' if !File.exist? @workspace_name or !File.exist? "Pods"
end