Advertisement
Guest User

Untitled

a guest
Jan 19th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 6.69 KB | None | 0 0
  1.  def PostCompileExtra(scope: Configuration) = (sourceDirectory in scope, dependencyClasspath in scope, compile in scope, javaSource in scope, managedSourceDirectories in scope, classDirectory in scope, cacheDirectory in scope, compileInputs in compile in scope) map { (src, deps, analysis, javaSrc, srcManaged, classes, cacheDir, inputs) =>
  2.  
  3.     val classpath = (deps.map(_.data.getAbsolutePath).toArray :+ classes.getAbsolutePath).mkString(java.io.File.pathSeparator)
  4.  
  5.     val timestampFile = cacheDir / "play_instrumentation"
  6.     val lastEnhanced = if (timestampFile.exists) IO.read(timestampFile).toLong else Long.MinValue
  7.     val javaClasses = (javaSrc ** "*.java").get flatMap { sourceFile =>
  8.       // PropertiesEnhancer is class-local, so no need to check outside the class.
  9.       if (analysis.apis.internal(sourceFile).compilation.startTime > lastEnhanced)
  10.         analysis.relations.products(sourceFile)
  11.       else
  12.         Nil
  13.     }
  14.     val templateClasses = (srcManaged ** "*.template.scala").get flatMap { sourceFile =>
  15.       if (analysis.apis.internal(sourceFile).compilation.startTime > lastEnhanced)
  16.         analysis.relations.products(sourceFile)
  17.       else
  18.         Nil
  19.     }
  20.  
  21.     import play.core.enhancers.PropertiesEnhancer
  22.  
  23.     val javaClassesWithGeneratedAccessors = javaClasses.filter(PropertiesEnhancer.generateAccessors(classpath, _))
  24.     val javaClassesWithAccessorsRewritten = javaClasses.filter(PropertiesEnhancer.rewriteAccess(classpath, _))
  25.     val enhancedTemplateClasses = templateClasses.filter(PropertiesEnhancer.rewriteAccess(classpath, _))
  26.  
  27.     val enhancedClasses = (javaClassesWithGeneratedAccessors ++ javaClassesWithAccessorsRewritten ++
  28.       enhancedTemplateClasses).distinct
  29.  
  30.     IO.write(timestampFile, System.currentTimeMillis.toString)
  31.  
  32.     val ebeanEnhancement = classpath.contains("play-java-ebean")
  33.  
  34.     // EBean
  35.     if (ebeanEnhancement) {
  36.  
  37.       val originalContextClassLoader = Thread.currentThread.getContextClassLoader
  38.  
  39.       try {
  40.  
  41.         val cp = deps.map(_.data.toURI.toURL).toArray :+ classes.toURI.toURL
  42.  
  43.         Thread.currentThread.setContextClassLoader(new java.net.URLClassLoader(cp, ClassLoader.getSystemClassLoader))
  44.  
  45.         import com.avaje.ebean.enhance.agent._
  46.         import com.avaje.ebean.enhance.ant._
  47.         import collection.JavaConverters._
  48.         import com.typesafe.config._
  49.  
  50.         val cl = ClassLoader.getSystemClassLoader
  51.  
  52.         val t = new Transformer(cp, "debug=-1;checkNullManyFields=false")
  53.  
  54.         val ft = new OfflineFileTransform(t, cl, classes.getAbsolutePath, classes.getAbsolutePath)
  55.  
  56.         lazy val file = {
  57.           Option(System.getProperty("config.file")).map(f => new File(f)).getOrElse(new File("conf/application.conf"))
  58.         }
  59.  
  60.         val config = Option(System.getProperty("config.resource"))
  61.           .map(ConfigFactory.parseResources(_)).getOrElse(ConfigFactory.parseFileAnySyntax(file))
  62.  
  63.         val models = try {
  64.           config.getConfig("ebean").entrySet.asScala.map(_.getValue.unwrapped).toSet.mkString(",")
  65.         } catch { case e: ConfigException.Missing => "models.*" }
  66.  
  67.         try {
  68.           ft.process(models)
  69.         } catch {
  70.           case NonFatal(_) =>
  71.         }
  72.  
  73.       } finally {
  74.         Thread.currentThread.setContextClassLoader(originalContextClassLoader)
  75.       }
  76.     }
  77.     // Copy managed classes - only needed in Compile scope
  78.     // This is done to ease integration with Eclipse, but it's doubtful as to how effective it is.
  79.     if (scope.name.toLowerCase == "compile") {
  80.       val managedClassesDirectory = classes.getParentFile / (classes.getName + "_managed")
  81.  
  82.       val managedClasses = ((srcManaged ** "*.scala").get ++ (srcManaged ** "*.java").get).map { managedSourceFile =>
  83.         analysis.relations.products(managedSourceFile)
  84.       }.flatten x rebase(classes, managedClassesDirectory)
  85.  
  86.       // Copy modified class files
  87.       val managedSet = IO.copy(managedClasses)
  88.  
  89.       // Remove deleted class files
  90.       (managedClassesDirectory ** "*.class").get.filterNot(managedSet.contains(_)).foreach(_.delete())
  91.     }
  92.  
  93.     // If ebean enhancement was done, then it's possible that any of the java classes were enhanced, we don't know
  94.     // which, otherwise it's just the enhanced classes that we did accessor generation/rewriting for
  95.     val possiblyEnhancedClasses = if (ebeanEnhancement) {
  96.       javaClasses ++ enhancedTemplateClasses
  97.     } else {
  98.       enhancedClasses
  99.     }
  100.  
  101.     if (!possiblyEnhancedClasses.isEmpty) {
  102.       /**
  103.        * Updates stamp of product (class file) by preserving the type of a passed stamp.
  104.        * This way any stamp incremental compiler chooses to use to mark class files will
  105.        * be supported.
  106.        */
  107.       def updateStampForClassFile(classFile: File, stamp: Stamp): Stamp = stamp match {
  108.         case _: Exists => Stamp.exists(classFile)
  109.         case _: LastModified => Stamp.lastModified(classFile)
  110.         case _: Hash => Stamp.hash(classFile)
  111.       }
  112.       // Since we may have modified some of the products of the incremental compiler, that is, the compiled template
  113.       // classes and compiled Java sources, we need to update their timestamps in the incremental compiler, otherwise
  114.       // the incremental compiler will see that they've changed since it last compiled them, and recompile them.
  115.       val updatedAnalysis = analysis.copy(stamps = possiblyEnhancedClasses.foldLeft(analysis.stamps) { (stamps, classFile) =>
  116.         val existingStamp = stamps.product(classFile)
  117.         if (existingStamp == Stamp.notPresent) {
  118.           throw new java.io.IOException("Tried to update a stamp for class file that is not recorded as "
  119.             + s"product of incremental compiler: $classFile")
  120.         }
  121.         stamps.markProduct(classFile, updateStampForClassFile(classFile, existingStamp))
  122.       })
  123.  
  124.       // Need to persist the updated analysis.
  125.       val agg = new AggressiveCompile(inputs.incSetup.cacheFile)
  126.       // Load the old one. We do this so that we can get a copy of CompileSetup, which is the cache compiler
  127.       // configuration used to determine when everything should be invalidated. We could calculate it ourselves, but
  128.       // that would by a heck of a lot of fragile code due to the vast number of things we would have to depend on.
  129.       // Reading it out of the existing file is good enough.
  130.       val existing: Option[(Analysis, CompileSetup)] = agg.store.get()
  131.       // Since we've just done a compile before this task, this should never return None, so don't worry about what to
  132.       // do when it returns None.
  133.       existing.foreach {
  134.         case (_, compileSetup) => agg.store.set(updatedAnalysis, compileSetup)
  135.       }
  136.  
  137.       updatedAnalysis
  138.     } else {
  139.       analysis
  140.     }
  141.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement