503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
# File 'lib/hx.rb', line 503
def load(io, config_file, option_overrides={})
raw_config = YAML.load(io)
options = {}
options[:base_dir] = File.dirname(config_file)
for key, value in raw_config.fetch('options', {})
options[key.intern] = value
end
options[:config_file] = config_file
options.update(option_overrides)
Hx.setup_require_path(options)
if raw_config.has_key? 'require'
for library in raw_config['require']
require library
end
end
raw_sources_by_name = raw_config.fetch('sources', {})
raw_outputs = raw_config.fetch('output', [])
for name, raw_source in raw_sources_by_name
raw_sources_by_name[name] = Hx.expand_chain(raw_source)
end
raw_outputs = raw_outputs.map! do |raw_output|
Hx.expand_chain(raw_output)
end
source_dependencies = Hash.new { |h,k| h[k] = Set.new }
source_count_by_dependency = Hash.new(0)
for name, raw_source in raw_sources_by_name
for input_name in Hx.get_input_names(raw_source)
source_dependencies[name].add input_name
source_count_by_dependency[input_name] += 1
end
end
for raw_output in raw_outputs
for input_name in Hx.get_input_names(raw_output)
source_count_by_dependency[input_name] += 1
end
end
source_names = raw_sources_by_name.keys
source_depths = Hash.new(0)
to_process = Set.new(source_names)
until to_process.empty?
need_updating = Set.new
for name in to_process
depth = source_depths[name] + 1
if depth > source_names.length
raise "cycle in source graph involving #{name}"
end
for input_name in source_dependencies[name]
if depth > source_depths[input_name]
source_depths[input_name] = depth
need_updating.add input_name
end
end
end
to_process = need_updating
end
depth_first_names = source_names.sort_by { |n| -source_depths[n] }
sources = {}
for name in depth_first_names
raw_source = raw_sources_by_name[name]
source = Hx.build_source(options, NULL_INPUT, sources, raw_source)
if source_count_by_dependency[name] > 1
source = Cache.new(source, options)
end
sources[name] = source
end
outputs = []
for raw_output in raw_outputs
outputs << Hx.build_source(options, NULL_INPUT, sources, raw_output)
end
new(options, sources, outputs)
end
|