rubyでコマンドラインアプリを作成するためのreptファイル

gem install rept

でreptをインストールすると、いくつかのサンプルも一緒にインストールされます。

c:/ruby/lib/ruby/gems/1.8/gems/rept-0.1.1/sample/ruby/command_line/command_line.rept

というファイルがあります。
これはrubyでコマンドラインアプリを作成するためのreptファイルです。

生成すると、

  1. 簡単なrakefile
  2. ディレクトリ構造(bin/, lib/, test/)
  3. 何も起きないアプリ(bin/reptname1)
  4. 成功する簡易テスト(test/test_reptname1)
  5. rake testでテストを実行
  6. rake packageでパッケージを作成(tar.gzとgemファイル)
  7. コマンド無しでrakeを実行するとテストとパッケージングを順番に行う

が作られます。

以下、実行時のログです。

bash% rept command_line.rept Ongaeshi
引数  : Ongaeshi
テスト: ongaeshi/rakefileは作成可能?
テスト: ongaeshi/bin/ongaeshiは作成可能?
テスト: ongaeshi/bin/ongaeshi_localは作成可能?
テスト: ongaeshi/lib/ongaeshi.rbは作成可能?
テスト: ongaeshi/test/test_ongaeshi.rbは作成可能?
実行  : ディレクトリ作成 --> ongaeshi
実行  : ファイル作成 --> ongaeshi/rakefile
実行  : ディレクトリ作成 --> ongaeshi/bin
実行  : ファイル作成 --> ongaeshi/bin/ongaeshi
実行  : ファイル作成 --> ongaeshi/bin/ongaeshi_local
実行  : ディレクトリ作成 --> ongaeshi/lib
実行  : ファイル作成 --> ongaeshi/lib/ongaeshi.rb
実行  : ディレクトリ作成 --> ongaeshi/test
実行  : ファイル作成 --> ongaeshi/test/test_ongaeshi.rb
bash% cd ongaeshi/
bash% ls -la
total 4
drwxrwxrwx+ 5 tuto0621 なし    0 May 10 23:40 .
drwxrwxrwx+ 4 tuto0621 なし    0 May 10 23:40 ..
drwxrwxrwx+ 2 tuto0621 なし    0 May 10 23:40 bin
drwxrwxrwx+ 2 tuto0621 なし    0 May 10 23:40 lib
-rwxrwxrwx  1 tuto0621 なし 1041 May 10 23:40 rakefile
drwxrwxrwx+ 2 tuto0621 なし    0 May 10 23:40 test
bash% ./bin/ongaeshi_local # ローカルテスト実行用
ongaeshi [option]
bash% rake # gemを生成
c:/users/tuto0621/work/rept/sample/ruby/command_line/ongaeshi/rakefile:6: warning: already initialized constant VERSION
C:/ruby/bin/ruby.exe -w -I"lib;c:/users/tuto0621/work/rept/sample/ruby/command_line/ongaeshi/lib" "C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.5/lib/rake/rake_test_loader.rb" "test/test_ongaeshi.rb" 
(in c:/users/tuto0621/work/rept/sample/ruby/command_line/ongaeshi)
Loaded suite C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.5/lib/rake/rake_test_loader
Started
.
Finished in 0.0 seconds.

1 tests, 1 assertions, 0 failures, 0 errors
rm -r ./pkg
rm -r pkg
mkdir -p ./pkg
mkdir -p ./pkg/ongaeshi-0.0.0
rm -f ./pkg/ongaeshi-0.0.0/rakefile
ln rakefile ./pkg/ongaeshi-0.0.0/rakefile
mkdir -p ./pkg/ongaeshi-0.0.0/bin
rm -f ./pkg/ongaeshi-0.0.0/bin/ongaeshi
ln bin/ongaeshi ./pkg/ongaeshi-0.0.0/bin/ongaeshi
rm -f ./pkg/ongaeshi-0.0.0/bin/ongaeshi_local
ln bin/ongaeshi_local ./pkg/ongaeshi-0.0.0/bin/ongaeshi_local
mkdir -p ./pkg/ongaeshi-0.0.0/lib
rm -f ./pkg/ongaeshi-0.0.0/lib/ongaeshi.rb
ln lib/ongaeshi.rb ./pkg/ongaeshi-0.0.0/lib/ongaeshi.rb
mkdir -p ./pkg/ongaeshi-0.0.0/test
rm -f ./pkg/ongaeshi-0.0.0/test/test_ongaeshi.rb
ln test/test_ongaeshi.rb ./pkg/ongaeshi-0.0.0/test/test_ongaeshi.rb
cd ./pkg
tar zcvf ongaeshi-0.0.0.tar.gz ongaeshi-0.0.0
ongaeshi-0.0.0/
ongaeshi-0.0.0/bin/
ongaeshi-0.0.0/bin/ongaeshi
ongaeshi-0.0.0/bin/ongaeshi_local
ongaeshi-0.0.0/lib/
ongaeshi-0.0.0/lib/ongaeshi.rb
ongaeshi-0.0.0/rakefile
ongaeshi-0.0.0/test/
ongaeshi-0.0.0/test/test_ongaeshi.rb
cd -
WARNING:  no author specified
WARNING:  no email specified
WARNING:  no homepage specified
mv ongaeshi-0.0.0.gem pkg/ongaeshi-0.0.0.gem
  Successfully built RubyGem
  Name: ongaeshi
  Version: 0.0.0
  File: ongaeshi-0.0.0.gem
bash% gem install pkg/ongaeshi-0.0.0.gem # gemのインストールテスト
Successfully installed ongaeshi-0.0.0
1 gem installed
Installing ri documentation for ongaeshi-0.0.0...
Installing RDoc documentation for ongaeshi-0.0.0...
bash% ongaeshi.bat # ちゃんと実行出来る
ongaeshi [option]
bash% gem uninstall ongaeshi # gemのアンインストール
Successfully uninstalled ongaeshi-0.0.0
Removing ongaeshi
bash% cd ..
bash% rept -u command_line.rept Ongaeshi # reptで生成したもの削除
確認  : 本当に元に戻していいですか?(y/n) y
y
引数  : Ongaeshi
実行  : ファイルを削除 ongaeshi/rakefile
実行  : ファイルを削除 ongaeshi/bin/ongaeshi
実行  : ファイルを削除 ongaeshi/bin/ongaeshi_local
実行  : 空になったのでディレクトリを削除しました ongaeshi/bin
実行  : ファイルを削除 ongaeshi/lib/ongaeshi.rb
実行  : 空になったのでディレクトリを削除しました ongaeshi/lib
実行  : ファイルを削除 ongaeshi/test/test_ongaeshi.rb
実行  : 空になったのでディレクトリを削除しました ongaeshi/test
bash% rm -r ongaeshi/ # pkg作ったので残りは手で消す必要がある

reptファイルはこんな感じです。

Args:
  アプリ名(キャメル)
Index: reptname1/rakefile
require 'rake/packagetask'
require 'rake/testtask'
require 'rake/gempackagetask'

# バージョン番号
VERSION = '0.0.0'

# パッケージファイルリスト
PKG_FILES = FileList["rakefile", "bin/*", "lib/*", "test/*"]

# ユニットテスト実行
Rake::TestTask.new do |t|
  t.libs << File.expand_path('lib')
  t.test_files = FileList["test/test_*.rb"]
  t.warning = true
  t.verbose = true
end

# パッケージ作成
Rake::PackageTask.new("reptname1", VERSION) do |p|
  p.package_dir = "./pkg"
  p.package_files.include(PKG_FILES)
  p.need_tar_gz = true
end

# gem作成
spec = Gem::Specification.new do |s|
  s.platform = Gem::Platform::RUBY
  s.name = 'reptname1'
  s.summary = "reptname1 summary"
  s.description = "reptname1 description"
  s.author = ""
  s.email = ""
  s.homepage = ""
  s.files = PKG_FILES
  s.has_rdoc = true
  s.rubyforge_project = "reptname1"
  s.executables = ['reptname1']
  s.default_executable = 'reptname1'
  s.version = VERSION
end

Rake::GemPackageTask.new(spec) do |pkg|
  pkg.need_tar_gz = true
end

task :default => [:test, :repackage]

Index: reptname1/bin/reptname1
#!/usr/bin/env ruby

begin
  require 'reptname1'
rescue LoadError
  require 'rubygems'
  require 'reptname1'
end

reptname1
Index: reptname1/bin/reptname1_local
#!/usr/bin/env ruby

require File.join(File.dirname(__FILE__), '../lib/reptname1')

reptname1
Index: reptname1/lib/reptname1.rb
require 'optparse'

def reptname1
  opt = OptionParser.new('reptname1 [option]')
  opt.parse!

  if (ARGV.size > 0)
    # ReptName1メイン処理
  else
    puts opt.help
  end	
end
Index: reptname1/test/test_reptname1.rb
require 'test/unit'
require 'reptname1'

class TC_ReptName1 < Test::Unit::TestCase
  def setup
  end

  def teardown
  end

  def test_reptname1
    assert_equal(0, 0)
  end
end