PerfectlyNormal/scorm

View on GitHub
bin/scorm-manifest-tree

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# encoding: utf-8
$:.unshift 'lib'

require 'scorm'

file = ARGV[0]
abort "Usage: #{$0} <imsmanifest.xml>" if !file
abort "File #{file} not found, or not readable" unless File.readable?(file)

$level = 0

def line(str, last = false, arm = "")
  puts "#{"    " * $level}#{arm}#{last ? "└" : "├"}── #{str}"
end

manifest = Scorm::Manifest.parse(File.read(file))
has_organizations = !manifest.organizations.empty?
has_resources     = !manifest.resources.empty?

puts ". #{file}"
line manifest.metadata, !has_organizations && !has_resources

# Print organizations
unless manifest.organizations.empty?
  # Print organization set
  line manifest.organization_set, !has_resources

  manifest.organizations.each_with_index do |org, org_index|
    last_organization = org_index == (manifest.organizations.length-1)

    arm = has_resources ? "│   " : "    "

    # Print organization
    line org, last_organization, arm

    # Print organization items
    org.items.each_with_index do |item, item_index|
      last_item = item_index == (org.items.length-1)
      item_arm  = has_resources     ? "│   " : "    "
      item_arm += last_organization ? "    "  : "│   "

      line item, last_item, item_arm

      descendants = [
        item.adlcp_time_limit_action,
        item.adlcp_data_from_lms,
        item.adlcp_data,
        item.adlcp_completion_threshold,
      ].compact
      descendants.each_with_index do |descendant, des_index|
        des_arm  = has_resources     ? "│  " : "   "
        des_arm += last_organization ? "  "  : "│ "
        last_descendant = des_index == (descendants.length-1)
        line descendant, last_descendant, item_arm + des_arm
      end
    end
  end
end

unless manifest.resources.empty?
  line "<Resources>", true
  $level += 1

  manifest.resources.each_with_index do |resource, res_index|
    last_resource = res_index == (manifest.resources.length-1)
    line resource, last_resource

    descendants = [resource.files, resource.dependencies].flatten
    descendants.each_with_index do |descendant, des_index|
      arm = last_resource ? "     " : "│    "
      last_descendant = des_index == (descendants.length-1)
      line descendant, last_descendant, arm
    end
  end
end