require 'pathname' module BuildMaster class SiteSpec def self.get_instance self.new() end attr_accessor :output_dir, :content_dir, :template, :template_file def properties @properties = Hash.new unless @properties return @properties end def validate_inputs validate_dir(@content_dir, :content_dir) end def load_template if (@template) return XTemplate.new(@template) else return XTemplate.new(File.open(@template_file)) end end def relative_to_root(path) to = path_name(path) from = path_name(@content_dir) return to.relative_path_from(from) end def evaluate(expression, path) value_from_properties = properties()[expression] if (value_from_properties) return value_from_properties end if (!respond_to?(expression, true)) raise "Neither property nor method found with name '#{expression}'" end relative_path = relative_to_root(path) if (expression == 'relative_to_root') return relative_path else send(expression, relative_to_root(path).to_s) end end def add_property(name, value) properties()[name] = value end private def path_name(path) return Pathname.new(path.gsub(/\\/, '/')) end def validate_dir(directory, symbol) if not directory raise "Directory for #{symbol.id2name} not specified" end if not File.exists? directory raise "Directory for #{symbol.id2name} -- <#{directory}> does not exist" end if not File.directory? directory raise "<#{directory}> should be a directory for #{symbol.id2name}" end end def validate_file(file, symbol) if not file raise "File for #{symbol.id2name} not specified" end if (not File.exists? file) raise "File for #{symbol.id2name} -- <#{file}> does not exist" end if not File.file? file raise "#<{file} should be a file for #{symbol.id2name}" end end end end