diff options
Diffstat (limited to 'stage1')
-rw-r--r-- | stage1/Stage1.scala | 77 |
1 files changed, 51 insertions, 26 deletions
diff --git a/stage1/Stage1.scala b/stage1/Stage1.scala index 36921be..32da3ed 100644 --- a/stage1/Stage1.scala +++ b/stage1/Stage1.scala @@ -8,66 +8,75 @@ import scala.collection.JavaConverters._ import paths._ -class Init(args: Array[String]) { +final case class Stage1ArgsParser(_args: Seq[String]) { /** * Raw parameters including their `-D` flag. **/ - val propsRaw: Seq[String] = args.toVector.filter(_.startsWith("-D")) + val propsRaw: Seq[String] = _args.toVector.filter(_.startsWith("-D")) /** * All arguments that weren't `-D` property declarations. **/ - val argsV: Seq[String] = args.toVector diff propsRaw + val args: Seq[String] = _args.toVector diff propsRaw /** * Parsed properties, as a map of keys to values. **/ - lazy val props = propsRaw + val props = propsRaw .map(_.drop(2).split("=")).map({ case Array(key, value) => key -> value }).toMap ++ System.getProperties.asScala - val logger = new Logger(props.get("log")) + val enabledLoggers = props.get("log") + + val admin = _args contains "admin" } -object Stage1{ +abstract class Stage2Base{ + def run( context: Stage2Args ): Unit +} + +case class Stage2Args( + cwd: File, + args: Seq[String], + cbtHasChanged: Boolean, + logger: Logger +) + +object Stage1{ protected def newerThan( a: File, b: File ) ={ a.lastModified > b.lastModified } - def main(args: Array[String], classLoader: ClassLoader): Unit = { - val mainClass = if(args contains "admin") "cbt.AdminStage2" else "cbt.Stage2" - val init = new Init(args) - val lib = new Stage1Lib(init.logger) - import lib._ - - logger.stage1(s"[$now] Stage1 start") - logger.stage1("Stage1: after creating lib") + def run(_args: Array[String], classLoader: ClassLoader, stage1SourcesChanged: java.lang.Boolean): Int = { + val args = Stage1ArgsParser(_args.toVector) + val logger = new Logger(args.enabledLoggers) + logger.stage1(s"Stage1 start") - val cwd = args(0) + val lib = new Stage1Lib(logger) + import lib._ - val src = stage2.listFiles.toVector.filter(_.isFile).filter(_.toString.endsWith(".scala")) + val sourceFiles = stage2.listFiles.toVector.filter(_.isFile).filter(_.toString.endsWith(".scala")) val changeIndicator = stage2Target ++ "/cbt/Build.class" - - val classLoaderCache = new ClassLoaderCache(logger) val deps = Dependencies( JavaDependency("net.incongru.watchservice","barbary-watchservice","1.0"), JavaDependency("org.eclipse.jgit", "org.eclipse.jgit", "4.2.0.201601211800-r") ) - val scalaXml = JavaDependency("org.scala-lang.modules","scala-xml_"+constants.scalaMajorVersion,constants.scalaXmlVersion) + val classLoaderCache = new ClassLoaderCache(logger) + val stage2SourcesChanged = sourceFiles.exists(newerThan(_, changeIndicator)) logger.stage1("before conditionally running zinc to recompile CBT") - if( src.exists(newerThan(_, changeIndicator)) ) { + if( stage2SourcesChanged ) { + val scalaXml = JavaDependency("org.scala-lang.modules","scala-xml_"+constants.scalaMajorVersion,constants.scalaXmlVersion) logger.stage1("cbt.lib has changed. Recompiling.") - zinc( true, src, stage2Target, nailgunTarget +: stage1Target +: Dependencies(deps, scalaXml).classpath, classLoaderCache, Seq("-deprecation") )( zincVersion = "0.3.9", scalaVersion = constants.scalaVersion ) + zinc( true, sourceFiles, stage2Target, nailgunTarget +: stage1Target +: Dependencies(deps, scalaXml).classpath, classLoaderCache, Seq("-deprecation") )( zincVersion = "0.3.9", scalaVersion = constants.scalaVersion ) } logger.stage1(s"[$now] calling CbtDependency.classLoader") - val cp = stage2Target val cl = classLoaderCache.transient.get( (stage2Target +: deps.classpath).string, cbt.URLClassLoader( @@ -80,10 +89,26 @@ object Stage1{ ) logger.stage1(s"[$now] Run Stage2") - val ExitCode(exitCode) = /*trapExitCode*/{ // this - runMain( mainClass, cwd +: args.drop(1).toVector, cl ) - } + val exitCode = ( + cl.loadClass( + if(args.admin) "cbt.AdminStage2" else "cbt.Stage2" + ) + .getMethod( "run", classOf[Stage2Args] ) + .invoke( + null, + Stage2Args( + new File( args.args(0) ), + args.args.drop(1).toVector, + // launcher changes cause entire nailgun restart, so no need for them here + cbtHasChanged = stage1SourcesChanged || stage2SourcesChanged, + logger + ) + ) match { + case code: ExitCode => code + case _ => ExitCode.Success + } + ).integer logger.stage1(s"[$now] Stage1 end") - System.exit(exitCode) + return exitCode; } } |