lib/server.rb in machinery-tool-1.14.2 vs lib/server.rb in machinery-tool-1.15.0

- old
+ new

@@ -33,16 +33,36 @@ "inspected host: '#{@description[scope].meta.hostname}', " \ "at: #{DateTime.parse(@description[scope].meta.modified).strftime("%F %T")})" end def scope_help(scope) - text = File.read(File.join(Machinery::ROOT, "plugins", "#{scope}/#{scope}.md")) + text = scope_info(scope)[:description] Kramdown::Document.new(text).to_html end + def scope_info(scope) + YAML.load(File.read(File.join(Machinery::ROOT, "plugins", "#{scope}/#{scope}.yml"))) + end + + def scope_title(scope) + scope_info(scope)[:name] + end + + def scope_initials(scope) + scope_info(scope)[:initials].upcase + end + + def nav_class(scope) + if @description + return @description[scope] ? "" : "disabled" + elsif @description_a && @description_b + return @description_a[scope] && @description_b[scope] ? "" : "disabled" + end + end + def safe_length(object, attribute) - if object && collection = object.send(attribute) + if collection = object.try(attribute) collection.length else 0 end end @@ -61,44 +81,62 @@ def changed "<h3>In both with different attributes:</h3>" end - def changed_packages + def pluralize_scope(object, singular, plural) + object.length.to_s + " " + Machinery.pluralize(object.length, singular, plural) + end + + def changed_elements(scope, opts) + optional_attributes = opts[:optional_attributes] || [] + changed = [] - @diff["packages"].changed.each do |change| + @diff[scope].changed.each do |change| changes = [] - relevant_attributes = ["version", "vendor", "arch"] + relevant_attributes = if opts[:attributes] + opts[:attributes].dup + else + change[0].attributes.keys & change[1].attributes.keys + end - if change[0].version == change[1].version - relevant_attributes.push("release") - if change[0].release == change[1].release - relevant_attributes.push("checksum") + (1..optional_attributes.length).each do |i| + if change[0][optional_attributes[i - 1]] == + change[1][optional_attributes[i - 1]] + relevant_attributes.push(optional_attributes[i]) + else + break end end - relevant_attributes.each do |attribute| if change[0][attribute] != change[1][attribute] - changes.push(attribute + ": " + change[0][attribute] + " ↔ " + change[1][attribute]) + changes.push( + attribute + ": " + human_readable_attribute(change[0], attribute) + " ↔ " + + human_readable_attribute(change[1], attribute) + ) end end - changed.push(change[0].name + " (" + changes.join(", ") + ")") + changed.push( + id: change[0][opts[:key]], + change: "(" + changes.join(", ") + ")", + diffable: change[0].is_a?(UnmanagedFile) && change[0].is_a?(UnmanagedFile) && + change[0].file? && change[1].file? + ) end changed end - def diffable_unmanaged_files - return @diffable_unmanaged_files if @diffable_unmanaged_files + def human_readable_attribute(object, attribute) + value = object[attribute] - return [] if !@diff["unmanaged_files"] || !@diff["unmanaged_files"].only_in1 || - !@diff["unmanaged_files"].only_in2 + case object + when Machinery::SystemFile + value = number_to_human_size(value) if attribute == "size" + end - files_in_1 = @diff["unmanaged_files"].only_in1.files.select(&:file?).map(&:name) - files_in_2 = @diff["unmanaged_files"].only_in2.files.select(&:file?).map(&:name) - - @diffable_unmanaged_files = files_in_1 & files_in_2 + value.to_s end def diff_to_object(diff) diff = Machinery.scrub(diff) lines = diff.lines[2..-1] @@ -184,17 +222,17 @@ @diff = {} Inspector.all_scopes.each do |scope| if @description_a[scope] && @description_b[scope] @diff[scope] = Comparison.compare_scope(@description_a, @description_b, scope) - else + elsif @description_a[scope] || @description_b[scope] @meta[:uninspected] ||= Hash.new - if !@description_a[scope] && @description_b[scope] + if !@description_a[scope] @meta[:uninspected][@description_a.name] ||= Array.new @meta[:uninspected][@description_a.name] << scope end - if !@description_b[scope] && @description_a[scope] + if !@description_b[scope] @meta[:uninspected][@description_b.name] ||= Array.new @meta[:uninspected][@description_b.name] << scope end end end