class DynTask::TaskMngr

Constants

TASKS

TODO: to put outside to be extended by users!

TASK_EXT

Public Class Methods

new() click to toggle source
# File lib/dyntask/task_mngr.rb, line 108
def initialize
  init_tasks
end

Public Instance Methods

add_task(task,id=nil) click to toggle source

possibly add condition to check before applying command+options

# File lib/dyntask/task_mngr.rb, line 122
def add_task(task,id=nil)
  task_cpt=id || DynTask.inc_task_cpt
  @task_ids[task_cpt] = [] unless @task_ids[task_cpt]
  @task_ids[task_cpt] << task
  task_cpt #id of the task
end
cd_new() click to toggle source
# File lib/dyntask/task_mngr.rb, line 269
def cd_new
  @curdir=Dir.pwd
  Dir.chdir(@workdir)
end
cd_old() click to toggle source
# File lib/dyntask/task_mngr.rb, line 274
def cd_old
  Dir.chdir(@curdir)
end
info_file(filename) click to toggle source
# File lib/dyntask/task_mngr.rb, line 209
def info_file(filename)
  return {} unless filename

  ##p [:info_file,filename]

  res = {
    dirname: File.dirname(filename),
    extname: File.extname(filename),
    basename: File.basename(filename,".*")
  }
  res[:filename]=res[:basename]+res[:extname]
  res[:full_filename]=File.join(res[:dirname],res[:filename])
  res
end
init_tasks() click to toggle source
# File lib/dyntask/task_mngr.rb, line 112
 def init_tasks
  @task_ids={}
end
load_user_task_plugins() click to toggle source
if option to not remove taskfiles, this is a clean!

def task_clean_taskfiles(task_filename)

dirname=File.dirname(task_filename)
basename=File.basename(task_filename,TASK_EXT+"*")

Dir[File.join(dirname,basename+TASK_EXT+"*")].each do |f|
  FileUtils.rm(f)
end

end

# File lib/dyntask/task_mngr.rb, line 205
def load_user_task_plugins
  Dir[File.join(DynTask.cfg_dir[:plugins],"*.rb")].each {|lib| require lib} if File.exists? DynTask.cfg_dir[:plugins]
end
make_dvipng() click to toggle source

make latex and dvipng

# File lib/dyntask/task_mngr.rb, line 423
def make_dvipng
    system "latex #{@basename}.tex"
    print "\nlatex #{@basename}.tex -> ok\n"
    system "dvipng --nogssafer #{@basename}.dvi -o #{@basename}.png"
    print "\ndvipng --nogssafer #{@basename}.dvi -o #{@basename}.png -> ok\n"
end
make_dyn() click to toggle source

make dyn

# File lib/dyntask/task_mngr.rb, line 288
def make_dyn
  opts = @task[:options] || ""
  p [:dyn_cmd,"dyn #{opts} #{@filename}"]
  `dyn #{opts} #{@filename}`
end
make_dyn_cli() click to toggle source

make dyn-cli

# File lib/dyntask/task_mngr.rb, line 296
def make_dyn_cli
  p [:dyn_cmd_cli,"dyn-cli #{@filename} #{@target[:filename]} "]
  `dyn-cli #{@filename} #{@target[:filename]}`
end
make_pandoc() click to toggle source

make pandoc

# File lib/dyntask/task_mngr.rb, line 353
def make_pandoc

  cfg_pandoc = DynTask.cfg_pandoc[:extra]

  format_doc,format_output=@task[:filter].split("2")
  #
  p [format_doc.to_s , format_output.to_s]
  append_doc= @task[:append_doc] || ""
  cmd_pandoc_options,pandoc_file_output,pandoc_file_input=[],"",nil
  case @task[:filter]
  when "md2odt"
    cmd_pandoc_options = cfg_pandoc["md2odt"] || []
    pandoc_file_output=@basename+append_doc+".odt"
  when "md2docx"
    cmd_pandoc_options= cfg_pandoc["md2docx"] || ["-s","-S"]
    pandoc_file_output=@basename+append_doc+".docx"
  when "tex2docx"
    pandoc_file_input=@filename
    cmd_pandoc_options= cfg_pandoc["tex2docx"] || ["-s"]
    pandoc_file_output=@basename+append_doc+".docx"
  when "md2beamer"
    cmd_pandoc_options= cfg_pandoc["md2beamer"] || ["-t","beamer"]
    pandoc_file_output=@basename+append_doc+".pdf"
  when "md2dzslides"
    cmd_pandoc_options= cfg_pandoc["md2dzslides"] || ["-s","--mathml","-i","-t","dzslides"]
    pandoc_file_output=@basename+append_doc+".html"
  when "md2slidy"
    cmd_pandoc_options= cfg_pandoc["md2slidy"] || ["-s","--webtex","-i","-t","slidy"]
    pandoc_file_output=@basename+append_doc+".html"
  when "md2s5"
    cmd_pandoc_options= cfg_pandoc["md2s5"] || ["-s","--self-contained","--webtex","-i","-t","s5"]
    pandoc_file_output=@basename+append_doc+".html"
  when "md2revealjs"
    cmd_pandoc_options= cfg_pandoc["md2revealjs"] || ["-s","--self-contained","--webtex","-i","-t","revealjs"]
    pandoc_file_output=@basename+append_doc+".html"
  when "md2slideous"
    cmd_pandoc_options=["-s","--mathjax","-i","-t","slideous"]
    pandoc_file_output=@basename+append_doc+".html"
  end

  opts=cmd_pandoc_options #OBSOLETE NOW!: +["-o",pandoc_file_output]

  output=if pandoc_file_input
    opts << pandoc_file_input
    Dyndoc::Converter.pandoc(nil,opts.join(" "))
  else
    @content=@task[:content]
    #
    p [:make_pandoc_content, opts.join(" "),@content]
    Dyndoc::Converter.pandoc(@content,opts.join(" "))
  end

  if pandoc_file_output
    File.open(pandoc_file_output,"w") do |f|
      f << output
    end
  end

end
make_pdflatex() click to toggle source

make pdf

# File lib/dyntask/task_mngr.rb, line 303
def make_pdflatex
  #p [:task,@task]
  nb_pass = @task[:nb_pass] || 1
  echo_mode=@task[:echo] || false

  ## Just in case of synchronisation delay!
  wait_time=@task[:wait_loop_time] || 0.5
  wait_nb=@task[:wait_loop_nb] || 20
  ok=DynTask.wait_for_file(@basename+".tex",wait_nb,wait_time)

  if ok
    nb_pass.times {|i| make_pdflatex_pass(echo_mode) }
  else
    puts "Warning: no way to apply pdflatex since #{@basename+'.tex'} does not exist!"
  end
end
make_pdflatex_pass(echo_mode=false) click to toggle source

make pdflatex

# File lib/dyntask/task_mngr.rb, line 322
def make_pdflatex_pass(echo_mode=false)
  unless File.exists? @basename+".tex"
    msg="No pdflatex #{@basename} in #{@workdir} since file does not exist!"
    print "\n==> "+msg
  end
  if File.read(@basename+".tex").empty?
    msg="No pdflatex #{@basename} in #{@workdir} since empty file!"
    print "\n==> "+msg
    ###$dyn_logger.write("ERROR pdflatex: "+msg+"\n") unless Dyndoc.cfg_dyn[:dyndoc_mode]==:normal
    return ""
  end
  print "\n==> #{Dyndoc.pdflatex} #{@basename} in #{@workdir}"

  out=`#{Dyndoc.pdflatex} -halt-on-error -file-line-error -interaction=nonstopmode #{@basename}`
  out=out.b if RUBY_VERSION >= "1.9" #because out is not necessarily utf8 encoded
  out=out.split("\n")
  puts out if echo_mode
  if out[-2].include? "Fatal error"
    #if Dyndoc.cfg_dyn[:dyndoc_mode]==:normal
    #  print " -> NOT OKAY!!!\n==> "
    #  puts out[-4...-1]
    #  raise SystemExit
    #end
  else
    print " -> OKAY!!!\n"
    ###@cfg[:created_docs] << @basename+".pdf" #( @dirname.empty? ? "" : @dirname+"/" ) + @basename+".pdf"
  end
end
make_png() click to toggle source

make png

# File lib/dyntask/task_mngr.rb, line 417
def make_png
  make_dvipng
end
make_sh() click to toggle source

make sh

# File lib/dyntask/task_mngr.rb, line 280
def make_sh
  shell_opts=@task[:shell_opts] || ""
  script_opts=@task[:script_opts] || ""
  `sh #{shell_opts} #{source[:filename]} #{script_opts}`
end
make_task() click to toggle source
# File lib/dyntask/task_mngr.rb, line 224
def make_task

  ##
  @source=info_file(DynTask.sys_root_path(@task[:source]))
  #p [:info,@task[:source]]

  #p [:source,@source]
  @basename=@source[:basename]
  @extname=@source[:extname]
  @dirname=@source[:dirname]
  @filename=@source[:filename]

  @target=info_file(@task[:target]) if @task[:target]

  cd_new

  ## This is a rule, if a @task contains both :source and :content
  ## then save the file @task[:source] with this content @task[:content]
  ## This is useful when delegating a next task in some dropbox-like environment: task and source are synchronized!
  if @task[:content] and @task[:source]
    #p [:content,@source[:filename]]
    File.open(@source[:filename],"w") do |f|
      f << @task[:content]
    end
  end

  method("make_"+@task[:cmd].to_s).call

  # case @task[:cmd].to_s
  # when "sh"
  #   make_sh
  # when "dyn"
  #   make_dyn
  # when "pdflatex"
  #   make_pdf
  # when "pandoc"
  #   make_pandoc
  # when "png"
  #   make_png
  # end

  cd_old

end
make_ttm() click to toggle source

make ttm

# File lib/dyntask/task_mngr.rb, line 432
    def make_ttm
#puts "make_ttm:begin"
      Dyndoc::Converter.ttm(File.read(@task[:filename]).force_encoding("utf-8"))
    end
read_tasks(task_filename) click to toggle source

maybe to maintain only one task, remove current task when the new one is created

# File lib/dyntask/task_mngr.rb, line 161
def read_tasks(task_filename)
  @task_filename=task_filename
  @tasks=Object.class_eval(File.read(@task_filename).force_encoding("utf-8"))
  ##p @tasks
  if @tasks.length>=1
    @task=@tasks.shift #first task to deal with
    ##p @task
    if @task[:workdir]
      # if workdir is specified inside the first task (almost required now)
      @workdir = @task[:workdir]
      dirname_next_task=nil #means default mode (from sys_root_path)
      if @workdir == :current #mode current
        @workdir=File.dirname(File.expand_path(@task_filename))
        dirname_next_task=@workdir
      else #mode
        # workdir is always relative from sys_root which could differ depending on the computer (or vm)
        @workdir = DynTask.sys_root_path(@workdir)
      end
      if File.exist? @workdir

        make_task
        if @tasks.length>=1
          dirname=File.dirname(task_filename)
          basename=File.basename(task_filename,".*")
          task_basename=File.join(dirname,basename)
          save_tasks(@task[:id],task_basename,dirname_next_task) # same id since it is a chaining (TO CHECK: when remote action maybe prefer uuid instead of counter to gnerate id)
        end
        # remove since it is executed!
        FileUtils.rm(task_filename)
      end
    end
  end
end
save_tasks(id,task_basename,task_dirname=nil) click to toggle source
# File lib/dyntask/task_mngr.rb, line 130
def save_tasks(id,task_basename,task_dirname=nil)
  task_dirname=DynTask.cfg_dir[:run] unless task_dirname
  task_dirname=File.expand_path task_dirname
  FileUtils.mkdir_p task_dirname unless File.directory? task_dirname
  if (tasks_to_save=@task_ids[id])
    ## delegate id
    tasks_to_save[0][:id]=id
    ## write next tasks to file
    task_filename=File.join(task_dirname,("%04d" % id)+"_"+task_basename+TASK_EXT+tasks_to_save[0][:cmd].to_s)
    File.open(task_filename,"w") do |f|
      f << tasks_to_save.inspect
    end
  else
    puts "DynTask WARNING: Nothing to save!"
  end
end
write_tasks(task_basename,tasks) click to toggle source
# File lib/dyntask/task_mngr.rb, line 147
def write_tasks(task_basename,tasks)
  @tasks=tasks
  id=DynTask.inc_task_cpt
  save_tasks(id,task_basename)
end