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.



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

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

	@generate = nil
end

Instance Method Details

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



71
72
73
# File 'lib/teapot/generator.rb', line 71

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

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/teapot/generator.rb', line 113

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



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

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

#generate(&block) ⇒ Object



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

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

#generate!(*args) ⇒ Object



42
43
44
# File 'lib/teapot/generator.rb', line 42

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

#is_binary(path) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/teapot/generator.rb', line 100

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



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 75

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



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/teapot/generator.rb', line 46

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



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

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