play 2.0.x から play 2.1.0 に移行した時のメモ

play2.0でMarkdownをリアルタイムプレビュー出来るサービスを作りました - takashabeのブログ
で作ったアプリをplay 2.0.4 から play 2.1.0に移行したのでその時の変更内容のメモ

移行した時のコミットはこちらです。
migration play 2.0.4 to play 2.1.0 · 9494b72 · takashabe/konbu.md · GitHub

sbtでのバージョン指定

playとsbtのバージョンをplay 2.1.0に合わせて変更する。

--- a/project/build.properties
+++ b/project/build.properties
@@ -1 +1 @@
-sbt.version=0.11.3
\ No newline at end of file
+sbt.version=0.12.2
\ No newline at end of file
--- a/project/plugins.sbt
+++ b/project/plugins.sbt
@@ -5,4 +5,4 @@ logLevel := Level.Warn
 resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

 // Use the Play sbt plugin for Play projects
-addSbtPlugin("play" % "sbt-plugin" % "2.0.4")
\ No newline at end of file
+addSbtPlugin("play" % "sbt-plugin" % "2.1.0")
\ No newline at end of file

パッケージ名の変更

PlayProjectパッケージが分割されたことによりimportとかを合わせて必要がある。

--- a/project/Build.scala
+++ b/project/Build.scala
@@ -1,17 +1,17 @@
 import sbt._
 import Keys._
-import PlayProject._
+import play.Project._

-    val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
+    val main = play.Project(appName, appVersion, appDependencies).settings(

モジュール化による変更

jdbcとかのプロジェクトがサブプロジェクトに分割されたため、該当の機能を使っていた場合はBuild.scalaのappDependenciesに追加する必要がある。
今回移行したアプリではその辺の機能を使っていなかったので特に変更はしていない。

アプリケーション名の変更

sbtで指定するappNameでドット(.)が使えなくなったっぽいので変更した。

--- a/project/Build.scala
+++ b/project/Build.scala
@@ -1,17 +1,17 @@

-    val appName         = "konbu.md"
+    val appName         = "konbumd"


play 2.1.0の変更点や移行の方法などはこちらを参考にしました。
playdocja/documentation/2.1.0/manual/Highlights.md at 5554e43d15ae55165632c7893956f323202fb7b6 · garbagetown/playdocja · GitHub
Play!framework2.0.xから2.1へ移行する時にやったこと - 新しいフォルダ (2)

play2.0でMarkdownをリアルタイムプレビュー出来るサービスを作りました

http://konbumd.herokuapp.com/

内部的にはクライアントサイドでMarkdownテキストに変更あればサーバに投げて、サーバではknockoffを使ってMarkdownからHTMLへ変換して返す感じです。 githubとかはてなのテキスト用に使ってください。

コードはこちらに置いてあります。 playでajaxする時の参考になれば幸いです。

ScalaでAndroid 2012年冬

このエントリはAndroid Advent Calendarの6日目裏です。今日の表は@ngsw_taroさんです。

さて、AndroidといえばScalaやKotlin、Haxeなど*1で書くことが多いと思いますが、今回はその中でも割とメジャーなScalaでのやり方についてまとめてみます。
Scalaで書くための手法はいくつかありますが、今回は現在最も主流である(と思われる) sbt android plugin (https://github.com/jberkel/android-plugin) を使った方法について書きます。
開発の流れとしてはターミナルでアプリケーションの作成とテスト、ビルドをして、コードはIDEとかで書く感じになります。

環境準備

何はともあれ開発環境を整える。入れなければならないのは以下の通り。

Scala本体とandroid sdkはいいとして、sbtはMavenっぽいScalaのビルドツール、giter8はプロジェクトのひな形を作るためのツールです。
いずれもTypesafe — Stack: Downloadに沿ってインストールしておきます。Macなら全てhomebrewで入るのでぬるげーです。ちなみに今回使用したバージョンは以下になります。

λ ~/sandbox/ → scala -version
Scala code runner version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL
λ ~/sandbox/ → sbt sbt-version
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Set current project to default-171b7b (in build file:/Users/takashabe/sandbox/)
[info] 0.12.1
λ ~/sandbox/ → g8

giter8 0.4.5

android sdkは普通にインストールしておき、pathを通して環境変数を作っておきます。r21を使っています。

λ ~/ → cat .zshrc_custom 
## いろいろ略

# ANDROID-SDK
export ANDROID_SDK_HOME=/Applications/android-sdk-macosx/
export PATH=/Applications/android-sdk-macosx/platform-tools:/Applications/android-sdk-macosx/tools:$PATH

プロジェクトを作る

何はともあれgiter8でひな形を作っておきます。注意点としてはProguardをtrueにしないとコンパイルに失敗します。

λ ~/sandbox/ → g8 jberkel/android-app

Template for Android apps in Scala 

package [my.android.project]: com.takashabe.sample
name [My Android Project]: sample
main_activity [MainActivity]: 
scala_version [2.9.2]: 
api_level [10]: 
useProguard [true]: 
scalatest_version [1.8]: 

Applied jberkel/android-app.g8 in sample

λ ~/sandbox/ → cd sample 
λ ~/sandbox/sample/ → ls
project src     tests

この状態で一度動かしてみます。テストとかビルドとかは全てsbt経由で行います。
android:package-debugコマンドでコンパイルして、android:start-deviceでapkを流し込むっぽいことをやってます。
なお、エミュレータで動かす場合はandroid:start-deviceをandroid:start-emulatorとすれば良いだけです。

λ ~/sandbox/sample/ → sbt
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Loading project definition from /Users/takashabe/sandbox/sample/project
[info] Updating {file:/Users/takashabe/sandbox/sample/project/}default-883c40...
[info] Resolving org.scala-sbt#precompiled-2_10_0-m7;0.12.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/takashabe/sandbox/sample/project/target/scala-2.9.2/sbt-0.12/classes...
[info] Set current project to sample (in build file:/Users/takashabe/sandbox/sample/)
> ;android:package-debug;android:start-device

## 略

[info] Dexing /Users/takashabe/sandbox/sample/target/classes.dex
[info] Packaging /Users/takashabe/sandbox/sample/target/sample-0.1.apk
[success] Total time: 19 s, completed Dec 5, 2012 2:15:11 AM
[info] Wrote /Users/takashabe/sandbox/sample/target/scala-2.9.2/src_managed/main/scala/com/takashabe/sample/TR.scala
ProGuard, version 4.6
ProGuard is released under the GNU General Public License. You therefore
must ensure that programs that link to it (scala, ...)
carry the GNU General Public License as well. Alternatively, you can
apply for an exception with the author of ProGuard.
The output seems up to date
[info] Packaging /Users/takashabe/sandbox/sample/target/sample-0.1.apk
[info] 	pkg: /data/local/tmp/sample-0.1.apk
[info] Success
[info] 3127 KB/s (177013 bytes in 0.055s)
[info] Starting: Intent { act=android.intent.action.MAIN cmp=com.takashabe.sample/.MainActivity }
[success] Total time: 3 s, completed Dec 5, 2012 2:15:14 AM
> 

Hello, world!
f:id:takashabe:20121205023134p:plain
この時点でビルドに失敗する場合はproject/plugin.sbtとかBuild.scalaで指定されているpluginのバージョンが間違っている可能性があります。っていうか以前はよくありました。

IDEとの連携

ビルド出来ることは分かったので、次はIDE上にプロジェクトをimportする方法について見ていきます。
sbtでは各種IDE用の設定ファイルを生成するためのPluginが存在するので、まずはそのPluginを使うようにします。
以下のようにplugin.sbtに下4行を追加すればいいだけですが、空行を開ける必要があるので注意です。1.2.0-SNAPSHOTの部分は適宜最新バージョンを指定してください。*2
ちなみにプロジェクトごとに毎回書くのがだるい場合は~/.sbt/plugins/build.sbtあたりに書いておけばプロジェクト全体に勝手に適用されます。

diff --git a/project/plugins.sbt b/project/plugins.sbt
index e3163f8..a7580b7 100644
--- a/project/plugins.sbt
+++ b/project/plugins.sbt
@@ -1,3 +1,7 @@
 resolvers += Resolver.url("scalasbt releases", new URL("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns)

 addSbtPlugin("org.scala-sbt" % "sbt-android-plugin" % "0.6.2")
+
+resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
+
+addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0-SNAPSHOT")

次にsbtを起動した時にPluginのインストールが始まるので、以下のコマンドでIDE用のファイルが生成されます。

> gen-idea

## 色々生成している

> exit
λ ~/sandbox/sample/ → λ git master* → l
total 0
drwxr-xr-x   9 takashabe  staff  306 Dec  6 20:12 .
drwxr-xr-x   3 takashabe  staff  102 Dec  5 02:07 ..
drwxr-xr-x  13 takashabe  staff  442 Dec  6 20:16 .git
drwxr-xr-x  11 takashabe  staff  374 Dec  6 20:12 .idea
drwxr-xr-x   5 takashabe  staff  170 Dec  6 20:12 .idea_modules
drwxr-xr-x   6 takashabe  staff  204 Dec  6 19:58 project
drwxr-xr-x   4 takashabe  staff  136 Dec  5 02:07 src
drwxr-xr-x  12 takashabe  staff  408 Dec  6 20:12 target
drwxr-xr-x   4 takashabe  staff  136 Dec  6 20:12 tests

あとはIDEA上でプロジェクトをopenすればOKです。Scala Pluginはインストールしておく必要があります。
syntax highlightされなかったりpathが見つからないとか言われた時はProject StructureでJDK, Android jar, Scalaのpathを編集すれば何とかなるはずです。。。この記事書いてる時点でクリーンな環境が無かったのであばばば
とりあえず今日発表されたばかりのIDEA 12でも動作してます。

f:id:takashabe:20121206204737p:plain

継続的にビルドする

sbtではファイルを監視して変更があれば任意のコマンドを実行する機能があります。先頭に ~ つけるだけです。ちなみにセミコロンはコマンドの区切りです。

> ~;android:package-debug;android:start-device

実行するコマンドは何でも良いので ~test とかやれば自動的にテストを回せて素敵です。
ただし、project配下のファイルの変更は監視しないのでplugin.sbtとかに設定を追加した場合は手動でupdate/reloadする必要があります。

Google Playにデプロイする

ではこのsampleアプリもGoolge Playでヒットするアプリに育ったのでデプロイしてみようと思います。
証明書を作る工程は通常と同じです。*3 今回はalias_nameで証明書を作っているものと仮定して進めます。
証明書を作ったらproject/Build.scalaのkey値を証明書に合わせて変更します。

diff --git a/project/Build.scala b/project/Build.scala
index 13dc305..423ec0c 100644
--- a/project/Build.scala
+++ b/project/Build.scala
@@ -23,7 +23,7 @@ object General {
     proguardSettings ++
     AndroidManifestGenerator.settings ++
     AndroidMarketPublish.settings ++ Seq (
-      keyalias in Android := "change-me",
+      keyalias in Android := "alias_name",
       libraryDependencies += "org.scalatest" %% "scalatest" % "1.8" % "test"
     )
 }

次にsbt上で署名してrelease用のapkを生成します。
target/sample-0.1-market.apkがrelease用のapkです。

> ;android:package-release;android:prepare-market

## 略

Enter password for keystore/alias_name: **********
[info] Signed /Users/takashabe/sandbox/sample/target/sample-0.1.apk
[info] Aligned /Users/takashabe/sandbox/sample/target/sample-0.1-market.apk
[success] Ready for publication: 
[success] /Users/takashabe/sandbox/sample/target/sample-0.1-market.apk
[success] Total time: 6 s, completed Dec 6, 2012 9:24:50 PM

ただ、この状態でGoogle Playにデプロイしようとするとエラー内容もなく怒られちゃいます。
f:id:takashabe:20121206221318j:plain

これは以前どハマりしたのですが、どうやらデフォルトのiconのままだとダメっぽい。なので適当な画像を用意してmanifestを書き換えます。
画像を置く場所はいつもどおりres以下に置きます。

λ ~/sandbox/sample/src/main/res/ → λ git master* → l
total 0
drwxr-xr-x    8 takashabe  staff   272 Dec  6 21:33 .
drwxr-xr-x    6 takashabe  staff   204 Dec  6 21:35 ..
drwxr-xr-x@ 145 takashabe  staff  4930 Dec  6 21:33 drawable-hdpi
drwxr-xr-x    3 takashabe  staff   102 Dec  6 21:33 drawable-ldpi
drwxr-xr-x    3 takashabe  staff   102 Dec  6 21:33 drawable-mdpi
drwxr-xr-x    3 takashabe  staff   102 Dec  6 21:33 drawable-xhdpi
drwxr-xr-x    3 takashabe  staff   102 Dec  5 02:07 layout
drwxr-xr-x    3 takashabe  staff   102 Dec  5 02:07 values

AndroidManifest.xmlも上記の画像を読み込むように変更。

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index 62f45c8..f7f1b26 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -2,7 +2,7 @@
     package="com.takashabe.sample">

     <application
-        android:icon="@drawable/android:star_big_on"
+        android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:debuggable="true">

もう一度release用のapkを作成して、再度デプロイ!
f:id:takashabe:20121206221314j:plain

そういえばバージョン0.1のままでしたので怒りを抑えて整数に直します。
ここで普通ならばAndroidManifest.xmlを直接編集しますが、sbtプロジェクトの場合はproject/Build.scalaのkeyを編集します。*4
でもさっきiconを変えるときにmanifest見たけどバージョンかかれてなかったぞ・・・?とか思われるかもしれませんが、src/main/AndroidManifest.xmlは最終的なmanifestではなくてコンパイル時にsbtがパースして別のmanifestを生成します。*5

diff --git a/project/Build.scala b/project/Build.scala
index 423ec0c..2bfbd80 100644
--- a/project/Build.scala
+++ b/project/Build.scala
@@ -6,8 +6,8 @@ import AndroidKeys._
 object General {
   val settings = Defaults.defaultSettings ++ Seq (
     name := "sample",
-    version := "0.1",
-    versionCode := 0,
+    version := "1.0",
+    versionCode := 1,
     scalaVersion := "2.9.2",
     platformName in Android := "android-10"
   )

これでもう一度apkを生成してデプロイします。
f:id:takashabe:20121206221258j:plain

やりました。メガヒット間違いなしですね。*6


ほんとはテストとかjenkinsとかも書きたかったのですが長くなってきたのと体調がひどい感じなのでここで終わります。テストとかはまた今後書くかも。
明日のAdvent Calendarは表 @chun_ryoさん、裏 @Arigata3さんです。たのしみ!

*1:最近はJavaで書くことも多いようだ

*2:https://github.com/mpeltonen/sbt-idea

*3:http://techbooster.org/android/environment/1445/

*4:sbtでのバージョンとapkのバージョンを合わせたかったのだと思います。たぶん。

*5:target/scala-x.x.x/resource_managed/main/AndroidManifest.xml

*6:大人の事情により消しました。

Scalaでnullチェック

Scalaを書いているとnullを書くことは殆ど無いし書きたくないけど、Javaのライブラリを使おうとするとnullがぽこじゃか顔を出してくる。
nullが入る可能性があるオブジェクトはもちろんnullチェックしなければならない。そんな時はOptionに包んであげることでnullを隠蔽出来てScalaっぽく書けます。

scala> def isNull(v: Any) = Option(v) match {
     |   case Some(x) => println("nullじゃない何か")
     |   case None    => println("nullだ!")
     | }
isNull: (v: Any)Unit

scala> isNull(null)
nullだ!

scala> isNull(1)
nullじゃない何か

こんな感じでパターンマッチにかけることでnullチェック忘れもコンパイラで検知してくれるし便利ですね。

Play2.0をHerokuにデプロイして開発環境を整えるまでのメモ その2

前回 Play 2.0でHerokuにデプロイして開発環境を整えるまでのメモ その1 - takashabeのブログ 作成したPlayアプリケーションをHerokuにデプロイする手順について書いていきます。

HerokuToolbeltの準備

コンソールからHerokuにpushしたりするためのコマンドを導入します。
まずは Heroku | Cloud Application Platform のSign Upからアドレスを登録してアカウントを作成する。直感的なのですぐ分かるはず。
アカウント登録後、HerokuToolbeltのダウンロードリンクがあるはずなのでそれを落としてインストールする。MacならHomebrewにあるので、brew install heroku-toolbeltで入れても良い。
とりあえずheroku versionで確認してみる。

λ ~/ → heroku version 
heroku-toolbelt/2.33.0 (x86_64-darwin12.2.0) ruby/1.9.3

Herokuアプリケーションの作成

herokuコマンドを使ってHerokuにデプロイする用のアプリケーションを作っていきます。
heorkuではgitリポジトリをそのままpushしてデプロイする感じになるので、まずはgit initしてcommitまでやっておきます。
ちなみにplay newした時にideaやeclipseも対応してるいい感じの.gitignoreが付いてくるので幸福感があります。

λ ~/work/sandbox/kome/ → 
λ ~/work/sandbox/kome/ → git init
Initialized empty Git repository in /Users/takashabe/work/sandbox/kome/.git/
λ ~/work/sandbox/kome/ → λ git master* → 
λ ~/work/sandbox/kome/ → λ git master* → git add .
λ ~/work/sandbox/kome/ → λ git master* → git commit -am 'first commit'
[master (root-commit) 8763f6c] first commit
 12 files changed, 139 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 README
 create mode 100644 app/controllers/Application.scala
 create mode 100644 app/views/index.scala.html
 create mode 100644 app/views/main.scala.html
 create mode 100644 conf/application.conf
 create mode 100644 conf/routes
 create mode 100644 project/Build.scala
 create mode 100644 project/build.properties
 create mode 100644 project/plugins.sbt
 create mode 100644 public/images/favicon.png
 create mode 100644 public/javascripts/jquery-1.7.1.min.js
 create mode 100644 public/stylesheets/main.css
λ ~/work/sandbox/kome/ → λ git master → 

gitリポジトリを作ったらheroku createでHerokuアプリケーションを作ります。
playをHerokuで動かすときは --stack cedar を付けておきます。これはHerokuでの動作環境を指定するもので引数無しのBambooスタックだとRubyのみ、cedarスタックでScalaとかClojureとかで動きます。

λ ~/work/sandbox/kome/ → λ git master → heroku create --stack cedar
Creating quiet-atoll-4971... done, stack is cedar
http://quiet-atoll-4971.herokuapp.com/ | git@heroku.com:quiet-atoll-4971.git
Git remote heroku added
λ ~/work/sandbox/kome/ → λ git master → git remote -v
heroku	git@heroku.com:quiet-atoll-4971.git (fetch)
heroku	git@heroku.com:quiet-atoll-4971.git (push)
λ ~/work/sandbox/kome/ → λ git master → 

heroku createの結果を見て分かるようにremoteリポジトリにherokuが登録されます。

Herokuへのデプロイ

gitのherokuリポジトリに対してごにょごにょしてHerokuにpushすればそれがそのままデプロイになります。

λ ~/work/sandbox/kome/ → λ git master → git push heroku master
Counting objects: 24, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (24/24), 36.02 KiB, done.
Total 24 (delta 0), reused 0 (delta 0)

-----> Heroku receiving push
-----> Play 2.0 - Scala app detected
-----> Installing OpenJDK 1.6...done
-----> Building app with sbt

〜色々ダウンロードしてる〜

       [success] Total time: 0 s, completed Nov 12, 2012 4:59:31 PM
-----> Dropping ivy cache from the slug
-----> Discovering process types
       Procfile declares types            -> (none)
       Default types for Play 2.0 - Scala -> web
-----> Compiled slug size: 87.8MB
-----> Launching... done, v6
       http://quiet-atoll-4971.herokuapp.com deployed to Heroku

To git@heroku.com:quiet-atoll-4971.git
 * [new branch]      master -> master
λ ~/work/sandbox/kome/ → λ git master → 

デプロイが完了したらブラウザからHerokuのDashboardにアクセスしてOpen Applicationするか、コンソールからheroku openでブラウザが起動してデプロイしたアプリケーションにお目にかかれます。

f:id:takashabe:20121113020914p:plain

あれ?localでplay runしたときはもっとごちゃごちゃ表示されたんだけど?みたいに思ったかもしれませんがplay runだとDevモードで起動するためにWelcomeページが色々と読み込まれていたわけです。ちなみに本番用のProdモードで動かすときはplay startする。
heroku logsを見ると(Prod)で動いているのがわかります。

λ ~/work/sandbox/kome/ → λ git master → heroku logs
2012-11-12T16:57:42+00:00 heroku[slugc]: Slug compilation started
2012-11-12T16:59:49+00:00 heroku[slugc]: Slug compilation finished
2012-11-12T17:00:01+00:00 heroku[web.1]: Starting process with command `target/start -Dhttp.port=23532 -Xmx384m -Xss512k -XX:+UseCompressedOops`
2012-11-12T17:00:03+00:00 app[web.1]: Play server process ID is 2
2012-11-12T17:00:04+00:00 app[web.1]: [info] play - Application started (Prod)   // ←Prod !!!
2012-11-12T17:00:04+00:00 app[web.1]: [info] play - Listening for HTTP on port 23532...
2012-11-12T17:05:23+00:00 heroku[router]: GET quiet-atoll-4971.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=561ms status=200 bytes=468
2012-11-12T17:05:25+00:00 heroku[router]: GET quiet-atoll-4971.herokuapp.com/assets/images/favicon.png dyno=web.1 queue=0 wait=1ms service=51ms status=200 bytes=687
2012-11-12T17:07:02+00:00 heroku[router]: GET quiet-atoll-4971.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=16ms status=200 bytes=468
2012-11-12T17:07:02+00:00 heroku[router]: GET quiet-atoll-4971.herokuapp.com/assets/stylesheets/main.css dyno=web.1 queue=0 wait=4ms service=26ms status=404 bytes=0
λ ~/work/sandbox/kome/ → λ git master → 
λ ~/work/sandbox/kome/ → λ git master → play start
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Loading project definition from /Users/takashabe/work/sandbox/kome/project
[info] Set current project to kome (in build file:/Users/takashabe/work/sandbox/kome/)

(Starting server. Type Ctrl+D to exit logs, the server will remain in background)

Play server process ID is 18287
[info] play - Application started (Prod)  // ←Prod !!!
[info] play - Listening for HTTP on port 9000...

IDEとの連携

最後にIntellij IDEAで開発するための手順を見ていきます。いわゆるsbt-ideaのこと
Playでは最初からidea用のモジュールを内蔵しているので(typesafehub/sbt-idea?)追加でpluginの設定はいりません。っていうか手動で設定すると名前空間が衝突してbuildに失敗します( ꒪⌓꒪)

λ ~/work/sandbox/kome/ → λ git master → play
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Loading project definition from /Users/takashabe/work/sandbox/kome/project
[info] Set current project to kome (in build file:/Users/takashabe/work/sandbox/kome/)
       _            _ 
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/ 
             
play! 2.0.4, http://www.playframework.org

> Type "help play" or "license" for more information.
> Type "exit" or use Ctrl+D to leave this console.

[kome] $ idea
[info] Trying to create an Idea module kome
[info] Excluding folder target
[info] Created /Users/takashabe/work/sandbox/kome/.idea/IdeaProject.iml
[info] Created /Users/takashabe/work/sandbox/kome/.idea
[info] Excluding folder /Users/takashabe/work/sandbox/kome/target/scala-2.9.1/cache
[info] Excluding folder /Users/takashabe/work/sandbox/kome/target/scala-2.9.1/classes
[info] Excluding folder /Users/takashabe/work/sandbox/kome/target/scala-2.9.1/classes_managed
[info] Excluding folder /Users/takashabe/work/sandbox/kome/target/streams
[info] Created /Users/takashabe/work/sandbox/kome/.idea_modules/kome.iml
[info] Created /Users/takashabe/work/sandbox/kome/.idea_modules/kome-build.iml
[kome] $ 

今のところ単純に使うだけだとPlayとHerokuもゆるふわでいい感じです。

Play 2.0でHerokuにデプロイして開発環境を整えるまでのメモ その1

Scalaでwebアプリ書くならPlayだよねー、PlayならHerokuだよねーってことでPlayからHerokuを使う場合の作業メモ。

環境は以下の通り。

  • play 2.0.4
  • scala 2.9.2
  • sbt 0.12.1

Playのプロジェクトを作成する

ここDocumentation: Installing — Playframework からPlayのバイナリを落としてきてpathを通しておきます。
macならhomebrewからも落とせるのでそっちのほうが楽。
play helpコマンドで以下のように無事かわいらしいAAが見れたらOK
f:id:takashabe:20121106020408p:plain


次は play new myApplicationName でアプリケーションを作る。
途中で作成するテンプレが聞かれるが質問の通り1で作る。

λ ~/work/sandbox/ → play new kome  

〜略〜

What is the application name? 
> kome

Which template do you want to use for this new application? 

  1 - Create a simple Scala application
  2 - Create a simple Java application
  3 - Create an empty project

> 1

OK, application kome is created.

Have fun!

λ ~/work/sandbox/ → 
λ ~/work/sandbox/ → 


これで最低限動くものが出来ているのでとりあえずplay runして動かしてみる。

λ ~/work/sandbox/ → cd kome 
λ ~/work/sandbox/kome/ → play run
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Loading project definition from /Users/takashabe/work/sandbox/kome/project
[info] Set current project to kome (in build file:/Users/takashabe/work/sandbox/kome/)

--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on port 9000...

(Server started, use Ctrl+D to stop and go back to the console...)


ブラウザから localhost:9000 にアクセスして確認してみる。
f:id:takashabe:20121106022317p:plain
なんか出てきました。やりました。
ちなみにデフォルトだと9000番ポートで走るけど、terminalでplayとだけ叩いてplay consoleに入った後にrun [port] でポート番号指定して走らせることが出来る。

λ ~/work/sandbox/kome/ → 
λ ~/work/sandbox/kome/ → play    
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Loading project definition from /Users/takashabe/work/sandbox/kome/project
[info] Set current project to kome (in build file:/Users/takashabe/work/sandbox/kome/)
       _            _ 
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/ 
             
play! 2.0.4, http://www.playframework.org

> Type "help play" or "license" for more information.
> Type "exit" or use Ctrl+D to leave this console.

[kome] $ run 8080

--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on port 8080...

(Server started, use Ctrl+D to stop and go back to the console...)

また、ここでもしsbtのglobal pluginなどにmpeltonen/sbt-ideaを設定している場合、名前が衝突してbuildに失敗することがある。その代わり、ideaのプロジェクトを生成するときはplay consoleから idea とだけ打てばOK。
詳細はこのpull requestなどを Fix namespace collisions with original mpeltonen version of sbt-idea by rtyley · Pull Request #1 · typesafehub/sbt-idea · GitHub

λ ~/.sbt/plugins/ → 
λ ~/.sbt/plugins/ → cat build.sbt 
resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.1.0")
λ ~/.sbt/plugins/ → 
λ ~/.sbt/plugins/ → cd -0 
~/work/sandbox/kome
λ ~/work/sandbox/kome/ → play
[info] Loading global plugins from /Users/takashabe/.sbt/plugins
[info] Loading project definition from /Users/takashabe/work/sandbox/kome/project
[error] java.lang.NoSuchMethodError: org.sbtidea.SbtIdeaPlugin$.ideaSettings()Lscala/collection/Seq;
[error] Use 'last' for the full log.
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?


タイトルにherokuにデプロイとか書いちゃったけど、眠いのでheroku編は後日上げることにする。

IdeaVimでセミコロンを使ってコマンドモードに入る - その2

以前書いた IdeaVimでセミコロンを使ってコマンドモードに入る - takashabeのブログ だとInsertモードでも ; でcommandモードに入ってしまうという罠があった。
というか前回の記事書いてからずっとこの設定使っていたんだけど、今まで気づいてなかったという…。Java書いてたら一瞬で気づいただろうな(遠い目

モードごとにkeymapを設定

GUIからkeymapを設定するとIdeaVimのモードに関係なく適用されてしまうのが今回の問題。
そこでこちらの素敵なソリューションを導入します。
2012-07-20 - marsのメモ

このパッチが当たったIdeaVimでモードごとのkeymapを追加出来るようになるので、設定ファイルにこんな感じに書けば捗ります。


設定するモードの種類(KeyParser.MAPPING_hoge)はここに定義されてます。
ideavim/src/com/maddyhome/idea/vim/key/KeyParser.java at master · JetBrains/ideavim · GitHub