From d256b50516599692d9d4d314c29e4621f374405a Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 9 Jan 2010 19:14:43 -0500 Subject: [PATCH] working on file processor --- classes/FileProcessor.rb | 83 +++++++++++++++++++++----------------- tests/TestFileProcessor.rb | 62 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 38 deletions(-) diff --git a/classes/FileProcessor.rb b/classes/FileProcessor.rb index 32bc4b8..8491038 100644 --- a/classes/FileProcessor.rb +++ b/classes/FileProcessor.rb @@ -21,8 +21,6 @@ class FileProcessor config.each do |type, info| if type != "Global" - input = nil; output = nil - fileinfo_key = (filename.instance_of? Array) ? filename.join(",") : filename file_fileinfo = (fileinfo_by_file[fileinfo_key]) ? fileinfo_by_file[fileinfo_key] : {} @@ -31,53 +29,19 @@ class FileProcessor input_obj, output_obj, targets = construct_filters_and_targets(filename, file_fileinfo, ok) - rebuild = false - - [ targets ].flatten.each do |t| - if !File.exists?(t) - rebuild = true - else - [ filename ].flatten.each do |f| - if File.basename(f) != "blank" - if File.mtime(f) > File.mtime(t) - rebuild = true - end - end - end - end - end - - if rebuild + if determine_rebuild(targets, filename) any_rebuilt = true puts "Rebuilding #{filename_display} (#{type})..." puts " Using #{filename} as a source" puts " and writing to #{targets.inspect}" - tmp_files = input_obj.build(filename) - - output_files = [] - case tmp_files.class.to_s - when "String" - output_obj.build(tmp_files, targets) - output_files << targets - when "Array" - [0,1].each do |i| - output_obj.build(tmp_files[i], targets[i], (i == 0) ? "left" : "right") - output_files << targets[i] - end - end - - input_obj.cleanup + do_build(targets, filename) end if info['is_paginated'] if !paginated_source_files[type]; paginated_source_files[type] = []; end paginated_source_files[type] << targets end - if info['rsync'] - if !rsync_files_by_target[info['rsync']]; rsync_files_by_target[info['rsync']] = []; end - rsync_files_by_target[info['rsync']] << targets - end end end @@ -159,6 +123,8 @@ class FileProcessor end def construct_filters_and_targets(filename, info, match_data) + input = nil; output = nil + extension = File.extname((filename.instance_of? Array) ? filename[0] : filename).downcase case extension @@ -190,4 +156,45 @@ class FileProcessor [ input_obj, output_obj, targets ] end + + def file_mtime(file) + File.mtime(file) + end + + def determine_rebuild(targets, filename) + rebuild = false + + [ targets ].flatten.each do |t| + if !File.exists?(t) + rebuild = true + break + else + [ filename ].flatten.each do |f| + if File.basename(f) != "blank" + if file_mtime(f) > file_mtime(t) + rebuild = true + break + end + end + end + end + end + + rebuild + end + + def do_build(targets, filename, input_obj, output_obj) + tmp_files = input_obj.build(filename) + + case tmp_files.class.to_s + when "String" + output_obj.build(tmp_files, targets) + when "Array" + [0, 1].each do |i| + output_obj.build(tmp_files[i], targets[i], (i == 0) ? "left" : "right") + end + end + + input_obj.cleanup + end end diff --git a/tests/TestFileProcessor.rb b/tests/TestFileProcessor.rb index 8ed697e..729ade4 100644 --- a/tests/TestFileProcessor.rb +++ b/tests/TestFileProcessor.rb @@ -1,5 +1,6 @@ require 'rubygems' require 'test/unit' +require 'fakefs/safe' Dir[File.dirname(__FILE__) + '/../classes/*'].each do |f| require f end @@ -115,4 +116,65 @@ class TestFileProcessor < Test::Unit::TestCase file_processor.construct_filters_and_targets(filename, info, match_data) end end + + def test_determine_rebuild + [ + [ [], 'source', false ], + [ [ 'r-test3' ], 'source', true ], + [ [ 'r-test' ], 'source', true ], + [ [ 'r-test2' ], 'source', false ], + [ [ 'r-test2' ], 'blank', false ], + ].each do |targets, filename, expected_return| + file_processor = FileProcessor.new({}) + + class << file_processor + def file_mtime(file) + case file + when 'r-test' + return 1 + when 'source' + return 2 + when 'r-test2' + return 3 + end + end + end + + FakeFS do + FileUtils.touch [ 'r-test', 'r-test2', 'source' ] + assert_equal expected_return, file_processor.determine_rebuild(targets, filename) + end + end + end + + def test_do_build + [ + [ 'target', 'input', 'return' ], + [ [ 'target1', 'target2' ], 'input', [ 'return1', 'return2' ] ] + ].each do |targets, filename, input_obj_build_return| + input = Class.new do + def build(filename); end + end.new + + output = Class.new do + def build(filename); end + end.new + + input.expects(:build).with(filename).returns(input_obj_build_return) + + case input_obj_build_return.class.to_s + when "String" + output.expects(:build).with(input_obj_build_return, targets) + when "Array" + [0, 1].each do |i| + output.expects(:build).with(input_obj_build_return[i], targets[i], (i == 0) ? "left" : "right") + end + end + + input.expects(:cleanup) + + file_processor = FileProcessor.new({}) + file_processor.do_build(targets, filename, input, output) + end + end end