lib/org-converge/command.rb in org-converge-0.0.2 vs lib/org-converge/command.rb in org-converge-0.0.3

- old
+ new

@@ -8,11 +8,15 @@ def initialize(options) @options = options @dotorg = options['<org_file>'] @logger = Logger.new(options['--log'] || STDOUT) @root_dir = options['--root-dir'] - @run_dir = File.expand_path('run') + @run_dir = if @root_dir + File.expand_path(File.join(@root_dir, 'run')) + else + File.expand_path('run') + end @ob = Orgmode::Parser.new(File.read(dotorg)).babelize @babel = nil end def execute! @@ -31,42 +35,76 @@ false end def converge! tangle! - run_blocks! + case @options['--runmode'] + when 'parallel' + run_blocks_in_parallel! + when 'sequential' + run_blocks_sequentially! + when 'runlist' + # TODO + else + run_blocks_in_parallel! + end end def tangle! results = babel.tangle! rescue Orgmode::Babel::TangleError logger.error "Cannot converge because there were errors during tangle step".red end + def run_blocks_sequentially! + @engine = OrgConverge::Engine.new(:logger => @logger, :babel => @babel) + babel.tangle_runnable_blocks!(@run_dir) + + runlist_stack = [] + babel.ob.scripts.each do |key, script| + runlist_stack << [key, script] + end + + while not runlist_stack.empty? + key, script = runlist_stack.shift + with_running_engine do |engine| + file = File.expand_path("#{@run_dir}/#{key}") + cmd = "#{script[:lang]} #{file}" + engine.register script[:lang], cmd, { :cwd => @root_dir, :logger => logger } + end + end + logger.info "Run has completed successfully.".green + end + # TODO: Too much foreman has made this running blocks in parallel the default behavior. # We should actually be supporting run lists instead, but liking this experiment so far. - def run_blocks! + def run_blocks_in_parallel! @engine = OrgConverge::Engine.new(:logger => @logger, :babel => @babel) babel.tangle_runnable_blocks!(@run_dir) babel.ob.scripts.each do |key, script| file = File.expand_path("#{@run_dir}/#{key}") cmd = "#{script[:lang]} #{file}" @engine.register script[:lang], cmd, { :cwd => @root_dir, :logger => logger } end logger.info "Running code blocks now! (#{babel.ob.scripts.count} runnable blocks found in total)" @engine.start + logger.info "Run has completed successfully.".green end + def with_running_engine + engine = OrgConverge::Engine.new(:logger => @logger, :babel => @babel) + yield engine + engine.start + end + def babel @babel ||= Orgmode::Babel.new(ob, { :logger => @logger, :root_dir => @root_dir }) end def showfiles - ob.tangle.each do |file, lines| + ob.tangle.each do |file, block| puts "---------- #{file} --------------".green - lines.each do |line| - puts line - end + puts block[:lines] end ob.scripts.each do |index, block| puts "---------- script: #{index} to be run with: #{block[:header][:shebang]} --------------".green puts block[:lines]