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

#freeze, #path, #pretty_print, #to_s

Constructor Details

#initialize(context, package, name) ⇒ Generator

Returns a new instance of Generator.



39
40
41
42
43
# File 'lib/teapot/generator.rb', line 39

def initialize(context, package, name)
  super context, package, name

  @generate = nil
end

Instance Method Details

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



78
79
80
# File 'lib/teapot/generator.rb', line 78

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

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/teapot/generator.rb', line 120

def copy(source, destination, substitutions = nil)
  source_path = Pathname(path) + source
  destination_path = Pathname(context.root) + destination

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

#copy_binary(source_path, destination_path) ⇒ Object



115
116
117
118
# File 'lib/teapot/generator.rb', line 115

def copy_binary(source_path, destination_path)
  destination_path.dirname.create
  FileUtils.cp source_path, destination_path
end

#generate(&block) ⇒ Object



45
46
47
# File 'lib/teapot/generator.rb', line 45

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

#generate!(*args) ⇒ Object



49
50
51
# File 'lib/teapot/generator.rb', line 49

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

#is_binary(path) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/teapot/generator.rb', line 107

def is_binary(path)
  if path.exist?
    return path.read(1024).bytes.find{|byte| byte >= 0 and byte <= 6}
  else
    return false
  end
end

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/teapot/generator.rb', line 82

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")

      result = Merge::combine(destination.readlines, temporary_file.readlines)

      destination.open("w") do |file|
        file.write result.join
      end
    ensure
      temporary_file.unlink
    end
  else
    write(source_path, destination_path, substitutions, "w")
  end
end

#substitute(text, substitutions) ⇒ Object



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

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/teapot/generator.rb', line 65

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