Sha256: 18c3d53adddda0231f0e5146a2862984559f4974d2d04257682bd8368e222aa1

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8

# TODO: run it as git hook before commit
class Repair < Thor
  desc "all", "Repair encoding, shebang and whitespace"
  def all
    self.encoding
    self.whitespace
    self.eof
  end

  desc "encoding", "Add missing coding declaration"
  def encoding
    ruby_files do |file, lines, original|
      if lines.length > 1 && ! lines.first.match(/^# encoding: utf-8\s*$/)
        puts "Added missing coding declaration to #{file}"
        lines.insert(0, "# encoding: utf-8\n\n")
        self.save(file, lines)
      end
    end
  end

  desc "whitespace", "Remove extra whitespace"
  def whitespace
    ruby_files do |file, lines, original|
      lines = original.map { |line| line.chomp(" ") }
      if original != lines
        puts "Removed extra whitespace from #{file}"
        self.save(file, lines)
      end
    end
  end

  desc "eof", "Add missing \\n to the end of files"
  def eof
    ruby_files do |file, lines, original|
      if original.last && ! original.last.match(/\n$/)
        puts "Added missing \\n to the end of #{file}"
        self.save(file, lines)
      end
    end
  end

  protected
  def root
    File.expand_path(File.join(File.dirname(__FILE__), ".."))
  end

  def save(file, lines)
    File.open(file, "w") do |file|
      file.puts(lines)
    end
  end

  def ruby_files(&block)
    Dir["#{self.root}/**/*.{rb,ru,thor}"].each do |file|
      unless File.directory?(file) # merb.thor etc
        original = File.readlines(file)
        lines = original.each { |line| line.chomp }
        block.call(file, lines, original)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rango-0.0.4 tasks/repair.thor