aboutsummaryrefslogtreecommitdiff
path: root/stage2/Stage2.scala
blob: 33a67ac9f572c9666b96b05bc3641be5f11bd476 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
  }
}