Class: BBSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-privacy/common/BBSpec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, alias_name, full_name, type) ⇒ BBSpec

Returns a new instance of BBSpec.



39
40
41
42
43
44
45
46
47
48
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 39

def initialize(name,alias_name,full_name,type)
  @rows = []
  @name = name
  @alias_name = alias_name
  @full_name = full_name
  @type = type
  @privacy_file = "Pod/Privacy/#{full_name}/PrivacyInfo.xcprivacy"
  confuse_file_name = @full_name.tr('.','_')
  @confuse_file = "Pod/Confuse/#{full_name}/#{confuse_file_name}_Confuse"
end

Instance Attribute Details

#alias_nameObject

Returns the value of attribute alias_name.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def alias_name
  @alias_name
end

#confuse_fileObject

Returns the value of attribute confuse_file.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def confuse_file
  @confuse_file
end

#exclude_filesObject

Returns the value of attribute exclude_files.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def exclude_files
  @exclude_files
end

#full_nameObject

Returns the value of attribute full_name.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def full_name
  @full_name
end

#nameObject

Returns the value of attribute name.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def name
  @name
end

#parentObject

Returns the value of attribute parent.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def parent
  @parent
end

#privacy_fileObject

Returns the value of attribute privacy_file.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def privacy_file
  @privacy_file
end

#rowsObject

Returns the value of attribute rows.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def rows
  @rows
end

#sources_filesObject

Returns the value of attribute sources_files.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def sources_files
  @sources_files
end

#typeObject

Returns the value of attribute type.



37
38
39
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 37

def type
  @type
end

Instance Method Details

#assemble_single_property_to_complex(property_name) ⇒ Object

单独属性转成spec字符串,方便解析



68
69
70
71
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 68

def assemble_single_property_to_complex(property_name)
  property_name += "s" if property_name == KResource_Bundle_Key #检测到单数resource_bundle,直接转成复数,功能一致
  property_name
end

#create_file_if_need(podspec_file_path) ⇒ Object

对应Spec新增隐私文件



90
91
92
93
94
95
96
97
98
99
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 90

def create_file_if_need(podspec_file_path)
  if @source_files_index
    if is_handle_privacy
      PrivacyUtils.create_privacy_if_empty(File.join(File.dirname(podspec_file_path), @privacy_file))
    elsif is_handle_confuse
      ConfuseUtils.create_confuse_if_empty(File.join(File.dirname(podspec_file_path), "#{@confuse_file}.h"))
      ConfuseUtils.create_confuse_if_empty(File.join(File.dirname(podspec_file_path), "#{@confuse_file}.swift"))
    end
  end
end

#fetch_mul_line_property(propertys_mul_line_hash) ⇒ Object

这里处理所有多行参数的解析,目前处理 source_filesexclude_filesresource_bundle 这三种输入格式 [‘.source_files’:false,‘.exclude_files’:true.…..] => true 代表会根据获取的重置属性,需要把多行多余的进行删除返回格式 ‘.source_files’:BBRow,.…..



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
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 112

def fetch_mul_line_property(propertys_mul_line_hash)
  property_hash = {}
  line_processing = nil
  property_config_processing = nil
  @rows.each_with_index do |line, index|
    if !line || line.is_a?(BBSpec) || line.is_comment
      next
    end

    property_find = propertys_mul_line_hash.find { |key, _| line.key && line.key.include?(key) } #查找不到返回nil 查到返回数组,key, value 分别在第一和第二个参数
    if property_find
      property_config_processing = property_find 
    end

    if property_config_processing
      begin
        property_name = property_config_processing.first
        is_replace_line = property_config_processing.second
        if line_processing
          code = "#{line_processing.value}#{line.content}"
        else
          code = "#{line.value}"
        end

        # 清除 content 和 value, 后面会把所有的content 组装起来,多余的内容要清除,避免重复
        if is_replace_line
          line.content = ''
          line.value = nil
        end

        property_name_complex = assemble_single_property_to_complex(property_name)
        spec_str = "Pod::Spec.new do |s|; s.#{property_name_complex} = #{code}; end;"
        RubyVM::InstructionSequence.compile(spec_str)
        spec = eval(spec_str)
        property_value = spec.attributes_hash[property_name_complex]
      rescue SyntaxError, StandardError => e
        unless line_processing
          line_processing = line
        end
        line_processing.value = code if line_processing #存储当前残缺的value,和后面完整的进行拼接
        next
      end

      final_line = (line_processing ? line_processing : line)
      final_line.value = property_value
      property_hash[property_name] = final_line
      line_processing = nil
      property_config_processing = nil
    end
  end

  property_hash
end

#fetch_string_or_array_files(podspec_file_path, line) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 177

def fetch_string_or_array_files(podspec_file_path,line)
  value = line.value
  if value.is_a?(String) && !value.empty?
    array = [value]
  elsif value.is_a?(Array)
    array = value
  else
    array = []
  end
  array
end

#handle_string_or_array_files(podspec_file_path, line) ⇒ Object

处理字符串或者数组,使其全都转为数组,并转成实际文件夹地址



167
168
169
170
171
172
173
174
175
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 167

def handle_string_or_array_files(podspec_file_path,line)
  value = line.value
  array = fetch_string_or_array_files(podspec_file_path,line)

  files = array.map do |file_path|
    File.join(File.dirname(podspec_file_path), file_path.strip)
  end
  files
end

#is_handle_confuseObject



105
106
107
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 105

def is_handle_confuse
  @type.include?(KSpecTypeConfuse)
end

#is_handle_privacyObject



101
102
103
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 101

def is_handle_privacy
  @type.include?(KSpecTypePrivacy)
end

#modify_resource_bundle_if_need(podspec_file_path) ⇒ Object

把新增的隐私文件 映射给 podspec && 解析 sources_files && 解析 exclude_files



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
225
226
227
228
229
230
231
232
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 190

def modify_resource_bundle_if_need(podspec_file_path)
  if @source_files_index

    # 这里处理所有多行参数的解析,目前处理 source_files\exclude_files\resource_bundle 这三种
    propertys_mul_line_hash = {}
    propertys_mul_line_hash[KSource_Files_Key] = false
    propertys_mul_line_hash[KExclude_Files_Key] = false
    propertys_mul_line_hash[KResource_Bundle_Key] = false 

    property_value_hash = fetch_mul_line_property(propertys_mul_line_hash)
    property_value_hash.each do |property, line|
      if property == KSource_Files_Key                 #处理 source_files
        @sources_files = handle_string_or_array_files(podspec_file_path,line)
        # puts "originSource = #{sources_files}"
        if is_handle_confuse
          source_array = fetch_string_or_array_files(podspec_file_path,line)
          source_array.push("#{@confuse_file}.{h,swift}")
          line.value = source_array.uniq
          line.content = "#{line.key}= #{line.value}"
        end
      elsif property == KExclude_Files_Key             #处理 exclude_files
        @exclude_files = handle_string_or_array_files(podspec_file_path,line)
      elsif property == KResource_Bundle_Key           #处理 原有resource_bundle 合并隐私清单文件映射
        # 仅在隐私清单时才去解析 resource_bundle
        privacy_resource_bundle = { "#{full_name}.privacy" => @privacy_file }
        if is_handle_privacy
          if @has_resource_bundle
            merged_resource_bundle = line.value.merge(privacy_resource_bundle)
            @resource_bundle = merged_resource_bundle
            line.value = merged_resource_bundle
            line.content = "#{line.key}= #{line.value}"
          else # 如果原先没有resource_bundle,需要单独加一行resource_bundle
            space = PrivacyUtils.count_spaces_before_first_character(rows[@source_files_index].content)
            line = "#{alias_name}.resource_bundle = #{privacy_resource_bundle}"
            line = PrivacyUtils.add_spaces_to_string(line,space)
            row = BBRow.new(line)
            @rows.insert(@source_files_index+1, row)
          end
        end
      end
    end
  end
end

#privacy_handle(podspec_file_path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 73

def privacy_handle(podspec_file_path)
  @rows.each_with_index do |line, index|
    if !line || line.is_a?(BBSpec) || !line.key || line.key.empty? 
      next
    end
     
    if !line.is_comment && line.key.include?("." + KResource_Bundle_Key)
      @has_resource_bundle = true
    elsif !line.is_comment && line.key.include?("." + KSource_Files_Key)
      @source_files_index = index
    end
  end
  create_file_if_need(podspec_file_path)
  modify_resource_bundle_if_need(podspec_file_path)
end

#uniq_full_name_in_parent(name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cocoapods-privacy/common/BBSpec.rb', line 51

def uniq_full_name_in_parent(name)
  names = []
  @rows.each_with_index do |line, index|
    if line && line.is_a?(BBSpec)  
      names << line.name
    end
  end

  #判断names 中是否包含 name,如果包含,那么给name 添加一个 “.diff” 后缀,一直到names 中没有包含name为止
  while names.include?(name)
    name = "#{name}.diff"
  end

  "#{@full_name}.#{name}"
end