境界線上をゆけ

テクノロジーとビジネスの狭間で格闘する

説明と違う!MacのPlay初心者がver.2.3インストールでつまづくポイント

Ruby on Railsライクな開発フレームワークPlay Framework2.3を触ってみました〜。ちなみに開発環境はMac+Terminal+brewです。

Play Framework 2徹底入門 JavaではじめるアジャイルWeb開発

Play Framework 2徹底入門 JavaではじめるアジャイルWeb開発

Guide to Play2 Scala

Guide to Play2 Scala

Mac での Play20 開発環境構築は brew install play だけ - seratch's weblog in Japanese

これらの本とブログを読んでインストールしようと思ったのですが、

これ。

$ brew install play

Error: No available formula for play
Play 2.3 replaces the play command with activator:
  brew install typesafe-activator

You can read more about this change at:
  http://www.playframework.com/documentation/2.3.x/Migration23
  http://www.playframework.com/documentation/2.3.x/Highlights23

手順に従ってPlayをinstallしようとしてもこんな風に怒られてまう。

公式のドキュメントもいまいち手順が書いてない。
https://www.playframework.com/documentation/ja/2.3.x/Installing

以前はこれでダウンロードもインストールも出来たんでしょうが、Playもバージョンアップしているので、2.3系では違うコマンドになっていますね。

でぇ、2014年12月現在、Playのver.2.3はこうするのが正解らしい。

typesafe-activator をインストール

$ brew install typesafe-activator

==> Downloading http://downloads.typesafe.com/typesafe-activator/1.2.10/typesafe-activator-1.2.10.zip
######################################################################## 100.0%

activatorを起動してみる

$ activator

Detected MAC OSX launched script....
Swapping to UI
Getting com.typesafe.activator activator-launcher 1.2.10 ...
downloading file:...

ものすごい勢いでjarファイルがダウンロードされていきます。

新しいPlay アプリケーションの雛形を作成

※APP_NAMEは自分でつけてください。

$ activator new APP_NAME

...

Browse the list of templates: http://typesafe.com/activator/templates
Choose from these featured templates or enter a template name:
  1) minimal-akka-java-seed
  2) minimal-akka-scala-seed
  3) minimal-java
  4) minimal-scala
  5) play-java
  6) play-scala
(hit tab to see a list of all templates)

//上記6つからどれかを選んでコマンド。今回はscala
$ play-scala

OK, application "APP_NAME" is being created using the "play-scala" template.

Playサーバーを起動

// アプリケーションのディレクトリに移動
$ cd APP_NAME

// Playのサーバー(簡易的なもの)を起動
$ activator run

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

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

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

[info] Compiling 5 Scala sources and 1 Java source to /Users/.../APP_NAME/target/scala-2.11/classes...
[info] 'compiler-interface' not yet compiled for Scala 2.11.1. Compiling...
[info]   Compilation completed in 71.384 s
[info] play - Application started (Dev)

これでサーバーが立ち上がったはず。Playはローカル環境は9000番ポートを使うのでここにアクセス。

http://localhost:9000

そこでこの画面が出たら完了です。
f:id:tairosan:20141207153223p:plain
これで無事にPlayアプリケーションのサーバーが起動しました。
※初回は結構時間がかかりました。。。

ちなみに、サーバーを停止するならCtrl + Dです。(RailsはCtrl+Cだった)

TOPの文字をちょっと書き換えてみる

このサイトのTOPページへのアクセスは、routing定義に従ってまずは/app/controllersにあるapplicationコントローラーが受け取ります。その後、/app/viewにあるindex.scala.htmlをActionが呼び出してます。その時にString型のmessageを渡して@mainにtitle要素を挿入しつつ同階層にあるtemplateであるmain.scala.htmlに引数を2つ渡しています。

// app/controllers/application.scala
package controllers

import play.api._
import play.api.mvc._

object Application extends Controller {

  def index = Action {
    Ok(views.html.index("ここが画面左上の<h1>タグに挿入されます"))
  }

}

// app/views/index.scala.html
@(message: String)

@main("ここが<title>タグに挿入されます") {

    @play20.welcome(message)

}

これでもう一度、localhost:9000にアクセスすると、画面左上の文字とタブのtitleが変わっているはずです。さすがにRailsほど高速とはいきませんが、URIにアクセスすると自動でコンパイルをして表示してくれるのでサクサク開発できます。

今後、playというコマンドはほぼactivatorに置き換えて考えた方が良さそうですね。

Scalaに興味のあるエンジニアの方はこちらへどうぞ。