Module: XPCOMCore::CommandParser::GenerateCommand::TemplateHelpers

Included in:
JewelerBuilderCommand
Defined in:
lib/xpcomcore-rubygem/commands/generate/template_helpers.rb

Defined Under Namespace

Classes: EvalContext

Constant Summary collapse

TemplateDir =
XPCOMCore::GemRoot + "templates"

Instance Method Summary collapse

Instance Method Details

#copy_file(entry, dest) ⇒ Object



64
65
66
67
# File 'lib/xpcomcore-rubygem/commands/generate/template_helpers.rb', line 64

def copy_file(entry, dest)
  puts "Creating #{dest.expand_path}"
  FileUtils.cp(entry.expand_path.to_s, dest.expand_path.to_s)
end

#copy_template(entry, dest, template_vars) ⇒ Object



57
58
59
60
61
62
# File 'lib/xpcomcore-rubygem/commands/generate/template_helpers.rb', line 57

def copy_template(entry, dest, template_vars)
  real_dest = Pathname(dest.expand_path.to_s.sub(/#{dest.extname}$/, ""))
  puts "Creating #{real_dest}"
  vars = determine_template_vars(real_dest, template_vars)
  real_dest.open('w') { |f| f << eval_template(entry, vars) }
end

#copy_template_directory(dir_name, dest, template_vars) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/xpcomcore-rubygem/commands/generate/template_helpers.rb', line 27

def copy_template_directory(dir_name, dest, template_vars)
  template_dir = (TemplateDir + dir_name).expand_path
  dest.mkpath
  Pathname.glob("#{template_dir}/**/*").each do |dir_entry|
    handle_entry_creation(template_dir, dir_entry, dest, template_vars)
  end
end

#determine_template_vars(dest, template_vars) ⇒ Object



69
70
71
72
73
# File 'lib/xpcomcore-rubygem/commands/generate/template_helpers.rb', line 69

def determine_template_vars(dest, template_vars)
  template_vars.detect do |path,values|
    dest.to_s =~ /#{Regexp.escape(path)}$/
  end.last
end

#eval_template(path, vars) ⇒ Object

EvalContext



21
22
23
24
25
# File 'lib/xpcomcore-rubygem/commands/generate/template_helpers.rb', line 21

def eval_template(path, vars)
  context = EvalContext.new(vars)
  template = ERB.new((TemplateDir + path).read)
  template.result(context.get_binding)
end

#handle_entry_creation(template_dir, dir_entry, dest_dir, template_vars) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/xpcomcore-rubygem/commands/generate/template_helpers.rb', line 35

def handle_entry_creation(template_dir, dir_entry, dest_dir, template_vars)
  relative_path = dir_entry.relative_path_from(template_dir)
  if dir_entry.directory?
    mkpath(dest_dir + relative_path)
  else
    handle_file(dir_entry, (dest_dir + relative_path), template_vars)
  end
end

#handle_file(entry, dest, template_vars) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/xpcomcore-rubygem/commands/generate/template_helpers.rb', line 44

def handle_file(entry, dest, template_vars)
  if entry.extname == ".erb"
    copy_template(entry, dest, template_vars)
  else
    copy_file(entry, dest)
  end
end

#mkpath(path) ⇒ Object



52
53
54
55
# File 'lib/xpcomcore-rubygem/commands/generate/template_helpers.rb', line 52

def mkpath(path)
  puts "Creating #{path}"
  path.mkpath
end