# # File 'modeltask.rb' created on 23 feb 2008 at 20:45:10. # # See 'dokkit.rb' or +LICENSE+ for licence information. # # (C) 2008 Andrea Fazzi (and contributors). # require 'yaml' require 'dokkit' module Dokkit class BuiltinTask attr_reader :models, :local_dir, :search_in def initialize(application) @application = application @local_dir = DEFAULT_LOCAL_DIR @search_in = [:dokkit_dir, :local_dir] @logger = @application.logger @models = fetch_models define_builtin_tasks end def dokkit_dir lib = nil unless lib = ENV['DOKKIT_LIB'] $:.each do |it| if File.exists?(File.join(it, 'dokkit/app.rb')) lib = it break end end end lib end def builtin_model_dir File.join(dokkit_dir, 'dokkit/models') end private def define_builtin_tasks define_list_model_task define_create_task end def from_location(location) return builtin_model_dir if location == :dokkit_dir return File.join(((ENV['HOMEPATH'] if RUBY_PLATFORM =~ /win32/) || ENV['HOME']), @local_dir) if location == :local_dir end def fetch_models models = { } @search_in.each do |location| Dir.glob(File.join(from_location(location), '**/model/Rakefile')).each do |model| models[model.scan(/models\/(.*)\/model\/Rakefile/).flatten.to_s] = File.expand_path(File.join(File.dirname(model), '..')) end end models end def define_list_model_task desc 'List the installed models' task 'list' do @models.each do |modelname, path| @logger.info("Found model '#{modelname}' in #{path}") end end end def define_create_task desc 'Bootstrap a new documentation project' task 'create', :model do |t, args| model = args.model || DEFAULT_MODEL if ARGV.size == 2 dest_dir = t.application.top_level_tasks.pop else @logger.error('A target directory must be provided.') end @logger.error("Directory '#{dest_dir}' exists.") if File.exists?(dest_dir) @logger.error("Model '#{model}' not found.") unless @models[model] @logger.info("Create '#{dest_dir}' based on '#{model}' model.") cp_r(File.join(@models[model], 'model'), dest_dir, :verbose => false) File.open(File.join(dest_dir, 'doc/configs/model.yaml'), 'w') do |file| file << { 'name' => model, 'from' => @models[model] }.to_yaml end end end end end