lib/buildmaster/site.rb in BuildMaster-0.5.0 vs lib/buildmaster/site.rb in BuildMaster-0.6.0

- old
+ new

@@ -1,17 +1,22 @@ $:.unshift File.dirname(__FILE__) require 'fileutils' require 'redcloth' -require 'webrick' require 'source_file_handler' require 'file_processor' module BuildMaster #todo match only beginning of the file TEXTILE_REGX = /---(-)*\n(.*)\n(-)*---/ + class NullLogger + def << message + puts "IGNORED: #{message}" + end + end + class SiteSpec def self.get_instance self.new() end @@ -71,41 +76,62 @@ method(action).call end def build @count = 0 - ensure_directory_exists(@spec.output_dir) build_directory(@spec.output_dir, @spec.content_dir, @template) puts "Generated file count: #{@count}" end - def server + def server(port_number=2000, log_file=$stdout, level=WEBrick::Log::INFO, access_log=nil) +require 'webrick' mime_types = WEBrick::HTTPUtils::DefaultMimeTypes.update( {"textile" => "text/plain"} ) - server = WEBrick::HTTPServer.new( - :Port => 2000, - :Logger => WEBrick::Log.new($stdout, WEBrick::Log::INFO), - :MimeTypes => mime_types + @server = WEBrick::HTTPServer.new( + :Port => port_number, + :Logger => WEBrick::Log.new(log_file, level), + :MimeTypes => mime_types, + :AccessLog => access_log ) - server.mount('/', SourceFileHandler, @spec) - server.mount('/source', WEBrick::HTTPServlet::FileHandler, @spec.content_dir, true) + @server.mount('/', SourceFileHandler, @spec) + @server.mount('/source', WEBrick::HTTPServlet::FileHandler, @spec.content_dir, true) ['INT', 'TERM'].each { |signal| - trap(signal){ server.shutdown} - } - server.start + trap(signal){ @server.shutdown} + } + @server.start 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 + 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) @@ -142,6 +168,7 @@ xhtml.write(file, 0, false, true) end end end + end