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

Scala example source code file (FastScalac.scala)

This example Scala source code file (FastScalac.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

boolean, boolean, compile, compile, fastscalac, list, list, nil, none, scalac, some, string, unit, unit

The Scala FastScalac.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala Ant Tasks                      **
**    / __/ __// _ | / /  / _ |    (c) 2005-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */


package scala.tools.ant

/** <p>
 *    An Ant task to compile with the fast Scala compiler (<code>fsc).
 *  </p>
 *  <p>
 *    In addition to the attributes shared with the <code>Scalac
 *    task, this task also accepts the following attributes:
 *  </p>
 *  <ul style="font-family:Courier;">
 *    <li>reset
 *    <li>server
 *    <li>shutdown
 *  </ul>
 *
 *  @author Stephane Micheloud
 */
class FastScalac extends Scalac {

  private var resetCaches: Boolean = false

  private var serverAddr: Option[String] = None

  private var shutdownServer: Boolean = false

/*============================================================================*\
**                             Properties setters                             **
\*============================================================================*/

  /** Sets the <code>reset attribute. Used by Ant.
   *
   *  @param input The value for <code>reset.
   */
  def setReset(input: Boolean): Unit =
    resetCaches = input

  /** Sets the <code>server attribute. Used by Ant.
   *
   *  @param input The value for <code>server.
   */
  def setServer(input: String): Unit = {
    serverAddr = Some(input)
  }

  /** Sets the <code>shutdown attribute. Used by Ant.
   *
   *  @param input The value for <code>shutdown.
   */
  def setShutdown(input: Boolean): Unit =
    shutdownServer = input

/*============================================================================*\
**                             The execute method                             **
\*============================================================================*/

  /** Performs the compilation. */
  override def execute() = {
    val (settings, sourceFiles, javaOnly) = initialize
    val s = settings

    if (!sourceFiles.isEmpty && !javaOnly) {
      def trim(xs: List[String]) = xs filter (x => x.length > 0)
      val reset = settings.BooleanSetting("-reset", "Reset compile server caches")
      val shutdown = settings.BooleanSetting("-shutdown", "Shutdown compile server")

      reset.value = resetCaches
      shutdown.value = shutdownServer
      
      /** XXX Since fsc is largely unmaintained, the set of options being individually
       *  assessed here is likely to bear little relationship to the current set of options.
       *  Most likely this manifests in confusing and very difficult to debug behavior in fsc.
       *  We should warn or fix.
       */
      val stringSettings =
        List(s.outdir, s.classpath, s.bootclasspath, s.extdirs, s.encoding) flatMap (x => List(x.name, x.value))
        
      val serverOption =
        serverAddr.toList flatMap (x => List("-server", x))  // '-server' option
        
      val choiceSettings =
        List(s.debuginfo, s.target) map (x => "%s:%s".format(x.name, x.value))
        
      val booleanSettings = 
        List(s.debug, s.deprecation, s.verbose, reset, shutdown) map (x => if (x.value) List(x.name) else Nil) flatten
        
      val phaseSetting = {
        val s = settings.log
        if (s.value.isEmpty) Nil
        else List("%s:%s".format(s.name, s.value.mkString(",")))
      }
      
      val cmdOptions =
        stringSettings ::: serverOption ::: choiceSettings ::: booleanSettings ::: phaseSetting

      val args = (cmdOptions ::: (sourceFiles map (_.toString))).toArray
      try {
        if (scala.tools.nsc.CompileClient.process(args) && failonerror)
          buildError("Compile failed; see the compiler error output for details.")
      } 
      catch {
        case exception: Throwable if (exception.getMessage ne null) =>
          exception.printStackTrace()
          buildError("Compile failed because of an internal compiler error (" +
            exception.getMessage + "); see the error output for details.")
        case exception =>
          exception.printStackTrace()
          buildError("Compile failed because of an internal compiler error " +
            "(no error message provided); see the error output for details.")
      }
    }
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala FastScalac.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.