ラベル coffeescript の投稿を表示しています。 すべての投稿を表示
ラベル coffeescript の投稿を表示しています。 すべての投稿を表示

2011年6月16日木曜日

rails自体の開発環境を作る4(完結)

■概要

さくらvpsを再インストールし下記を目標に再構築した際の記録を残しておきます。

・サーバとして一般的に行う設定がなされている事
・最低限のサーバセキュリティを維持している事
・rails自体の開発出来る事
・node.js、coffeescriptが動く事

■パッケージ管理・導入(centos5.5 x86_64版 さくらvps)

●パッケージ最新化
# yum -y update

●当面必要そうなパッケージをインストール
# yum -y install vim-enhanced nmap curl

●webサーバ/DB系をインストール
# yum -y install httpd httpd-devel
# yum -y install sqlite sqlite-devel
# yum -y install mysql mysql-server mysql-devel
# yum -y install postgresql84 postgresql84-contrib postgresql84-devel postgresql84-libs postgresql84-server

●rpmforgeの設定を行う
# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
# rpm -i rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
# rm rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
# vim /etc/yum.repos.d/rpmforge.repo
(8行目)
enabled = 1 から enabled = 0 に変更(デフォルトで動かないようにする)

●gitインストール
# yum -y install git --enablerepo=rpmforge

●memcachedインストール
# yum -y install libevent libevent-devel
(素直に依存パッケージが解決しないので手動でインストールする)
# wget http://packages.sw.be/perl-Net-SSLeay/perl-Net-SSLeay-1.36-1.el5.rfx.x86_64.rpm
# wget http://packages.sw.be/perl-Net-SSLeay/perl-Net-SSLeay-1.36-1.el5.rfx.i386.rpm
# wget http://packages.sw.be/perl-IO-Socket-SSL/perl-IO-Socket-SSL-1.34-1.el5.rfx.noarch.rpm
# rpm -Uvh perl-Net-SSLeay-1.36-1.el5.rfx.x86_64.rpm
# rpm -Uvh perl-Net-SSLeay-1.36-1.el5.rfx.i386.rpm
# rpm -Uvh perl-IO-Socket-SSL-1.34-1.el5.rfx.noarch.rpm
# rm *.rpm
# yum -y install memcached memcached-devel --enablerepo=rpmforge

参考 http://nakoruru.jp/?p=439

●その他開発用パッケージのインストール
# yum -y install openssl-devel curl-devel readline-devel zlib-devel
# yum -y install java
# yum -y install libxml2 libxml2-devel libxslt-devel
# yum -y install libyaml-devel libffi-devel --enablerepo=rpmforge

●sqlite3が古いので自分でインストール
# wget http://www.sqlite.org/sqlite-autoconf-3070603.tar.gz
# tar zxvf sqlite-autoconf-3070603.tar.gz
# cd sqlite-autoconf-3070603
# ./configure
# make
# make install

※yumでインストールした物と競合しているはず。
ただyumでの依存関係があるので、あえて競合してインストールしてみた。

■設定

●日本語化
# vim /etc/sysconfig/i18n
(1行目)
LANG="C" から LANG="ja_JP.UTF-8" に変更

●不要デーモンの停止設定
# chkconfig auditd off
# chkconfig autofs off
# chkconfig avahi-daemon off
# chkconfig bluetooth off
# chkconfig cups off
# chkconfig firstboot off
# chkconfig gpm off
# chkconfig haldaemon off
# chkconfig hidd off
# chkconfig kudzu off
# chkconfig lvm2-monitor off
# chkconfig mcstrans off
# chkconfig mdmonitor off
# chkconfig messagebus off
# chkconfig netfs off
# chkconfig nfslock off
# chkconfig pcscd off
# chkconfig portmap off
# chkconfig rawdevices off
# chkconfig restorecond off
# chkconfig rpcgssd off
# chkconfig rpcidmapd off
# chkconfig smartd off
# chkconfig xfs off
# chkconfig yum-updatesd off

参考 http://tanaka.sakura.ad.jp/archives/001065.html

●firewall設定
# system-config-securitylevel-tui
(firewallをenabledにし、22/80/443/3000/10022(後述)/8080(後述)を開けた)

●作業用ユーザ作成
# useradd kennyj
# passwd kennyj

●sudo設定
# /usr/sbin/visudo
(一番下に追記)
kennyj  ALL=(ALL)       ALL

●ssh設定
# vim /etc/ssh/sshd_config
(13行目)
#Port 22 => Port 10022 (22番は非常に攻撃されやすいので)
(39行目)
#PermitRootLogin yes => PermitRootLogin no
(59行目)
#PermitEmptyPasswords no => PermitEmptyPasswords no

# /etc/init.d/sshd restart

●firewall再設定
# vim /etc/sysconfig/iptables
(22番ポートを閉じる)

●時間同期設定(ntp)
(さくらVPSでは最初から設定されていました)
# yum -y install ntp
# vim /etc/ntp.conf
# ntpdate ntp.nict.jp (とりあえず一旦近づけておく)
# /etc/init.d/ntpd start
# chkconfig ntpd on

参考 http://centossrv.com/ntp.shtml

●postgresql準備
# service postgresql initdb

●mysql準備
# vim /etc/my.cnf
(文字列エンコーディングをUTF-8に設定)
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
old_passwords=1
default-character-set = utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
default-character-set = utf8

[mysql]
default-character-set = utf8

●必要に応じてサービス起動設定
# chkconfig httpd on
# chkconfig mysqld on
# chkconfig postgresql on
# chkconfig memcached on

●一旦再起動
# reboot

※これ以降はkennyjユーザで作業しています。

■javascript関係

●node.js/npm/coffeeスクリプトのインストール
$ wget http://nodejs.org/dist/node-v0.4.8.tar.gz
$ tar zxvf node-v0.4.8.tar.gz
$ cd node-v0.4.8
$ ./configure
$ make
$ sudo make install
$ cd ..
$ curl http://npmjs.org/install.sh > npm.sh
$ chmod a+x npm.sh
$ sudo PATH=/usr/local/bin:$PATH ./npm.sh
$ sudo PATH=/usr/local/bin:$PATH npm install -g coffee-script
$ rm npm.sh

■ruby関係

●rvmのインストール
$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
$ source ~/.bash_profile
$ rvm pkg install iconv
$ rvm install 1.9.2 --with-iconv-dir=$rvm_path/usr
$ rvm install ree   --with-iconv-dir=$rvm_path/usr
$ rvm install 1.8.7 --with-iconv-dir=$rvm_path/usr
$ rvm install jruby
$ rvm use --default 1.9.2

●railsインストール
$ rvm 1.8.7,1.9.2,ree,jruby gem install rails --no-rdoc --no-ri

※jruby1.6.1では落ちたので注意
  arel2.0.10がNG。jruby1.6.2では直っています
  http://jira.codehaus.org/browse/JRUBY-5581
  https://github.com/jruby/jruby/commit/dd1d9382a72af181e9d3998f2680154ebf1e651c

●sassのインストール
$ rvm 1.8.7,1.9.2,ree,jruby gem install sass --no-rdoc --no-ri

■rails自体の開発環境

●mysqlの設定
$ sudo /etc/init.d/mysqld start
$ mysql -u root
mysql> GRANT ALL PRIVILEGES ON activerecord_unittest.*  to 'rails'@'localhost';
mysql> GRANT ALL PRIVILEGES ON activerecord_unittest2.* to 'rails'@'localhost';

●postgresqlの設定
$ sudo /etc/init.d/postgresql start
$ sudo -u postgres createuser --superuser $USER

●gitの設定
$ git config --global user.name "kennyj"          # 名前
$ git config --global user.email kennyj@gmail.com # メールアドレス

●railsの取得とテスト
$ git clone git://github.com/rails/rails.git
$ cd rails
$ bundle install
$ cd activerecord
$ rake mysql:build_databases
$ rake postgresql:build_databases
$ cd ..
$ rake test

●railsを修正しパッチを作成する
$ git checkout -b xxxxx # ブランチ名
(ソース修正)
$ git commit -a -m "コミットメッセージ"
$ git checkout master
$ git pull
$ git checkout xxxxx
$ git rebase master
$ git commit -a
$ git format-patch master --stdout > xxxxx.diff
$ cat xxxxx.diff

■更新記録

2011/06/23 ntpについて追記
サービス起動設定について追記

2011/08/02 rvm package -> rvm pkgに変更

2011年4月22日金曜日

javascriptでのCIを目指して、phantomjsの環境を作成する4

■概要

前回までで、phantomjsが大体わかったので、jasmine with CoffeeScriptな環境を作成します。

■説明

・CoffeeScriptのコンパイルを楽したい。ここではwatchrを利用してみます。

$ sudo gem install watchr

$ mkdir demo; cd demo
$ mkdir src; mkdir spec
$ vim coffee.watchr

watch(/(src|spec)\/.*\.coffee/) { |md|
  system("coffee -cb #{md[0]}")
}

$ watchr coffee.watchr

(別の端末で)

$ vim src/test.coffee

console.log "a"

$ ls src
test.coffee test.js <= できてます!

・ではjasmineを利用してブラウザからテストを実行します。OOPでは古典的な"カウンター"を作ってみます。
$ wget http://pivotal.github.com/jasmine/downloads/jasmine-standalone-1.0.2.zip
$ unzip *.zip
$ rm *.zip
$ vim spec/CounterSpec.coffee

describe "Counter", ->
  beforeEach ->
    @counter = new Counter()

  describe "#constructor", ->
    it "の初期値は0である", ->
      expect(@counter.value()).toEqual(0)

  describe "#incr", ->
    it "は値を1増加させるはず", ->
      @counter.incr()
      expect(@counter.value()).toEqual(1)

$ vim src/Counter.coffee

class Counter
  constructor: () ->
    @count = 0

  incr: () ->
    @count++

  value: () ->
    @count

$ vim SpecRunner.html (Counter.js/CounterSpec.jsの読み込みを記載する)

ブラウザで http://localhost/demo/SpecRunner.htmlを確認するといい感じです!。
最後にphantomjsからjasmineのSpecRunner.htmlを起動します。
$ vim src/run-jasmine.coffee

if phantom.state.length is 0
  if phantom.args.length isnt 1
    console.log 'Usage: run-jasmine.js URL'
    phantom.exit()
  else
    phantom.state = 'run-jasmine'
    phantom.open phantom.args[0]
else
  window.setInterval ->
    if document.body.querySelector('.finished-at')
      console.log document.body.querySelector('.description').innerText
      for el in document.body.querySelectorAll('div.jasmine_reporter > div.suite.failed')
        console.log ''
        for e in el.querySelectorAll('.description')
          console.log e.innerText
      phantom.exit()
  , 100

$ phantomjs src/run-jasmine.coffee http://localhost/demo/SpecRunner.html
2 specs, 0 failures in 0.039s <= テスト結果が得られてます。いいじゃん!

2011年4月21日木曜日

javascriptでのCIを目指して、phantomjsの環境を作成する3

■概要

前回に引き続きphantomjsのQuickStartを参考にCoffeeScriptを書いてみる。

■説明

・Loading & Rendering

Loadingの説明を見ているといくつかポイントが見えてきます。

・コマンドライン引数に指定されたスクリプトは、複数回呼び出される
※phantom.exit()するまで
・phantom.stateは値が維持されるので、初回呼び出しかどうか判断可能
初回は空文字列
・phantom.openは引数で指定されたページを読み込む
読み込み終了までblockingされる
・phantom.loadStatusはphantom.open後の状態が保持される
"success" もしくは "fail"

という訳で、LoadPage(とRendering)をCoffeeScriptで書いてみます。

$ vim google.coffee

LoadPage = (name, url, action) ->
  if !phantom.state
    phantom.state = name
    phantom.open url
  else
    action()
    phantom.exit()

LoadPage "google", "http://www.google.co.jp", ->
  phantom.render "#{phantom.state}.png"

$ phantomjs google.coffee
$ ls google*
google.coffee google.png

ちゃんと出来ました!なおphantom.viewportSizeの設定をしないとwidth/heightは適当な値になるようです














ちなみにdom treeは読み込み後documentオブジェクトを操作できます。

LoadPage "google", "http://www.google.co.jp", ->
  console.log document.getElementById("prm").innerText

$ phantomjs google.coffee
世界中から寄せられた日本へのメッセージが日本語で見られるサイトを開設しました。


■参照

phantomjsのQuickStart

■雑感

次回は本当にCIができたらな~と思いつつ(ゴールは近いはず)

2011年4月20日水曜日

javascriptでのCIを目指して、phantomjsの環境を作成する2

■概要
前回 phantomjsのインストールに成功したので、色々触って見ます。

・ちなみにphantomjsってCoffeeScriptいけるんですね!

$ phantomjs

Usage: phantomjs [options] script.[js|coffee]
  [script argument [script argument ...]]

・次からはphantomjsのQuickStartを、CoffeeScriptに書き写しながら進めます。

■説明

・まずはhello world

$ vim hello.coffee

console.log "hello, world!"
phantom.exit()

$ phantomjs hello.coffee
hello, world!

・次に同期sleep

$ vim delay.coffee

for t in [10..1]
  console.log t
  phantom.sleep 1000
console.log 'BLAST OFF'
phantom.exit()

$ phantomjs delay.coffee
10
9
...
1
BLAST OFF

・ちなみに非同期sleepは

$ vim adelay.coffee

fibs = [0, 1]

ticker = window.setInterval ->
  console.log fibs[fibs.length - 1]
  fibs.push fibs[fibs.length - 1] + fibs[fibs.length - 2]
  if fibs.length > 10
    window.clearInterval ticker
    phantom.exit()
, 300

$ phantomjs adelay.coffee
1
1
2
...
21
34
functionオブジェクトが最後の引数ではない場合ここにカンマ何ですね。。

・コマンドライン引数は

$ vim arguments.coffee

if phantom.args.length is 0
  console.log 'Try to pass some args when invoking this script!'
else
  phantom.args.forEach (arg, i) -> console.log "#{i}: #{arg}"
phantom.exit()

$ phantomjs arguments.coffee
Try to pass some args when invoking this script!

$ phantomjs arguments.coffee a b c
0: a
1: b
2: c

次回は、感じのページオープンとdom tree周り経由でCIにつなげたいと思う今日この頃

■参考

phantomjsのQuickStart

■雑感

CoffeeScriptの構文は、あまり違和感が無いな~
CoffeeScriptを習得するには、js -> coffeeの翻訳が一番?

■更新履歴

2011/04/20 http://tech.kayac.com/ を参考にさせて頂き、setIntervalの記載方法を変更しました。

2011年1月29日土曜日

CoffeeScriptをWindowsで使う

■概要

CoffeeScript面白いのですが、*nix系でしか利用できない。と思ってましたが、*.coffee -> *.jsのコンパイルだけならWindowsでも可能な方法を発見したのでご報告します!

■方法

http://jashkenas.github.com/coffee-script/ からcoffee-scriptをダウンロード。
・解凍して、extras/coffee-script.jsを取り出す。
・coffee-script.jsと同じフォルダに、下記の様な感じでcoffee-scriptコンパイラを利用するスクリプトを作成する
  ※色々処理はしょってるので要注意!

// compiler.js
var fs = WScript.CreateObject("Scripting.FileSystemObject"); 

//coffeescriptを読み込んで評価
var path = WScript.ScriptFullName.replace(WScript.ScriptName,"");
eval(fs.OpenTextFile(path + "coffee-script.js", 1).ReadAll()); 

//
// ここからメインです。
//
var args = WScript.Arguments;
if (args.length == 0) {
  WScript.Echo("error: please Drag And Drop *.coffee file!");
} else {
  for (var i = 0; i < args.length; i++) {
    var src      = fs.OpenTextFile(args.Item(i), 1).ReadAll();
    var compiled = CoffeeScript.compile(src);
    var file     = args.Item(i).replace(".coffee", ".js");
    fs.OpenTextFile(file, 2, true).Write(compiled);
  }
}

・*.coffeeなファイルをドラッグ & ドロップすると、*.coffeeと同じフォルダに*.jsが出来上がります!

■参考

・JScriptでimport スマートにできないかなぁ http://d.hatena.ne.jp/TakamiChie/20080403/1207227691

■2011/01/29 12:45

書き込み時に新規作成フラグtrueがついていませんでしたので修正しました。

2011年1月4日火曜日

CoffeeScript面白そう2

前回インストールしてみたCoffeeScriptだが、jasmineと組み合わせるとこんな感じで、javascriptのspecが書けた。


describe "String クラスは", ->
  str = ""
  beforeEach ->
    str = "jasmine"
  it "split メソッドを持っている", ->
    expect(str.split).toBeDefined()
  describe "ネストは", ->
    it "当然いけます", ->
      expect(str).toEqual("jasmine")


オリジナルのjasmine specと比較して良いのは

  • {} とか () とか が激減している
  • functionって書かなくて良い
  • varも書かなくて良い

辺りでしょうか。本気で使うなら課題は

  • coffeeの実行(*.coffee -> *.jsにコンパイル)を自動化
  • expect ~ toEqualとかをもっと"rspec"らしくしたい
  • 上の"str"をrspecっぽく何とかしたい(無理かな?)

といった所かな。少し考えるか。。

2011/01/04 0:52 追記


変数にrubyっぽく@をつけて(thisとバインド)、上記は

describe "String クラスは", ->
  beforeEach ->
    @str = "jasmine"
  it "split メソッドを持っている", ->
    expect(@str.split).toBeDefined()

で動くんだ。

2011年1月3日月曜日

CoffeeScript面白そう

CoffeeScriptとは「javascript悪くないけどね...文法がちょっと...」という人の為の言語で、
コンパイルするとjavascriptのソースが出力されます。

  • ruby(やpython)が大好き
  • "var"って書くのを忘れる
  • "function"って綴り長げー
  • "return"書きたくない
  • "if"を一行で書きたい

という私にぴったり。ちなみに37 signalsも使っているらしいですね

下記は「centos 5.5」に動く環境を作成するまでの記録です(2011/1/3現在)

■node.jsのインストール(v8もインストールされるようです)

# wget http://nodejs.org/dist/node-v0.2.6.tar.gz
# tar zxvf node-v0.2.6.tar.gz
# cd node-v0.2.6
# ./configure
# make
(# make test)
# make install

centos5.xにはshasumコマンドが存在しないようので、make testでエラーが発生します。
http://groups.google.com/group/nodejs/browse_thread/thread/4f8290fc06baab3b?pli=1
の様な話があるので

# ln -s /usr/bin/sha1sum /usr/bin/shasum

としておけば、落ちたテストは通ります。
(上記を実行しても私の環境的に multicast はテストが通りません...)

■npm(the Node Package Manager)のインストール

# curl http://npmjs.org/install.sh | sh

# npm -v (確認)
0.2.13-3

危ないからrootで実行するなとか言われましたが「It worked」だそうなのでほっときました。

■coffee scriptのインストール

# npm install coffee-script
# coffee -v (確認)
CoffeeScript version 1.0.0

■とりあえず実行してみる

# vim test.coffee で下記のように記載

debug = (x) -> console.log(x)
debug num for num in [1, 2, 3]

# coffee test.coffee
1
2
3
って表示されればOkay!

あとは、CoffeeScriptのサンプルを見てみましょう!