aboutsummaryrefslogblamecommitdiff
path: root/stage2/Stage2.scala
blob: 33a67ac9f572c9666b96b05bc3641be5f11bd476 (plain) (tree)
1
2
3
4
5
6
7
8
9
           
                
                           
 
                                 
                                    
                                                                           

   
                                           
                      
                                                 
                  
                                  
                                  
 
                                  
 
                                            
                                                     

               
                                  

                                    
                              
           
                                    
                          

                   
                   
                               




                                                                           
     







                                                                                 
       





















                                                                                      

   
package cbt
import java.io._
import java.util.{Set=>_,_}

object Stage2 extends Stage2Base{
  def getBuild(context: Context) = {
    new Lib( context.logger ).loadRoot( context ).finalBuild( context.cwd )
  }

  def run( args: Stage2Args ): ExitCode = {
    import args.logger
    val paths = CbtPaths(args.cbtHome,args.cache)
    import paths._
    val lib = new Lib(args.logger)
    logger.stage2(s"Stage2 start")

    val task = args.args.lift( 0 )

    import scala.collection.JavaConverters._
    val context: Context = new ContextImplementation(
      args.cwd,
      args.cwd,
      args.args.drop( 1 ).toArray,
      logger.enabledLoggers.toArray,
      logger.start,
      args.stage2LastModified,
      null,
      args.classLoaderCache.hashMap,
      args.transientCache,
      args.cache,
      args.cbtHome,
      args.cbtHome,
      args.compatibilityTarget,
      null,
      NailgunLauncher.compatibilitySourceFiles.asScala.toArray[File]
        ++ NailgunLauncher.nailgunLauncherSourceFiles.asScala.toArray[File]
        ++ NailgunLauncher.stage1SourceFiles.asScala.toArray[File]
        ++ args.stage2sourceFiles.toArray[File]
    )
    def loop( code: ExitCode, files: () => Set[File] ): ExitCode = {
      code match {
        case c@ExitCode(253 | 130) => c // CBT signals loop | user pressed ctrl+C
        case c if !task.contains("loop") => c
        case c =>
          // this allows looping over broken builds
          lib.watch{ files }()
          c
      }
    }
    val code = lib.trapExitCode{
      val first = lib.loadRoot( context )
      val build = first.finalBuild( context.cwd )
      val code = lib.callReflective(build, task, context)
      if( !context.loopFile.exists ){
        loop( code, () => build.triggerLoopFiles )
      }
      code
    }
    if( context.loopFile.exists ){
      loop(
        code,
        () => {
          val files = context.loopFile.readAsString.split("\n").map(new File(_)).toSet
          logger.loop("Looping change detection over:\n - "++files.mkString("\n - "))
          files
        }
      )
    }
    logger.stage2(s"Stage2 end with exit code "+code.integer)
    code
  }
}