class LaTeXProjectTemplate::Task

Attributes

clean[RW]
default[RW]
latexmk[RW]

Public Class Methods

new(target) { |self| ... } click to toggle source
# File lib/latex_project_template/task.rb, line 82
def initialize(target, &block)
  @target = target
  @latexmk = Latexmk.new
  @clean = Cleaning.new
  @default = :pdf
  yield(self) if block_given?
  define_task
end

Public Instance Methods

define_task() click to toggle source
# File lib/latex_project_template/task.rb, line 163
def define_task
  desc "Default task"
  task :default => @default

  desc "Clean up temporary files."
  task :clean do |t|
    @latexmk.clean(@target)
    @clean.clean
  end

  desc "Clean up all files."
  task :distclean do |t|
    @latexmk.distclean(@target)
    @clean.clean
  end

  Latexmk::COMMAND_TO_PRODUCE_FILE.each do |type|
    desc "Create #{type.to_s}."
    task type => [@target] do |t|
      @latexmk.__send__(type, @target)
    end
  end

  desc "Create snapshot file."
  task :snapshot, [:type,:commit] do |t, args|
    type = args.type && args.type.size > 0 ? args.type.intern : :pdf
    unless Latexmk::COMMAND_TO_PRODUCE_FILE.include?(type)
      raise "Invalid option to produce file: #{type}."
    end
    if commit = args.commit
      path = snapshot_of_commit(type, commit)
    else
      path = snapshot_of_current(type)
    end
    if path
      $stdout.puts "Save to #{path}"
    else
      $stdout.puts "We can not create a file."
    end
  end

  desc "Create source of particular commit."
  task :source, [:commit] do |t, args|
    if commit = args.commit
      if date = commit_date(commit)
        if source_directory = extract_source(commit)
          $stdout.puts "Save to #{source_directory}"
        else
          $stdout.puts "We can not create directory of source files."
        end
      else
        $stderr.puts "The commit '#{commit}' does not exist."
      end
    else
      $stderr.puts "Please set commit."
    end
  end
end

Private Instance Methods

commit_date(commit) click to toggle source
# File lib/latex_project_template/task.rb, line 118
def commit_date(commit)
  log_data = `git log --date=iso -1 #{commit}`
  if $? == 0
    require 'time'
    l = log_data.split("\n").find { |s| /^Date:.*$/ =~ s }
    Time.parse(l.sub("Date:", ''))
  else
    nil
  end
end
extension_from_command_type(type) click to toggle source
# File lib/latex_project_template/task.rb, line 91
def extension_from_command_type(type)
  if /^pdf/ =~ type.to_s
    ".pdf"
  else
    ".#{type.to_s}"
  end
end
extract_source(commit) click to toggle source
# File lib/latex_project_template/task.rb, line 130
def extract_source(commit)
  source_directory = FileName.create('src', :type => :time, :directory => :self, :add => :always)
  c = "git archive --format=tar #{commit} | tar -C #{source_directory} -xf -"
  system(c)
  source_directory
end
snapshot_of_commit(type, commit) click to toggle source
# File lib/latex_project_template/task.rb, line 138
def snapshot_of_commit(type, commit)
  if date = commit_date(commit)
    source_directory = extract_source(commit)
    path = FileName.create(source_directory, File.basename(@target), :add => :prohibit, :extension => extension_from_command_type(type))
    path_base = File.basename(path).sub(/\.#{type}$/, "_#{date.strftime("%Y%m%d_%H%M%S")}.#{type}")
    snapshot_path = FileName.create("snapshot", path_base, :directory => :parent, :position => :middle)
    cd source_directory
    begin
      sh "rake #{type}"
    rescue
      $stderr.puts "Can not compile. Please edit files in #{source_directory}."
    end
    if File.exist?(path)
      move(path, snapshot_path)
      cd '..'
      rm_r source_directory
      return snapshot_path
    end
  else
    $stderr.puts "The commit '#{commit}' does not exist."
  end
  nil
end
snapshot_of_current(type) click to toggle source
# File lib/latex_project_template/task.rb, line 100
def snapshot_of_current(type)
  path = FileName.create(@target, :add => :prohibit, :extension => extension_from_command_type(type))
  snapshot_path = FileName.create("snapshot", File.basename(path),
                                  :type => :time, :directory => :parent, :position => :middle,
                                  :delimiter => '', :add => :always, :format => "%Y%m%d_%H%M%S")
  begin
    Rake::Task[type].execute
  rescue
    $stderr.puts "Can not compile"
  end
  if File.exist?(path)
    move(path, snapshot_path)
    return snapshot_path
  end
  nil
end