プラグインをgem化する
Version 1 (Haru Iida, 10/24/2009 08:04 am)
| 1 | 1 | Haru Iida | h1. プラグインをgem化する |
|---|---|---|---|
| 2 | 1 | Haru Iida | |
| 3 | 1 | Haru Iida | "r2904":http://www.redmine.org/projects/redmine/repository/revisions/2904 よりRedmineはgemでインストールしたプラグインを使えるようになりました。 |
| 4 | 1 | Haru Iida | |
| 5 | 1 | Haru Iida | プラグインをgemとしてインストールすることにより1台のサーバに複数のredmineを構築している場合でもそれぞれにプラグインを入れる必要がありません。また、プラグインのバージョンアップも非常に楽です。 |
| 6 | 1 | Haru Iida | |
| 7 | 1 | Haru Iida | gemでインストールしたプラグインをredmineにロードするためにはenvironment.rbに以下のように記述します。 |
| 8 | 1 | Haru Iida | |
| 9 | 1 | Haru Iida | <pre><code class="Ruby"> |
| 10 | 1 | Haru Iida | Rails::Initializer.run do |config| |
| 11 | 1 | Haru Iida | ... |
| 12 | 1 | Haru Iida | config.gem 'プラグイン名' |
| 13 | 1 | Haru Iida | end |
| 14 | 1 | Haru Iida | </code></pre> |
| 15 | 1 | Haru Iida | |
| 16 | 1 | Haru Iida | ではプラグインをgemに変換する方法です。jewelerというツールを使ってgemを作成する方法を説明します。 |
| 17 | 1 | Haru Iida | |
| 18 | 1 | Haru Iida | h2. 準備 |
| 19 | 1 | Haru Iida | |
| 20 | 1 | Haru Iida | 以下の2つのツールがインストールされている必要があります。 |
| 21 | 1 | Haru Iida | |
| 22 | 1 | Haru Iida | * jeweler |
| 23 | 1 | Haru Iida | * git |
| 24 | 1 | Haru Iida | |
| 25 | 1 | Haru Iida | gitはリポジトリとしてgitを使っているかどうかにかかわらず必要です。jewelerが内部で使っているためです。 |
| 26 | 1 | Haru Iida | |
| 27 | 1 | Haru Iida | jewelerは以下のコマンドでインストールできます。 |
| 28 | 1 | Haru Iida | |
| 29 | 1 | Haru Iida | <pre> |
| 30 | 1 | Haru Iida | gem install jeweler |
| 31 | 1 | Haru Iida | </pre> |
| 32 | 1 | Haru Iida | |
| 33 | 1 | Haru Iida | gitは"本家":http://git-scm.com/ のサイトなどを参考にプラットフォームにあったものをインストールしてください。 |
| 34 | 1 | Haru Iida | |
| 35 | 1 | Haru Iida | h2. Rakeファイルを作る |
| 36 | 1 | Haru Iida | |
| 37 | 1 | Haru Iida | gem化したいプラグインのトップフォルダにRakefileがあれば、以下の記述を加えてください。無ければ新規にRakefileを作成してください。 |
| 38 | 1 | Haru Iida | |
| 39 | 1 | Haru Iida | <pre><code class="Ruby"> |
| 40 | 1 | Haru Iida | #プラグイン名は適宜変えてください。 |
| 41 | 1 | Haru Iida | @plugin_name = 'code_review' |
| 42 | 1 | Haru Iida | |
| 43 | 1 | Haru Iida | begin |
| 44 | 1 | Haru Iida | require 'jeweler' |
| 45 | 1 | Haru Iida | description = 'TODO' |
| 46 | 1 | Haru Iida | author = 'TODO' |
| 47 | 1 | Haru Iida | url = 'TODO' |
| 48 | 1 | Haru Iida | |
| 49 | 1 | Haru Iida | redmine_init_content = File.read('init.rb') |
| 50 | 1 | Haru Iida | if redmine_init_content.match(/description (.*$)/) |
| 51 | 1 | Haru Iida | description = $1.gsub("'",'').gsub('"','') |
| 52 | 1 | Haru Iida | end |
| 53 | 1 | Haru Iida | |
| 54 | 1 | Haru Iida | if redmine_init_content.match(/author (.*$)/) |
| 55 | 1 | Haru Iida | author = $1.gsub("'",'').gsub('"','') |
| 56 | 1 | Haru Iida | end |
| 57 | 1 | Haru Iida | |
| 58 | 1 | Haru Iida | if redmine_init_content.match(/[^ \t]*url ['"](.*)['"]/) |
| 59 | 1 | Haru Iida | url = $1 |
| 60 | 1 | Haru Iida | end |
| 61 | 1 | Haru Iida | |
| 62 | 1 | Haru Iida | if redmine_init_content.match(/[^ \t]*url ['"](.*)['"]/) |
| 63 | 1 | Haru Iida | url = $1 |
| 64 | 1 | Haru Iida | end |
| 65 | 1 | Haru Iida | |
| 66 | 1 | Haru Iida | Jeweler::Tasks.new do |s| |
| 67 | 1 | Haru Iida | s.name = "#{@plugin_name}" |
| 68 | 1 | Haru Iida | s.summary = "#{description}" |
| 69 | 1 | Haru Iida | s.homepage = "#{url}" |
| 70 | 1 | Haru Iida | s.description = "#{description}" |
| 71 | 1 | Haru Iida | s.authors = ["#{author}"] |
| 72 | 1 | Haru Iida | s.rubyforge_project = "#{@plugin_name}" # TODO |
| 73 | 1 | Haru Iida | s.files = FileList[ |
| 74 | 1 | Haru Iida | "[A-Z]*", |
| 75 | 1 | Haru Iida | "init.rb", |
| 76 | 1 | Haru Iida | "rails/init.rb", |
| 77 | 1 | Haru Iida | "{bin,generators,lib,test,app,assets,config,lang}/**/*", |
| 78 | 1 | Haru Iida | 'lib/jeweler/templates/.gitignore' |
| 79 | 1 | Haru Iida | ] |
| 80 | 1 | Haru Iida | end |
| 81 | 1 | Haru Iida | Jeweler::GemcutterTasks.new |
| 82 | 1 | Haru Iida | Jeweler::RubyforgeTasks.new do |rubyforge| |
| 83 | 1 | Haru Iida | rubyforge.doc_task = "rdoc" |
| 84 | 1 | Haru Iida | end |
| 85 | 1 | Haru Iida | |
| 86 | 1 | Haru Iida | rescue LoadError |
| 87 | 1 | Haru Iida | puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com" |
| 88 | 1 | Haru Iida | end |
| 89 | 1 | Haru Iida | |
| 90 | 1 | Haru Iida | task "create-version" do |
| 91 | 1 | Haru Iida | # VERSION |
| 92 | 1 | Haru Iida | File.open('VERSION','w') do |version_file| |
| 93 | 1 | Haru Iida | redmine_init_content = File.read('init.rb') |
| 94 | 1 | Haru Iida | if redmine_init_content.match(/^[ \t]*version (.*$)/) |
| 95 | 1 | Haru Iida | version = $1.gsub("'",'').gsub('"','') |
| 96 | 1 | Haru Iida | version_file.puts version |
| 97 | 1 | Haru Iida | end |
| 98 | 1 | Haru Iida | end |
| 99 | 1 | Haru Iida | end |
| 100 | 1 | Haru Iida | |
| 101 | 1 | Haru Iida | task "create_init_rb_for_gem" do |
| 102 | 1 | Haru Iida | Dir.mkdir("rails") unless File.exist?("rails") |
| 103 | 1 | Haru Iida | FileUtils.cp "init.rb", "rails/init.rb" |
| 104 | 1 | Haru Iida | end |
| 105 | 1 | Haru Iida | |
| 106 | 1 | Haru Iida | task "build-gem" => ["create-version", "create_init_rb_for_gem", "gemspec:generate", "gemspec:validate", "build"] do |
| 107 | 1 | Haru Iida | |
| 108 | 1 | Haru Iida | end |
| 109 | 1 | Haru Iida | </code></pre> |
| 110 | 1 | Haru Iida | |
| 111 | 1 | Haru Iida | そして以下のコマンドを実行してください。 |
| 112 | 1 | Haru Iida | |
| 113 | 1 | Haru Iida | <pre> |
| 114 | 1 | Haru Iida | rake build-gem |
| 115 | 1 | Haru Iida | </pre> |
| 116 | 1 | Haru Iida | |
| 117 | 1 | Haru Iida | pkgというディレクトリの下にgemファイルができます。 |
| 118 | 1 | Haru Iida | |
| 119 | 1 | Haru Iida | また、gemをインストールする場合には |
| 120 | 1 | Haru Iida | |
| 121 | 1 | Haru Iida | <pre> |
| 122 | 1 | Haru Iida | rake install |
| 123 | 1 | Haru Iida | </pre> |
| 124 | 1 | Haru Iida | |
| 125 | 1 | Haru Iida | でインストールできます。 |