Class: Xcodeproj::XCodeProject

Inherits:
Object
  • Object
show all
Defined in:
lib/podfileDep/check/project.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXCodeProject

Returns a new instance of XCodeProject.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/podfileDep/check/project.rb', line 28

def initialize
  @shell_project = nil
  @pod_project = nil
  @module_items = {}
  @version_map = {}
  @lock_content = {}
  init_shell_project
  init_pod_project
  init_lockfile
  init_version_map
  expand_module
  init_vendor_frameworks
end

Instance Attribute Details

#lock_contentHash

Returns:

  • (Hash)


23
24
25
# File 'lib/podfileDep/check/project.rb', line 23

def lock_content
  @lock_content
end

#module_itemsHash{String => ModuleItem}

Returns:



20
21
22
# File 'lib/podfileDep/check/project.rb', line 20

def module_items
  @module_items
end

#pod_projectObject

Pods工程xcodeproj对象

Returns:

  • Xcodeproj::Project



17
18
19
# File 'lib/podfileDep/check/project.rb', line 17

def pod_project
  @pod_project
end

#shell_projectObject

壳工程xcodeproj对象

Returns:

  • Xcodeproj::Project



13
14
15
# File 'lib/podfileDep/check/project.rb', line 13

def shell_project
  @shell_project
end

#version_mapHash

Returns:

  • (Hash)


26
27
28
# File 'lib/podfileDep/check/project.rb', line 26

def version_map
  @version_map
end

Instance Method Details

#deal_frameworks(frameworks) ⇒ Object



281
282
283
284
285
286
287
288
# File 'lib/podfileDep/check/project.rb', line 281

def deal_frameworks(frameworks)
  # "frameworks/WCDB/WCDB.framework",
  # "openssl.xcframework"
  temp_array = frameworks.split("/")
  result = temp_array.last
  result = result.gsub(".xcframework","")
  result.gsub(".framework", "")
end

#expand_moduleObject

展开模块



99
100
101
# File 'lib/podfileDep/check/project.rb', line 99

def expand_module
  self.expand_with_module(pod_project.main_group, nil)
end

#expand_with_module(group, module_name) ⇒ Object

展开模块



104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/podfileDep/check/project.rb', line 104

def expand_with_module(group, module_name)

  if group.class != Xcodeproj::Project::Object::PBXGroup
    return
  end

  if not module_name and group.hierarchy_path
    group_hierarchy_path_array = group.hierarchy_path.split("/")
    module_name = group_hierarchy_path_array.size >= 3 ? group_hierarchy_path_array[2] : nil
  end

  # 创建 source_file_item
  source_file_item = @module_items[module_name]
  if module_name and not source_file_item
    source_file_item = ModuleItem.new(module_name)
    source_file_item.podspec_path = get_podspec_path(module_name)
    @module_items[module_name] = source_file_item
  end

  # 更新module_path
  if source_file_item and not source_file_item.module_path and group.hierarchy_path
    group_hierarchy_path_array = group.hierarchy_path.split("/")
    if group_hierarchy_path_array.size == 3
      source_file_item.module_path = group.real_path
    end
    if group_hierarchy_path_array.include?("Development Pods")
      source_file_item.is_development_module = true
    end
  end

  # 文件引用
  file_reference_class = Xcodeproj::Project::Object::PBXFileReference

  not_child_names = ["Podfile","Frameworks", "Products","Targets Support Files","Support Files"]
  source_file_extension = %w[.h .m .mm .pch .c .cc .cpp .hpp]
  header_file_extension = %w[.h .hpp]

  group.children.each {|child|

    module_name = source_file_item ? source_file_item.module_name : nil

    if not_child_names.include?(child.name)
      # puts "不检查:#{child.name} #{child.real_path}"
      next
    end

    if child.class != file_reference_class
      # puts "不是文件引用:#{child.class}  #{child.real_path}"
      self.expand_with_module(child, module_name)
      next
    end

    hierarchy_path_array = child.hierarchy_path.split("/")
    if hierarchy_path_array.size < 3
      self.expand_with_module(child, module_name)
      next
    end

    if source_file_item
      extend_name = child.real_path.basename.extname
      basename = child.real_path.basename
      if basename.to_s == "#{module_name}.podspec" or basename.to_s == "#{module_name}.podspec.json"
        source_file_item.podspec_path = child.real_path
      elsif source_file_extension.include?(extend_name)
        source_file_item.source_files[child.real_path.basename.to_s] = child.real_path
        if header_file_extension.include?(extend_name)
          source_file_item.header_files[child.real_path.basename.to_s] = child.real_path
        end
      else
        # puts child.real_path
      end
    end

    # 递归展开
    self.expand_with_module(child, module_name)
  }

end

#get_podspec_path(module_name) ⇒ Object

读取某个模块的podspec路径



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/podfileDep/check/project.rb', line 184

def get_podspec_path(module_name)
  check_options = lock_content["CHECKOUT OPTIONS"]
  if check_options and check_options[module_name]
    check_option = check_options[module_name]
    request = Pod::Downloader::Request.new(released:false, name:module_name, params:check_option)
    slug_opts = {}
    cache_path = request.slug(**slug_opts)
    return "~/Library/Caches/CocoaPods/Pods/Specs/#{cache_path}.podspec.json"
  end

  # :path: localPods/XX
  local_spec = lock_content["EXTERNAL SOURCES"]
  if local_spec and local_spec[module_name]
    path = local_spec[module_name][:path]
    podspec_path = "#{path}/#{module_name}.podspec"
    podspec_json_path = "#{path}/#{module_name}.podspec.json"
    if File.exist?(podspec_path)
      return Pathname(podspec_path).expand_path
    elsif File.exist?(podspec_json_path)
      return Pathname(podspec_json_path).expand_path
    else
      return nil
    end
  end

  # 4.0.1-24ee2.podspec.json
  version_spec = @lock_content["SPEC CHECKSUMS"]
  md5_to5 = ""
  if version_spec and version_spec[module_name]
    md5 =  version_spec[module_name]
    md5_to5 = md5.slice(0,5)
  end
  version = @version_map[module_name]
  if version
    podspec_json_name = "#{version}-#{md5_to5}.podspec.json"
    path = "~/Library/Caches/CocoaPods/Pods/Specs/Release/#{module_name}/#{podspec_json_name}"
    return Pathname(path).expand_path
  end

  nil
end

#init_lockfileObject



69
70
71
72
73
74
75
76
77
# File 'lib/podfileDep/check/project.rb', line 69

def init_lockfile
  lockfile_path = MyConstants::PODFILE_LOCK

  unless File.exist?(lockfile_path)
    return
  end

  @lock_content = YAML.load_file(lockfile_path)
end

#init_pod_projectObject



60
61
62
63
64
65
66
67
# File 'lib/podfileDep/check/project.rb', line 60

def init_pod_project
  xcodeproj_path = "#{Dir.pwd}/Pods/Pods.xcodeproj"
  unless File.exist?(xcodeproj_path)
    return nil
  end

  @pod_project = Xcodeproj::Project.open(xcodeproj_path)
end

#init_shell_projectObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/podfileDep/check/project.rb', line 43

def init_shell_project
  xcodeproj_file = nil
  Dir.foreach(Dir.pwd) do |file|
    if file.end_with?(".xcodeproj")
      xcodeproj_file = file
      break
    end
  end

  xcodeproj_path = "#{Dir.pwd}/#{xcodeproj_file}"
  unless File.exist?(xcodeproj_path)
    return nil
  end

  @shell_project = Xcodeproj::Project.open(xcodeproj_path)
end

#init_vendor_frameworksObject

初始化模块的vendor_frameworks



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/podfileDep/check/project.rb', line 228

def init_vendor_frameworks
  @module_items.each_value do |module_item|
    module_item.frameworks << module_item.module_name

    unless module_item.podspec_path
      puts "⚠️ 读取#{module_item.module_name}的podspec路径为空,请发送Podfile.lock文件联系开发者修复"
      next
    end

    podspec_content = Util.get_podspec_content(module_item.podspec_path)

    unless podspec_content
      next
    end

    update_dep_frameworks(module_item.frameworks, podspec_content)

    subspec_content = podspec_content["subspecs"]
    if subspec_content and subspec_content.class == Array
      subspec_content.each { |json_content|
        update_dep_frameworks(module_item.frameworks, json_content)
      }
    end
  end

end

#init_version_mapObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/podfileDep/check/project.rb', line 78

def init_version_map
  lock_content = @lock_content

  remote_dependencies = lock_content["PODS"]
  unless remote_dependencies
    return
  end

  remote_dependencies.each do |dependency|
    if dependency.class == Hash
      dependency = dependency.keys.first
    end
    temp = dependency.split("(")
    name = temp[0].split("/")[0] # UMCShare/Social/ReducedWeChat
    name = name.gsub(" ", "").gsub(")", "")
    version = temp[1].gsub("= ", "").gsub(")", "")
    @version_map[name] = version
  end
end

#update_dep_frameworks(item_frameworks, json_content) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/podfileDep/check/project.rb', line 255

def update_dep_frameworks(item_frameworks, json_content)
  vendor_frameworks = json_content["vendored_frameworks"]
  unless vendor_frameworks
    ios = json_content["ios"]
    if ios and ios.class == Hash
      vendor_frameworks = ios["vendored_frameworks"]
    end
  end

  if vendor_frameworks
    if vendor_frameworks.class == Array
      vendor_frameworks.each do |vendor_framework|
        frameworks = deal_frameworks(vendor_framework)
        if frameworks != "*"
          item_frameworks << frameworks
        end
      end
    elsif vendor_frameworks.class == String
      frameworks = deal_frameworks(vendor_frameworks)
      if frameworks != "*"
        item_frameworks << frameworks
      end
    end
  end
end