summaryrefslogtreecommitdiff
path: root/main/src/mill/Main.scala
blob: d6a908d3362f47b702a93bbb59291770ff7b1a35 (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
168
169
170
171
172
173
174
package mill

import java.io.{InputStream, PrintStream}

import ammonite.main.Cli._
import ammonite.ops._
import ammonite.util.Util
import mill.clientserver.{Client, FileLocks}
import mill.eval.Evaluator
import mill.util.DummyInputStream


object ClientMain {
  def initServer(lockBase: String) = {
    val selfJars = new java.lang.StringBuilder
    var current = getClass.getClassLoader
    while(current != null){
      getClass.getClassLoader match{
        case e: java.net.URLClassLoader =>
          val urls = e.getURLs
          var i = 0
          while(i < urls.length){
            if (selfJars.length() != 0) selfJars.append(':')
            selfJars.append(urls(i))
            i += 1
          }
        case _ =>
      }
      current = current.getParent
    }

    val l = new java.util.ArrayList[String]
    l.add("java")
    val props = System.getProperties
    val keys = props.stringPropertyNames().iterator()
    while(keys.hasNext){
      val k = keys.next()
      if (k.startsWith("MILL_")) l.add("-D" + k + "=" + props.getProperty(k))
    }
    l.add("-cp")
    l.add(selfJars.toString)
    l.add("mill.ServerMain")
    l.add(lockBase)
    new java.lang.ProcessBuilder()
      .command(l)
      .redirectOutput(new java.io.File(lockBase + "/logs"))
      .redirectError(new java.io.File(lockBase + "/logs"))
      .start()
  }
  def main(args: Array[String]): Unit = {
    val exitCode = Client.WithLock(1) { lockBase =>
      val c = new Client(
        lockBase,
        () => initServer(lockBase),
        new FileLocks(lockBase),
        System.in,
        System.out,
        System.err
      )
      c.run(args)
    }
    System.exit(exitCode)
  }
}
object ServerMain extends mill.clientserver.ServerMain[Evaluator.State]{
  def main0(args: Array[String],
            stateCache: Option[Evaluator.State],
            mainInteractive: Boolean,
            watchInterrupted: () => Boolean,
            stdin: InputStream,
            stdout: PrintStream,
            stderr: PrintStream) = Main.main0(
    args,
    stateCache,
    mainInteractive,
    watchInterrupted,
    DummyInputStream,
    stdout,
    stderr
  )
}
object Main {

  def main(args: Array[String]): Unit = {
    val (result, _) = main0(
      args,
      None,
      ammonite.Main.isInteractive(), () => false,
      System.in,
      System.out,
      System.err
    )
    System.exit(if(result) 0 else 1)
  }

  def main0(args: Array[String],
            stateCache: Option[Evaluator.State],
            mainInteractive: Boolean,
            watchInterrupted: () => Boolean,
            stdin: InputStream,
            stdout: PrintStream,
            stderr: PrintStream): (Boolean, Option[Evaluator.State]) = {
    import ammonite.main.Cli

    val removed = Set("predef-code", "home", "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",
      (c, v) =>{
        interactive = true
        c
      }
    )
    val millArgSignature =
      Cli.genericSignature.filter(a => !removed(a.name)) :+ interactiveSignature

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

        val repl = leftoverArgs.isEmpty
        val config =
          if(!repl) cliConfig
          else cliConfig.copy(
            predefCode =
              """import $file.build, build._
                |implicit val replApplyHandler = mill.main.ReplApplyHandler(
                |  interp.colors(),
                |  repl.pprinter(),
                |  build.millSelf.get,
                |  build.millDiscover
                |)
                |repl.pprinter() = replApplyHandler.pprinter
                |import replApplyHandler.generatedEval._
                |
              """.stripMargin,
            welcomeBanner = None
          )

        val runner = new mill.main.MainRunner(
          config.copy(home = pwd / "out" / ".ammonite", colored = Some(mainInteractive)),
          stdout, stderr, stdin,
          watchInterrupted,
          stateCache
        )

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