44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
99
100
101
102
103
104
|
# File 'lib/ree/handlers/template_handler.rb', line 44
def generate
project_path = @project_path
template_detector = Ree::TemplateDetector.new(project_path)
@template_directory = template_detector.detect_template_folder(@template_name)
@destination_directory = File.join(project_path, @local_path)
template_files_list = Dir
.glob(File.join(@template_directory, '**', '*'), File::FNM_DOTMATCH)
.reject { |path| REJECTED_TEMPLATE_FILES.include? File.basename(path) }
template_files_list.each do |path|
@missing_variables.concat(
Ree::TemplateRenderer.get_undefined_variables(get_destination_path(path), @locals)
)
if handle_file_content?(path)
@missing_variables.concat(
Ree::TemplateRenderer.get_undefined_variables(File.read(path), @locals)
)
end
end
if @missing_variables.any?
@missing_variables.uniq!
@stdout.puts "Undefined variables were found:"
@missing_variables.size.times { |t| @stdout.puts " #{t+1}. #{@missing_variables[t]}" }
@missing_variables.each do |var|
@stdout.print "Type value for '#{var}': "
@locals[var] = @stdin.gets.chomp
end
end
template_files_list.map! do |path|
rendered_abs_path = Ree::TemplateRenderer.handle(get_destination_path(path), @locals)
rendered_rel_path = Pathname.new(rendered_abs_path).relative_path_from Pathname.new(project_path)
if File.file?(rendered_abs_path) && File.exist?(rendered_abs_path)
@stdout.puts "Warning! #{rendered_rel_path} already exists. Skipping file creation..."
next
end
if File.directory?(path)
FileUtils.mkdir_p(rendered_abs_path)
next
end
rendered_file_content = handle_file_content?(path) ?
Ree::TemplateRenderer.handle(File.read(path), @locals) :
File.read(path)
FileUtils.mkdir_p(File.dirname(rendered_abs_path))
File.open(rendered_abs_path, 'w') { |f| f.write rendered_file_content }
rendered_rel_path
end
template_files_list.compact
end
|