summaryrefslogtreecommitdiff
path: root/main/src/MillMain.scala
blob: f1a7a9e7b190cff6089bd6fda1561a048c969a81 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package mill

import java.io.{InputStream, PrintStream}

import scala.collection.JavaConverters._
import ammonite.main.Cli._
import io.github.retronym.java9rtexport.Export
import mill.eval.Evaluator
import mill.api.DummyInputStream

object MillMain {

  def main(args: Array[String]): Unit = {
    val as = args match {
      case Array(s, _*) if s == "-i" || s == "--interactive" => args.tail
      case _ => args
    }
    val (result, _) = main0(
      as,
      None,
      ammonite.Main.isInteractive(),
      System.in,
      System.out,
      System.err,
      System.getenv().asScala.toMap,
      b => ()
    )
    System.exit(if(result) 0 else 1)
  }

  def main0(args: Array[String],
            stateCache: Option[Evaluator.State],
            mainInteractive: Boolean,
            stdin: InputStream,
            stdout: PrintStream,
            stderr: PrintStream,
            env: Map[String, String],
            setIdle: Boolean => Unit): (Boolean, Option[Evaluator.State]) = {
    import ammonite.main.Cli
    
    val millHome = mill.api.Ctx.defaultHome

    val removed = Set("predef-code", "no-home-predef")
    var interactive = false
    val interactiveSignature = Arg[Config, Unit](
      "interactive", Some('i'),
      "Run Mill in interactive mode, suitable for opening REPLs and taking user input. In this mode, no mill server will be used.",
      (c, v) =>{
        interactive = true
        c
      }
    )



    var disableTicker = false
    val disableTickerSignature = Arg[Config, Unit](
      "disable-ticker", None,
      "Disable ticker log (e.g. short-lived prints of stages and progress bars)",
      (c, v) =>{
        disableTicker = true
        c
      }
    )

    var debugLog = false
    val debugLogSignature = Arg[Config, Unit](
      name = "debug", shortName = Some('d'),
      doc = "Show debug output on STDOUT",
      (c, v) => {
        debugLog = true
        c
      }
    )

    var keepGoing = false
    val keepGoingSignature = Arg[Config, Unit] (
      name = "keep-going", shortName = Some('k'), doc = "Continue build, even after build failures",
      (c,v) => {
        keepGoing = true
        c
      }
    )

    val millArgSignature =
      Cli.genericSignature.filter(a => !removed(a.name)) ++
        Seq(interactiveSignature, disableTickerSignature, debugLogSignature, keepGoingSignature)

    Cli.groupArgs(
      args.toList,
      millArgSignature,
      Cli.Config(home = millHome, remoteLogging = false)
    ) match{
      case _ if interactive =>
        stderr.println("-i/--interactive must be passed in as the first argument")
        (false, None)
      case Left(msg) =>
        stderr.println(msg)
        (false, None)
      case Right((cliConfig, _)) if cliConfig.help =>
        val leftMargin = millArgSignature.map(ammonite.main.Cli.showArg(_).length).max + 2
        stdout.println(
        s"""Mill Build Tool
           |usage: mill [mill-options] [target [target-options]]
           |
           |${formatBlock(millArgSignature, leftMargin).mkString(ammonite.util.Util.newLine)}""".stripMargin
        )
        (true, None)
      case Right((cliConfig, leftoverArgs)) =>

        val repl = leftoverArgs.isEmpty
        if (repl && stdin == DummyInputStream) {
          stderr.println("Build repl needs to be run with the -i/--interactive flag")
          (false, stateCache)
        }else{
          val config =
            if(!repl) cliConfig
            else cliConfig.copy(
              predefCode =
                s"""import $$file.build, build._
                  |implicit val replApplyHandler = mill.main.ReplApplyHandler(
                  |  os.Path(${pprint.apply(cliConfig.home.toIO.getCanonicalPath.replaceAllLiterally("$", "$$")).plainText}),
                  |  $disableTicker,
                  |  interp.colors(),
                  |  repl.pprinter(),
                  |  build.millSelf.get,
                  |  build.millDiscover,
                  |  $debugLog,
                  |  keepGoing = $keepGoing
                  |)
                  |repl.pprinter() = replApplyHandler.pprinter
                  |import replApplyHandler.generatedEval._
                  |
                """.stripMargin,
              welcomeBanner = None
            )

          val runner = new mill.main.MainRunner(
            config.copy(colored = config.colored orElse Option(mainInteractive)),
            disableTicker,
            stdout, stderr, stdin,
            stateCache,
            env,
            setIdle,
            debugLog,
            keepGoing = keepGoing
          )

          if (mill.main.client.Util.isJava9OrAbove) {
            val rt = cliConfig.home / Export.rtJarName
            if (!os.exists(rt)) {
              runner.printInfo(s"Preparing Java ${System.getProperty("java.version")} runtime; this may take a minute or two ...")
              Export.rtTo(rt.toIO, false)
            }
          }

          if (repl){
            runner.printInfo("Loading...")
            (runner.watchLoop(isRepl = true, printing = false, _.run()), runner.stateCache)
          } else {
            (runner.runScript(os.pwd / "build.sc", leftoverArgs), runner.stateCache)
          }
      }

    }
  }
}