Class: Gitplate::Plate

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gitplate/plate.rb

Instance Method Summary collapse

Constructor Details

#initializePlate

Returns a new instance of Plate.



7
8
9
# File 'lib/gitplate/plate.rb', line 7

def initialize
  @tasks = Hash.new
end

Instance Method Details

#add_init(&block) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/gitplate/plate.rb', line 11

def add_init(&block)
  if (@init != nil)
    Gitplate.fatal_msg_and_fail "Init can only be defined once"
  end

  Gitplate.debug_msg "  found init"
  @init = block
end

#add_task(task_name, &block) ⇒ Object



20
21
22
23
# File 'lib/gitplate/plate.rb', line 20

def add_task(task_name, &block)
  Gitplate.debug_msg "  found task '#{task_name}'"
  @tasks[task_name.to_s] = block
end

#custom(&block) ⇒ Object



30
31
32
# File 'lib/gitplate/plate.rb', line 30

def custom(&block)
  block.call
end

#expand_path(path) ⇒ Object



86
87
88
# File 'lib/gitplate/plate.rb', line 86

def expand_path(path)
  File.expand_path(File.join(@project_dir, path))
end

#load_plate(file, args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/gitplate/plate.rb', line 71

def load_plate(file, args)
  @project_name = args[:project_name]
  @project_dir = args[:project_dir]
  
  #file = "/Users/lukesmith/Projects/gitplate/tmp/plate"

  Gitplate.debug_msg "loading plate from '#{file}'"
  load file
  Gitplate.debug_msg "loaded plate"
end

#output(type, msg) ⇒ Object



25
26
27
28
# File 'lib/gitplate/plate.rb', line 25

def output(type, msg)
  instance = Gitplate::Outputs.const_get(to_class_name(type)).new
  instance.execute msg
end

#project_dirObject



43
44
45
# File 'lib/gitplate/plate.rb', line 43

def project_dir
  @project_dir
end

#project_nameObject



39
40
41
# File 'lib/gitplate/plate.rb', line 39

def project_name
  @project_name
end

#rename(from = nil, to = nil) ⇒ Object



34
35
36
37
# File 'lib/gitplate/plate.rb', line 34

def rename(from = nil, to = nil)
  Gitplate.debug_msg "  renaming #{from} to #{to}"
  File.rename(expand_path(from), expand_path(to))
end

#run(file, args) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/gitplate/plate.rb', line 47

def run(file, args)
  load_plate file, args

  if (@init != nil)
    Gitplate.debug_msg "running plate - start"
    @init.call
    Gitplate.debug_msg "running plate - completed"
  end
end

#run_task(file, task_name, args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gitplate/plate.rb', line 57

def run_task(file, task_name, args)
  load_plate file, args

  task = @tasks[task_name]

  if (task == nil)
    Gitplate.fatal_msg_and_fail "Unable to find custom task '#{task_name}'"
  end

  Gitplate.debug_msg "running task '#{task_name}' - start"
  task.call
  Gitplate.debug_msg "running task '#{task_name}' - completed"
end

#to_class_name(type) ⇒ Object



82
83
84
# File 'lib/gitplate/plate.rb', line 82

def to_class_name(type)
  type.to_s.split('_').map{|word| word.capitalize}.join
end