Skip to content

Commit bba8b6a

Browse files
committed
Merge pull request #385 from spastorino/accept_tt_at_end
Accept .tt as for templates
2 parents c6452cf + 883f6dd commit bba8b6a

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

lib/thor/actions.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,14 @@ def source_paths
131131
# Receives a file or directory and search for it in the source paths.
132132
#
133133
def find_in_source_paths(file)
134+
possible_files = [file, file + TEMPLATE_EXTNAME]
134135
relative_root = relative_to_original_destination_root(destination_root, false)
135136

136137
source_paths.each do |source|
137-
source_file = File.expand_path(file, File.join(source, relative_root))
138-
return source_file if File.exist?(source_file)
138+
possible_files.each do |f|
139+
source_file = File.expand_path(f, File.join(source, relative_root))
140+
return source_file if File.exist?(source_file)
141+
end
139142
end
140143

141144
message = "Could not find #{file.inspect} in any of your source paths. "

lib/thor/actions/directory.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ def execute!
8888
dirname = File.dirname(file_destination).gsub(/\/\.$/, '')
8989
next if dirname == given_destination
9090
base.empty_directory(dirname, config)
91-
when /\.tt$/
92-
destination = base.template(file_source, file_destination[0..-4], config, &@block)
91+
when /#{TEMPLATE_EXTNAME}$/
92+
base.template(file_source, file_destination[0..-4], config, &@block)
9393
else
94-
destination = base.copy_file(file_source, file_destination, config, &@block)
94+
base.copy_file(file_source, file_destination, config, &@block)
9595
end
9696
end
9797
end

lib/thor/actions/file_manipulation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def get(source, *args, &block)
108108
#
109109
def template(source, *args, &block)
110110
config = args.last.is_a?(Hash) ? args.pop : {}
111-
destination = args.first || source.sub(/\.tt$/, '')
111+
destination = args.first || source.sub(/#{TEMPLATE_EXTNAME}$/, '')
112112

113113
source = File.expand_path(find_in_source_paths(source.to_s))
114114
context = instance_eval('binding')

lib/thor/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Thor
1919
THOR_RESERVED_WORDS = %w(invoke shell options behavior root destination_root relative_root
2020
action add_file create_file in_root inside run run_ruby_script)
2121

22+
TEMPLATE_EXTNAME = '.tt'
23+
2224
module Base
2325
attr_accessor :options, :parent_options, :args
2426

spec/actions/file_manipulation_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ def file
184184
expect(File.exists?(file)).to be true
185185
end
186186

187+
it "accepts filename without .tt for template method" do
188+
expect(runner).to receive(:file_name).and_return("rdoc")
189+
action :template, "doc/%file_name%.rb"
190+
file = File.join(destination_root, "doc/rdoc.rb")
191+
expect(File.exists?(file)).to be true
192+
end
193+
187194
it "logs status" do
188195
expect(capture(:stdout) { runner.template("doc/config.rb") }).to eq(" create doc/config.rb\n")
189196
end

0 commit comments

Comments
 (0)