Class: PProf::OutputFormatter
- Inherits:
-
Object
- Object
- PProf::OutputFormatter
- Defined in:
- lib/pprof/output_formatter.rb
Overview
A helper tool to pretty-print Provisioning Profile informations
Defined Under Namespace
Classes: ASCIITable
Constant Summary collapse
- MAIN_PROFILE_KEYS =
List of properties of a ‘PProf::ProvisioningProfile` to print when using the `-i` flag
i[name uuid app_id_name app_id_prefix creation_date expiration_date ttl team_ids team_name]
Class Method Summary collapse
Instance Method Summary collapse
-
#as_json(profile, options = {}) ⇒ Hash
Returns a Provisioning Profile hash ready to be printed as a JSON output.
-
#initialize(output = $stdout) ⇒ OutputFormatter
constructor
Initialize a new OutputFormatter.
-
#print_error(message, file) ⇒ Object
Prints an error message.
-
#print_filtered_list(dir = PProf::ProvisioningProfile::DEFAULT_DIR, filters = {}, list_options = { mode: :table }) ⇒ Object
Prints the filtered list of Provisioning Profiles.
-
#print_info(profile, options = nil) ⇒ Object
Prints the description of a Provisioning Profile.
-
#print_json(profile, options = {}) ⇒ Object
Prints a Provisioning Profile as JSON.
-
#print_json_list(dir = PProf::ProvisioningProfile::DEFAULT_DIR, options) { ... } ⇒ Object
Prints the filtered list of profiles as a JSON array.
-
#print_list(dir = PProf::ProvisioningProfile::DEFAULT_DIR, options) { ... } ⇒ Object
Prints the filtered list of UUIDs or Paths only.
-
#print_table(dir = PProf::ProvisioningProfile::DEFAULT_DIR) { ... } ⇒ Object
Prints the filtered list as a table.
Constructor Details
#initialize(output = $stdout) ⇒ OutputFormatter
Initialize a new OutputFormatter
18 19 20 |
# File 'lib/pprof/output_formatter.rb', line 18 def initialize(output = $stdout) @output = output end |
Class Method Details
.match_aps_env(actual, expected) ⇒ Object
282 283 284 285 286 287 |
# File 'lib/pprof/output_formatter.rb', line 282 def self.match_aps_env(actual, expected) return false if actual.nil? # false if no Push entitlements return true if expected == true # true if Push present but we don't filter on specific env actual =~ expected # true if Push present and we filter on specific env end |
Instance Method Details
#as_json(profile, options = {}) ⇒ Hash
Returns a Provisioning Profile hash ready to be printed as a JSON output
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/pprof/output_formatter.rb', line 111 def as_json(profile, = {}) hash = profile.to_hash.dup hash.delete 'DER-Encoded-Profile' hash.delete 'ProvisionedDevices' unless [:devices] if [:certs] hash['DeveloperCertificates'] = developer_certificates.map do |cert| { subject: cert.subject, issuer: cert.issuer, serial: cert.serial, expires: cert.not_after } end else hash.delete 'DeveloperCertificates' end hash end |
#print_error(message, file) ⇒ Object
Prints an error message
57 58 59 |
# File 'lib/pprof/output_formatter.rb', line 57 def print_error(, file) @output.puts "\u{274c} #{file} - #{message}" end |
#print_filtered_list(dir = PProf::ProvisioningProfile::DEFAULT_DIR, filters = {}, list_options = { mode: :table }) ⇒ Object
Prints the filtered list of Provisioning Profiles
Convenience method. Calls self.print_list with a filter block build from a filter hash
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/pprof/output_formatter.rb', line 162 def print_filtered_list(dir = PProf::ProvisioningProfile::DEFAULT_DIR, filters = {}, = { mode: :table }) filter_func = lambda do |p| (filters[:name].nil? || p.name =~ filters[:name]) && (filters[:appid_name].nil? || p.app_id_name =~ filters[:appid_name]) && (filters[:appid].nil? || p.entitlements.app_id =~ filters[:appid]) && (filters[:uuid].nil? || p.uuid =~ filters[:uuid]) && (filters[:team].nil? || p.team_name =~ filters[:team] || p.team_ids.any? { |id| id =~ filters[:team] }) && (filters[:exp].nil? || (p.expiration_date < DateTime.now) == filters[:exp]) && (filters[:has_devices].nil? || !(p.provisioned_devices || []).empty? == filters[:has_devices]) && (filters[:all_devices].nil? || p.provisions_all_devices == filters[:all_devices]) && (filters[:aps_env].nil? || match_aps_env(p.entitlements.aps_environment, filters[:aps_env])) && true end case [:mode] when :table print_table(dir, &filter_func) when :json print_json_list(dir, , &filter_func) else print_list(dir, , &filter_func) end end |
#print_info(profile, options = nil) ⇒ Object
Prints the description of a Provisioning Profile
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pprof/output_formatter.rb', line 68 def print_info(profile, = nil) ||= { info: true } if [:info] keys = MAIN_PROFILE_KEYS keys.each do |key| @output.puts "- #{key}: #{profile.send(key.to_sym)}" end @output.puts '- Entitlements:' @output.puts(profile.entitlements.to_s.split("\n").map { |line| " #{line}" }) end # rubocop:disable Style/GuardClause if [:info] || [:certs] @output.puts "- #{profile.developer_certificates.count} Developer Certificates" if [:certs] profile.developer_certificates.each do |cert| @output.puts " - #{cert.subject}" @output.puts " issuer: #{cert.issuer}" @output.puts " serial: #{cert.serial}" @output.puts " expires: #{cert.not_after}" end end end if [:info] || [:devices] @output.puts "- #{(profile.provisioned_devices || []).count} Provisioned Devices" profile.provisioned_devices.each { |udid| @output.puts " - #{udid}" } if [:devices] @output.puts "- Provision all devices: #{profile.provisions_all_devices.inspect}" end # rubocop:enable Style/GuardClause end |
#print_json(profile, options = {}) ⇒ Object
Prints a Provisioning Profile as JSON
139 140 141 |
# File 'lib/pprof/output_formatter.rb', line 139 def print_json(profile, = {}) @output.puts JSON.pretty_generate(as_json(profile, )) end |
#print_json_list(dir = PProf::ProvisioningProfile::DEFAULT_DIR, options) { ... } ⇒ Object
Prints the filtered list of profiles as a JSON array
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/pprof/output_formatter.rb', line 269 def print_json_list(dir = PProf::ProvisioningProfile::DEFAULT_DIR, ) # rubocop:disable Style/OptionalArguments errors = [] profiles = Dir['*.mobileprovision', base: dir].map do |file_name| file = File.join(dir, file_name) p = PProf::ProvisioningProfile.new(file) as_json(p, ) unless block_given? && !yield(p) rescue StandardError => e errors << { message: e, file: file } end.compact errors.each { |e| print_error(e[:message], e[:file]) } unless errors.empty? @output.puts JSON.pretty_generate(profiles) end |
#print_list(dir = PProf::ProvisioningProfile::DEFAULT_DIR, options) { ... } ⇒ Object
Prints the filtered list of UUIDs or Paths only
240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/pprof/output_formatter.rb', line 240 def print_list(dir = PProf::ProvisioningProfile::DEFAULT_DIR, ) # rubocop:disable Style/OptionalArguments errors = [] Dir['*.mobileprovision', base: dir].each do |file_name| file = File.join(dir, file_name) p = PProf::ProvisioningProfile.new(file) next if block_given? && !yield(p) @output.print [:mode] == :list ? p.uuid.chomp : file.chomp @output.print [:zero] ? "\0" : "\n" rescue StandardError => e errors << { message: e, file: file } end errors.each { |e| print_error(e[:message], e[:file]) } unless errors.empty? end |
#print_table(dir = PProf::ProvisioningProfile::DEFAULT_DIR) { ... } ⇒ Object
Prints the filtered list as a table
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 |
# File 'lib/pprof/output_formatter.rb', line 196 def print_table(dir = PProf::ProvisioningProfile::DEFAULT_DIR) count = 0 errors = [] table = PProf::OutputFormatter::ASCIITable.new(36, 60, 45, 25, 2, 10) @output.puts table.separator @output.puts table.row('UUID', 'Name', 'AppID', 'Expiration Date', ' ', 'Team Name') @output.puts table.separator Dir['*.mobileprovision', base: dir].each do |file_name| file = File.join(dir, file_name) begin p = PProf::ProvisioningProfile.new(file) next if block_given? && !yield(p) state = DateTime.now < p.expiration_date ? "\u{2705}" : "\u{274c}" # 2705=checkmark, 274C=red X @output.puts table.row(p.uuid, p.name, p.entitlements.app_id, p.expiration_date.to_time, state, p.team_name) rescue StandardError => e errors << { message: e, file: file } end count += 1 end @output.puts table.separator @output.puts "#{count} Provisioning Profiles found." errors.each { |e| print_error(e[:message], e[:file]) } unless errors.empty? end |