class FilenameCleaner::CLI

Public Instance Methods

rename() click to toggle source
# File lib/filename_cleaner/cli.rb, line 28
def rename
  opts = options.deep_symbolize_keys
  if opts[:version]
    puts "You are using Filename Cleaner version #{FilenameCleaner::VERSION}"
    exit
  end
  sanitize_and_rename(opts)
end
usage() click to toggle source
# File lib/filename_cleaner/cli.rb, line 38
    def usage
      puts <<-EOS
Usage:
  filename_cleaner

Options:
  -b, [--base-dir=BASE_DIR]                # Base directory
                                           # Default: . (current directory)
  -e, [--exts=one two three]               # List of extensions to search for
  -r, [--recursive], [--no-recursive]      # Search for files recursively
                                           # Default: true
  -s, [--sep-char=SEP_CHAR]                # Separator char to use
                                           # Default: _
  -d, [--downcase], [--no-downcase]        # Convert each word int the filename to lowercase
                                           # Default: --no-downcase
  -t, [--capitalize], [--no-capitalize]    # Capitalize each word in the filename
                                           # Default: --no-capitalize
  -c, [--commit], [--no-commit]            # Commit your changes
                                           # Default: --no-commit
  -v, [--version], [--no-version]          # Display version information
                                           # Default: --no-version
Sanitize and rename file with special characters
      EOS
    end

Private Instance Methods

compare_and_rename(old_name, new_name, commit) click to toggle source

rubocop:enable LineLength

# File lib/filename_cleaner/cli.rb, line 91
def compare_and_rename(old_name, new_name, commit)
  if new_name != old_name
    puts "FYI: old name: #{old_name}"
    puts "FYI: new name: #{new_name}"
    # Don't override the file if it is already exist!
    unless File.exist?(new_name) || !commit
      FileUtils.mv old_name, new_name
    end
  else
    puts "FYI: same file #{old_name}"
  end
end
sanitize_and_rename(options = {}) click to toggle source

rubocop:disable LineLength

# File lib/filename_cleaner/cli.rb, line 68
def sanitize_and_rename(options = {})
  files = CodeLister.files(options)
  if files.empty?
    puts "No match found for your options :#{options}"
  else
    files.each_with_index do |file, index|
      puts "FYI: process : #{index + 1} of #{files.size}"
      dirname  = File.dirname(File.expand_path(file))
      filename = File.basename(file)
      new_name = FilenameCleaner.formatted_name(filename, options)
      old_name = File.expand_path(file)
      new_name = File.expand_path([dirname, new_name].join(File::SEPARATOR))
      compare_and_rename(old_name, new_name, options[:commit])
    end
    unless options[:commit]
      puts "--------------------------------------------------------------"
      puts "This is a dry run, to commit your change, please use --commit option"
      puts "--------------------------------------------------------------"
    end
  end
end