require "isbn" module Saber module Task # Generate meta data file. # # Usage # ----- # # Task["generate"].invoke(:generate, ["ebook", ["harry_potter", isbn], ...]) # > generate Hello.epub.yml data file. # class Generate < Base include Thor::Actions def self.source_paths ["#{Rc.p.home}/templates"] end source_root "#{Rc.p.root}/templates" desc "generate", "generate" def generate(type, *filenames) template_file = find_in_source_paths("#{type}.yml") filenames.each {|filename, isbn| isbn = ISBN.thirteen(isbn) rescue nil Saber.ui.say "Populating {#{isbn}} #{filename} ..." dest = "#{filename}.yml" if isbn require "yaml" populate = {} data = YAML.load_file(template_file) data.merge! Book.populate(isbn, filename) data.merge! YAML.load_file("#{Rc.p.database}/#{isbn}.yml") if Pa.exists?("#{Rc.p.database}/#{isbn}.yml") # tags if data["tags"] Tracker.trackers.each {|name, tracker_class| if data["#{name}.tags"].nil? and tracker_class.method_defined?(:convert_tags) tracker = tracker_class.new(options) data["#{name}.tags"] = tracker.convert_tags(*data["tags"].split(/, */)).join(", ") end } end create_file dest, YAML.dump(data) else copy_file template_file, dest end } end end end end