#!/usr/bin/env ruby require 'dirtree' require 'optparse' require 'json' require 'erb' require 'open-uri' require 'tempfile' templates_dir = File.join(File.dirname(__FILE__), '..', 'templates') templates = Dir.open(templates_dir).map do |file| file[0...file.index('.')] end.reject(&:empty?) options = { template: 'tree' } OptionParser.new do |opts| opts.banner = 'Usage: dirtree [options]... [file]...' opts.on( '-v', '--version', 'Print version' ) do puts "Dirtree version #{Dirtree::VERSION}" exit end opts.on( '-h', '--help', 'Show this help text' ) do puts opts exit end opts.on( '-l', '--local-dependencies', 'Use saved JavaScript libraries instead of downloading them' ) do options[:local_dependencies] = true end opts.on( '-s', '--screenshot', 'Get an image screenshot of the directory tree' ) do options[:screenshot] = true end opts.on( '-oFile.html', '--output=File.html', 'Specify a path to write HTML output' ) do |value| raise 'missing filename after ‘-o’' if value.empty? options[:output] = value end opts.on( '-tTemplateName', '--template=TemplateName', 'Specify the template name, available templates ' + templates.to_s ) do |value| options[:template] = value end end.parse! files = ARGV.empty? ? STDIN.read.lines : ARGV files.map!(&:strip) root = Dirtree::Node.new('/') files.each { |file| root.insert(file.split('/')) } template_file = File.join(templates_dir, options[:template] + '.html.erb') template = File.read(File.expand_path(template_file)) tree = root.as_json result = ERB.new(template).result binding if options[:local_dependencies] result.gsub! %r{(' injection end end File.write(options[:output], result) if options.key?(:output) if options.key?(:screenshot) unless options.key?(:output) tmp = Tempfile.new tmp.write(result) tmp.close end file = options[:output] || tmp.path %w[chromium chrome google-chrome google-chrome-stable].each do |chrome| system( chrome, '--disable-gpu', '--headless', '--screenshot', '--window-size=1500,1500', "file://" + file ) && break end end puts result unless options.key?(:output) || options.key?(:screenshot)