Class: DynTask::TaskMngr

Inherits:
Object
  • Object
show all
Defined in:
lib/dyntask/task_mngr.rb

Constant Summary collapse

TASKS =

TODO: to put outside to be extended by users!

["pdflatex","pandoc","png","dyn","sh","dyn_cli"]
TASK_EXT =
".task_"

Instance Method Summary collapse

Constructor Details

#initializeTaskMngr



108
109
110
# File 'lib/dyntask/task_mngr.rb', line 108

def initialize
  init_tasks
end

Instance Method Details

#add_task(task, id = nil) ⇒ Object

possibly add condition to check before applying command+options



122
123
124
125
126
127
# 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_newObject



269
270
271
272
# File 'lib/dyntask/task_mngr.rb', line 269

def cd_new
  @curdir=Dir.pwd
  Dir.chdir(@workdir)
end

#cd_oldObject



274
275
276
# File 'lib/dyntask/task_mngr.rb', line 274

def cd_old
  Dir.chdir(@curdir)
end

#info_file(filename) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# 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_tasksObject



112
113
114
# File 'lib/dyntask/task_mngr.rb', line 112

def init_tasks
  @task_ids={}
end

#load_user_task_pluginsObject

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



205
206
207
# 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_dvipngObject

make latex and dvipng



423
424
425
426
427
428
# 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_dynObject

make dyn



288
289
290
291
292
# 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_cliObject

make dyn-cli



296
297
298
299
# 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_pandocObject

make pandoc



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# 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_pdflatexObject

make pdf



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# 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) ⇒ Object

make pdflatex



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# 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_pngObject

make png



417
418
419
# File 'lib/dyntask/task_mngr.rb', line 417

def make_png
  make_dvipng
end

#make_shObject

make sh



280
281
282
283
284
# 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_taskObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# 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_ttmObject

make ttm



432
433
434
435
# 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) ⇒ Object

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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) ⇒ Object



147
148
149
150
151
# 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