alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (PlayEclipse.scala)

This example Play Framework source code file (PlayEclipse.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

compile, eclipsetransformerfactory, java, node, play, play framework, projectref, rewriterule, scala, seq, state, validation

The PlayEclipse.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play

import sbt._
import sbt.Keys._
import Keys._

trait PlayEclipse {
  this: PlayCommands =>

  private def generateJavaPrefFile(): Unit = {
    val coreSettings = file(".settings") / "org.eclipse.core.resources.prefs"
    if (!coreSettings.exists) {
      IO.createDirectory(coreSettings.getParentFile)
      IO.write(coreSettings,
        """|eclipse.preferences.version=1
           |encoding/<project>=UTF-8""".stripMargin
      )
    }
  }

  /**
   * provides Settings for the eclipse project
   * @param mainLang mainly scala or java?
   */
  def eclipseCommandSettings(mainLang: String): Seq[Setting[_]] = {
    import com.typesafe.sbteclipse.core._
    import com.typesafe.sbteclipse.core.EclipsePlugin._
    import com.typesafe.sbteclipse.core.Validation
    import scala.xml._
    import scala.xml.transform.RewriteRule

    val `/` = java.io.File.separator

    lazy val addClassesManaged = new EclipseTransformerFactory[RewriteRule] {
      override def createTransformer(ref: ProjectRef, state: State): Validation[RewriteRule] = {
        setting(crossTarget in ref, state) map { ct =>
          new RewriteRule {
            override def transform(node: Node): Seq[Node] = node match {
              case elem if (elem.label == "classpath" && (ct / "classes_managed").exists) =>
                val newChild = elem.child ++
                  <classpathentry path={ (ct / "classes_managed").getAbsolutePath } kind="lib"></classpathentry>
                Elem(elem.prefix, "classpath", elem.attributes, elem.scope, false, newChild: _*)
              case other =>
                other
            }
          }
        }
      }
    }

    lazy val addScalaLib = new EclipseTransformerFactory[RewriteRule] {
      override def createTransformer(ref: ProjectRef, state: State): Validation[RewriteRule] = {
        evaluateTask(dependencyClasspath in Runtime, ref, state) map { classpath =>
          val scalaLib =
            classpath.find(_.get(moduleID.key).exists(moduleFilter(organization = "org.scala-lang", name = "scala-library"))).map(_.data.getAbsolutePath)
              .getOrElse(throw new RuntimeException("could not find scala-library.jar"))
          new RewriteRule {
            override def transform(node: Node): Seq[Node] = node match {
              case elem if (elem.label == "classpath") =>
                val newChild = elem.child ++ <classpathentry path={ scalaLib } kind="lib"></classpathentry>
                Elem(elem.prefix, "classpath", elem.attributes, elem.scope, false, newChild: _*)
              case other =>
                other
            }
          }
        }
      }
    }

    lazy val addSourcesManaged = new EclipseTransformerFactory[RewriteRule] {
      override def createTransformer(ref: ProjectRef, state: State): Validation[RewriteRule] = {
        setting(crossTarget in ref, state) map { ct =>
          new RewriteRule {
            override def transform(node: Node): Seq[Node] = node match {
              case elem if (elem.label == "classpath" && (ct / "src_managed" / "main").exists) =>
                val newChild = elem.child ++
                  <classpathentry path={ "target" + `/` + ct.getName + `/` + "src_managed" + `/` + "main" } kind="src"></classpathentry>
                Elem(elem.prefix, "classpath", elem.attributes, elem.scope, false, newChild: _*)
              case other =>
                other
            }
          }
        }
      }
    }

    mainLang match {
      case SCALA =>
        EclipsePlugin.eclipseSettings ++ Seq(
          EclipseKeys.projectFlavor := EclipseProjectFlavor.Scala,
          EclipseKeys.preTasks := Seq(compile in Compile),
          EclipseKeys.classpathTransformerFactories := Seq(addSourcesManaged)
        )
      case JAVA =>
        generateJavaPrefFile()
        EclipsePlugin.eclipseSettings ++ Seq(
          EclipseKeys.projectFlavor := EclipseProjectFlavor.Java,
          EclipseKeys.preTasks := Seq(compile in Compile),
          EclipseKeys.classpathTransformerFactories := Seq(addClassesManaged, addScalaLib)
        )
    }
  }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework PlayEclipse.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.