3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WindowsでCapfileをUTF-8で書く

Posted at

Windows で Capfile を UTF-8 で書けたのがうれしかったのでメモ。結論、1行目に coding: utf-8 ってコメント書けばいいだけなんですけどね。なにそれ普通の .rb でも一緒じゃんって話です。

Before coding: utf-8

Capfile
desc "負荷などを表示します."
task "uptime" do
  run("uptime")
end
C:\Users\rch850\Desktop>cap -T
C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/configuration/loading.rb:93:in `instance_eval': Capfile:1: invalid multibyte char (Windows-31J) (SyntaxError)
Capfile:1: syntax error, unexpected tSTRING_BEG
Capfile:2: syntax error, unexpected tIDENTIFIER, expecting $end
task "uptime" do
            ^
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/configuration/loading.rb:93:in `load'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/configuration/loading.rb:172:in `load_from_file'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/configuration/loading.rb:89:in `load'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/configuration/loading.rb:86:in `block in load'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/configuration/loading.rb:86:in `each'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/configuration/loading.rb:86:in `load'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/cli/execute.rb:65:in `block in load_recipes'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/cli/execute.rb:65:in `each'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/cli/execute.rb:65:in `load_recipes'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/cli/execute.rb:31:in `execute!'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/lib/capistrano/cli/execute.rb:14:in `execute'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/capistrano-2.12.0/bin/cap:4:in `<top (required)>'
        from C:/Ruby193/bin/cap:23:in `load'
        from C:/Ruby193/bin/cap:23:in `<main>'

After coding: utf-8

Capfile

# coding: utf-8
desc "負荷などを表示します."
task "uptime" do
  run("uptime")
end
C:\Users\rch850\Desktop>cap -T
cap invoke # Invoke a single command on the remote servers.
cap shell  # Begin an interactive Capistrano session.
cap uptime # 負荷などを表示します.

Extended help may be available for these tasks.
Type `cap -e taskname' to view it.

なんでこんなことやってんの

Shift_JIS で Capfile 書いていればこんなこと必要ないんですよね。でも Shift_JIS にするのは魂が許さなかったので UTF-8 でこんな風に書いてました。

#
# とりあえず書いてみた Capfile です
#
# coding: utf-8
desc "負荷などを表示します."
task "uptime" do
  run("uptime")
end

これだとエラーになっちゃうんですよね。うーん、こりゃ UTF-8 諦めるしかないかなーと思って、desc は英語で書いてました。

でも、コードの引き継ぎとかで「なんで英語なの気取っちゃって」みたいに思われたくないので、なんとか日本語にできないかなーと調べてた所、coding: utf-8 は基本的に1行目に書かなければならないという事が分かりました。はい、ただそれだけです。

結局、一般的な Ruby の話ということで落ち着いたんですが、eval がどうこうってエラーが出てたので Capfile 独自の問題だと勘違いしてて無駄に時間を取られました。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?