module Modder

extensions

module methods

Public Class Methods

confirm?() click to toggle source

double check transformations

# File lib/modname/modder.rb, line 73
def confirm?
  print "Are these changes ok? [yN] "
  ($stdin.gets.chomp!).downcase[0] == "y"
end
execute(transfer, force=false) click to toggle source

rename all files

# File lib/modname/modder.rb, line 99
def execute(transfer, force=false)
  transfer.each { |o, n| Modder.rename o, n, force }
end
files(recurse) click to toggle source

return a list of files to examine

# File lib/modname/modder.rb, line 79
def files(recurse)
  if recurse
    Dir['**/*'].select { |f| File.file?(f) }
  else
    Dir.entries(Dir.pwd).select { |f| File.file? f }
  end
end
finish(transfer, force=false) click to toggle source

finish up execution, highest level wrapper

# File lib/modname/modder.rb, line 105
def finish(transfer, force=false)

  # print changes, return if none
  Modder.status transfer
  return if transfer.empty?

  if force || Modder.confirm?
    Modder.execute transfer, force
    puts "Modifications complete."
  else
    puts "No modifications done."
  end
end
parse(args) click to toggle source

return appropriate args, repairing if undefined

# File lib/modname/modder.rb, line 64
def parse(args)
  match = args.shift
  trans = args.shift
  match = "" if match.nil?
  trans = "" if trans.nil?
  return match, trans
end
rename(o, n, force) click to toggle source

try to rename a given file

# File lib/modname/modder.rb, line 120
def rename(o, n, force)
  begin
    exist = "#{'Error:'.red} target file |#{n.green}| already exists"

    # only overwrite when forced
    if (!force) && File.exist?(n)
      raise(exist)
    else
      File.rename(o, n)
    end

  rescue => e
    puts "#{'Error:'.red} could not move |#{o.red}| to |#{n.green}|"
    puts e.message
  end
end
status(transfer) click to toggle source

show the status of current files

# File lib/modname/modder.rb, line 89
def status(transfer)
  if transfer.empty?
    puts "No matches found.".yellow
  else
    puts "Planned file actions:".green
    transfer.each { |o, n| puts "\t#{o} -> #{n.green}" }
  end
end
undercase_ext_get(ext, recurse) click to toggle source

get all extensions to change

# File lib/modname/modder.rb, line 139
def undercase_ext_get(ext, recurse)
  transfer = Hash.new
  allexts = ext.empty?
  Modder.files(recurse).each do |file|
    ext = file.split(".").last if allexts

    new = file.sub(/#{ext}$/i, ext.downcase)
    next if new == file || ext == file # no changes or extension

    transfer[file] = new
  end

  transfer
end
undercase_ext_set(ext, transfer, force) click to toggle source

set all extensions to change this involves moving it to a tmp file first, since most file systems are not case sensitive and therefore wont distiniguish between HI and hi. to get around this, we can set HI to HI.hash, then set HI.hash to hi

# File lib/modname/modder.rb, line 158
def undercase_ext_set(ext, transfer, force)
  puts "Lowering extension: ".green + (ext.empty?? "*" : ext)

  Modder.status transfer
  return if transfer.empty?

  # confirm current changes
  if force || Modder.confirm?
    final = {}
    temp = {}

    # create hash temp map
    transfer.each do |k, v|
      tempfile = (v.hash * v.object_id).abs.to_s
      final[tempfile] = v
      temp[k] = tempfile
    end

    Modder.execute temp
    Modder.execute final
    puts "Modifications complete."
  else
    puts "No modifications done."
  end
end

Public Instance Methods

exts(args = []) click to toggle source

change one file extension to another's type

# File lib/modname/modder.rb, line 30
def exts(args = [])
  match, trans = Modder.parse args

  if match.empty? && trans.empty? # do all
    undercase_ext

  elsif trans.empty? # undercase one ext
    undercase_ext match

  else # move match extension to targeted
    Modder.files(@options[:recurse]).each do |file|
      new = file.sub(/#{match}$/, trans)

      next if new == file # no changes

      @transfer[file] = new
    end

    Modder.finish @transfer, @options[:force]
  end
end
regex(args = []) click to toggle source

rename files based on regular expressions

# File lib/modname/modder.rb, line 14
def regex(args = [])
  match, trans = Modder.parse args

  Modder.files(@options[:recurse]).each do |file|
    new = file.sub Regexp.new(match), trans

    next if (new == file || new == "") # no changes

    @transfer[file] = new
  end

  Modder.finish @transfer, @options[:force]
end
undercase_ext(ext = "") click to toggle source

top level wrapper for exts

# File lib/modname/modder.rb, line 53
def undercase_ext(ext = "")
  transfer = Modder.undercase_ext_get ext, @options[:recurse]
  Modder.undercase_ext_set ext, transfer, @options[:force]
end