Play Framework 2.4 + ã³ã³ãã¤ã«æDI㧠Play Module ãå©ç¨ããæ¹æ³
Play Framework 2.4 ããªãªã¼ã¹ãããã®ã¯2015å¹´5æãªã®ã§ãããä»æ´æã¯ããã®ã§ããçµæ§ããã£ãï¼ããããããè¨äºãè¦å½ãããªãã£ãã®ã§ã¾ã¨ãã¦ãããã¨ã«ãã¾ãã
å¿ãã人ã¯æå¾ã®ã¾ã¨ãã ãèªãã°OKãããããªãã§ãã
Play Framework 2.4 ã® Dependency Injection
Play Framework 2.4 ã§ã¯ Dependency Injection ï¼ä»¥ä¸DIï¼ããã¬ã¼ã ã¯ã¼ã¯ã§ãµãã¼ãããã¾ããã â¦â¦ã¨ãããããããã¬ã¼ã ã¯ã¼ã¯ã®æ¨æºæ©è½ã§ã¬ãããªå©ç¨ãããããã«ãªãã¾ããã
ããã¥ã¢ã«ãèªãã¨ã2種é¡ã®DIãæ¡å ããã¦ãã¾ãã
- Runtime Dependency Injectionï¼å®è¡æã®åçãªDIï¼
- Compile Time Dependency Injectionï¼ã³ã³ãã¤ã«æã®éçãªDIï¼
åè
㯠Google Guice ãå©ç¨ããå®è£
ã§ã@Singleton
ãã @Inject
ããã®ã¢ããã¼ã·ã§ã³ãããããåºã¦ãã¦ããã Java ã£ã½ãã§ãï¼Guice ã Java åããªã®ã§å½ããåã§ããï¼ã
DIã®æ¹æ³ã¯æè¬ãã³ã³ã¹ãã©ã¯ã¿DIãã§ãä¾åæ§ã®æ³¨å
¥å
ã¨ãªãã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã®ãã©ã¡ã¼ã¿ã¨ãã¦ä¾åãããªãã¸ã§ã¯ããåãåãã¾ãã
誰ããã©ããã§è¨ã£ã¦ãã¾ãããè¨èªã¬ãã«ã§ã·ã³ã°ã«ãã³ãªãã¸ã§ã¯ããå®ç¾©ã§ãã Scala 㧠@Singleton
ã¨ããã¢ããã¼ã·ã§ã³ã«ã¯å°ãéåæãããã¾ãã
å¾è
ã¯ç¹ã«ããã¨è¨ã£ãæ©è½ãã©ã¤ãã©ãªã¯æ¨æºã§ã¯æä¾ããã¦ãããããã³ã³ãã¤ã«æã«å¥½ããªããã«DIããã°ãããããªããã¨ããã¹ã¿ã¤ã«ã§ãã
play.api.ApplicationLoader
ãç¶æ¿ããç¬èªã¯ã©ã¹ãä½æããæ¨æºã®ãã®ã¨å·®ãæ¿ãããã¨ã§å®ç¾ãã¾ãã
詳ããæ¹æ³ãªã©ã¯ããã¥ã¢ã«ãåç
§ãã¦ãã ããã
åå®å ¨æå¾ã¨ãã¦ã¯ãã¡ããå¾è ãé¸æããã®ã§ãããããã«ãã£ã¦è¦å´ããã¨ããã話ã
Play Module ã«ã¤ãã¦
Play Framework 2.4 ã§ã¯DIã®ä»ã«ã大ããªå¤æ´ãããã¤ãããããã®ãã¡ã®ã²ã¨ã¤ã Plugin ã«ä»£ãã£ã¦ç»å ´ãã Module ã§ãã
Plugin 㯠play.api.Plugin
ãç¶æ¿ã play.plugins ã«è¨è¿°ãããã¨ã§å©ç¨ãã¦ãã¾ããã Module 㯠play.api.inject.Module
ãç¶æ¿ã application.conf ã«æ¬¡ã®ãããªè¨è¿°ããããã¨ã§å©ç¨ãããã¨ã«ãªãã¾ããã
play.modules.enabled += "my.module.MyModule"
ä»åå©ç¨ãããã¨ãã Module 㯠flyway-play 㨠scalikejdbc-play-support ã®2ã¤ã§ããããããã README ã«ã¦ä¸è¨ã®ãããªè¨å®ããããã¨ã§ä½¿ããâ¦â¦ã¨æ¡å ããã¦ãã¾ããã â¦â¦ããããã¯ããã¾ã§ Guice ã«ããå®è¡æDIãå©ç¨ãã¦ããå ´åã®ã¿ã®ã話ã ã£ããã¨ããããã§ãã
Play Module ãã©ã®ããã«èªã¿è¾¼ã¾ããã
ãã¦ãã¨ããããã§åã¢ã¸ã¥ã¼ã«ã libraryDependencies
ã«è¿½å ã㦠play.modules.enabled += ...
ãè¨è¿°ãã¦ãä¸åã«ã¢ã¸ã¥ã¼ã«ãåä½ãã¦ããæ§åããªããã¨ããããã¾ããã
æåã¯åå ãããããå³å¾å·¦å¾ãã¦ããã®ã§ãããã©ãã«ããªããªãã®ã§ Playframework ã®ã³ã¼ãã調ã¹ã¦ã¿ã¾ããã
ã¾ã㯠application.conf ã® play.modules.enabled
ãã©ãã§èªã¿è¾¼ã¾ãã¦ãããã§ãã
play.modules.enabled
ã§æ¤ç´¢ãã¦ã¿ã㨠Modules
ãªãã¸ã§ã¯ãã® locate
ã¡ã½ããã§è¨å®ãèªã¾ãã¦ããããã§ãã
def locate(environment: Environment, configuration: Configuration): Seq[Any] = { val includes = configuration.getStringSeq("play.modules.enabled").getOrElse(Seq.empty) val excludes = configuration.getStringSeq("play.modules.disabled").getOrElse(Seq.empty) ...
次㫠Modules.locate
ãã©ãã§å¼ã³åºããã¦ãããã§ããããã㯠GuiceableModule
ãªãã¸ã§ã¯ãã® loadModules
ã¡ã½ããã§ãã
def loadModules(environment: Environment, configuration: Configuration): Seq[GuiceableModule] = { Modules.locate(environment, configuration) map guiceable }
ãªãã¸ã§ã¯ãåã« Guice ã¨åºã¦ãã¦å«ãªäºæãâ¦â¦ã¨æã£ãã®ãæã®éãããã«è¾¿ã㨠GuiceApplicationBuilder
ãã㦠GuiceApplicationLoader
ã«è¾¿ãçãã¾ããã
ã¤ã¾ãã³ã³ãã¤ã«æDIã使ã£ã¦ç¬èªã® ApplicationLoader
ãå®ç¾©ãå©ç¨ãã¦ããå ´åã¯ããã application.conf ã«è¨è¿°ãã¦ã Module ãèªã¿è¾¼ã¾ãããã¨ã¯ãªãã®ã§ãã
ã³ã³ãã¤ã«æDI + Play Module ãå©ç¨ããæ¹æ³
ããããæ¬é¡ã§ãã
ãã°ããæ©ãã ã®ã§ãããflyway-play 㨠scalikejdbc-play-support ã®åã¢ã¸ã¥ã¼ã«ã®ã³ã¼ããèªãã§ããã¨ãããã PlayModule
ã¨ããã¯ã©ã¹ãã PlayInitializer
ã¨ããåæåã¯ã©ã¹ãå¼ã³åºãã¦ãããã¨ããæ§é ã«ãªã£ã¦ãã¾ãã
ãããã® PlayModule
ã PlayInitializer
ã®ããã®ã¨ã³ããªãã¤ã³ããã¨ããã«ã³ã¸ã㤠PlayInitializer
ã®ã³ã³ã¹ãã©ã¯ã¿ããå¼ã³åºãã¦ãã¾ãã° Module ã®æ©è½ãå©ç¨ã§ãããã§ãã
ã¨ããããã§æ¬¡ã®ããã«ãªãã¾ãã
import play.api.ApplicationLoader.Context import play.api.BuiltInComponentsFromContext class MyComponents(context: Context) extends BuiltInComponentsFromContext(context) { ... new scalikejdbc.PlayInitializer(applicationLifecycle, configuration) new org.flywaydb.play.PlayInitializer(configuration, environment, webCommands) }
ä¸è¨ã¯ã³ã³ãã¤ã«æDIãæåã§ã´ãªã´ãªé å¼µã£ã¦ãã人åãã§ããmacwire ã使ã£ã¦ãããªã次ã®ããã«ããã°OKã§ãã
import com.softwaremill.macwire._ import play.api.ApplicationLoader.Context import play.api.BuiltInComponentsFromContext class MyComponents(context: Context) extends BuiltInComponentsFromContext(context) { ... wire[scalikejdbc.PlayInitializer] wire[org.flywaydb.play.PlayInitializer] }
macwire 便å©ã ããï¼
ã¾ã¨ã
- application.conf ã«
play.modules.enabled
ãè¨è¿°ãã¦ã¢ã¸ã¥ã¼ã«ãèªã¿è¾¼ã¾ããã®ã¯æ¨æºã®ApplicationLoader
ã使ã£ã¦ããå ´åã®ã¿ã - ã³ã³ãã¤ã«æDIã使ãå ´åã¯èªä½ã®
ApplicationLoader
ã®ä¸ã«èªåã§ãªãã¨ãããå¿ è¦ãããã - macwire 便å©ï¼å¤§äºãªãã¨ãªã®ã§ï¼ã
Scala + sbt-android + IntelliJ ã§å¿«é©Androidã¢ããªéçº
ã¾ããã
ãã®ã¨ã³ããªã¯ Android Advent Calendar 2013 ãªãã³ã« Scala Advent Calendar 2013 ã®6æ¥ç®(12/6)ã®è¨äºã«ãªãã¾ãã
åã Scala ã«è§¦ãå§ããã®ã¯2012å¹´ã®11æé ãªã®ã§ããããã¡ããã©1å¹´åãããã§ãã 以åãã Android ã¢ããªãéçºãã¦ãã身ã¨ãã¦ã¯å½ç¶ Android ã¢ããªã Scala ã§æ¸ããããªããã¨èããããã§ããå½æãScala Androidãã¨ãã§ã°ã°ã㨠ç¾ç¶ #Scala 㧠#Android ãããã®ã¯æã£ã¦ãã以ä¸ã«ç½ ãå¤ãã¨ãã話 ã¿ããã¾ã¨ãããã£ãããã¦ãåå¿è ãæããæããããã£ãã®ãè¦ãã¦ãã¾ãã
ãã®å¹´ã® Android Advent Calendar 2012 ã§ãããªè¨äºãæ¸ãã¦ã人ããããã ä»å¹´ã«å ¥ã£ã¦ãããªè¨äºãè¦ãããããã¦ã æ¹ãã¦ããããæå¤ã¨ç°¡åã«ã§ããããããï¼ãã¨æã£ã¦æãåºãããçµå±ãã£ã±ãè²ã ã¨ç½ ã«éã£ãã®ã§ãã®è¾ºã®ãã¦ãã¦ãã¾ã¨ãã¦ã¿ã¾ããã
æ¬è¨äºã®å 容
- Android Support Library, revision 19 ã使ç¨ã㦠API Level 9 ããµãã¼ããã¤ã¤ Fragment ã ActionBar ã使ç¨ããã¢ããªã Scala ã§æ¸ãããã®ç°å¢æ§ç¯
- Scala + sbt-android + IntelliJ ã«ããã¢ããªéçºã®ç°¡åãªèª¬æã¨å¾ ã¡åããç½
éçºç°å¢
éçºç°å¢ã®æ¦è¦ã¯æ¬¡ã®éãã
éçºç°å¢ãæ§ç¯ãã
Android SDK
ããããªãã¨å§ã¾ããªãã
å
¬å¼ãµã¤ã ããããããã®ãã©ãããã©ã¼ã åãã®ã¢ã¼ã«ã¤ãï¼ADT Bundle ãããªãæ¹ï¼ããã¦ã³ãã¼ããã¦é©å½ãªå ´æã«å±éãã¦ãã ããã
èªåã®å ´åï¼OS Xï¼ã¯ /opt/local/android-sdk-macosx
ã«è¨ç½®ãã¦ãã¾ãã
è¨ç½®å¾ã.bashrc ãªã .zshrc ãªããç·¨éãã¦è¨ç½®ãã¹ãç°å¢å¤æ° ANDROID_HOME
ã¨ã㦠export ãã¾ããã¤ãã§ã«ã³ãã³ãã«ããã¹ãéãã¦ããã¾ãã
export ANDROID_HOME=/opt/local/android-sdk-macosx export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
ãã®æ®µéã§ã¯åãã¼ã¸ã§ã³ã®SDKãæªå°å
¥ã®ç¶æ
ãªã®ã§ã¿ã¼ããã«ãã android update sdk -u
ãå®è¡ãã¦ä¸å¼ãã¦ã³ãã¼ããã¦ãã ããï¼çµæ§æéãæããã¾ãï¼ã
Maven 3.1.x
次㮠Maven Android SDK Deployer ãå©ç¨ããããã«å¿
è¦ã«ãªãã¾ãã
OS X ã®å ´å㯠brew install maven
ã§OKãä»ã®ç°å¢ã®ãã¨ã¯ããç¥ããªãã®ã§ãMaven ã¤ã³ã¹ãã¼ã«ãã¨ãã§ã°ã°ã£ã¦ãã ããã
3.0.x ã ã¨ãã¾ãåããªãã®ã§ 3.1.x ã使ç¨ããå¿
è¦ãããã¾ãã
Maven Android SDK Deployer
Android Support Library ãªã© Maven Central Repository ã«ãªãããã±ã¼ã¸ããã¼ã«ã«ã® Maven Repository ãã解決ã§ããããã«ãã¦ãããã¦ã¼ãã£ãªãã£ã§ãã
github ããææ°ã®ã½ã¼ã¹ã³ã¼ãä¸å¼ã git clone
ã㦠mvn install
ããã ãã§ãããªã«æéãæãããã«çµããã¯ãã§ãã
$ git clone [email protected]:mosabua/maven-android-sdk-deployer.git $ cd maven-android-sdk-deployer $ mvn install
sbt
Android ã®ãã«ãã¨è¨ãã°æè¿ã¯ãã£ã±ã Gradle ãªããã§ããä»å㯠Scala ã®å®çªãã«ããã¼ã« sbt ã使ç¨ãã¾ãã 大ããªçç±ã¨ãã¦ã¯ Scala ã§æ¸ãã Android ã¢ããªã±ã¼ã·ã§ã³ããã«ãããããã®ãã©ã°ã¤ã³ sbt-android ããã§ã«åå¨ããããã§ãã
OS X ã®å ´åã¯ããã brew install sbt
ã§OKã§ãã
ãã®ä»ã®ç°å¢ã®å ´åãå
¬å¼ãµã¤ãã§åç°å¢åãã®ããã±ã¼ã¸ãé
å¸ããã¦ããã®ã§ãã¡ãã使ãã°å¤åOKã§ãã
giter8
giter8 㯠Scala 製ã®ããã¸ã§ã¯ãçæãã¼ã«ã§ãã使ããªãã¦ãå°ãã¯ããªãã§ãããã£ãæ¹ãå§åçã«ä¾¿å©ã
ããã OS X ãªã brew install giter8
ã§ã¤ã³ã¹ãã¼ã«ã§ãã¾ãããã®ä»ã®ç°å¢ã®å ´å㯠Conscript ã使ã£ã¦ã¤ã³ã¹ãã¼ã«ããã®ããªã¹ã¹ã¡ããããã§ãããå
¬å¼READMEã§ãç´¹ä»ããã¦ãããã¡ãã®è¨äºï¼æ¥æ¬èªï¼ãåèã«ãªãããããªããã¨æãã¾ãã
[IntelliJ IDEA]
Android éçºè ã«ã¨ã£ã¦ã¯ Android Studio ã®ãã¼ã¹ã¨ãªã£ã¦ãããã¨ã§ã注ç®ããã¦ããæè¿æµè¡ã®IDEï¼ç·åéçºç°å¢ï¼ã§ãã Eclipseï¼ãããç¥ããªãåã§ããâ¦â¦ã
èªåã®å ´åã¯å ã Scala ã§ã®éçºç¨ã«ä½¿ã£ã¦ããã®ããã£ã¦ IntelliJ IDEA + Android Plugin + Scala Plugin ã§éçºãã¦ãã¾ãã ã©ã¡ãã®ãã©ã°ã¤ã³ãç¡æçã® Community Edition ã§ãµãã¼ãããã¦ããã®ã§ä»åã¯ç¡æçã§ååã§ããä»åã¯ã¨ãã£ã¿å ¼ãããã¬ã¨ãã¦ä½¿ç¨ãã¾ãã
æªå°å ¥ã®å ´åã¯ãã®ãã¼ã¸ãããã¦ã³ãã¼ããã¦ã¤ã³ã¹ãã¼ã«ãã¦ãã ããã Android Support/Android Designer ãã©ã°ã¤ã³ã¯æ¨æºã§ã¤ã³ã¹ãã¼ã«ããã¦ããã®ã§ Scala Plugin ã追å ããã ãã§ããã«ä½¿ãã¾ãã
ã¨ããã§ããã®è¨äºãæ¸ãã¦ããæä¸ã« IntelliJ IDEA 13 ããªãªã¼ã¹ããã¾ãããä»å㯠IntelliJ IDEA 12 ãã¼ã¹ã§è©±ãé²ãã¾ããIntelliJ IDEA 13 ã§ã¯ sbt ãæ¨æºã§ãµãã¼ãããããªã©æ°ã«ãªãæ°æ©è½ãå¤ãã®ã§æ©ã試ãããã§ãã
ããã¸ã§ã¯ããä½æãã
éçºç°å¢ãæ§ç¯ã§ããã giter8 ã使ã£ã¦ããã¸ã§ã¯ããä½æãã¾ãã ä»åã®ç®çã§ãããAndroid Support Library ã使ã£ãã¢ããªãã®ããã®ãã³ãã¬ã¼ãã GitHub ã§å ¬éãã¦ããã®ã§ä»åã¯ããã使ç¨ãã¾ããããã±ã¼ã¸åã¯ãcom.example.helloworldãããã¸ã§ã¯ãåã¯ãHello Worldãã¨ãã¾ããããã©ã«ãå¤ã§åé¡ãªãå ´åã¯ãªã«ãå ¥åããã«ã¨ã³ã¿ã¼ãã¼ãæ¼ãã°OKã§ãã
$ g8 shogogg/scala-android-app.g8 ï¼ä¸ç¥ã»ååèµ·åæã¯å¿ è¦ãªã©ã¤ãã©ãªçããã¦ã³ãã¼ãããã®ã§ã¡ãã£ã¨æéãæããï¼ Template for Android apps in Scala package [com.example.android.app]: com.example.helloworld name [My Android Project]: Hello World keyalias [alias]: <ãªãªã¼ã¹æã®ç½²åã«ä½¿ç¨ãããã¼ã¹ãã¢ã®ã¨ã¤ãªã¢ã¹> scalaVersion [2.10.3]: <ãã«ãã«ä½¿ç¨ãã Scala ã®ãã¼ã¸ã§ã³> versionCode [0]: <ã¢ããªã®ãã¼ã¸ã§ã³ï¼ç®¡çç¨ã»æ´æ°ï¼> version [0.1.0]: <ã¢ããªã®ãã¼ã¸ã§ã³ï¼è¡¨ç¤ºç¨ã»æååï¼> targetSdkVersion [19]: <ã¢ããªã® targetSdkVersion> mainActivity [MainActivity]: <ã¢ããªã®ã¡ã¤ã³ã¢ã¯ãã£ããã£å> minSdkVersion [9]: <ã¢ããªã® minSdkVersion> Template applied in ./hello-world
ããã©ã«ãã§ã¯ targetSdkVersion ãææ°ã® 19 [Android 4.4 KitKat], minSdkVersion ã 9 [Android 2.3.x Gingerbread] ã¨ãã¦ãã¾ãã
ãã£ã¬ã¯ããªæ§æãªã©
sbt-android ãã£ã¬ã¯ããªæ§æã¯ãããªã«ã³ã¸ã«ãªãã¾ãã
$ cd /path/to/hello-world $ tree . . âââ project â  âââ build.properties â  âââ build.scala â  âââ plugins.sbt âââ src âââ main âââ AndroidManifest.xml âââ res â  âââ layout â  â  âââ main.xml â  âââ values â  âââ strings.xml â  âââ styles.xml âââ scala âââ com âââ example âââ helloworld âââ MainActivity.scala âââ support âââ ActivitySupport.scala âââ AndroidFuture.scala âââ AndroidPromise.scala âââ AsyncSupport.scala âââ ScalaAsyncTask.scala âââ TypedViewContainer.scala âââ package.scala 11 directories, 15 files
Eclipse + ADT ãªã©ã§ä½æããã¢ããªã®ãã£ã¬ã¯ããªæ§æãªããã¨ã¯éåéãã¾ãã
src
ãã£ã¬ã¯ããªä»¥ä¸ã«ããã®ãã½ã¼ã¹ã³ã¼ãã¨ãªã½ã¼ã¹é¢é£ã®ãã¡ã¤ã«ã«ãªãã¾ãã
ã¡ãªã¿ã« src/main/java
ãã£ã¬ã¯ããªãä½æããã° Java ã®ã½ã¼ã¹ã³ã¼ããæ··å¨ããããã¨ãã§ãã¾ãã,
Assets ã使ç¨ãããå ´å㯠src/main/assets
ãã£ã¬ã¯ããªãä½æããã°OKã§ãã
src/main/scala/com/example/helloworld/support
以ä¸ã«ããã®ã¯è£å©ç¨ã®ã¯ã©ã¹ã§ããå¾ã§è§£èª¬ãã¾ãã
project
ãã£ã¬ã¯ããªä»¥ä¸ã«ããã®ããã«ãé¢é£ã®è¨å®ã»å®ç¾©ãã¡ã¤ã«ã§ãã以ä¸ãããããã«ã¤ãã¦ç°¡åã«è§£èª¬ãã¾ãã
project/build.properties
project/build.properties
ã§ã¯ä½¿ç¨ãã sbt ã®ãã¼ã¸ã§ã³ãæå®ãã¦ãã¾ãã
sbt.version=0.12.4
project/plugins.sbt
project/plugins.sbt
ã§ã¯ sbt ã§ä½¿ç¨ãããã©ã°ã¤ã³ãæå®ãã¦ãã¾ãã
resolvers += Resolver.url("scalasbt releases", new URL("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns) addSbtPlugin("org.scala-sbt" % "sbt-android" % "0.7") addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.2")
1è¡ç®ã¯ãªãã¸ããªã®è¿½å ã3,5è¡ç®ã¯ãããã sbt-android ãã©ã°ã¤ã³ã¨ sbt-idea ãã©ã°ã¤ã³ãããã¸ã§ã¯ãã«è¿½å ãã¦ãã¾ãã
sbt ã¤ã³ã¿ã¼ãã§ã¼ã¹ä¸ã§
plugins.sbt
ãã¡ã¤ã«ã§ã¯åã³ãã³ãã®éã«ç©ºè¡ãå¿
è¦ã«ãªãã®ã§ç·¨éããå ´åã¯æ³¨æãã¦ãã ããã
project/build.scala
project/build.scala
ã¯å種ãã«ãæ
å ±ãå®ç¾©ãããã¡ã¤ã«ã§ãã
é常æ§æã® Android ããã¸ã§ã¯ãã§ã¯ AndroidManifest.xml
ã«è¨è¿°ãã¦ãããããªå
容ã®ä¸é¨ããã¡ãã«è¨è¿°ãã¾ãã
ã¡ãã£ã¨é·ãã®ã§ããã«ã¯è¼ãã¾ãããå°ããç¯ä»ãªç¨åº¦ã«ã³ã¡ã³ããã¦ãããã®ã§åèã«ãã¦ãã ããã
src/main/AndroidManifest.xml
é常æ§æã® Android ããã¸ã§ã¯ãã§ã¯ã«ã¼ããã£ã¬ã¯ããªç´ä¸ã«ãã AndroidManifest.xml
ã§ãã sbt-android ãªããã¸ã§ã¯ãã§ã¯ src/main
以ä¸ã«é
ç½®ããã¦ãã¾ãã
ä¸é¨ã®å
容ã project/build.scala
ã«è¨è¿°ãã以å¤ã¯é常ã®ããã¸ã§ã¯ãã¨åæ§ã§ãã
ãã«ããã¦ã¿ã
g8 ã³ãã³ãã§çæãããããã¸ã§ã¯ããã£ã¬ã¯ããªã«ç§»åå¾, sbt
ã³ãã³ããå®è¡ã㦠sbt ã¤ã³ã¿ã¼ãã§ã¼ã¹ãèµ·åãã¾ãã
ããã¸ã§ã¯ãã git ã§ç®¡çããå ´åã¯ãã®ã¿ã¤ãã³ã°ã§ first commit ããã®ãããã¨æãã¾ãã
# ãã£ã¬ã¯ããªã«ç§»å $ cd ./hello-world # git ãªãã¸ããªãä½æãã¦ååã³ããã $ git init $ git add --all $ git commit -m 'first commit' # sbt ãèµ·å $ sbt [info] Loading project definition from /path/to/hello-world/project [info] Set current project to Hello World (in build file:/path/to/hello-world/) >
ãã® sbt ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§ã¯æ§ã ãªã¿ã¹ã¯ï¼ã³ãã³ãï¼ãå®è¡ãããã¨ãã§ãã¾ãã主ãªã¿ã¹ã¯ã¯æ¬¡ã®éãã
compile: å種ã½ã¼ã¹ã³ã¼ããã³ã³ãã¤ã«ã .class ãã¡ã¤ã«ãçæãã¾ãã apk: JAR, DEX, APKãã¡ã¤ã«ãçæãã¾ããæªã³ã³ãã¤ã«ã®å ´åã¯èªåã§ã³ã³ãã¤ã«ãå®è¡ãã¾ãã install: çæãããAPKãã¡ã¤ã«ãå®æ©ã«è»¢éãã¾ããAPKãçæããã¦ããªãå ´åã¯èªåã§çæãã¾ãã start: çæãããAPKãã¡ã¤ã«ã転éããå®æ©ä¸ã§ã¢ããªãèµ·åãã¾ããAPKãçæããã¦ããªãå ´åã¯èªåã§çæãã¾ãã reload: ãã«ãå®ç¾©ãã¡ã¤ã«ã®åèªè¾¼ãè¡ãã¾ãããã«ãå®ç¾©ãä¿®æ£ãããããå ´åã«ä½¿ç¨ãã¾ãã console: REPLãèµ·åãã¾ãã exit: sbt ã¤ã³ã¿ã¼ãã§ã¼ã¹ãçµäºãã¾ãã
éå½¢ã®ããã¸ã§ã¯ãã¯ãã®ã¾ã¾ãã«ãã§ããç¶æ
ãªã®ã§ start
ã³ãã³ãã§ã³ã³ãã¤ã« â APKçæ â å®æ©ã«è»¢é â å®è¡ã¾ã§ãè¡ã£ã¦ã¿ã¾ãã
> start ï¼ä¸ç¥ï¼ [info] Dexing /path/to/hello-world/target/classes-hello-world-compile-0.1.0.dex [info] Packaging /path/to/hello-world/target/hello-world-compile-0.1.0.apk [info] Installing hello-world-compile-0.1.0.apk [success] Total time: 30 s, completed 2013/12/06 18:07:50 >
å®æ©ä¸ã§ãããªç»é¢ã表示ãããã°æåã§ãã
ã¡ãªã¿ã« sbt ã®åã³ãã³ãï¼ã¿ã¹ã¯ï¼ã®åã«ãã«ãï¼~ï¼ãä»ããã¨ããã¡ã¤ã«ã®å¤æ´ãç£è¦ãã¦å¤æ´ãããªã¬ã¼ã«èªåã§ã¿ã¹ã¯ãå®è¡ãã¦ããã¾ãã ä¾ãã°
> ~compile
ã¨ãã¦ãããã¨ã§èªåã§ã³ã³ãã¤ã«ãèµ°ãã¾ãã便å©ã
IntelliJ IDEA ã§ããã¸ã§ã¯ããéã
gen-idea
ã¿ã¹ã¯ãå®è¡ãããã¨ã§ IntelliJ IDEA ã®ããã¸ã§ã¯ãã«å¿
è¦ãªã¡ã¿ãã¡ã¤ã«ãçæãããã¨ãã§ãã¾ãã
ãã®è¾ºã¯ããããããææ°ã® IntelliJ IDEA 13 ã§ã¯ãããªããªãããã
> gen-idea [info] Creating IDEA module for project 'Hello World' ... [info] Running compile:managed-sources ... [info] Extracting library compatibility-v7-appcompat-19.apklib [info] Generated /path/to/hello-world/target/scala-2.10/src_managed/main/AndroidManifest.xml [info] Generated 0 source files from 1 ApkLibs [info] Running AAPT for package com.example.helloworld [info] Wrote /path/to/hello-world/target/scala-2.10/src_managed/main/scala/com/example/helloworld/TR.scala [info] Running AAPT for package android.support.v7.appcompat [info] Resolving android.support#compatibility-v7-appcompat;19 ... [info] Excluding folder target [info] Created /path/to/hello-world/.idea/IdeaProject.iml [info] Created /path/to/hello-world/.idea [info] Excluding folder /path/to/hello-world/target/scala-2.10/aarlib_managed [info] Excluding folder /path/to/hello-world/target/scala-2.10/cache [info] Excluding folder /path/to/hello-world/target/scala-2.10/classes [info] Excluding folder /path/to/hello-world/target/scala-2.10/apklib_managed/res [info] Excluding folder /path/to/hello-world/target/resolution-cache [info] Excluding folder /path/to/hello-world/target/streams [info] Created /path/to/hello-world/.idea_modules/Hello World.iml [info] Created /path/to/hello-world/.idea_modules/Hello World-build.iml >
gen-idea
ã¿ã¹ã¯ãç¡äºã«å®äºããã IntelliJ IDEA ã§ããã¸ã§ã¯ããéãã¦ã¿ã¾ãã Open Project ãã hello-world ãã£ã¬ã¯ããªãé¸æããã°OKã§ãã
ãããã¬ã使ã
IntelliJ ãããããã¬ãèµ·åãã¦ã¿ã¾ãã ã¾ãã¡ãã¥ã¼ãã Run â Edit Configurations... ãé¸æãã¾ãã
å·¦ä¸ã® ï¼ ãã¯ãªãã¯ã㦠Android Application ãé¸æã
åé ç®ã®è¨å®ã¯æ¬¡ã®éãã
- Name ã«ã¯é©å½ã«ååãå ¥å
- Module ã¯ä»åä½æããããã¸ã§ã¯ãï¼Hello Worldï¼ãé¸æ
- Launche default Activity ã«ãã§ãã¯ãå ¥ãã
- Deploy Application ã®ãã§ãã¯ãå¤ã
- Target Device ã¯å¥½ã¿ã§
- Before launch ã® Make ãé¸æãã¦ãâããã¯ãªãã¯ãã¦åé¤ãã
- OK ãã¿ã³ãã¯ãªãã¯ãã¦å®äº
ããã ãã§ä½¿ããã°å®ç§ãªãã§ããããã¨ãã1ã«æè¨å®ãå¿ è¦ã§ããå·¦ã®ããã¸ã§ã¯ãããªã¼ã®ã«ã¼ããå³ã¯ãªãã¯ã㦠Open Module Settings ãã¯ãªãã¯ã
Modules â Android ã® Structure ã¿ãã« Manifest file ã¨ããé
ç®ãããã®ã§ããã /path/to/hello-world/target/scala-2.10/src_managed/main/AndroidManifest.xml
ã«å¤æ´ãã¾ãã
ããã§æºåã¯å®äºã§ãã Run â Debug 'Hello World' ãé¸æãã¦ãããã¬ãèµ·åãã¾ãã ãããªç»é¢ã表示ããã端æ«ä¸ã§ã¢ããªãèµ·åããã°æåã§ãã
ãã¡ãããã¬ã¼ã¯ãã¤ã³ããè¨å®ãããã¨ãã§ãã¾ãã
以ä¸ã§ç°å¢è¨å®ã¯å®äºã§ããããã§ã¢ããªéçºãã¹ã¿ã¼ãã§ãã¾ããï¼
å¾ ã¡åããç½
å®ã¯ãããã¾ã§è³ãã¾ã§ã«ããã¤ãã®ç½ ã«ãããã¾ããã
ã©ã¤ãã©ãªããã¸ã§ã¯ãã®ãªã½ã¼ã¹ãåç §ã§ããªãï¼æªè§£æ±ºï¼
Scala + sbt-android ã¯ãã«ãããã¸ã§ã¯ãã«å¯¾å¿ãã¦ãã¾ãã ãã¡ããã©ã¤ãã©ãªããã¸ã§ã¯ããä½æãããããå©ç¨ãããã¨ãã§ããã®ã§ããã¡ã¤ã³ã®ããã¸ã§ã¯ãããã©ã¤ãã©ãªããã¸ã§ã¯ãã®ãªã½ã¼ã¹ãåç §ãããã¨ãã§ãã¾ããï¼ãããããã°ï¼ã
Scala 㧠Android ã¢ããªãä½ããã¨æãç«ã£ãã¨ã Android Support Library v7 appcompat ã使ããã¨ã¯å¿ é æ¡ä»¶ã§ããã ããã Android Support Library v7 appcompat 㯠ActionBar ã«å¯¾å¿ããããã«é常ã®æé ã§ã¯ã©ã¤ãã©ãªããã¸ã§ã¯ãã¨ãã¦åç §ãããã®ãªã½ã¼ã¹ã使ãå¿ è¦ãããã¾ãã
ä»å㯠Maven Android SDK Deployer ã使ã£ã¦ãã¼ã«ã«ãªãã¸ããªãæ§ç¯ããä¾åé¢ä¿ã« jar 㨠apklib ãå«ãããã¨ã§å¯¾å¿ãã¾ããã
Scala ã® Future ã¯ä½¿ããªãï¼ä½¿ãã¥ããï¼
Scala å¢ã¯ãåããã¨æãã¾ãã Scala ã«ã¯éåæå¦çãç°¡åã«æ±ãã scala.concurrent.Future ã¨ããã¯ã©ã¹ãããã¾ãã
ä¸è¨ã®æ§ã« future { ... }
ã§æ¬ã£ãé¨åãå¥ã¹ã¬ããã§å®è¡ããããã®çµæã使ã£ã¦å¥ã®è¨ç®ãè¡ããã¨ããã³ã¼ããä¸è¨ã®æ§ã«ç°¡åã«æ¸ããã¨ãã§ãã¾ãã
import scala.concurrent._ import ExecutionContext.Implicits.global val x: Future[String] = future { "Hello, %s".format("World") } x onSuccess { msg => println(msg) }
ã¨ãããããã Android ã§ä½¿ããã¨æã£ãã¨ãã«ä¸ç¹åé¡ãããã¾ãã
ãã㯠Future
ã使ã£ãå¦ç㯠onSuccess
ã onFailure
, onComplete
ã§å®è¡ãããå¦çãå¥ã¹ã¬ããã§è¡ãããï¼UIã¹ã¬ããã§ãªããã¨ããåé¡ã§ãã
Android ã§ã¯UIã«é¢ããå¦çã¯ã¡ã¤ã³ã¹ã¬ããã§ããUIã¹ã¬ããã§è¡ãå¿
è¦ãããã¾ãã Future
ã使ãã¨UIã¹ã¬ããã«å¦çã帰ã£ã¦ãããã¨ã¯ããã¾ããã
ãã¨ãªãã AsyncTask
ã使ãã¾ãããã
2013-12-10 追è¨: AsyncTask
ã Activity#runOnUiThread
ã使ã£ã ExecutionContext
ãç¬èªå®è£
ãã onSuccess
ã®å¼æ°ã«æ示çã«æå®ããã° Future
ã使ã£ã¦éåæã«å¦çããã¤ã¤ onSuccess
ã UIã¹ã¬ããä¸ã§å®è¡ã§ããã¿ããã§ãï¼åè: @OE_uia ããã® Gistï¼ã
ã¨ããã AsyncTask ãç´ ç´ã«ã¯ä½¿ããªã
Future
ã使ãã¥ãããã¨ããããã¬ãã«ãªããã®ãæã®é, AsyncTask
ã使ãã®ã«ãç½ ãå¾
ã¡åãã¦ãã¾ããã
ã¾ãæåã¯æ¬¡ã®ããã«ç´ ç´ã« AsyncTask
ãç¶æ¿ããã¯ã©ã¹ãä½æãã¾ããã
import android.os.AsyncTask class SomeAsyncTask extends AsyncTask[Void, Void, String] { def doInBackground(params: Void*): String = "Hello, %s".format("World") override def onPostExecute(result: String): Unit = { /* Something to do */ } } val task = new SomeAsyncTask task.execute()
ããããã«ãããå®è¡ãã㨠AbstractMethodError
ãçºçã doInBackground ã¡ã½ããããªã ã¨æããã¾ãã
å®ç¾©ãã¦ãã®ã«ãé¢ãããããã«ããéã£ã¦ããã®ã«ãé¢ããããã§ãã
åå 㯠Scala 㨠Java 㧠ã¸ã§ããªã¯ã¹ï¼åãã©ã¡ã¼ã¿ï¼ã使ã£ãå¯å¤é·å¼æ°ã®ãã«ãçµæãç°ãªã ãã¨ã§ãã 詳ãããã¨ã¯ãã¡ãã®ãã¼ã¸ã詳ããã§ãã
解決æ¹æ³ã¨ãã¦ã次ã®ãããªã¯ã©ã¹ãå®ç¾©ãã¦ãã¡ããç¶æ¿ãããã¨ã§åé¡ãªã AsyncTask
ã使ããã¨ãã§ãã¾ãã
import android.os.AsyncTask abstract class ScalaAsyncTask[Params,Progress,Result] extends AsyncTask[Params, Progress, Result] { // doInBackground ã§ã¯ Scala ã§æ°ãã«å®ç¾©ãã asyncAction ã¡ã½ããã«å¦çãå§è²ãã final def doInBackground(params: Params*): Result = asyncAction(params: _*) // onProgressUpdate ã§ãåæ§ã« Scala ã§æ°ãã«å®ç¾©ãã onProgress ã«å¦çãå§è²ãã override final def onProgressUpdate(progress: Progress*) = onProgress(progress: _*) // Scala ã§æ°ããªã¡ã½ãããå®ç¾©ãã // doInBackground/onProgressUpdate ã¯ãªã¼ãã¼ã©ã¤ãããããã¡ãããªã¼ãã¼ã©ã¤ããã¦ä½¿ç¨ãã def asyncAction(params: Params*): Result def onProgress(progress: Progress*): Unit = {} }
å ·ä½çã«ã¯å¯å¤é·å¼æ°ã®é¨åãåãã©ã¡ã¼ã¿ã®ã¾ã¾ãªã¼ãã¼ã©ã¤ãããå®éã«ä½¿ç¨ãããµãã¯ã©ã¹ã§å®ç¾©ã»ãªã¼ãã¼ã©ã¤ãããã®ã¯ Scala ã§æ°ãã«å®ç¾©ããã¡ã½ããã«ãããã¨ã§ãã«ãçµæã®å·®ããªããã¦ãã¾ãã
ä»åç¨æãã g8 ãã³ãã¬ã¼ã ã«ã¯ä¸è¨ã® ScalaAsyncTask
ãæ¹è¯ããã¯ã©ã¹ãå«ã¾ãã¦ãã¾ãã
ã¾ãã以ä¸ã®ããã«ãã¦åç´ãªéåæå¦çã§ããã°åè¿°ã® Future
風ã«ï¼ã¨ããã㨠Scala ã¬ãå¢ããããµã«ãªé£ãã§ãããã§ããâ¦â¦ï¼ç°¡åã«æ¸ããããã«ãã¦ããã¾ãã
import android.widget.Toast import com.example.helloworld.support._ val x = async { "Hello, %s".format("World") } x onSuccess { text => Toast.makeText(context, text, Toast. LENGTH_SHORT).show() }
æå¾ã«
ç¾å¨, Android ã¢ããªãé»è©±å¸³Rãã Scala ã§æ¸ãç´ãã¦ãã¾ãã æåã«æ¸ããã¨ããå°ãåã¾ã§ Scala 㧠Android ã¢ããªãä½ãï¼ãã¿æ±ãã§ããããå人çãªæ触ã§ã¯æå¤ã«å®ç¨çï¼ã«ãªã£ãï¼ãªããããªãããªã¼ã¨æãã¾ãã ãªã«ãã Scala ã¯æ¸ãã¦ãã¦æ¥½ããã§ãã
2014å¹´ã¯ãã£ã¨ Scala ãæµè¡ãã¾ãããã«ï¼ ãã¨ããã®ä¸ãã Gingerbread æè¼ç«¯æ«ãé§éããã¾ãããã«ï¼ï¼
åèURLä¸è¦§
AngularJS ã® $locationProvider.html5mode ã«ã¤ãã¦
AngularJS ã§æ§ç¯ããã¢ããªã±ã¼ã·ã§ã³ã§ $locationProvider.html5Mode ãã©ãè¨å®ãããå°ä¸æéæ©ãã çµæãã¾ã¨ãã¦ã¿ã¾ããã
å°å ¥
AngularJS ã§ã¯ $routeProvider ã使ç¨ãã¦ã¯ã©ã¤ã¢ã³ããµã¤ãã§ã®ã«ã¼ãã£ã³ã°ãå¯è½ã§ãã
angular.module('app').config(['$routeProvider', function($routeProvider) { $routeProvider .when('/aaa/', {controller: 'AaaController', templateUrl: 'aaa.html'}) .when('/bbb/', {controller: 'BbbController', templateUrl: 'bbb.html'}); }]);
ãããªã«ã³ã¸ã§æå®ãããã¨ã§URLã«å¿ã㦠Controller 㨠View ãå¤æ´ãããããã«ãªãã¾ãã便å©ã ãï¼
ã¨ããã§ããã®URLã§ããæ¨æºã§ã¯ãããªã«ã³ã¸ã«ãªãã¾ãã
http://www.example.com/#/aaa/
ãã㯠Hashbang Mode ã¨å¼ã°ããããã·ã¥(#)以éã®é¨åãã¢ããªã±ã¼ã·ã§ã³ã®ãã¹ã¨ãã¦å©ç¨ããã¢ã¼ãã§ãã ã¡ãªã¿ã« Hashbang ã¨ã¯ "#!" ã®ãã¨ã§ãä¸æåã« Ajax ãå¤ç¨ãããµã¤ãã§URLã«ãã使ããã¦ãã¾ããï¼ãªãå¤ç¨ãããç¾å¨å»ãã¤ã¤ããã®ã㯠"Hashbang URL" ã¨ãã§ã°ã°ãã¾ãããï¼ã
AngularJS ã®æ¨æºã§ã¯ "!" ã¤ã¾ãbangé¨åããªãã®ã§ãªãã ãã¡ãã£ã¨éåæãããã¾ãã ã¡ãªã¿ã« hashPrefix ã¨ããè¨å®ããããã¨ã§æ£å½ãª(?) Hashbang ã«ãããã¨ãã§ãã¾ãã
angular.module('app').config(['$locationProvider', function($locationProvider) { $locationProvider.hashPrefix('!'); }]);
æ¬é¡
åè¿°ã® Hashbang ãå©ç¨ããURLã¯ç¾å¨ã§ã¯ããã¾ã使ãã¹ãã§ã¯ãªããã¨è¨ããã¦ãã¾ãã ã¾ã HTML5 㧠pushState ã¨ããæ©è½ãæ¡ç¨ãã Hashbang ã使ããã¨ããã©ã¦ã¶ãã¹ããªãè¨é²ãã¤ã¤ã¯ã©ã¤ã¢ã³ããµã¤ãã§ç»é¢ãæ¸ãæãããã¨ãã§ããããã«ãªãã¾ããã
AngularJS ã§ã¯ $locationProvider.html5Mode ãå¤æ´ãããã¨ã§ Hashbang Mode ãã HTML5 Mode ã«å¤æ´ãããã¨ãã§ãã¾ãã
angular.module('app').config(['$locationProvider', function($locationProvider) { $locationProvider.html5Mode(true); }]);
Hashbang Mode 㨠HTML5 Mode ããããã§URLã¯æ¬¡ã®ããã«ãªãã¾ãã
# Hashbang Mode http://www.example.com/#/aaa/ # HTML5 Mode http://www.example.com/aaa/
ãã ã pushState ã«å¯¾å¿ãã¦ããªã IE9 ãªã©ã®ãã©ã¦ã¶ãã¾ã ã¾ã ç¾å½¹ã§ãã AngularJS ã§ã¯ãããã£ã pushState æªå¯¾å¿ãã©ã¦ã¶ã®å ´åã¯èªå㧠Hashbang Mode ã«ãã©ã¼ã«ããã¯ãã¦ããã¾ããè¶ ä¾¿å©ã ãï¼
Hashbang Mode 㨠HTML5 Mode ã®éã
å è¿°ã®éã Hashbang ã使ç¨ããURLã¯å»ãã¤ã¤ããã¾ããâ¦â¦ããHTML5 Mode ãä¸è½ã¨ããããã§ã¯ããã¾ããã
å ã»ã©ã®ããã« HTML5 Mode ã使ã㨠http://www.example.com/aaa/ ã®æ§ã«URLã«ããã·ã¥ãå«ã¾ããªããªãã¾ãã ãã®çµæãHashbang Mode 㨠HTML5 Mode ã§æ¬¡ã®ãããªéããç¾ãã¦ãã¾ãã¾ãã
# Hashbang Mode http://www.example.com/#/aaa/ -> WEBãµã¼ãã¼ www.example.com ã® / ã«ãªã¯ã¨ã¹ããé£ã¶ # HTML5 Mode http://www.example.com/aaa/ -> WEBãµã¼ãã¼ www.example.com ã® /aaa/ ã«ãªã¯ã¨ã¹ããé£ã¶
ã¤ã¾ã次ã®éãã
- Hashbang Mode ã§ã¯ã¯ã©ã¤ã¢ã³ããµã¤ãã§ãã¼ã¸ãçæããã°OKã
- HTML5 Mode ã§ã¯ãµã¼ãã¼å´ã§ /aaa/ ã«å¯¾å¿ãããã¼ã¸ãçæããå¿ è¦ããããæè¬ pjax ã«ãµã¼ãã¼å´ã§å¯¾å¿ããªãã¦ã¯ãªããªãã
å ´åã«ãã£ã¦ã¯å¾è ã¯ã¨ã¦ãé¢åã§ãã
Hashbang URL ã®åé¡ç¹ã¨ã¯ï¼
ã¨ãã㧠Hashbang ã使ãã¹ãã§ã¯ãªãçç±ã¨ã¯ãªãã§ãããããåèãµã¤ãã«ããã°ãã£ãã次ã®ãããªã«ã³ã¸ã§ãã
- GoogleBot 以å¤ã® Hashbang ã«å¯¾å¿ãã¦ããªãã¯ãã¼ã©ã¼ãããããªã©ãã³ã³ãã³ãã«ã¢ã¯ã»ã¹ã§ããªããªã
- ãã©ã¦ã¶å¤ã®ãã£ãã·ã¥ãã§ããªããªã
- Microformats ã使ãé£ããªã
- Facebook ã® Like widgets ãªã©ãé£ãããªã
- Referer ã«ãã¼ã¸ã¬ãã«ã®æ å ±ãç¡ããªã
- JavaScript ã«ã¨ã©ã¼ãããã¨ãªã«ã表示ãããªã
- arrayã®çµãã«,ãæ®ã£ã¦ããã ãã§IEå©ç¨è ã«ã¯çã£ç½ãªç»é¢ããã¿ããªããªã
- éçºè ã console.log ãééãã¦æ®ãã¦ãã¾ãã¨ã»ã¨ãã©ã®ã¦ã¼ã¶ã¼ï¼IEå©ç¨è ï¼ãçã£ç½ãªç»é¢ãè¦ããã¨ã«ãªã
- ãµã¤ã管çè ã®ã³ã³ããã¼ã«ä¸ã«ãªãåºåã®JavaScriptã«ã¨ã©ã¼ãããã¨ã³ã³ãã³ããè¦ããªããªã
çµè«ï¼ã©ã¡ãã®ã¢ã¼ãã使ãã¹ãã
å ã»ã©ã®åèãµã¤ãã«ãã»ã¼åãäºãæ¸ãã¦ããã¾ããã次ã®ããã«èãããã¨ãã§ããã¨æãã¾ãã
- ååãã¼ã¸ãªã©SEOçãéè¦ã«ãªãæè¬ãã³ã³ãã³ããã®å ´åã¯ç©æ¥µçã« HTML5 Mode ãæ¡ç¨ããã¹ãã
- 管çç»é¢ãããã©ã¼ã ãªã©ã³ã³ãã³ãã¨ããããã¯ãã¢ããªã±ã¼ã·ã§ã³ãã¨å¼ã¶ã¹ããã¼ã¸ã®å ´åã¯å ´åã«ãã£ã¦ Hashbang Mode ã®æ¡ç¨ãæ¤è¨ããã
- ãã ããä¸è¬ã«å ¬éããããåãåãããã©ã¼ã ãªã©ã§ãå¤é¨ã®åºåç¨ã¹ã¯ãªãããªã©ãå©ç¨ããå ´åã¯ç´°å¿ã®æ³¨æãæãï¼ã§ãããªã HTML5 Modeï¼ã使ãã
以ä¸ã
åè
VirtualBox + nginx ã§éçãã¡ã¤ã«ãæ£å¸¸ã«éä¿¡ãããªãã¨ãã¯
Vagrant + Chef Solo ã使ã£ã¦ VirtualBox ä¸ã«éçºç°å¢ãæ§ç¯ãããã©ããªã«ãã .js ã .css ãªã©ã®éçãã¡ã¤ã«ãéä¸ã§éåãã¦æ£å¸¸ã«éä¿¡ããã¦ããªãï¼åä¿¡ã§ããªãï¼ã
調ã¹ã¦è¦ãã¨ãããã©ããã nginx ãå©ç¨ãã¦ãã sendfile ã¨ããæ©è½ã VirtualBox ã¨ç¸æ§ãæªããããããããªãã㧠nginx.conf ã次ã®ããã«ä¿®æ£ãã¦åèµ·åããã¨ãããç¡äºã«éçãã¡ã¤ã«ãèªããããã«ãªã£ãã
nginx.conf ãä¿®æ£åã
sendfile on
nginx.conf ãä¿®æ£å¾ã
sendfile off
ãã§ãããã§ããã
Vagrant ãå°å ¥ãã
WEB+DB PRESS vol.75 ã§ãç´¹ä»ããã¦ãã Vagrant ãé ãã°ããªããå°å ¥ãã¾ããã Vagrant ã®å°å ¥è¨äºãªãã¦å··ã«æº¢ãã¦ãã¾ãããè¨äºãå¤ãã£ãããã¦ããªãæ¯ãåãããã®ã§èªåã§ãã¾ã¨ãã¦ã¿ããã¨ã«ãã¾ããã
ãã¤ã³ã
- gem ã使ããå ¬å¼ã®ããã±ã¼ã¸ããã¤ã³ã¹ãã¼ã«ãã
VirtualBox ã 4.2.14 ã«ã¢ããã°ã¬ã¼ããã¦ã¯ãããªã- 2013-07-09 追è¨ï¼VirtualBox 4.2.16 ããªãªã¼ã¹ããã¾ããï¼åèï¼ãVirtualBox 4.2.16 + Vagrant 1.2.2 ã®çµã¿åããã§æ£å¸¸ã«åä½ãããã¨ã確èªãã¾ããã
Vagrant ã¨ã¯ï¼
Vagrant ã¯ä»®æ³ãã·ã³ã®ç«ã¡ä¸ããèªååããã³ãã³ãã©ã¤ã³ãã¼ã«ã§ãã å ã 㯠Oracle VirtualBox åãã®ãã¼ã«ã ã£ãããã§ãããç¾å¨ã§ã¯ VMWare ã§ã使ããããã«ãªã£ãã¿ããã§ãã
ã¤ã³ã¹ãã¼ã«
å ¬å¼ã®ã¤ã³ã¹ãã¼ã«ããã±ã¼ã¸ã使ãã¾ãããã
2013å¹´6æç¾å¨ Vagrant ã®ææ°ãã¼ã¸ã§ã³ã¯ 1.2.2 ã§ãããå°å
¥è¨äºã«ãããã gem install vagrant
ã§å°å
¥ããå ´åå¤ã 1.0.7 ãã¤ã³ã¹ãã¼ã«ããã¦ãã¾ãã¾ããã
å¤ããã¼ã¸ã§ã³ã使ç¨ããã¨ããã®å¾ã®æé ã§ãã¦ã³ãã¼ããã box ãã¡ã¤ã«ã«äºææ§ããªãããé·æéå¾ ã£ãæãå¥ã«å¤±æãã¦ä½ãæ®ããªãã¨ããæ²æ¨ãªç®ã«éãã¾ãï¼éãã¾ããï¼ã
ã¾ã gem ã使ãããã« OS X + Homebrew ç°å¢ã§ ruby ãå°å
¥ããã®ã§ãããã®ç¶æ
㧠gem install vagrant
ãã¦ã vagrant ã³ãã³ãã«ãã¹ãéãããå¥éä½æ¥ãå¿
è¦ã«ãªããªã©é常ã«æéãæããã¾ãã
box ã追å ãã
Vagrant ã§ã¯æ°ããä»®æ³ãã·ã³ãä½æããéãéå½¢ã¨ãªã box ã¨å¼ã°ãããã®ãå¿ è¦ã¨ãªããããããããã¼ã«ã«ãã·ã³ã« box ã追å ããã¦ããªãã¦ã¯ãªãã¾ããã ãããã® box ã¯æ§ã ãªOSã»ãã¼ã¸ã§ã³ã»æ§æã®ãã®ãå ¬éããã¦ããããã®ä¸è¦§ã http://www.vagrantbox.es/ ã§è¦ããã¨ãã§ãã¾ãã
box ã追å ããããã®ã³ãã³ãã¯æ¬¡ã®éããboxåã¯å¥½ãã«æå®ã§ãã¾ãã
$ vagrant box add <boxå> <url>
ä»å㯠VirtualBox åãã® CentOS 6.4 x86_64 Minimal ãå°å ¥ãã¦ã¿ã¾ãã ãªãå種 box ãã¡ã¤ã«ã¯å°ãããã®ã§ãç´300MBç¨åº¦ããã®ã§ããªãã¹ãé«éãªéä¿¡ç°å¢ã§è¡ãäºããªã¹ã¹ã¡ãã¾ãã
$ vagrant box add centos-6.4-x86_64 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box Downloading or copying the box... Extracting box...te: 684k/s, Estimated time remaining: 0:00:01)) Successfully added box 'centos-64-x86_64' with provider 'virtualbox'! $ vagrant box list centos-64-x86_64 (virtualbox)
Vagrantfile ãä½æãã
次ã®ã³ãã³ããå®è¡ããã¨ã«ã¬ã³ããã£ã¬ã¯ããªã« Vagrantfile ã¨ããååã®ãã¡ã¤ã«ãä½æããã¾ããä»®æ³ãã·ã³ãä½æãããããã§ã¯ããã¾ããã
$ vagrant init centos-64-x86_64 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
Vagrantfile ã«ã¯ãã©ããªä»®æ³ãã·ã³ãè¨å®ããã®ããã¨ãã£ãè¨å®æ å ±ã Ruby ã§è¨è¿°ããã¦ãã¾ãã ä»®æ³ãã·ã³ã®ä½æãèµ·åã¯ãã® Vagrantfile ã®ãããã£ã¬ã¯ããªã«ç§»åãã¦ããå®è¡ãããã¨ã«ãªãã¾ãã
ä»®æ³ãã·ã³ãèµ·åãã
ä»®æ³ãã·ã³ãèµ·åãããå ´å㯠Vagrantfile ã®ãããã£ã¬ã¯ããªã«ç§»åãã¦ãã次ã®ã³ãã³ããå®è¡ãã¾ãã ååã«å®è¡ããå ´åã®ã¿èªå㧠env_1234567890 ã¿ãããªååã®ä»®æ³ãã·ã³ãä½æããã¾ãã
Vagrantfile ãç¹ã«ç·¨éããªãå ´åãããã¯ã¼ã¯ã¢ããã¿ã¯ NAT ã«è¨å®ãããSSHæ¥ç¶ãè¡ãããã«ã²ã¹ãã®22çªãã¼ããããã¹ãã®2222çªãã¼ãã¸ã®ãã©ã¯ã¼ãã£ã³ã°ãè¨å®ããã¾ãã
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... [default] Importing base box 'centos-64-x86_64'... [default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] Configuring and enabling network interfaces... [default] Mounting shared folders... [default] -- /vagrant
ä»®æ³ãã·ã³ã«SSHã§æ¥ç¶ãã
ç¡äºã«ä»®æ³ãã·ã³ãèµ·åããã次ã®ã³ãã³ãã§SSHæ¥ç¶ãããã¨ãã§ãã¾ãã
ããã©ã«ãã®ç¶æ ã§ã¯ Vagrantfile ãè¨ç½®ãããã£ã¬ã¯ããªã VirtualBox ã®å ±æãã©ã«ãã¼æ©è½ã使ç¨ã㦠/vagrant ã«ãã¦ã³ãããã¦ãã¾ããVagrantfile ãç·¨éãããã¨ã§å ±æãã©ã«ããå¤æ´ã»è¿½å ãããã¨ãã§ãããã§ãã
$ vagrant ssh Welcome to your Vagrant-built virtual machine. [vagrant@localhost ~]$ pwd /home/vagrant [vagrant@localhost ~]$ ls -l /vagrant åè¨ 8 -rw-r--r-- 1 vagrant vagrant 4359 6æ 27 09:01 2013 Vagrantfile [vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed.
ä»®æ³ãã·ã³ãåæ¢ãã
ä»®æ³ãã·ã³ãåæ¢ããå ´åã¯æ¬¡ã®ããããã®ã³ãã³ããå®è¡ãã¾ãã
vagrant suspend
â¦â¦ ä»®æ³ãã·ã³ã®ç¶æ ãä¿åãã¦çµäºãã¾ããvagrant halt
â¦â¦ ä»®æ³ãã·ã³ãã·ã£ãããã¦ã³ãã¾ããvagrant destroy
â¦â¦ ä»®æ³ãã·ã³ãå¼·å¶çµäºããç ´æ£ï¼åé¤ï¼ãã¾ãã