#!/usr/bin/env ruby SCRIPTNAME = 'extra_spaces_checker.rb' SYNOPTIC = "#{SCRIPTNAME} はコミットされたファイルの先頭や末尾に 余計な改行や空白が含まれていないかをチェックするスクリプトである. なお, ファイルの末尾には1つだけ改行が含まれていなければならない." USAGE = "#{SCRIPTNAME} [OPTIONS] file [file ...]" MAINTAINERS = ['Yasuhiro MORIKAWA '] UPDATE = '2007/10/25' VER = '0.2' URL = 'http://epa.scitec.kobe-u.ac.jp/~itpass' # 履歴: # # Ver. 0.2, 2007/10/25 森川 靖大 # - 始めはファイルの最後に 1 つ改行が入っているかチェックする # スクリプトだったが, ファイルの先頭や末尾に不要な空行が # 入っていることもチェックするように改良. # # Ver. 0.1, 2007/10/25 森川 靖大 # - とりあえずつくった. # オプション解析 require 'optparse' opt = OptionParser.new('', 20, ' ' * 6) OPTS = Hash.new OPTS[:submit] = 'save' opt.on('-h', '-H', '--help', 'このヘルプメッセージを出力.' ) { OPTS[:help] = true } opt.parse!(ARGV) rescue OPTS[:error] = $!.to_s # [-h, -H, --help] でヘルプを出力して終了 if OPTS[:help] || ARGV.size < 1 HELP = " #{SCRIPTNAME} SYNOPTIC: #{SYNOPTIC} USAGE: #{USAGE} OPTION: #{opt.help} EXAMPLE: #{SCRIPTNAME} hogehoge foo.txt VERSION: #{SCRIPTNAME} Version #{VER}, Last Update: #{UPDATE}. MAINTAINERS: " print(HELP) MAINTAINERS.each { |v| print "%6s" % ' ', v, "\n" } print "%8s" % ' ', "All Right Reserved.\n\n" exit 0 end files = ARGV files.each{ |f| contents = [] next unless File.exist?(f) open(f) {|line| while l = line.gets contents << l end } if contents[0] =~ /^$/ STDERR.print " Error (#{f}): \n" STDERR.print " please remove blank line at the head of the file" exit 1 elsif contents[-1] =~ /^$/ STDERR.print " Error (#{f}): \n" STDERR.print " please remove blank line at the end of the file" exit 1 elsif ! ( contents[-1] =~ /\n$/ ) STDERR.print " Error (#{f}): \n" STDERR.print " please add a line feed at end of the file" exit 1 end } exit 0