Feed on
Posts
Comments

http://wiki.rubyonrails.org/rails/pages/HowtoUseRailsWithSubversion
(日本語訳はhttp://techno.hippy.jp/rorwiki/?HowtoUseRailsWithSubversion
http://randomutterings.com/articles/2007/09/19/subversion-script-for-rails-developers

を見てやってみた。当方leopard。

rails-svnというコマンドとrailscommitというコマンドを作る。

rails-svnはrails hogeとアプリを作る際に一緒にレポジトリも指定しcommitする必要のないファイルを削除指定しcommitまで済ませるコマンド。railscommitはscaffold等大量のファイルを作った作業後svn addするのは手間なのでそれらを一括してするコマンド。
rails-svn

#!/bin/bash

if [ "$#" != "3" ]; then
  echo "Usage: rails-svn app_dir repository username"
  exit 1
fi

APPDIR=./$1
SVN_TRUNK=$2
SVN_USER=$3

function check_if_exist () {
  if [[ -e $1 ]]; then
    echo ""
    echo "$1 already exists, overwrite? y or n"
    echo ""
    read OVERWRITE
    case "$OVERWRITE" in
    y)
      echo "Overwriting..."
      ;;
    *)
      echo "Action canceled"
      exit 1
      ;;
    esac
  fi
}

check_if_exist ${APPDIR}
rails $APPDIR
svn import $APPDIR $SVN_TRUNK -m "Import" --username $SVN_USER
sudo rm -r $APPDIR
svn checkout $SVN_TRUNK $APPDIR
cd $APPDIR

svn remove log/*
svn commit -m "removing log files"
svn propset svn:ignore "*.log" log/
svn update log/
svn commit -m "Ignoring all files in /log/ ending in .log"
svn move config/database.yml config/database.example
svn commit -m "Moving database.yml to database.example to provide a template for anyone who checks out the code"
svn propset svn:ignore "database.yml" config/
svn update config/
svn commit -m "Ignoring database.yml"
svn remove tmp/*
svn propset svn:ignore "*" tmp/
svn update tmp/
svn commit -m "ignore tmp/ content from now"
svn propset svn:ignore ".htaccess" public/
svn update public/
svn commit -m "Ignoring .htaccess"
svn propset svn:ignore "dispatch.fcgi" public/
svn update public/
svn commit -m "Ignoring dispatch.fcgi"

これを何かのテキストエディタで”rails-svn”で保存して、「~」あたりに保存

Macintosh-2:~ hoge$ sudo mv /Users/hoge/rails-svn /usr/bin/rails-svn
Password:
Macintosh-2:~ hoge$ sudo chmod 755 /usr/bin/rails-svn
Macintosh-2:~ hoge$ rails-svn
Usage: rails-svn app_dir repository username
Macintosh-2:~ hoge$

保存したのを/user/binに移動後chmodで755にする。rails-svnしてみるとちゃんと動いてそう。自分の場合はsvnをassemblaで利用してるので、

Macintosh-2:~ hoge$ rails-svn example http://svn2.assembla.com/svn/example/trunk hoge
      create
      create  app/controllers
      create  app/helpers
      .
      .
      .
      create  log/test.log
Adding         example/test
Adding         example/test/unit
Adding         example/test/test_helper.rb
.
.
.
Adding         example/public/favicon.ico

Committed revision 1.
A    example/test
A    example/test/unit
A    example/test/test_helper.rb
.
.
.
A    example/public/favicon.ico
Checked out revision 1.
D         log/development.log
D         log/production.log
D         log/server.log
D         log/test.log
Deleting       log/development.log
Deleting       log/production.log
Deleting       log/server.log
Deleting       log/test.log

Committed revision 2.
property 'svn:ignore' set on 'log'
At revision 2.
Sending        log

Committed revision 3.
A         config/database.example
D         config/database.yml
Adding         config/database.example
Deleting       config/database.yml

Committed revision 4.
property 'svn:ignore' set on 'config'
At revision 4.
Sending        config

Committed revision 5.
D         tmp/cache
D         tmp/pids
D         tmp/sessions
D         tmp/sockets
property 'svn:ignore' set on 'tmp'
At revision 5.
Sending        tmp
Deleting       tmp/cache
Deleting       tmp/pids
Deleting       tmp/sessions
Deleting       tmp/sockets

Committed revision 6.
property 'svn:ignore' set on 'public'
At revision 6.
Sending        public

Committed revision 7.
property 'svn:ignore' set on 'public'
At revision 7.
Sending        public

Committed revision 8.
Macintosh-2:~ hoge$

rails example後、自動でtmpとかlogとかversion管理禁止にしてくれてる。人によって違うdatabase.ymlもdatabase.exampleに書き換えとかもしてくれる。

で、プラギンインスコしたり、scaffoldしたり、アプリをいろいろいじる。で、svn statusを見てみると

Macintosh-2:example hoge$ svn status
?      test/unit/hoge_test.rb
?      test/functional/hoges_controller_test.rb
?      test/fixtures/hoges.yml
?      app/helpers/hoges_helper.rb
?      app/models/hoge.rb
?      app/controllers/hoges_controller.rb
?      app/views/hoges
?      app/views/layouts/hoges.html.erb
M      config/routes.rb
?      db/schema.rb
?      db/development.sqlite3
?      db/migrate
?      vendor/plugins/restful_authentication
?      public/stylesheets/scaffold.css
Macintosh-2:example hoge$

こんなに多くを1つ1つaddしたくねぇ。
railscommit

#!/bin/bash
echo ""
echo "Here's what we're going to do:"
echo " "
echo "Add the following files"
echo "-----------------------"
svn status | awk '/^?/ {print $2}'
echo " "
echo "Remove the following files"
echo "--------------------------"
svn status | awk '/^!/ {print $2}'
echo " "
echo "Check in the following modified files"
echo "-------------------------------------"
svn status | awk '/^M/ {print $2}'
echo " "
echo "Proceed with commit? [yn]"
read answer
if [ "$answer" = "y" ]
then
    echo " "
    echo "Adding the following files"
    svn status | awk '/^?/ {print $2}' | xargs svn add
    echo " "
    echo "Removing the following files"
    svn status | awk '/^!/ {print $2}' | xargs svn remove
    echo " "
    echo "Committing changes to repository"
    svn commit
else
    echo " "
    echo "Commit cancelled"
fi

echo " "
echo "Want to check for updates? [yn]"
read answer
if [ "$answer" = "y" ]
then
    svn update
else
    echo " "
    echo "Update cancelled"
fi

で、これを”railscommit”で「~」あたりに保存

Macintosh-2:example hoge$ sudo mv /Users/hoge/railscommit /usr/bin/railscommit
Password:
Macintosh-2:example hoge$ chmod 755 /usr/bin/railscommit

rails-svnと同様

Macintosh-2:example hoge$ railscommit

Here's what we're going to do:

Add the following files
-----------------------
test/unit/hoge_test.rb
test/functional/hoges_controller_test.rb
test/fixtures/hoges.yml
app/helpers/hoges_helper.rb
app/models/hoge.rb
app/controllers/hoges_controller.rb
app/views/hoges
app/views/layouts/hoges.html.erb
db/schema.rb
db/development.sqlite3
db/migrate
vendor/plugins/restful_authentication
public/stylesheets/scaffold.css

Remove the following files
--------------------------

Check in the following modified files
-------------------------------------
config/routes.rb

Proceed with commit? [yn]
y

Adding the following files
A         test/unit/hoge_test.rb
A         test/functional/hoges_controller_test.rb
A         test/fixtures/hoges.yml
.
.
.
A         public/stylesheets/scaffold.css

Removing the following files

Committing changes to repository
svn: Commit failed (details follow):
svn: Could not use external editor to fetch log message; consider setting the $SVN_EDITOR environment variable or using the --message (-m) or --file (-F) options
svn: None of the environment variables SVN_EDITOR, VISUAL or EDITOR is set, and no 'editor-cmd' run-time configuration option was found

Want to check for updates? [yn]
y
At revision 8.
Macintosh-2:example hoge$ svn status
A      test/unit/hoge_test.rb
A      test/functional/hoges_controller_test.rb
A      test/fixtures/hoges.yml
.
.
.
A      public/stylesheets/scaffold.css
Macintosh-2:example hoge$ svn commit -m "someplugin and scaffold"
Adding         app/controllers/hoges_controller.rb
Adding         app/helpers/hoges_helper.rb
Adding         app/models/hoge.rb
.
.
.
Adding         vendor/plugins/restful_authentication/lib/restful_authentication/rails_commands.rb
Transmitting file data ............................................
Committed revision 9.
Macintosh-2:example hoge$ 

全自動でcommit完了。

Leave a Reply