Class: BBSpec

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, alias_name, full_name) ⇒ BBSpec

Returns a new instance of BBSpec.



38
39
40
41
42
43
44
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 38

def initialize(name,alias_name,full_name)
  @rows = []
  @name = name
  @alias_name = alias_name
  @full_name = full_name
  @privacy_file = "Pod/Privacy/#{full_name}/PrivacyInfo.xcprivacy"
end

Instance Attribute Details

#alias_nameObject

Returns the value of attribute alias_name.



36
37
38
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 36

def alias_name
  @alias_name
end

#full_nameObject

Returns the value of attribute full_name.



36
37
38
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 36

def full_name
  @full_name
end

#nameObject

Returns the value of attribute name.



36
37
38
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 36

def name
  @name
end

#parentObject

Returns the value of attribute parent.



36
37
38
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 36

def parent
  @parent
end

#privacy_exclude_filesObject

Returns the value of attribute privacy_exclude_files.



36
37
38
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 36

def privacy_exclude_files
  @privacy_exclude_files
end

#privacy_fileObject

Returns the value of attribute privacy_file.



36
37
38
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 36

def privacy_file
  @privacy_file
end

#privacy_sources_filesObject

Returns the value of attribute privacy_sources_files.



36
37
38
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 36

def privacy_sources_files
  @privacy_sources_files
end

#rowsObject

Returns the value of attribute rows.



36
37
38
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 36

def rows
  @rows
end

Instance Method Details

#assemble_single_property_to_complex(property_name) ⇒ Object

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



64
65
66
67
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 64

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

#create_privacy_file_if_need(podspec_file_path) ⇒ Object

对应Spec新增隐私文件



86
87
88
89
90
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 86

def create_privacy_file_if_need(podspec_file_path)
  if @source_files_index
    PrivacyUtils.create_privacy_if_empty(File.join(File.dirname(podspec_file_path), @privacy_file))
  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,.…..



95
96
97
98
99
100
101
102
103
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
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 95

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

#handle_string_or_array_files(podspec_file_path, line) ⇒ Object

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



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

def handle_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

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

#modify_privacy_resource_bundle_if_need(podspec_file_path) ⇒ Object

把新增的隐私文件 映射给 podspec && 解析 privacy_sources_files && 解析 privacy_exclude_files



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 167

def modify_privacy_resource_bundle_if_need(podspec_file_path)
  if @source_files_index
    privacy_resource_bundle = { "#{full_name}.privacy" => @privacy_file }

    # 这里处理所有多行参数的解析,目前处理 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
    if @has_resource_bundle
      propertys_mul_line_hash[KResource_Bundle_Key] = true #需要根据生成的重置属性
    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
    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
        @privacy_sources_files = handle_string_or_array_files(podspec_file_path,line)
      elsif property == KExclude_Files_Key             #处理 exclude_files
        @privacy_exclude_files = handle_string_or_array_files(podspec_file_path,line)
      elsif property == KResource_Bundle_Key           #处理 原有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}"
      end
    end
  end
end

#privacy_handle(podspec_file_path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 69

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_privacy_file_if_need(podspec_file_path)
  modify_privacy_resource_bundle_if_need(podspec_file_path)
end

#uniq_full_name_in_parent(name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cocoapods-privacy/privacy/PrivacyModule.rb', line 47

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