鶏頭のプログラム

Ruby, Javascript, C言語, SQLなどのプログラミング

gritを使ってgitを自動で操作してみる。

gitサーバーをCentOS上に構築 - 鶏頭のプログラムで書きましたように
gitのサーバーを構築しました。

さらに今回は sphinx でドキュメント管理をして
それを即時反映Webで公開可能な状態にするということに取り組みます。

というわけで今回はWebサーバーとしては

RubySinatraを使用
httpのサービスはapache

を使用して環境構築を行います。

まずはSinatraの準備から(今回はとても簡単に)。
ディレクトリ構成は下記のようになります。

├── Gemfile
├── compile.rb
├── config.ru
├── route.rb
├── log
│   ├── access.log
│   └── error.log
├── public
│   └── sphinxでmakeしたhtmlファイルを配置
└── sphinx
       └──  documents
           ├── .git
           └── server
               └── sphinxのファイル一覧

とくに特筆すべき点はないのですが一応ファイルの中身は

Gemfile

source 'https://rubygems.org'
gem 'sinatra', '1.4.5'
gem 'grit', '2.5.0'

config.ru

require File.expand_path(File.dirname(__FILE__)) + '/route'
set :public, File.dirname(__FILE__) + '/static'
run Sinatra::Application

route.rb

require 'sinatra'
require './compile'
get '/' do
  redirect '/documents/html/index.html'
end

get '/compile' do
  documents_coompile
  redirect '/'
end

compile.rb

require 'grit'

def documents_coompile 
# カレントディレクトリを
  dir = Dir.pwd
# .gitのあるディレクトリまで移動
  Dir.chdir("./sphinx/documents/")
  repo = Grit::Repo.new(".")
  repo.git.pull
# sphinxで make html が可能なディレクトリに移動
  Dir.chdir("./server")
  system("make server")
# もとにディレクトリに戻す
  Dir.chdir(dir)
end

gitを使用し、sphinxで書かれたドキュメントを管理しているので
リクエストで /compile が来たときにgit pullを行い、ソースを最新の状態にしてから
sphinxのmake serverを実行するように compile.rb を書きました。
make serverはmake htmlをちょっとだけいじったものです。
詳しくは SphinxのMakefileをいじる - 鶏頭のプログラム


今回はサーバー側でソースがいじられることはないので変更の取り消しを行うための
コマンド等は入れていません。



ちなみに余談ですが、gritで git pull を行う場合は、ちゃんとカレントディレクトリを
移動しておかないと変更があったファイル等が今のカレントディレクトリを基準として
出力されるので注意が必要です。

それでは次回はsphinxのほうの修正を書いていきます。
SphinxのMakefileをいじる - 鶏頭のプログラム