Sha256: 769b049b08343cd3e180f74a33642b12ae04e4366a5eaabdab5cdc5b5fd24f29

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

module RoCommands
  class Refactor < Base
    desc 'rename', ''

    def rename(from, to)
      _rename(from, to)
      _rename(from.camelize, to.camelize)
    end

    private

    def _rename(from, to)
      return if from == to
      if from.is_a?(String) and to.is_a?(String)
        fs =find('.')
        fs.each do |f|
          if test(?f, f)
            sub_file(f, from, to)
          else
            sub_dir(f, from, to)
          end
        end
      end
    end

    private

    def sub_dir(f, from, to)
      parent, basename = parent_basename(f)
      sub_basename(parent, basename, from, to)
    end

    def sub_file(f, from, to)
      parent, basename, ctn = parent_basename_ctn(f)
      if basename.match from
        basename = sub_basename(parent, basename, from, to)
      end

      begin
        r = ctn[/\b#{from}\b/]
      rescue => e
        puts "Unknown content in #{f}"
        puts e
      end

      if r
        puts "substitute in #{basename}, from #{from} to #{to}"
        new_ctn = ctn.gsub whole_match(from), ''
        File.write File.join(parent, basename), new_ctn
      end
    end

    def sub_basename(parent, basename, from, to)
      new_name = basename.gsub(from, to)
      puts "rename from #{basename} to #{new_name}"
      FileUtils.mv(File.join(parent, basename), File.join(parent, new_name))
      basename = new_name
    end

    def whole_match(from)
      /\b#{from}\b/
    end

    private

    def parent_basename_ctn(file)
      parent, basename = parent_basename(file)
      ctn = File.read file
      return parent, basename, ctn
    end

    def parent_basename(file)
      parent = File.dirname file
      basename = File.basename file
      return parent, basename
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ro_commands-0.0.3 lib/ro_commands/refactor.rb
ro_commands-0.0.2 lib/ro_commands/refactor.rb
ro_commands-0.0.1 lib/ro_commands/refactor.rb