class LaTeXProjectTemplate::Directory

Constants

IGNORE_FILE_REGEXP

Attributes

name[R]
path[R]

Public Class Methods

new(path) click to toggle source
# File lib/latex_project_template.rb, line 107
def initialize(path)
  @path = File.expand_path(path)
  @name = File.basename(@path)
end

Public Instance Methods

copy_to_directory(target_dir, erb_binding_obj, files = nil) click to toggle source
# File lib/latex_project_template.rb, line 145
def copy_to_directory(target_dir, erb_binding_obj, files = nil)
  create_directory_if_needed(target_dir)
  if files
    file_list = files.map do |file_path|
      fullpath = File.join(@path, file_path)
      if File.exist?(fullpath)
        fullpath
      else
        nil
      end
    end.compact
  else
    file_list = Dir.glob(File.join(@path, '**', '*')).sort
  end
  created_files = []
  file_list.each do |file|
    next if IGNORE_FILE_REGEXP =~ file
    create_directory_if_needed(File.dirname(file))
    unless File.directory?(file)
      case file
      when /\.erb$/
        created = create_erb_template(file, erb_binding_obj, erb_binding_obj.project_name, target_dir)
      else
        created = target_path(erb_binding_obj.project_name, target_dir, file)
        FileUtils.cp(file, created)
      end
      created_files << created
    end
  end
  created_files
end
files_to_import() click to toggle source
# File lib/latex_project_template.rb, line 177
def files_to_import
  import = Hash.new { |h, k| h[k] = [] }
  import_list_path = File.join(@path, '__IMPORT__')
  if File.exist?(import_list_path)
    File.read(import_list_path).each_line do |l|
      l.strip!
      if n = l.index('/')
        k = l.slice!(0...n)
        import[k] << l[1..-1]
      end
    end
  end
  import
end

Private Instance Methods

create_directory_if_needed(target_dir) click to toggle source
# File lib/latex_project_template.rb, line 112
def create_directory_if_needed(target_dir)
  if File.exist?(target_dir)
    unless File.directory?(target_dir)
      raise "Can not create directory: #{target_dir}"
    end
  else
    File.mkdir_p(target_dir)
  end
end
create_erb_template(erb_file, erb_obj, project_name, target_dir) click to toggle source

Create file of which name is created by removing '.erb' from name of original file

# File lib/latex_project_template.rb, line 131
def create_erb_template(erb_file, erb_obj, project_name, target_dir)
  product_path = target_path(project_name, target_dir, erb_file.sub(/\.erb$/, ''))
  erb_obj.instance_exec(erb_file, product_path) do |path, out|
    erb = ERB.new(File.read(path))
    open(out, 'w') do |f|
      f.print erb.result(binding)
    end
  end
  product_path
end
target_path(project_name, target_dir, target_name) click to toggle source
# File lib/latex_project_template.rb, line 123
def target_path(project_name, target_dir, target_name)
  target_name = target_name.gsub(/__DOT__/, '.')
  target_name = target_name.gsub(/__PROJECT__/, project_name)
  File.join(target_dir, target_name.sub(/^#{Regexp.escape(@path)}/, ''))
end