class ThorTemplate::Renamer

Public Class Methods

new(name) click to toggle source
# File lib/thor_template/renamer.rb, line 3
def initialize(name)
  @name = name
end

Public Instance Methods

empty_directories() click to toggle source
# File lib/thor_template/renamer.rb, line 61
def empty_directories
  Dir['**/*'].
    select { |d| File.directory? d }.
    select { |d| (Dir.entries(d) - %w[ . .. ]).empty? }.to_a
end
remove_empty_directories() click to toggle source

Thanks stackoverflow.com/users/123094/db stackoverflow.com/questions/1290670/ruby-how-do-i-recursively-find-and-remove-empty-directories

# File lib/thor_template/renamer.rb, line 55
def remove_empty_directories
  until empty_directories.empty?
    empty_directories.each { |d| Dir.rmdir(d) }
  end
end
rename!() click to toggle source
# File lib/thor_template/renamer.rb, line 7
def rename!
  Dir.chdir(@name) do
    Dir.glob("**/*") do |path|
      next unless File.file?(path)
      rename_content(path)

      next unless File.file?(path)
      rename_path(path)
    end # Dir.glob

    remove_empty_directories
  end
end
rename_content(path) click to toggle source
# File lib/thor_template/renamer.rb, line 21
def rename_content(path)
  content = IO.readlines(path)
  result = content.map do |line|
    line = line.gsub(/ThorTemplate/, @name.underscore.camelize)
    line = line.gsub(/thor_template/, @name.underscore)
    line = line.gsub("USER_PROVIDED_NAME", @name) # special case
    line
  end
  IO.write(path, result.join(''))
end
rename_path(src) click to toggle source
# File lib/thor_template/renamer.rb, line 32
def rename_path(src)
  dest = special_rename?(src) ?
    src.gsub(/thor_template/, @name) :
    src.gsub(/thor_template/, @name.underscore)

  folder = File.dirname(dest)
  FileUtils.mkdir_p(folder) unless File.exist?(folder)

  FileUtils.mv(src, dest) unless src == dest
end
special_rename?(path) click to toggle source

These paths should be rename with the actually named provied by the user, the the underscored version.

# File lib/thor_template/renamer.rb, line 45
def special_rename?(path)
  %w[
    thor_template.gemspec
    exe/thor_template
    lib/thor_template.rb
  ].include?(path)
end