cocoapods-gdepen 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 642a7e5e540f4c8e21835ef005cee506986c721115722e20873b2cc0c9463ba9
4
+ data.tar.gz: 195fa9fd9d8da58bc10a6b3cc002229f128787d4a8a3716b17959a1138d5f7a2
5
+ SHA512:
6
+ metadata.gz: 4d3aaefcf100746e7e48c02b820f6336efe6770ba34d06296242c05eed28c9e40aaa677e83f11398e03cd4184031dcad2b3c0c000d67b1970aaa999230fc9ad8
7
+ data.tar.gz: 759e71ca997f0e4bfbe423d30062e30e844f6bf60d0b58af4e012149e593846255191602f1ed8535b142d418dbeef1f4f174c9fc36dd0cf0aadf6ee7d1253e8e
@@ -0,0 +1,48 @@
1
+ require_relative 'generate_dependencies'
2
+
3
+ module Pod
4
+ class Command
5
+ # This is an example of a cocoapods plugin adding a top-level subcommand
6
+ # to the 'pod' command.
7
+ #
8
+ # You can also create subcommands of existing or new commands. Say you
9
+ # wanted to add a subcommand to `list` to show newly deprecated pods,
10
+ # (e.g. `pod list deprecated`), there are a few things that would need
11
+ # to change.
12
+ #
13
+ # - move this file to `lib/pod/command/list/deprecated.rb` and update
14
+ # the class to exist in the the Pod::Command::List namespace
15
+ # - change this class to extend from `List` instead of `Command`. This
16
+ # tells the plugin system that it is a subcommand of `list`.
17
+ # - edit `lib/cocoapods_plugins.rb` to require this file
18
+ #
19
+ # @todo Create a PR to add your plugin to CocoaPods/cocoapods.org
20
+ # in the `plugins.json` file, once your plugin is released.
21
+ #
22
+ class Gdepen < Command
23
+ self.summary = '查找podfile的依赖,并且插入到podfile中'
24
+
25
+ self.description = <<-DESC
26
+ 查找podfile的依赖,并且插入到podfile中.
27
+ DESC
28
+
29
+ def self.options
30
+ []
31
+ end
32
+
33
+ self.arguments = []
34
+
35
+ def initialize(argv)
36
+ super
37
+ end
38
+
39
+ def validate!
40
+ super
41
+ end
42
+
43
+ def run
44
+ CocoapodsGdepen::GenerateDependencies.new.generate_dependencies()
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,395 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cocoapods-core'
4
+ require 'cocoapods-pod-linkage'
5
+
6
+ module CocoapodsGdepen
7
+ class GenerateDependencies
8
+ # 所有hellotalk的依赖
9
+ $all_hellotalk_dependencies = []
10
+ # 所有hellotalk的动态库的依赖
11
+ $all_hellotalk_dynamic_dependencies = []
12
+ # 所有HelloTalk的本地依赖
13
+ $all_hellotalk_local_dependencies = []
14
+ # hellotalk json格式的podfile
15
+ $hellotalk_podfile_json = {}
16
+ # hellotalk项目的根目录
17
+ $hellotalk_project_root_dir = ''
18
+ # hellotalk的Podfile.lock的checkout options
19
+ $hellotalk_podlock_checkout_options = {}
20
+
21
+ # 已经存在podfile的依赖
22
+ $existing_podfile_dependencies = []
23
+ # 提取的依赖
24
+ $extracted_podfile_dependencies = []
25
+ # 版本决议之后的依赖
26
+ $resolved_podfile_dependencies = []
27
+ # 已经提取过的podspec文件路径
28
+ $extracted_podspec_paths = []
29
+
30
+ # 通过podfile文件提取所有hellotalk的依赖
31
+ def extract_all_hellotalk_dependencies(podfile_path)
32
+ podfile = Pod::Podfile.from_file(podfile_path)
33
+ hellotalk_target_definition = podfile.target_definition_list.find { |element| element.name == 'HelloTalk_Binary'}
34
+ $all_hellotalk_dependencies = hellotalk_target_definition.dependencies
35
+ $all_hellotalk_dynamic_dependencies = hellotalk_target_definition.explicit_pod_linkage
36
+
37
+ $hellotalk_podfile_json = podfile.to_hash
38
+ # puts JSON.pretty_generate($hellotalk_podfile_json)
39
+
40
+ for dependency in $all_hellotalk_dependencies
41
+ if dependency.local?
42
+ $all_hellotalk_local_dependencies << dependency
43
+ end
44
+ end
45
+
46
+
47
+ lockfile_path = Pathname.new(podfile_path + ".lock")
48
+ lockfile_hash = Pod::Lockfile.from_file(lockfile_path).to_hash
49
+ checkout_options = lockfile_hash["CHECKOUT OPTIONS"]
50
+ if checkout_options
51
+ $hellotalk_podlock_checkout_options = checkout_options
52
+ end
53
+ end
54
+
55
+ # 提取podfile的依赖
56
+ def extract_podfile_dependencies(podfile_path)
57
+ # 先清空自动插入的依赖
58
+ update_podfile(podfile_path, "")
59
+
60
+ podfile = Pod::Podfile.from_file(podfile_path)
61
+
62
+ if podfile && podfile.dependencies
63
+ # 查找存在的依赖
64
+ podfile.dependencies.each do |dependency|
65
+ $existing_podfile_dependencies << dependency
66
+ end
67
+
68
+ # 导出存在的依赖的依赖
69
+ podfile.dependencies.each do |dependency|
70
+ local_dependency = get_local_dependency(dependency.name)
71
+ if local_dependency
72
+ # 递归查找依赖的依赖
73
+ local_podspec_path = $hellotalk_project_root_dir + local_dependency.external_source&.dig(:path) + "/#{dependency.name}.podspec"
74
+ extract_podspec_dependencies(local_podspec_path)
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ # 提取podspec文件提取组件的所有依赖
81
+ def extract_podspec_dependencies(podspec_path)
82
+ # 提取过的组件,不要重复提取
83
+ if $extracted_podspec_paths.include?(podspec_path)
84
+ return
85
+ end
86
+
87
+ $extracted_podspec_paths << podspec_path
88
+
89
+ spec = Pod::Specification.from_file(podspec_path)
90
+
91
+ if spec && spec.dependencies
92
+ spec.dependencies.each do |dependency|
93
+ append_extract_dependency(dependency)
94
+ local_dependency = get_local_dependency(dependency.name)
95
+ if local_dependency
96
+ # 递归查找依赖的依赖
97
+ local_podspec_path = $hellotalk_project_root_dir + local_dependency.external_source&.dig(:path) + "/#{dependency.name}.podspec"
98
+ extract_podspec_dependencies(local_podspec_path)
99
+ end
100
+ end
101
+ end
102
+
103
+ if spec && spec.subspecs
104
+ spec.subspecs.each do |subspec|
105
+ extract_subspec_dependencies(subspec)
106
+ end
107
+ end
108
+ end
109
+
110
+ # 提取subspec的所有依赖
111
+ def extract_subspec_dependencies(subspec)
112
+ if subspec.dependencies
113
+ subspec.dependencies.each do |dependency|
114
+ append_extract_dependency(dependency)
115
+ local_dependency = get_local_dependency(dependency.name)
116
+ if local_dependency
117
+ # 递归查找依赖的依赖
118
+ local_podspec_path = $hellotalk_project_root_dir + local_dependency.external_source&.dig(:path) + "/#{dependency.name}.podspec"
119
+ extract_podspec_dependencies(local_podspec_path)
120
+ end
121
+ end
122
+ end
123
+
124
+ if subspec.subspecs
125
+ subspec.subspecs.each do |subsubspec|
126
+ # 递归查找子pod的子pod
127
+ extract_subspec_dependencies(subsubspec)
128
+ end
129
+ end
130
+ end
131
+
132
+ # 追加一个导出的依赖
133
+ def append_extract_dependency(new_dependency)
134
+ # 已经存在的依赖
135
+ existing_dependency = nil
136
+
137
+ $existing_podfile_dependencies.each do |dependency|
138
+ if new_dependency.name == dependency.name
139
+ existing_dependency = dependency
140
+ break
141
+ end
142
+
143
+ if is_subdependency(new_dependency)
144
+ if new_dependency.name.start_with?(dependency.name + "/")
145
+ existing_dependency = dependency
146
+ break
147
+ end
148
+ end
149
+ end
150
+
151
+ if existing_dependency == nil
152
+ $extracted_podfile_dependencies << new_dependency
153
+ end
154
+ end
155
+
156
+ # 是否有本地依赖
157
+ def get_local_dependency(name)
158
+ $all_hellotalk_local_dependencies.each do |dependency|
159
+ if dependency.local? && dependency.name == name
160
+ return dependency
161
+ end
162
+ end
163
+ return nil
164
+ end
165
+
166
+ # 查找到的依赖去重
167
+ def uniq_podfile_dependencies
168
+ uniq_dependencies = []
169
+ for dependency in $extracted_podfile_dependencies
170
+ exist_dependencies = []
171
+
172
+ uniq_dependencies.delete_if do |element|
173
+ if element.name == dependency.name
174
+ exist_dependencies << element
175
+ true
176
+ else
177
+ false
178
+ end
179
+ end
180
+
181
+ exist_dependency = exist_dependencies.first
182
+
183
+ if exist_dependency
184
+ if dependency.requirement.none?
185
+ uniq_dependencies << exist_dependency
186
+ else
187
+ uniq_dependencies << dependency
188
+ end
189
+ else
190
+ uniq_dependencies << dependency
191
+ end
192
+ end
193
+
194
+ $extracted_podfile_dependencies = uniq_dependencies
195
+ end
196
+
197
+ # 决议podfile的所有依赖
198
+ def resolve_podfile_dependencies
199
+ for podfile_dependency in $extracted_podfile_dependencies
200
+ result = podfile_dependency
201
+ for hellotalk_dependency in $all_hellotalk_dependencies
202
+ if hellotalk_dependency.name == podfile_dependency.name
203
+ result = hellotalk_dependency
204
+ break
205
+ end
206
+ end
207
+
208
+ # 是子依赖
209
+ if is_subdependency(result)
210
+ # 查找父依赖
211
+ parent_dependency = get_parent_dependency(result)
212
+ if parent_dependency
213
+ # 子依赖使用父依赖的要求
214
+ subdependency_name = result.name
215
+ result = parent_dependency
216
+ result.name = subdependency_name
217
+ end
218
+ end
219
+
220
+ $resolved_podfile_dependencies << result
221
+ end
222
+ end
223
+
224
+ # 判断是否是子依赖
225
+ def is_subdependency(dependency)
226
+ return dependency.name.include?('/')
227
+ end
228
+
229
+ def get_parent_dependency(sub_dependency)
230
+ for parent_dependency in $all_hellotalk_dependencies
231
+ if sub_dependency.name.start_with?(parent_dependency.name + "/")
232
+ return parent_dependency.dup
233
+ end
234
+ end
235
+
236
+ return nil
237
+ end
238
+
239
+ # 生成podfile的内容
240
+ def generate_podfile_string
241
+ podfile = ""
242
+ $resolved_podfile_dependencies.each do |dependency|
243
+ podfile += dependency_to_podfile_format(dependency)
244
+ end
245
+ return podfile
246
+ end
247
+
248
+ def dependency_to_podfile_format(dependency)
249
+ pod_string = ""
250
+ external_source = dependency.external_source
251
+
252
+ if dependency.local?
253
+ local_path = "../../../" + external_source&.dig(:path)
254
+ pod_string = "pod '#{dependency.name}', :path => '#{local_path}'"
255
+ elsif external_source&.dig(:git)
256
+ git_params = ":git => '#{external_source[:git]}'"
257
+
258
+ if external_source[:tag]
259
+ git_params += ", :tag => '#{external_source[:tag]}'"
260
+ elsif external_source[:branch]
261
+ git_params += ", :branch => '#{external_source[:branch]}'"
262
+ elsif external_source[:commit]
263
+ # podfile中的commit
264
+ git_params += ", :commit => '#{external_source[:commit]}'"
265
+ end
266
+
267
+ # podfile中没有指定commit,则用lockfile文件中的commit
268
+ if git_params.include?(":commit") == false
269
+ checkout_options = $hellotalk_podlock_checkout_options[dependency.name]
270
+ if checkout_options == nil
271
+ # 如果没有查找到commit,可能是子依赖,通过子依赖的父依赖再去查找一遍
272
+ if is_subdependency(dependency)
273
+ super_name = dependency.name.split("/").first
274
+ checkout_options = $hellotalk_podlock_checkout_options[super_name]
275
+ end
276
+ end
277
+
278
+ if checkout_options
279
+ commit = checkout_options[:commit]
280
+ if commit
281
+ git_params += ", :commit => '#{commit}'"
282
+ end
283
+ end
284
+ end
285
+
286
+ pod_string = "pod '#{dependency.name}', #{git_params}"
287
+ elsif dependency.requirement.nil?
288
+ # 处理其他类型的依赖,或者抛出异常,视情况而定
289
+ return ""
290
+ else
291
+ requirement = dependency.requirement
292
+ pod_string = "pod '#{dependency.name}', '#{requirement}'"
293
+ end
294
+
295
+ configuration_pod_whitelist = $hellotalk_podfile_json.dig("target_definitions", 0, "children", 0, "configuration_pod_whitelist")
296
+ if configuration_pod_whitelist
297
+ configurations = []
298
+
299
+ debug_dependencies = configuration_pod_whitelist.dig("Debug")
300
+ if debug_dependencies
301
+ if debug_dependencies.include?(dependency.name)
302
+ configurations << "'Debug'"
303
+ end
304
+ end
305
+
306
+ release_dependencies = configuration_pod_whitelist.dig("Release")
307
+ if release_dependencies
308
+ if release_dependencies.include?(dependency.name)
309
+ configurations << "'Release'"
310
+ end
311
+ end
312
+
313
+ if configurations.count > 0
314
+ pod_string = pod_string + ", :configurations => [#{configurations.join(", ")}]"
315
+ end
316
+
317
+ end
318
+
319
+ use_modular_headers_not_for = $hellotalk_podfile_json.dig("target_definitions", 0, "children", 0, "use_modular_headers", "not_for_pods")
320
+ if use_modular_headers_not_for
321
+ if use_modular_headers_not_for.include?(dependency.name)
322
+ pod_string = pod_string + ", :modular_headers => false"
323
+ end
324
+ end
325
+
326
+ if $all_hellotalk_dynamic_dependencies
327
+ if $all_hellotalk_dynamic_dependencies.include?(dependency.name)
328
+ pod_string = pod_string + ", :linkage => :dynamic"
329
+ end
330
+ end
331
+
332
+ pod_string = " " + pod_string + "\n"
333
+
334
+ return pod_string
335
+
336
+ end
337
+
338
+ # 更新生成的pod引用
339
+ def update_podfile(podfile_path, replacement_string)
340
+ file_contents = File.read(podfile_path)
341
+
342
+ # 使用正则表达式匹配并替换内容
343
+ pattern = /^def dependencies_for_script_automatic_insertion\b.*?^end\b/m
344
+ file_contents.gsub!(pattern, "def dependencies_for_script_automatic_insertion\n#{replacement_string}end")
345
+
346
+ # 将修改后的内容写回文件
347
+ File.open(podfile_path, 'w') do |file|
348
+ file.puts file_contents
349
+ end
350
+ end
351
+
352
+ def generate_dependencies()
353
+ # 当前目录
354
+ pwd = Dir.pwd + "/"
355
+
356
+ # podfile路径
357
+ podfile_path = pwd + "Podfile"
358
+
359
+ # 确保当前目录Podfile文件存在
360
+ unless File.file?(podfile_path)
361
+ puts "No pod file exists at #{podfile_path}"
362
+ return
363
+ end
364
+
365
+ # hellotalk根目录
366
+ $hellotalk_project_root_dir = File.dirname(File.dirname(File.dirname(pwd))) + "/"
367
+
368
+ # 提取所有hellotalk的依赖
369
+ extract_all_hellotalk_dependencies($hellotalk_project_root_dir + "podfile")
370
+
371
+ # 提取podfile的依赖
372
+ extract_podfile_dependencies(podfile_path)
373
+
374
+ # 去重
375
+ $extracted_podfile_dependencies = $extracted_podfile_dependencies.uniq
376
+
377
+ # 手动去重
378
+ uniq_podfile_dependencies()
379
+
380
+ # 排序
381
+ $extracted_podfile_dependencies = $extracted_podfile_dependencies.sort
382
+
383
+ # 版本决议
384
+ resolve_podfile_dependencies()
385
+
386
+ # 生成podfile
387
+ podfile_string = generate_podfile_string()
388
+
389
+ # 更新podfile文件
390
+ update_podfile(podfile_path, podfile_string)
391
+ end
392
+ end
393
+ end
394
+
395
+ # CocoapodsGdepen::GenerateDependencies.new.generate_dependencies()
@@ -0,0 +1,7 @@
1
+ module CocoapodsGdepen
2
+ class Test
3
+ def hello
4
+ puts "hello world"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ require 'cocoapods-gdepen/command/gdepen'
@@ -0,0 +1,3 @@
1
+ module CocoapodsGdepen
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require 'cocoapods-gdepen/gem_version'
@@ -0,0 +1,15 @@
1
+ require 'cocoapods-gdepen/command'
2
+ require 'cocoapods-gdepen/command/generate_dependencies'
3
+ require 'cocoapods-gdepen/command/test'
4
+
5
+ module CocoapodsGdepen
6
+ generated = false
7
+
8
+ Pod::HooksManager.register('cocoapods-gdepen', :source_provider) do |context|
9
+ if generated == false
10
+ CocoapodsGdepen::Test.new.hello()
11
+ CocoapodsGdepen::GenerateDependencies.new.generate_dependencies()
12
+ generated = true
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cocoapods-gdepen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ description: A short description of cocoapods-gdepen.
42
+ email:
43
+ - zhuzhi@hellotalk.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/cocoapods-gdepen.rb
49
+ - lib/cocoapods-gdepen/command.rb
50
+ - lib/cocoapods-gdepen/command/gdepen.rb
51
+ - lib/cocoapods-gdepen/command/generate_dependencies.rb
52
+ - lib/cocoapods-gdepen/command/test.rb
53
+ - lib/cocoapods-gdepen/gem_version.rb
54
+ - lib/cocoapods_plugin.rb
55
+ homepage: https://github.com/EXAMPLE/cocoapods-gdepen
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.4.10
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: A longer description of cocoapods-gdepen.
78
+ test_files: []