Class: Teapot::Generator

Inherits:
Definition show all
Defined in:
lib/teapot/generator.rb

Instance Attribute Summary

Attributes inherited from Definition

#context, #description, #name, #package

Instance Method Summary collapse

Methods inherited from Definition

#path, #to_s

Constructor Details

#initialize(context, package, name) ⇒ Generator

Returns a new instance of Generator.



28
29
30
31
32
# File 'lib/teapot/generator.rb', line 28

def initialize(context, package, name)
  super context, package, name
  
  @generate = nil
end

Instance Method Details

#append(source, destination, substitutions = nil) ⇒ Object



67
68
69
# File 'lib/teapot/generator.rb', line 67

def append(source, destination, substitutions = nil)
  write(source, destination, substitutions, "a")
end

#copy(source, destination, substitutions = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/teapot/generator.rb', line 100

def copy(source, destination, substitutions = nil)
  source_path = Pathname(path) + source
  
  if source_path.directory?
    destination_path = Pathname(context.root) + destination
    
    source_path.children(false).each do |child_path|
      copy(source_path + child_path, destination_path + substitute(child_path.to_s, substitutions), substitutions)
    end
  else
    merge(source_path, destination, substitutions)
  end
end

#generate(&block) ⇒ Object



34
35
36
# File 'lib/teapot/generator.rb', line 34

def generate(&block)
  @generate = Proc.new(&block)
end

#generate!(*args) ⇒ Object



38
39
40
# File 'lib/teapot/generator.rb', line 38

def generate!(*args)
  @generate[*args]
end

#merge(source, destination, substitutions = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/teapot/generator.rb', line 71

def merge(source, destination, substitutions = nil)
  source_path = Pathname(path) + source
  destination_path = Pathname(context.root) + destination
  
  if destination_path.exist?
    temporary_file = Tempfile.new(destination_path.basename.to_s)
    
    # This functionality works, but needs improvements.
    begin
      # Need to ask user what to do?
      write(source_path, temporary_file.path, substitutions, "w")
      
      if temporary_file.read != destination_path.read
        if `which opendiff`.chomp != ''
          system("opendiff", "-merge", destination_path.to_s, destination_path.to_s, temporary_file.path)
        elsif `which sdiff`.chomp != ''
          system("sdiff", "-o", destination_path.to_s, destination_path.to_s, temporary_file.path)
        else
          abort "Can't find diff/merge tools. Please install sdiff!"
        end
      end
    ensure
      temporary_file.unlink
    end
  else
    write(source_path, destination_path, substitutions, "w")
  end
end

#substitute(text, substitutions) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/teapot/generator.rb', line 42

def substitute(text, substitutions)
  return text unless substitutions
  
  if Hash === substitutions
    pattern = Regexp.new(substitutions.keys.map{|x| Regexp.escape(x)}.join('|'))
  
    text.gsub(pattern) {|key| substitutions[key]}
  else
    substitutions.call(text)
  end
end

#write(source, destination, substitutions = nil, mode = "a") ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/teapot/generator.rb', line 54

def write(source, destination, substitutions = nil, mode = "a")
  source_path = Pathname(path) + source
  destination_path = Pathname(context.root) + destination
  
  destination_path.dirname.mkpath
  
  File.open(destination_path, mode) do |file|
    text = File.read(source_path)
    
    file.write substitute(text, substitutions)
  end
end