$:.unshift File.join(File.dirname(__FILE__), '..') require 'fileutils' require 'file_processor' require 'site_spec' module BuildMaster class NullLogger def << message puts "IGNORED: #{message}" end end class Site def initialize(spec) @spec = spec @spec.validate_inputs @template = @spec.load_template end public def execute(arguments) action = 'build' if arguments.size > 0 action = arguments[0] end send action end def build @count = 0 build_directory(@spec.output_dir, @spec.content_dir, @template) puts "Generated file count: #{@count}" end def server(*args) require 'site_server' @server = SiteServer.new(*args) @server.start(@spec) end def test(port_number=2000) launch_server(port_number) {|port_number| SiteTester.test("http://localhost:#{port_number}")} end def test_offline(port_number=2000) launch_server(port_number) {|port_number| SiteTester.test_offline("http://localhost:#{port_number}")} end private def launch_server(port_number) require 'buildmaster/site_tester' Thread.new() {server(port_number, NullLogger.new, 0, NullLogger.new)} begin sleep(2) yield port_number ensure @server.stop end end def ensure_directory_exists(dir_name) if (not File.exist?(dir_name)) ensure_directory_exists(File.join(dir_name, '..')) Dir.mkdir dir_name end end def build_directory(out_dir, content_dir, template) ensure_directory_exists(out_dir) Dir.foreach(content_dir) do |item| content_path = File.join(content_dir, item) if (item == '.' || item == '..' || item == '.svn' || item == 'CVS') elsif (File.directory? content_path) build_directory(File.join(out_dir, item), content_path, template) else @current_file_name = content_path process_file(content_path, out_dir, content_dir, item) @count = @count + 1 end end end def process_file(path_to_content, out_dir, content_dir, item) print ">> #{path_to_content}\n" extension = File.extname(path_to_content) isIndex = @current_file_name =~ /^index/ file_processor = FileProcessor.new(@template, path_to_content, @spec) file_processor.write_to_target end end end