「送る」でファイルをバックアップするスクリプト

「年月日_」をファイルの頭に付けてコピーするだけのスクリプトです。普段、バージョン管理システムを使うのが面倒なファイルはこんな感じでその日の作業前にバックアップをしています。
Rakeを使ってExerbのバイナリを作って、c:\Documents and Settings\ユーザ名\SendToにできた実行ファイルをコピーしてください(手抜き)。
スクリプトで特筆すべぎところは… 何か例外が出たらメッセージボックスを表示するようにしているところでしょうか。あ、そうそう、Exceptionクラスで例外の内容を適当に読み易い文字列にしてくれるメソッドってありませんでしたっけ?(今回自前で実装しています。)
ファイル: bk.rb

#!/usr/bin/env ruby

require 'Win32API'
require 'pathname'
require 'fileutils'

class Win32API
  # type flag
  MB_OK               = 0
  MB_OKCANCEL         = 1
  MB_ABORTRETRYIGNORE = 2
  MB_YESNOCANCEL      = 3
  MB_YESNO            = 4
  MB_RETRYCANCEL      = 5

  # return values
  IDOK     = 1
  IDCANCEL = 2
  IDABORT  = 3
  IDRETRY  = 4
  IDIGNORE = 5
  IDYES    = 6
  IDNO     = 7

  def Win32API.MessageBox(wnd, text, caption, type = MB_OK)
    messagebox = Win32API.new('user32', 'MessageBox', %w(p p p i), 'i')
    messagebox.call(wnd, text, caption, type)
  end

  def Win32API.MessageBoxEx(wnd, text, caption, type = MB_OK, languageid = 0)
    messagebox = Win32API.new('user32', 'MessageBoxEx', %w(p p p i i), 'i')
    messagebox.call(wnd, text, caption, type, languageid)
  end
end

class Exception
  def inspect
    mesgs = [self.class.to_s + ': ' + message]
    trace = backtrace.map {|s| '  - ' + s }
    (mesgs + trace).join("\n")
  end
end

class Pathname
  def copy_file(dst, *rests)
    FileUtils.copy_file(self, dst, *rests)
  end
end

def ARGV.each_path
  each {|file| yield(Pathname(file)) }
end

if $0 == __FILE__
  begin
    prefix = Time.now.strftime('%Y%m%d_')
    ARGV.each_path do |src|
      next unless src.file?
      dst = src.dirname + (prefix + src.basename)
      src.copy_file(dst)
    end
  rescue Exception => ex
    Win32API.MessageBox(0, ex.inspect, 'Exception')  
  end
end

ファイル: bk.exy

general:
  startup: bk.rb
  core: gui
  kcode: none

file:
  bk.rb:
  Win32API.so:
    file: C:/Program Files/ruby-1.8/lib/ruby/1.8/i386-mswin32/Win32API.so
    type: extension-library
  pathname.rb:
    file: C:/Program Files/ruby-1.8/lib/ruby/1.8/pathname.rb
  etc.so:
    file: C:/Program Files/ruby-1.8/lib/ruby/1.8/i386-mswin32/etc.so
    type: extension-library
  fileutils.rb:
    file: C:/Program Files/ruby-1.8/lib/ruby/1.8/fileutils.rb

ファイル: Rakefile

# -*- ruby -*-

base = 'bk'

require 'rake/clean'
require 'rake/packagetask'

task :default => ["#{base}.exe"]

file "#{base}.exe" => ["#{base}.rb", "#{base}.exy"] do
  sh "exerb.bat #{base}.exy"
  sh "upx #{base}.exe"
end

CLEAN.include("#{base}.exe")

Rake::PackageTask.new(base, '0.1a') do |p|
  p.package_dir = './pkg'
  p.package_files.include('Rakefile')
  p.package_files.include("#{base}.*")
  p.need_zip = true
end