Sha256: e5ae04a86fbab763c9b93609b4caffe320dde2408aa5e0c8250e2433cacf7a4e

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Trim

  class Renamer

    def rename(directory)
      file_names = Dir["#{directory}/**/*"]
      # loop backwards
      file_names.reverse.each do |path|
        if File.directory? path
          # dir
          rename_directory(path)
        else
          # files
          rename_file(path)
        end
      end
    end

    private

    def yaml
      YAML::load_file(File.join(File.dirname(__FILE__), '../../data/xcode.yml'))
    end

    def product
      yaml["product"]
    end

    def organization
      yaml["organization"]
    end

    def company
      yaml["company"]
    end

    def prefix
      yaml["prefix"]
    end

    def rename_directory(path)
      new_path = replaced_path(path, /PRODUCT_NAME/)
      FileUtils.mv(path, new_path) if new_path
    end

    def rename_file(file)
      rename_match(file, /PRODUCT_NAME/)
      rename_match(file, /CLASS_PREFIX/)
    end

    def rename_match(file, match)
      new_name = replaced_path(file, /#{match}/)
      File.rename(file, new_name) if new_name
    end

    def replaced_path(path, regex)
      new_path = nil
      components = path.split("/")
      last = components.pop

      if last.match(/#{regex}/)
        new_name = last.gsub(/#{regex}/, product)
        if components.length > 0
          new_path = components.join("/") + "/" + new_name
        else
          new_path = new_name
        end
      end
      new_path
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trim-0.0.1 lib/trim/renamer.rb