working on file processor

This commit is contained in:
John Bintz 2010-01-09 19:14:43 -05:00
parent 480a911bf6
commit d256b50516
2 changed files with 107 additions and 38 deletions

View File

@ -21,8 +21,6 @@ class FileProcessor
config.each do |type, info| config.each do |type, info|
if type != "Global" if type != "Global"
input = nil; output = nil
fileinfo_key = (filename.instance_of? Array) ? filename.join(",") : filename fileinfo_key = (filename.instance_of? Array) ? filename.join(",") : filename
file_fileinfo = (fileinfo_by_file[fileinfo_key]) ? fileinfo_by_file[fileinfo_key] : {} 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) input_obj, output_obj, targets = construct_filters_and_targets(filename, file_fileinfo, ok)
rebuild = false if determine_rebuild(targets, filename)
[ 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
any_rebuilt = true any_rebuilt = true
puts "Rebuilding #{filename_display} (#{type})..." puts "Rebuilding #{filename_display} (#{type})..."
puts " Using #{filename} as a source" puts " Using #{filename} as a source"
puts " and writing to #{targets.inspect}" puts " and writing to #{targets.inspect}"
tmp_files = input_obj.build(filename) do_build(targets, 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
end end
if info['is_paginated'] if info['is_paginated']
if !paginated_source_files[type]; paginated_source_files[type] = []; end if !paginated_source_files[type]; paginated_source_files[type] = []; end
paginated_source_files[type] << targets paginated_source_files[type] << targets
end 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
end end
@ -159,6 +123,8 @@ class FileProcessor
end end
def construct_filters_and_targets(filename, info, match_data) def construct_filters_and_targets(filename, info, match_data)
input = nil; output = nil
extension = File.extname((filename.instance_of? Array) ? filename[0] : filename).downcase extension = File.extname((filename.instance_of? Array) ? filename[0] : filename).downcase
case extension case extension
@ -190,4 +156,45 @@ class FileProcessor
[ input_obj, output_obj, targets ] [ input_obj, output_obj, targets ]
end 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 end

View File

@ -1,5 +1,6 @@
require 'rubygems' require 'rubygems'
require 'test/unit' require 'test/unit'
require 'fakefs/safe'
Dir[File.dirname(__FILE__) + '/../classes/*'].each do |f| Dir[File.dirname(__FILE__) + '/../classes/*'].each do |f|
require f require f
end end
@ -115,4 +116,65 @@ class TestFileProcessor < Test::Unit::TestCase
file_processor.construct_filters_and_targets(filename, info, match_data) file_processor.construct_filters_and_targets(filename, info, match_data)
end end
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 end