設定ファイルをまとめて管理するための補助スクリプト

設定ファイルをまとめて管理するをさらに省力化。テスト中だけどリリース。つーか、作りっぱで干されてた。ソースは下。

$ ./mkmf.rb --help
Usage: mkmf [options]
        --help                       print this message.
        --add VAL                    add new config file.
        --del VAL                    delete existed config file.

--add と--del にはフルパスで指定する。target_files.txt に管理したいファイルのリストを書きこんでいる。手で追加・削除した時、Makefile を更新するには、オプションなしで実行する。


通常の流れはこんな感じか:

$ ./mkmf.rb --add /etc/issue # 追加
$ vi etc/issue               # 編集
$ sudo make                  # 更新


Subversion コマンドはコメントアウトしてある。MAKEFILE と LISTFILE は適宜修正して下ちい。

#!/usr/bin/env ruby
require 'optparse'
require 'pathname'
require 'fileutils'

MAKEFILE = '/home/babie/config/Makefile'
LISTFILE = '/home/babie/config/target_files.txt'


def create_makefile
        rules = []
        target_files = File.read(LISTFILE).split rescue []
        target_files.sort.each do |target_file|
                rule = <<EOF
#{target_file}: #{target_file[1..-1]}
\t${INSTALL} $? $@

EOF
                rules << rule
        end
        File.open(MAKEFILE, "w") { |out|
                out.puts <<MAKE
#
# 「設定ファイルをまとめて管理する」より
# http://playrecord.org/archive/config-files-on-Unix/myconf.html
#
# this makefile created by #{$0}.
# (1) $ ruby #{$0}
# (2) $ sudo make
#

TARGET_FILES = \\
#{target_files.sort.map do |e| "\t"+e end.join(" \\\n")}

#{rules}
MAKE
        }
end

def add_target_file src
        #src = Pathname.new(src).realpath.to_s
        target_files = []
        target_files += File.read(LISTFILE).split
        target_files << src
        target_files.uniq!

        myconf = src[1..-1]
        FileUtils.mkdir_p(File.dirname(myconf))
        FileUtils.cp(src, myconf)

        File.open(LISTFILE, "w") { |list|
                list.puts target_files.sort
        }
        #`svn add #{myconf}`
        #`svn up`
end

def del_target_file src
        #src = Pathname.new(src).realpath.to_s
        target_files = File.read(LISTFILE).split rescue []
        target_files.delete(src)
        target_files.uniq!

        File.open(LISTFILE, "w") { |list|
                list.puts target_files.sort
        }
        #myconf = src[1..-1]
        #`svn rm #{myconf}`
        #`svn up`
end


$OPT = Struct.new("Opt", :func, :file).new
ARGV.options { |o|
        o.def_option('--help', 'print this message.') { puts o; exit 0 }
        o.def_option('--add VAL', 'add new config file.') { |v| $OPT.func = :add; $OPT.file = v }
        o.def_option('--del VAL', 'delete existed config file.') { |v| $OPT.func = :del; $OPT.file = v }
        o.parse!
}


if __FILE__ == $0
        case $OPT.func
        when :add
                add_target_file $OPT.file
        when :del
                del_target_file $OPT.file
        end

        create_makefile
end