lib/saber/task/generate.rb in saber-1.1.1 vs lib/saber/task/generate.rb in saber-1.2.0

- old
+ new

@@ -1,39 +1,56 @@ +require "isbn" + module Saber module Task # Generate meta data file. # # Usage # ----- # - # Task["generate"].invoke(:generate, ["bib", "ebook", "Hello.epub", isbn]) + # 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(tracker_name, type, filename, *args) - require "saber/tracker/#{tracker_name}" if !args.empty? - template_file = find_in_source_paths("#{tracker_name}/#{type}.yml") - dest = "#{filename}.yml" + def generate(type, *filenames) + template_file = find_in_source_paths("#{type}.yml") - if !args.empty? and Tracker[tracker_name].can_populate?(type) - require "yaml" - populate = {} - data = YAML.load_file(template_file) + filenames.each {|filename, isbn| + isbn = ISBN.thirteen(isbn) rescue nil + Saber.ui.say "Populating {#{isbn}} #{filename} ..." + dest = "#{filename}.yml" - tracker = Tracker[tracker_name].new - tracker.login - populate = tracker.populate(type, *args) - data.merge!(populate) + 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") - create_file dest, YAML.dump(data) - else - copy_file template_file, dest - end + # 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