Skip to content
Browse files

Enforce strict `.erb` filename matching

It is possible that `.erb` occurs in the middle of a filename, e.g.:
`'gitconfig.erb.tmp'.sub('.erb', '') # => "gitconfig.tmp"`
Also, the `.` in the pattern `/.erb/` is a wildcard.
  • Loading branch information...
1 parent 2e9813c commit 5d1de6ea6081df744e7f35eba31af122361c1a7e @zhangsu committed
Showing with 9 additions and 9 deletions.
  1. +9 −9 Rakefile
View
18 Rakefile
@@ -7,13 +7,13 @@ task :install do
Dir['*'].each do |file|
next if %w[Rakefile README.rdoc LICENSE].include? file
- if File.exist?(File.join(ENV['HOME'], ".#{file.sub('.erb', '')}"))
- if File.identical? file, File.join(ENV['HOME'], ".#{file.sub('.erb', '')}")
- puts "identical ~/.#{file.sub('.erb', '')}"
+ if File.exist?(File.join(ENV['HOME'], ".#{file.sub(/\.erb$/, '')}"))
+ if File.identical? file, File.join(ENV['HOME'], ".#{file.sub(/\.erb$/, '')}")
+ puts "identical ~/.#{file.sub(/\.erb$/, '')}"
elsif replace_all
replace_file(file)
else
- print "overwrite ~/.#{file.sub('.erb', '')}? [ynaq] "
+ print "overwrite ~/.#{file.sub(/\.erb$/, '')}? [ynaq] "
case $stdin.gets.chomp
when 'a'
replace_all = true
@@ -23,7 +23,7 @@ task :install do
when 'q'
exit
else
- puts "skipping ~/.#{file.sub('.erb', '')}"
+ puts "skipping ~/.#{file.sub(/\.erb$/, '')}"
end
end
else
@@ -33,14 +33,14 @@ task :install do
end
def replace_file(file)
- system %Q{rm -rf "$HOME/.#{file.sub('.erb', '')}"}
+ system %Q{rm -rf "$HOME/.#{file.sub(/\.erb$/, '')}"}
link_file(file)
end
def link_file(file)
- if file =~ /.erb$/
- puts "generating ~/.#{file.sub('.erb', '')}"
- File.open(File.join(ENV['HOME'], ".#{file.sub('.erb', '')}"), 'w') do |new_file|
+ if file =~ /\.erb$/
+ puts "generating ~/.#{file.sub(/\.erb$/, '')}"
+ File.open(File.join(ENV['HOME'], ".#{file.sub(/\.erb$/, '')}"), 'w') do |new_file|
new_file.write ERB.new(File.read(file)).result(binding)
end
else

0 comments on commit 5d1de6e

Please sign in to comment.
Something went wrong with that request. Please try again.