Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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) =>
- val classpath = (deps.map(_.data.getAbsolutePath).toArray :+ classes.getAbsolutePath).mkString(java.io.File.pathSeparator)
- val timestampFile = cacheDir / "play_instrumentation"
- val lastEnhanced = if (timestampFile.exists) IO.read(timestampFile).toLong else Long.MinValue
- val javaClasses = (javaSrc ** "*.java").get flatMap { sourceFile =>
- // PropertiesEnhancer is class-local, so no need to check outside the class.
- if (analysis.apis.internal(sourceFile).compilation.startTime > lastEnhanced)
- analysis.relations.products(sourceFile)
- else
- Nil
- }
- val templateClasses = (srcManaged ** "*.template.scala").get flatMap { sourceFile =>
- if (analysis.apis.internal(sourceFile).compilation.startTime > lastEnhanced)
- analysis.relations.products(sourceFile)
- else
- Nil
- }
- import play.core.enhancers.PropertiesEnhancer
- val javaClassesWithGeneratedAccessors = javaClasses.filter(PropertiesEnhancer.generateAccessors(classpath, _))
- val javaClassesWithAccessorsRewritten = javaClasses.filter(PropertiesEnhancer.rewriteAccess(classpath, _))
- val enhancedTemplateClasses = templateClasses.filter(PropertiesEnhancer.rewriteAccess(classpath, _))
- val enhancedClasses = (javaClassesWithGeneratedAccessors ++ javaClassesWithAccessorsRewritten ++
- enhancedTemplateClasses).distinct
- IO.write(timestampFile, System.currentTimeMillis.toString)
- val ebeanEnhancement = classpath.contains("play-java-ebean")
- // EBean
- if (ebeanEnhancement) {
- val originalContextClassLoader = Thread.currentThread.getContextClassLoader
- try {
- val cp = deps.map(_.data.toURI.toURL).toArray :+ classes.toURI.toURL
- Thread.currentThread.setContextClassLoader(new java.net.URLClassLoader(cp, ClassLoader.getSystemClassLoader))
- import com.avaje.ebean.enhance.agent._
- import com.avaje.ebean.enhance.ant._
- import collection.JavaConverters._
- import com.typesafe.config._
- val cl = ClassLoader.getSystemClassLoader
- val t = new Transformer(cp, "debug=-1;checkNullManyFields=false")
- val ft = new OfflineFileTransform(t, cl, classes.getAbsolutePath, classes.getAbsolutePath)
- lazy val file = {
- Option(System.getProperty("config.file")).map(f => new File(f)).getOrElse(new File("conf/application.conf"))
- }
- val config = Option(System.getProperty("config.resource"))
- .map(ConfigFactory.parseResources(_)).getOrElse(ConfigFactory.parseFileAnySyntax(file))
- val models = try {
- config.getConfig("ebean").entrySet.asScala.map(_.getValue.unwrapped).toSet.mkString(",")
- } catch { case e: ConfigException.Missing => "models.*" }
- try {
- ft.process(models)
- } catch {
- case NonFatal(_) =>
- }
- } finally {
- Thread.currentThread.setContextClassLoader(originalContextClassLoader)
- }
- }
- // Copy managed classes - only needed in Compile scope
- // This is done to ease integration with Eclipse, but it's doubtful as to how effective it is.
- if (scope.name.toLowerCase == "compile") {
- val managedClassesDirectory = classes.getParentFile / (classes.getName + "_managed")
- val managedClasses = ((srcManaged ** "*.scala").get ++ (srcManaged ** "*.java").get).map { managedSourceFile =>
- analysis.relations.products(managedSourceFile)
- }.flatten x rebase(classes, managedClassesDirectory)
- // Copy modified class files
- val managedSet = IO.copy(managedClasses)
- // Remove deleted class files
- (managedClassesDirectory ** "*.class").get.filterNot(managedSet.contains(_)).foreach(_.delete())
- }
- // If ebean enhancement was done, then it's possible that any of the java classes were enhanced, we don't know
- // which, otherwise it's just the enhanced classes that we did accessor generation/rewriting for
- val possiblyEnhancedClasses = if (ebeanEnhancement) {
- javaClasses ++ enhancedTemplateClasses
- } else {
- enhancedClasses
- }
- if (!possiblyEnhancedClasses.isEmpty) {
- /**
- * Updates stamp of product (class file) by preserving the type of a passed stamp.
- * This way any stamp incremental compiler chooses to use to mark class files will
- * be supported.
- */
- def updateStampForClassFile(classFile: File, stamp: Stamp): Stamp = stamp match {
- case _: Exists => Stamp.exists(classFile)
- case _: LastModified => Stamp.lastModified(classFile)
- case _: Hash => Stamp.hash(classFile)
- }
- // Since we may have modified some of the products of the incremental compiler, that is, the compiled template
- // classes and compiled Java sources, we need to update their timestamps in the incremental compiler, otherwise
- // the incremental compiler will see that they've changed since it last compiled them, and recompile them.
- val updatedAnalysis = analysis.copy(stamps = possiblyEnhancedClasses.foldLeft(analysis.stamps) { (stamps, classFile) =>
- val existingStamp = stamps.product(classFile)
- if (existingStamp == Stamp.notPresent) {
- throw new java.io.IOException("Tried to update a stamp for class file that is not recorded as "
- + s"product of incremental compiler: $classFile")
- }
- stamps.markProduct(classFile, updateStampForClassFile(classFile, existingStamp))
- })
- // Need to persist the updated analysis.
- val agg = new AggressiveCompile(inputs.incSetup.cacheFile)
- // Load the old one. We do this so that we can get a copy of CompileSetup, which is the cache compiler
- // configuration used to determine when everything should be invalidated. We could calculate it ourselves, but
- // that would by a heck of a lot of fragile code due to the vast number of things we would have to depend on.
- // Reading it out of the existing file is good enough.
- val existing: Option[(Analysis, CompileSetup)] = agg.store.get()
- // Since we've just done a compile before this task, this should never return None, so don't worry about what to
- // do when it returns None.
- existing.foreach {
- case (_, compileSetup) => agg.store.set(updatedAnalysis, compileSetup)
- }
- updatedAnalysis
- } else {
- analysis
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement