cocoapods-privacy 0.6.1 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 149efd9549824e388ee859167394477eabe82e7a715ef49ba43b195d9b501746
4
- data.tar.gz: 2949e52fffbefec5c2cb401711533e8a6e38d7f9cf53a1d19919a794a3a6dc8f
3
+ metadata.gz: 5cba9b9c9c115fa3aa4ce42e7a16a941ff121244e8293b73ef966d66e83a1d6f
4
+ data.tar.gz: dc65d1598e50bcda55d989d70e515e3197540d43e952d2fa4d3941010f585080
5
5
  SHA512:
6
- metadata.gz: ff8fbcab1c042ea1bf24e503d75b593f7f6a2adc59836cd822ed5e2ddf58f6a1e3efc91cd7c8e79d8647f42936d8e21c215d25a9c943a940df86e4ed090149c9
7
- data.tar.gz: 5037a9f32b471b8ec69172389969a568a7f63adc31f0278379a5d048828eaaa92d5457082809723db438ef39a155a61cbdac8dbf998e5e91b92dcf412ff5d09d
6
+ metadata.gz: c72e3521078489bed4c1e4dfea1f9cf229d5ff9a315750bb7043106078225c8e976894306a8997af365b418a6c03132b837c6364895f12d8e7aae5a920db1a5f
7
+ data.tar.gz: 4e2e36942669c75e9ff98be1224989bd4682830cf25ca2c6763b57fb0fd24ded722909e090389aeaf5a76e3a831c68f629945594d94c4783a58857fd655d1d19
@@ -45,7 +45,7 @@ module Confuse
45
45
  func = "public extension #{key} {\n" + values.map { |value|
46
46
  value.split("\n").map { |line| " #{line}" }.join("\n") # 每行前加四个空格
47
47
  }.join("\n\n") + "\n}"
48
- }.join("\n")
48
+ }.join("\n\n")
49
49
  confuse_func_content = funcs
50
50
  puts "混淆(扩展):\n 文件:#{swift_confuse_file_path}\n 内容👇:\n#{confuse_func_content}"
51
51
 
@@ -104,7 +104,7 @@ module Confuse
104
104
  swift_extension_funcBody_map = {}
105
105
 
106
106
  #oc 暴露给swift 的扩展名称
107
- swift_extension = ""
107
+ swift_extension = []
108
108
 
109
109
  # 段注释和单行注释标志
110
110
  in_block_comment = false
@@ -177,48 +177,92 @@ module Confuse
177
177
  if !in_block_comment && !in_line_comment
178
178
 
179
179
  ###----- 处理oc ------
180
- # 查找并处理注解:__attribute__((annotate("BB_?Confuse:xxx")))
181
- #.*?: 匹配任意字符(包括冒号),但它会尽量少匹配直到遇到冒号为止。
182
- #.*? 匹配冒号后面的一些字符(任意字符,直到遇到下一个双引号)。
183
- #这样可以确保匹配的字符串中至少包含一个冒号。
184
- line_scrub.scan(/__attribute__\(\(annotate\("(.*?:.*?)"\)\)\)/) do |match|
185
- # # 将注解内容按逗号分割功能,并去重
186
- commands = match.first.split(',').map(&:strip)
187
- commands.map do |commandInfo|
188
- puts commandInfo
189
- commandKeyAndValue = commandInfo.split(':')
190
- commandKey = commandKeyAndValue.first
191
- commandValue = commandKeyAndValue.last
192
- if commandKey && commandKey =~ /BB_?Confuse/
193
- swift_extension = commandValue
194
- end
180
+ #### 检测BBConfuseOCToSwift("xxxx") 用来处理oc函数混淆暴露给swift
181
+ line_scrub.scan(/^BBConfuseOCToSwift\(\"(.*?)\"\)/) do |match|
182
+ literals = match.first.split(',').map(&:strip)
183
+ unless literals.empty?
184
+ swift_extension = literals
195
185
  end
186
+ end
187
+
188
+ ### 检测BBConfuseOCClass("xxxx") 用来处理oc 类混淆
189
+ line_scrub.gsub!(/^BBConfuseOCClass\(\"(.*?)\"\)/) do |match|
190
+ modified = true #开启编辑
191
+ original_api = $1
192
+ new_api = encrypted_api([original_api]).first
193
+ puts "asasd = #{new_api} "
194
+ "BBConfuseOCClass(\"#{new_api}\")"
195
+ end
196
196
 
197
- # new_parts = encrypted_api(apis)
198
197
 
199
- # apis.each_with_index do |seg, index|
200
- # apis_define_map[seg.strip] = new_parts[index]
201
- # end
198
+ #### 检测BBConfuseOCClass("xxxx") 用来处理oc 类混淆
199
+ # line_scrub.scan(/^BBConfuseOCClass\(\"(.*?)\"\)/) do |match|
200
+ # puts "match = #{match}"
201
+ # apis = [match.first]
202
+ # puts "apis = #{apis}"
203
+ # new_parts = encrypted_api(apis)
204
+ # apis.each_with_index do |seg, index|
205
+ # apis_define_map[seg.strip] = new_parts[index]
206
+ # end
207
+ # end
208
+
209
+ # @property (nonatomic, assign) BOOL coldLaunch BBConfuseOCProperty;
210
+ #### 检测@protperty(xxxx)mmm *nnn BBConfuseOCProperty 用来处理oc 属性混淆
211
+ line_scrub.scan(/^@property\s*\((.*?)\)\s*(\w+)\s*([\*&]?)\s*(\w+)\s*BBConfuseOCProperty/) do |match|
212
+ puts "BBConfuseOCProperty = #{match}"
213
+ is_nullable = match.first.include?("nullable") || match.first.include?("weak")
214
+ return_type = match.second + match[2]
215
+ property_name = match[3]
216
+ apis = [property_name]
217
+ new_parts = encrypted_api(apis)
218
+
219
+ if !swift_extension.empty? && return_type && !return_type.empty?
220
+ swift_return_type = ConfuseUtils.convert_oc_type_to_swift(return_type)
221
+ swift_property_body = SwiftCallAssembly.assemblyProperty(property_name,new_parts.first,swift_return_type,is_nullable)
222
+
223
+ swift_extension.each do |extension|
224
+ swift_extension_funcBody_map[extension] ||= []
225
+ swift_extension_funcBody_map[extension].push(swift_property_body)
226
+ end
227
+ end
202
228
 
203
- # puts "__attribute__ = #{match}"
229
+ apis.each_with_index do |seg, index|
230
+ apis_define_map[seg.strip] = new_parts[index]
231
+ end
204
232
  end
205
233
 
206
- # 查找并处理注解:__attribute__((annotate("xxx")))
207
- # @regex = /^[+-]\s*\(\s*([^$]+)\s*\**\s*\)\s*(.+)/
208
- line_scrub.scan(/(^[-+]\s*\(.*?\)\s*(\w+)\s*([\w\s]+))\s*__attribute__\(\(annotate\(/) do |match|
234
+ #### BBConfuseOCFunc 用来处理oc 函数混淆
235
+ # ^ 匹配行首 + (BOOL)
236
+ # [-+] 匹配 + - 符号(ObjC 方法声明开头) +
237
+ # \s* 匹配 0 个或多个空白字符 空格
238
+ # \([\w\s\*]+\) 匹配一对圆括号里的类型声明,含字母、空格、* 指针符 (BOOL) / (NSString *)
239
+ # \s* 匹配 0 个或多个空白字符 空格
240
+ # (\w+) 捕获组 1:匹配方法名(只匹配到冒号前) uxxx_BB_Confuse_BBMUserCenter
241
+ # \s* 匹配 0 个或多个空白字符 空格
242
+ # (.*?) 捕获组 2:匹配“方法声明后到 __attribute__ 前的所有内容”,但用非贪婪方式,遇到 __attribute__ 就停 :(NSString *)name tip:(BOOL)ret
243
+ # \s*__attribute__\(\(annotate\( 匹配固定字符串 __att
244
+ regular = ConfuseUtils.oc_func_regular(line_scrub,false)
245
+ if regular
246
+ regular = /#{regular}\s*BBConfuseOCFunc/
247
+ line_scrub.scan(regular) do |match|
209
248
  apis = [match.second]
210
249
  new_parts = encrypted_api(apis)
250
+ funcStr = match.first.sub(';','').sub(/BBConfuseOCFunc.*/, '')
251
+ is_nullable, return_type, swift_method_declaration ,params = ObjCMethodAPIConverter.convert(funcStr)
211
252
 
212
- funcStr = match.first.sub(';','').sub(/__attribute__\(\(annotate\(["][^"]*["]\)\)\).*/, '')
213
- swift_method_declaration,params = ObjCMethodAPIConverter.convert(funcStr)
214
253
  if !swift_extension.empty? && swift_method_declaration && !swift_method_declaration.empty?
215
- swift_func_body = SwiftCallAssembly.assembly(new_parts.first,swift_method_declaration,params)
216
- swift_extension_funcBody_map[swift_extension] ||= []
217
- swift_extension_funcBody_map[swift_extension].push(swift_func_body)
254
+ swift_func_body = SwiftCallAssembly.assembly(new_parts.first, swift_method_declaration, params, is_nullable, return_type)
255
+
256
+ swift_extension.each do |extension|
257
+ swift_extension_funcBody_map[extension] ||= []
258
+ swift_extension_funcBody_map[extension].push(swift_func_body)
259
+ end
218
260
  end
261
+
219
262
  apis.each_with_index do |seg, index|
220
263
  apis_define_map[seg.strip] = new_parts[index]
221
264
  end
265
+ end
222
266
  end
223
267
 
224
268
  ###----- 处理swift 类 ------
@@ -281,10 +325,17 @@ module Confuse
281
325
  if line_scrub.strip.start_with?(confuse_func_pre_literal)
282
326
  #BBConfuseMixObjc("#selector(abcdefg(in:sencName:))"); asdasagwtrqwetr
283
327
 
284
-
285
328
  #selector(.*?)\)
286
329
  #解析带参数的
287
- # line_scrub.gsub!(/#{confuse_func_pre_literal}\(#selector\((.*?)\)\)\);(?:.*?@objc\((\S+)\))?(?:.*?@objc\s*)?/) do |match|
330
+ # | 部分 | 作用 | 举例 |
331
+ # |:-------------------------|:------------------------------------------------|:---------------------------------|
332
+ # | `#{confuse_func_pre_literal}\(` | 匹配你之前定义好的前缀字串(比如 `confuseMethod(`)和一个 `(` | `confuseMethod(` |
333
+ # | `(?:#selector\((.*?)\)\))?` | **非捕获组**,匹配 `#selector(XXX)`,括号内的 `XXX` 放进 `捕获组1` | `#selector(someMethod)` |
334
+ # | `(?:"([^"]*:[^"]*)")?` | **可选**,匹配 `:"xxx:xxx"` 格式的字符串,放进 `捕获组2` | `"abc:def"` |
335
+ # | `\);` | 匹配 `);` |
336
+ # | `(?:.*?@objc\((\S+)\))?` | **非捕获组**,可选,匹配 `@objc(XXX)`,把 `XXX` 放进 `捕获组3` | `@objc(mySelector)` |
337
+ # | `(?:.*?@objc\s*)?` | **非捕获组**,可选,匹配剩下一个 `@objc` 没括号的情况 | `@objc` |
338
+
288
339
  line_scrub.gsub!(/#{confuse_func_pre_literal}\((?:#selector\((.*?)\)\))?(?:"([^"]*:[^"]*)")?\);(?:.*?@objc\((\S+)\))?(?:.*?@objc\s*)?/) do |match|
289
340
  modified = true
290
341
  selector_match = $1
@@ -302,9 +353,15 @@ module Confuse
302
353
  end
303
354
 
304
355
  #解析不带参数的
305
- # line_scrub.gsub!(/#{confuse_func_pre_literal}\(#selector\(([^\(]+)\)\);(?:.*?@objc\((\S+)\))?(?:.*?@objc\s*)?/) do |match|
356
+ # | 部分 | 作用 | 举例 |
357
+ # |:-------------------------------|:--------------------------------------------------|:--------------------------|
358
+ # | `#{confuse_func_pre_literal}\(` | 匹配你的宏/方法名前缀 + `(`,比如 `confuseMethod(` | `confuseMethod(` |
359
+ # | `(?:#selector\(([^\(]+)\))?` | **非捕获组**,可选。匹配 `#selector(...)` 里的内容,放进 `捕获组1`,这里用 `([^\(]+)` 匹配**非左括号字符**,直到遇到 `)` | `#selector(doSomething)` → `doSomething` |
360
+ # | `(?:"([^":]+)")?` | **可选**,匹配 `"xxx"`,但里面不能有 `:` 或 `"`,放进 `捕获组2`。注意这版里不再要求冒号存在,单纯匹配一段字符串 | `"helloWorld"` |
361
+ # | `\);` | 匹配 `);` 结尾 |
362
+ # | `(?:.*?@objc\((\S+)\))?` | **非捕获组**,可选。匹配 `@objc(XXX)`,把 `XXX` 放进 `捕获组3`。`(\S+)` 匹配非空白连续字符 | `@objc(doSomethingElse)` |
363
+ # | `(?:.*?@objc\s*)?` | **非捕获组**,可选。匹配剩下没括号的 `@objc` | `@objc` |
306
364
  line_scrub.gsub!(/#{confuse_func_pre_literal}\((?:#selector\(([^\(]+)\))?(?:"([^":]+)")?\);(?:.*?@objc\((\S+)\))?(?:.*?@objc\s*)?/) do |match|
307
- # line_scrub.gsub!(/#{confuse_func_pre_literal}\((#selector\((.*?)\)|"([^"]+)")\);(?:.*?@objc\((\S+)\))?(?:.*?@objc\s*)?/) do |match|
308
365
  modified = true
309
366
  selector_match = $1
310
367
  str_match = $2
@@ -465,9 +522,30 @@ module Confuse
465
522
  end
466
523
  end
467
524
  end
525
+
526
+ # 用 apis_define_map 中对应的 value,更新 swift_extension_funcBody_map 中相同 key 的 key, 避免AClass同时定义了类混淆且暴露给swift, swift 扩展使用原先未混淆的类的问题
527
+ # update_func_body_map_keys!($apis_define_map, $swift_extension_funcBody_map)
528
+
468
529
  return $apis_define_map, $swift_extension_funcBody_map
469
530
  end
470
531
 
532
+ # 用 apis_define_map 中对应的 value,更新 swift_extension_funcBody_map 中相同 key 的 key, 避免AClass同时定义了类混淆且暴露给swift, swift 扩展使用原先未混淆的类的问题
533
+ def update_func_body_map_keys!(apis_define_map, swift_extension_funcBody_map)
534
+ new_map = {}
535
+
536
+ swift_extension_funcBody_map.each do |key, value|
537
+ if apis_define_map.has_key?(key)
538
+ confuse_value = apis_define_map[key]
539
+ new_key = confuse_value
540
+ new_map[new_key] = value
541
+ else
542
+ new_map[key] = value
543
+ end
544
+ end
545
+
546
+ swift_extension_funcBody_map.replace(new_map)
547
+ end
548
+
471
549
 
472
550
  def encrypted_api(apis)
473
551
  encrypted_api = ""
@@ -18,7 +18,6 @@ module ConfuseUtils
18
18
  # xcode工程地址
19
19
  def self.project_path
20
20
  matching_files = Dir[File.join(Pathname.pwd, '*.xcodeproj')].uniq
21
- puts "matching_files = #{matching_files}"
22
21
  matching_files.first
23
22
  end
24
23
 
@@ -104,4 +103,174 @@ module ConfuseUtils
104
103
  "#{spaces}#{str}"
105
104
  end
106
105
 
106
+ def self.oc_func_regular(str,isCapture = false)
107
+ if str =~ /^[-+]\s*\(.*?\)/ #为函数
108
+ num = match_nesting_parentheses_first(str)
109
+ if isBlockReturn(str) === true #block 返回参数函数
110
+ if num > 1
111
+ regular = oc_block_regular(num - 1,isCapture)
112
+ regular = /(^[-+]#{regular}(\w+)\s*(.+))/
113
+ return regular
114
+ end
115
+ else
116
+ regular = oc_normal_regular(num - 1,isCapture)
117
+ regular = /(^[-+]#{regular}(\w+)\s*(.+))/
118
+ return regular
119
+ end
120
+ end
121
+ return nil
122
+ end
123
+
124
+ def self.match_nesting_parentheses_first(str)
125
+ stack = []
126
+ count = 0
127
+
128
+ str.each_char.with_index do |char, idx|
129
+ if char == '('
130
+ stack.push(idx) # 记录左括号的位置
131
+ elsif char == ')'
132
+ count += 1
133
+ stack.pop # 弹出左括号位置
134
+ if stack.empty?
135
+ return count
136
+ end
137
+ end
138
+ end
139
+ 0 # 如果没有匹配到指定数量的括号对,返回 nil
140
+ end
141
+
142
+ def self.isBlockReturn(str)
143
+ if str =~ /^[-+]\s*\(([^)]*\^.+?)\)/ #block 返回参数函数
144
+ return true
145
+ else
146
+ return false
147
+ end
148
+ end
149
+
150
+ def self.oc_block_regular(num,isCapture = false)
151
+ if isCapture == false
152
+ "\\s*\\((?:[^\\)]+\\)){#{num}}.*?\\)\\s*"
153
+ else
154
+ "\\s*\\(((?:[^\\)]+\\)){#{num}}.*?)\\)\\s*"
155
+ end
156
+ end
157
+
158
+ def self.oc_normal_regular(num,isCapture = false)
159
+ if isCapture == false
160
+ "\\s*\\(.*?\\)\\s*"
161
+ else
162
+ "\\s*\\((.*?)\\)\\s*"
163
+ end
164
+ end
165
+
166
+ # Convert the Objective-C return type to a Swift type
167
+ def self.convert_oc_type_to_swift(return_type)
168
+ # 去除类型额外的修饰
169
+ is_nullable = (return_type =~ /\b(__nullable|nullable|_Nullable)\b/)
170
+ clean_type = return_type.gsub(/\b(__nullable|nullable|_Nullable|__nonnull|nonnull|_Nonnull)\b/, '').strip
171
+ # 去除类型末尾的指针符号和周围空格,生成基础类型名
172
+ base_type = clean_type.gsub(/(\w+)\s*\*+\z/, '\1').strip
173
+
174
+ # base_type = return_type
175
+ # puts "base_type = #{return_type}"
176
+
177
+ case base_type
178
+ # Foundation 基础类型
179
+ when "NSString" then "String"
180
+ when "NSMutableString" then "String" # Swift 中 String 是值类型
181
+ when "NSArray" then "Array<Any>"
182
+ when "NSMutableArray" then "Array<Any>"
183
+ when "NSSet" then "Set<Any>"
184
+ when "NSMutableSet" then "Set<Any>"
185
+ when "NSDictionary" then "Dictionary<AnyHashable, Any>"
186
+ when "NSMutableDictionary" then "Dictionary<AnyHashable, Any>"
187
+ when "NSError" then "Error?"
188
+ when "NSURL" then "URL"
189
+ when "NSData" then "Data"
190
+ when "NSNumber" then "NSNumber" # 特殊处理需要结合实际数值类型
191
+ when "NSDate" then "Date"
192
+
193
+ # 基础数值类型
194
+ when "NSInteger" then "Int"
195
+ when "NSUInteger" then "UInt"
196
+ when "CGFloat" then "CGFloat"
197
+ when "CGPoint" then "CGPoint"
198
+ when "CGRect" then "CGRect"
199
+ when "CGSize" then "CGSize"
200
+ when "BOOL" then "Bool"
201
+ when "Boolean" then "Bool"
202
+ when "double" then "Double"
203
+ when "float" then "Float"
204
+ when "long" then "Int"
205
+ when "long long" then "Int64"
206
+
207
+
208
+ # 特殊类型
209
+ when "id" then "AnyObject"
210
+ when "Class" then "AnyClass"
211
+ when "SEL" then "Selector"
212
+ when "Protocol" then "Protocol"
213
+ when "instancetype" then "Self"
214
+ when "void" then "Void"
215
+
216
+ # # # 函数指针/Block 类型(需二次处理)
217
+ # when /void\s*\(\^\)/ then convert_block_to_closure(return_type)
218
+ # when /(.+?)\s*\(\^\)/ then convert_block_to_closure(return_type)
219
+
220
+ # # 指针类型保留原始表示
221
+ # when /(.+?\s*\*+)/ then return_type
222
+
223
+ else
224
+ # # 处理Block类型
225
+ if clean_type.match(/^\s*(?:void|.+?)\s*\(\^\)/)
226
+ convert_block_to_closure(clean_type)
227
+ # 处理自定义的block
228
+ elsif clean_type =~ /(.*?[B|b]lock$)/
229
+ if is_nullable
230
+ "#{clean_type}?"
231
+ else
232
+ "@escaping #{clean_type}"
233
+ end
234
+ # 处理其他指针类型(去掉指针符号后返回基础类型名)
235
+ elsif clean_type.include?('*')
236
+ base_type
237
+ else
238
+ # 默认保留原类型
239
+ clean_type
240
+ end
241
+ end
242
+ end
243
+
244
+ # # 测试用例
245
+ # puts convert_block_to_closure('void (^)()') # 输出: () -> Void
246
+ # puts convert_block_to_closure('NSInteger (^)(BOOL)') # 输出: (Bool) -> Int
247
+ # puts convert_block_to_closure('NSString *(^)(NSInteger)') # 输出: (Int) -> String?
248
+ # puts convert_block_to_closure('void (^)(NSInteger, BOOL)') # 输出: (Int, Bool) -> Void
249
+ def self.convert_block_to_closure(block_signature)
250
+ # 正则表达式解释:
251
+ # ^([\w\s\*]+)\s*\(\^\)\s*(?:\((.*?)\))?\s*$
252
+ # 1. ([\w\s\*]+): 捕获返回值类型(如 void、NSInteger、NSString * 等)。
253
+ # 2. \(\^\): 匹配 '^' 表示这是一个 block。
254
+ # 3. (?:\((.*?)\))? : 可选地捕获参数列表(括号中的内容)。
255
+ if block_signature =~ /^([\w\s\*]+)\s*\(\^\)\s*(?:\((.*?)\))?\s*$/
256
+ return_type = $1.strip
257
+ params = $2 ? $2.split(',').map(&:strip) : []
258
+
259
+ # 转换返回值类型
260
+ swift_return_type = convert_oc_type_to_swift(return_type)
261
+
262
+ # 转换参数类型
263
+ swift_params = params.map do |param|
264
+ param_parts = param.split(' ')
265
+ param_type =convert_oc_type_to_swift(param_parts.first)
266
+ "#{param_type}"
267
+ end.join(", ").gsub("Void","")
268
+
269
+ # 构造最终的 Swift 闭包类型
270
+ result = "@escaping (#{swift_params}) -> #{swift_return_type}"
271
+ else
272
+ raise ArgumentError, "Invalid block signature: #{block_signature}"
273
+ end
274
+ end
275
+
107
276
  end
@@ -3,7 +3,9 @@ module Confuse
3
3
  class ObjCMethodAPIConverter
4
4
  # Regular expression to match Objective-C method declarations
5
5
  # def initialize
6
- @regex = /^[+-]\s*\(\s*([\w\s*]+)\s*\**\s*\)\s*(.+)/
6
+
7
+ # @blockRegex = /#{ConfuseUtils.oc_block_regular}/
8
+ # @regex = /#{ConfuseUtils.noneBlock}/
7
9
  @param_regex = /(\w+):\s*\(([^:]+)\)(\w+)/x
8
10
  # end
9
11
 
@@ -16,102 +18,43 @@ module Confuse
16
18
  is_static = objc_method_declaration.start_with?("+")
17
19
  preFuncDes = is_static ? "static " : ""
18
20
 
19
-
21
+ regular = ConfuseUtils.oc_func_regular(objc_method_declaration,true)
20
22
  # Match the method declaration using the regex
21
- matches = objc_method_declaration.match(@regex)
23
+ matches = objc_method_declaration.match(regular)
22
24
  return nil,nil unless matches
23
25
 
24
26
  # Extract components from the matched regex groups
25
- return_type = matches[1] || "Void"
26
- param_section = matches[2] || ""
27
- method_name = matches[2]
27
+ return_type = matches[2] || "Void"
28
+ method_name = matches[3]
29
+ param_section = (matches[3] + (matches[4] || "")) || ""
30
+ # puts "matches = #{matches}"
31
+ # puts "objc_method_declaration = #{objc_method_declaration}"
28
32
  # puts "return_type = #{return_type}"
29
33
  # puts "param_section = #{param_section}"
30
34
  # puts "method_name = #{method_name}"
31
35
 
36
+ is_nullable = return_type.include?("nullable")
37
+
32
38
  # Convert return type to Swift equivalent
33
39
  return_type = convert_oc_type_to_swift(return_type)
40
+ return_type.gsub!(Regexp.union('nullable', 'nonnull', 'null_unspecified', 'null_resettable'), '')
34
41
 
35
42
  # Extract parameters from the method declaration
36
43
  params = extract_parameters(param_section)
37
44
  method_name = extract_methodName(param_section,method_name)
38
- # puts "params = #{params}"
39
45
 
40
46
  # Construct the Swift method declaration
41
47
  swift_method_declaration = "#{preFuncDes}public func #{method_name}(#{params.join(', ')})"
42
48
  swift_method_declaration += " -> #{return_type}" unless return_type == "Void"
43
49
 
44
- return swift_method_declaration, extract_parameter_names(param_section)
50
+ return is_nullable, return_type, swift_method_declaration, extract_parameter_names(param_section)
45
51
  end
46
52
 
47
53
  private
48
54
 
49
55
  # Convert the Objective-C return type to a Swift type
50
56
  def self.convert_oc_type_to_swift(return_type)
51
- # 去除类型末尾的指针符号和周围空格,生成基础类型名
52
- # base_type = return_type.gsub(/\s*\*+$/, '').strip
53
- base_type = return_type.gsub(/(\w+)\s*\*+\z/, '\1').strip
54
- # base_type = return_type
55
- # puts "base_type = #{return_type}"
56
-
57
- case base_type
58
- # Foundation 基础类型
59
- when "NSString" then "String"
60
- when "NSMutableString" then "String" # Swift 中 String 是值类型
61
- when "NSArray" then "Array<Any>"
62
- when "NSMutableArray" then "Array<Any>"
63
- when "NSSet" then "Set<Any>"
64
- when "NSMutableSet" then "Set<Any>"
65
- when "NSDictionary" then "Dictionary<AnyHashable, Any>"
66
- when "NSMutableDictionary" then "Dictionary<AnyHashable, Any>"
67
- when "NSError" then "Error?"
68
- when "NSURL" then "URL"
69
- when "NSData" then "Data"
70
- when "NSNumber" then "NSNumber" # 特殊处理需要结合实际数值类型
71
- when "NSDate" then "Date"
72
-
73
- # 基础数值类型
74
- when "NSInteger" then "Int"
75
- when "NSUInteger" then "UInt"
76
- when "CGFloat" then "CGFloat"
77
- when "CGPoint" then "CGPoint"
78
- when "CGRect" then "CGRect"
79
- when "CGSize" then "CGSize"
80
- when "BOOL" then "Bool"
81
- when "Boolean" then "Bool"
82
- when "double" then "Double"
83
- when "float" then "Float"
84
- when "long" then "Int"
85
- when "long long" then "Int64"
86
-
87
-
88
- # 特殊类型
89
- when "id" then "Any"
90
- when "Class" then "AnyClass"
91
- when "SEL" then "Selector"
92
- when "Protocol" then "Protocol"
93
- when "instancetype" then "Self"
94
- when "void" then "Void"
95
-
96
- # # # 函数指针/Block 类型(需二次处理)
97
- # when /void\s*\(\^\)/ then convert_block_to_closure(return_type)
98
- # when /(.+?)\s*\(\^\)/ then convert_block_to_closure(return_type)
99
-
100
- # # 指针类型保留原始表示
101
- # when /(.+?\s*\*+)/ then return_type
102
-
103
- else
104
- # # 处理Block类型
105
- if return_type.match(/^\s*(?:void|.+?)\s*\(\^\)/)
106
- convert_block_to_closure(return_type)
107
- # 处理其他指针类型(去掉指针符号后返回基础类型名)
108
- elsif return_type.include?('*')
109
- base_type
110
- else
111
- # 默认保留原类型
112
- return_type
113
- end
114
- end
57
+ return ConfuseUtils.convert_oc_type_to_swift(return_type)
115
58
  end
116
59
 
117
60
  # Extract parameters from the parameter section
@@ -157,38 +100,7 @@ module Confuse
157
100
  end
158
101
 
159
102
 
160
- # # 测试用例
161
- # puts convert_block_to_closure('void (^)()') # 输出: () -> Void
162
- # puts convert_block_to_closure('NSInteger (^)(BOOL)') # 输出: (Bool) -> Int
163
- # puts convert_block_to_closure('NSString *(^)(NSInteger)') # 输出: (Int) -> String?
164
- # puts convert_block_to_closure('void (^)(NSInteger, BOOL)') # 输出: (Int, Bool) -> Void
165
- def self.convert_block_to_closure(block_signature)
166
-
167
- # 正则表达式解释:
168
- # ^([\w\s\*]+)\s*\(\^\)\s*(?:\((.*?)\))?\s*$
169
- # 1. ([\w\s\*]+): 捕获返回值类型(如 void、NSInteger、NSString * 等)。
170
- # 2. \(\^\): 匹配 '^' 表示这是一个 block。
171
- # 3. (?:\((.*?)\))? : 可选地捕获参数列表(括号中的内容)。
172
- if block_signature =~ /^([\w\s\*]+)\s*\(\^\)\s*(?:\((.*?)\))?\s*$/
173
- return_type = $1.strip
174
- params = $2 ? $2.split(',').map(&:strip) : []
175
-
176
- # 转换返回值类型
177
- swift_return_type = convert_oc_type_to_swift(return_type)
178
-
179
- # 转换参数类型
180
- swift_params = params.map do |param|
181
- param_parts = param.split(' ')
182
- param_type =convert_oc_type_to_swift(param_parts.first)
183
- "#{param_type}"
184
- end.join(", ").gsub("Void","")
185
-
186
- # 构造最终的 Swift 闭包类型
187
- result = "@escaping (#{swift_params}) -> #{swift_return_type}"
188
- else
189
- raise ArgumentError, "Invalid block signature: #{block_signature}"
190
- end
191
- end
103
+
192
104
  end
193
105
  end
194
106
 
@@ -4,11 +4,12 @@ module Confuse
4
4
  #method_confuse 用来替换掉函数名 儒 BB_Confuse_asdasfas
5
5
  #swift 完整函数 如 @objc public func calculateAreaWithWidth(width: (Bool, String) -> Void, height: CGFloat) -> CGFloat
6
6
  #params 参数名数组 width, height
7
- def self.assembly(method_confuse,swift_method_declaration,params)
8
- is_return = true
7
+ def self.assembly(method_confuse,swift_method_declaration,params, is_nullable, return_type)
8
+ is_return = (return_type != "Void")
9
9
  is_static = swift_method_declaration.start_with?("static")
10
10
  caller = is_static ? "Self" : "self"
11
11
  returnFlag = is_return ? "return " : ""
12
+ optional = is_nullable ? "?" : ""
12
13
 
13
14
  params = params.map.with_index do |param, index|
14
15
  if index == 0
@@ -17,7 +18,17 @@ module Confuse
17
18
  "#{param}: #{param}"
18
19
  end
19
20
  end
20
- funcBody = "#{swift_method_declaration} {\n #{returnFlag}#{caller}.#{method_confuse}(#{params.join(', ')})\n}"
21
+ funcBody = "#{swift_method_declaration}#{optional} {\n #{returnFlag}#{caller}.#{method_confuse}(#{params.join(', ')})\n}"
22
+ return funcBody
23
+ end
24
+
25
+ #把oc属性组装成swift 计算属性
26
+ def self.assemblyProperty(property_origin, property_confuse, return_type, is_nullable)
27
+
28
+ final_return_type = (is_nullable ? "#{return_type}?" : return_type)
29
+
30
+ #如果是Any
31
+ funcBody = "public var #{property_origin} : #{final_return_type} {\n get {\n #{property_confuse}\n }\n set {\n self.#{property_confuse} = newValue\n }\n}\n"
21
32
  return funcBody
22
33
  end
23
34
  end
@@ -69,6 +69,11 @@ module Pod
69
69
  project = Xcodeproj::Project.open(File.basename(project_path))
70
70
  target = project.targets.first
71
71
  plist_path = target.resolved_build_setting('INFOPLIST_FILE', resolve_against: target).first.last
72
+
73
+ if project_path.nil? || plist_path.nil? || project_path.empty? || plist_path.empty?
74
+ return
75
+ end
76
+
72
77
  plist_path = File.join(File.dirname(project_path), plist_path)
73
78
  plist = Plist.parse_xml(plist_path)
74
79
  puts plist_path
@@ -76,6 +81,7 @@ module Pod
76
81
  unless plist.nil?
77
82
  is_confuse = plist['ISConfuse']
78
83
  if is_confuse.nil?
84
+ puts "None key for ISConfuse"
79
85
  # 没有 --confuse 命令和 Info 配置, 不处理
80
86
  elsif is_confuse == true
81
87
  enable_confuse()
@@ -1,3 +1,3 @@
1
1
  module CocoapodsPrivacy
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocoapods-privacy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - youhui
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-04-29 00:00:00.000000000 Z
11
+ date: 2025-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler