Class: AppEntitlementsStatistics::Analyzer

Inherits:
Object
  • Object
show all
Includes:
Helper::Utils
Defined in:
lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Utils

#defalut_store_path, #entitlements_yaml_name, #report_file_name, #versions_yaml_name

Constructor Details

#initialize(identifier, report_path: nil, store_path: nil) ⇒ Analyzer

Returns a new instance of Analyzer.



17
18
19
20
21
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 17

def initialize(identifier,report_path: nil, store_path: nil)
    @identifier = identifier
    @report_path = report_path 
    @store_path = store_path
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



15
16
17
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 15

def identifier
  @identifier
end

#report_pathObject (readonly)

Returns the value of attribute report_path.



14
15
16
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 14

def report_path
  @report_path
end

#store_pathObject (readonly)

Returns the value of attribute store_path.



13
14
15
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 13

def store_path
  @store_path
end

Instance Method Details

#analyze(versions = []) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 23

def analyze(versions=[])
    versions.sort{|x,y| y<=>x } unless versions.size < 2
    if versions.length < 2
        load_version
        # return singleGenerate unless @versions.length > 1
    else
        @versions = versions
    end
    pre_entitlements = load_entitlements_yaml(@versions[1])
    cur_entitlements = load_entitlements_yaml(@versions[0])
    return "" unless pre_entitlements.any?
    return "" unless cur_entitlements.any?

    capabilities_diff = analyze_diff(pre_entitlements,cur_entitlements)
    report_path = generate(capabilities_diff)
    # puts report_path
    report_path
end

#analyze_diff(pre, cur) ⇒ Object



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
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 105

def analyze_diff(pre,cur)
    output = ""
    cur_capabilities_summary =  cur['Capabilities_Summary']
    pre_capabilities_summary = pre['Capabilities_Summary']

    cur_usage_descs_summary =  cur['PermissionsUsageDescription_Summary']
    pre_usage_descs_summary = pre['PermissionsUsageDescription_Summary']

    # cur_capabilities = cur['Capabilities']
    # pre_capabilities = pre['Capabilities']

    # cur_usage_descs = cur['PermissionsUsageDescription']
    # pre_usage_descs = pre['PermissionsUsageDescription']

    # 修改
    comm_capabilities = cur_capabilities_summary.keys & pre_capabilities_summary.keys
    modifyed_rows = []
    comm_capabilities.each do |key|
        if cur_capabilities_summary[key] != pre_capabilities_summary[key]
            modifyed_rows << [ '- ' + key ]
        end
    end

    comm_usage_descs = cur_usage_descs_summary.keys & pre_usage_descs_summary.keys
    comm_usage_descs.each do |key|
        if cur_usage_descs_summary[key] != pre_usage_descs_summary[key]
            modifyed_rows << [ '- ' + key  ]
        end
    end


    #新增
    add_capabilities = cur_capabilities_summary.keys - pre_capabilities_summary.keys
    added_rows = []
    add_capabilities.each do |key|
        added_rows << ['- ' + key ] #+ ': ' + cur_capabilities[key].to_s
    end

    add_usage_descs = cur_usage_descs_summary.keys - pre_usage_descs_summary.keys
    add_usage_descs.each do |key|
        added_rows << [ '- ' + key ] #+ ': ' + cur_usage_descs[key].to_s 
    end

    #移除
    remove_capabilities = pre_capabilities_summary.keys - cur_capabilities_summary.keys
    removed_rows = []
    remove_capabilities.each do |key|
        removed_rows << ['- ' + key ] #+ ': ' + pre_capabilities[key].to_s
    end

    remove_usage_descs = pre_usage_descs_summary.keys - cur_usage_descs_summary.keys
    remove_usage_descs.each do |key|
        removed_rows << [  '- ' + key ] #+ ': ' + pre_usage_descs[key].to_s 
    end

    # Summary
    max_count = [modifyed_rows.size,added_rows.size,removed_rows.size].max
    if max_count == 0
        return output
    end

    table = Terminal::Table.new do |t|
        t.title = "Compared #{@versions[0]} to #{@versions[1]} Summary"
        t.add_row ["Modifyed", modifyed_rows.size ]
        t.add_row ["New", added_rows.size ]
        t.add_row ["Remove", removed_rows.size ]
    end
    output += table.to_s + "\n\n"

    def get_value(arr,n)
        return "" unless arr.length > n
        return arr[n].first unless arr.length == 0
        return ""
    end

    detail_table = Terminal::Table.new do |t|
        t.headings = ['Modifyed', 'New', 'Remove']
        (0..max_count-1).each do |n|
            t.add_row [
                get_value(modifyed_rows,n),
                get_value(added_rows,n),
                get_value(removed_rows,n),
            ]
        end
    end
    output += detail_table.to_s + "\n\n"    
    output
end

#generate(capabilities_diff) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 57

def generate(capabilities_diff)
    return unless @versions.length > 1
    cur_version = @versions[0]
    pre_version = @versions[1]
    output = ""
    output += "\n" + capabilities_diff unless capabilities_diff.empty?
    output += "\n\nThere is no difference between the two versions  【 #{@versions[0]} vs #{@versions[1]}】\n\n" unless !capabilities_diff.empty?
    # puts output
    output += "\n\n\n#{cur_version} entitlements list: "
    output += "\n" + read_each_line(cur_version)
    output += "\n" + "----------------------------------------"
    output += "\n\n#{pre_version} entitlements list: "
    output += "\n" + read_each_line(pre_version)
    # puts "generate"
    report_path = report_file_name(path: @report_path)
    File.open(report_path, 'w') { |file|
        file.write(output)
    }
    # puts "detail file: #{File.expand_path(report_path)}"
    report_path
end

#get_value(arr, n) ⇒ Object



174
175
176
177
178
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 174

def get_value(arr,n)
    return "" unless arr.length > n
    return arr[n].first unless arr.length == 0
    return ""
end

#load_entitlements_yaml(version) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 95

def load_entitlements_yaml(version)
    yaml_name = entitlements_yaml_name(version,@identifier, path: @store_path)
    yaml_content = [ ]
    if File.exist?(yaml_name)
        yaml_content = YAML.load_file(yaml_name)
    end
    puts "#{yaml_name} not found!!!" unless !yaml_content.empty?
    yaml_content
end

#load_versionObject



88
89
90
91
92
93
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 88

def load_version
    yaml_name = versions_yaml_name(@identifier, path: @store_path)
    if File.exist?(yaml_name) 
        @versions = YAML.load_file(yaml_name)
    end
end

#read_each_line(version) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 79

def read_each_line(version)
    file_name = entitlements_yaml_name(version,@identifier, path: @store_path)
    file_content = ""
    File.open(file_name,"r").each_line do |line|
        file_content += "\n" + line
    end
    file_content
end

#singleGenerateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cocoapods-entitlements-statistics/app_entitlements_statistics/analyze.rb', line 42

def singleGenerate
    return "" unless @versions.length > 0
    output = "no more versions to compare !! "
    output += "\n\n\n#{@versions.first} entitlements list: "
    output += "\n" + read_each_line(@versions.first)
    output += "\n" + "----------------------------------------"
    # puts "singleGenerate"
    report_path = report_file_name(path: @report_path)
    File.open(report_path, 'w') { |file|
        file.write(output)
    }
    report_path
end