summaryrefslogtreecommitdiff
path: root/build.sc
blob: 5fdfaa2ac078f72a077e5f194aaa0df380169e94 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import $file.shared
import java.io.File

import ammonite.ops._
import coursier.maven.MavenRepository
import mill._
import mill.scalaplugin._, publish._
import mill.modules.Jvm.createAssembly

trait MillPublishModule extends PublishModule {
  def publishWithFullScalaVersion = true

  def pomSettings = PomSettings(
    organization = "com.lihaoyi",
    description = publishName(),
    developers = Seq(Developer("lihaoyi", "Li Haoyi", "https://github.com/lihaoyi/mill")),
    licenses = Seq(License("MIT License", "https://spdx.org/licenses/MIT.html#licenseText")),
    scm = SCM("https://github.com/lihaoyi/mill", "scm:git:https://github.com/lihaoyi/mill.git"),
    url = "https://github.com/lihaoyi/mill"
  )
}

object CompilerPlugin extends SbtScalaModule{
  def scalaVersion = "2.12.4"
  def basePath = pwd / 'plugin
  def ivyDeps = Seq(
    Dep.Java("org.scala-lang", "scala-compiler", scalaVersion()),
    Dep("com.lihaoyi", "sourcecode", "0.1.4")
  )
}

trait MillModule extends SbtScalaModule{ outer =>
  def scalaVersion = "2.12.4"

  def compileIvyDeps = Seq(Dep("com.lihaoyi", "acyclic", "0.1.7"))
  def scalacOptions = Seq("-P:acyclic:force")
  def scalacPluginIvyDeps = Seq(Dep("com.lihaoyi", "acyclic", "0.1.7"))

  def repositories = super.repositories ++ Seq(
    MavenRepository("https://oss.sonatype.org/content/repositories/releases")
  )

  def testArgs = T{ Seq.empty[String] }

  val test = new Tests
  class Tests extends super.Tests{
    def defaultCommandName() = "forkTest"
    def forkArgs = T{ testArgs() }
    def projectDeps =
      if (this == Core.test) Seq(Core)
      else Seq(outer, Core.test)
    def ivyDeps = Seq(Dep("com.lihaoyi", "utest", "0.6.0"))
    def testFramework = "mill.UTestFramework"
    def scalacPluginClasspath = super.scalacPluginClasspath() ++ Seq(CompilerPlugin.jar())
  }
}

object Core extends MillModule {
  def projectDeps = Seq(CompilerPlugin)

  def compileIvyDeps = Seq(
    Dep.Java("org.scala-lang", "scala-reflect", scalaVersion())
  )

  def ivyDeps = Seq(
    Dep("com.lihaoyi", "sourcecode", "0.1.4"),
    Dep("com.lihaoyi", "pprint", "0.5.3"),
    Dep.Point("com.lihaoyi", "ammonite", "1.0.3-20-75e58ac"),
    Dep("com.typesafe.play", "play-json", "2.6.6"),
    Dep("org.scala-sbt", "zinc", "1.0.5"),
    Dep.Java("org.scala-sbt", "test-interface", "1.0")
  )

  def basePath = pwd / 'core

  def generatedSources = T{
    mkdir(T.ctx().dest)
    shared.generateSources(T.ctx().dest)
    PathRef(T.ctx().dest)
  }

  def allSources = super.allSources() ++ Seq(generatedSources())
  val test = new Tests{
    def generatedSources = T{
      mkdir(T.ctx().dest)
      shared.generateTests(T.ctx().dest)
      PathRef(T.ctx().dest)

    }
    def allSources = super.allSources() ++ Seq(generatedSources())
  }

  val cross =
    for(jarLabel <- mill.define.Cross("jarA", "jarB", "jarC"))
    yield new mill.Module{
      def printIt() = T.command{
        println("PRINTING IT: " + jarLabel)
      }
      def jar = T{
        val dest = T.ctx().dest
        ammonite.ops.mkdir(dest/ammonite.ops.up)
        ammonite.ops.cp(Core.jar().path, dest)

        PathRef(dest)
      }
    }
}

val bridgeVersions = Seq("2.10.6", "2.11.8", "2.11.11", "2.12.3", "2.12.4")

val bridges = for(crossVersion <- mill.define.Cross(bridgeVersions:_*)) yield new MillPublishModule {
  def publishName = "mill-bridge"
  def publishVersion = "0.1"

  def basePath = pwd / 'bridge
  def scalaVersion = crossVersion
  def sources = T.source {
    val path = basePath / 'src
    mkdir(path)
    path
  }
  def allSources = T{
    Seq(PathRef(shared.downloadBridgeSource(T.ctx().dest, crossVersion)))
  }
  def ivyDeps = Seq(
    Dep.Java("org.scala-lang", "scala-compiler", crossVersion),
    Dep.Java("org.scala-sbt", "compiler-interface", "1.0.5")
  )
}

object ScalaPlugin extends MillModule {
  def projectDeps = Seq(Core)
  def basePath = pwd / 'scalaplugin

  def bridgeCompiles = mill.define.Task.traverse(bridges.items)(_._2.compile)
  def testArgs = T{
    val bridgeVersions = bridges.items.map(_._1.head.toString)

    for((version, compile) <- bridgeVersions.zip(bridgeCompiles()))
    yield {
      val underscored = version.replace('.', '_')
      val key = s"MILL_COMPILER_BRIDGE_$underscored"
      val value = compile.classes.path
      s"-D$key=$value"
    }
  }
}
val jsbridges = for{
  scalajsBinary <- mill.define.Cross("0.6", "1.0")
} yield new MillModule{
  def basePath = pwd / 'scalajsplugin / s"bridge_${scalajsBinary.replace('.', '_')}"
  val scalajsVersion = scalajsBinary match {
    case "0.6" => "0.6.21"
    case "1.0" => "1.0.0-M2"
  }
  override def ivyDeps = Seq(
    Dep("org.scala-js", "scalajs-tools", scalajsVersion)
  )
}

object ScalaJSPlugin extends MillModule {

  def projectDeps = Seq(ScalaPlugin)
  def basePath = pwd / 'scalajsplugin

  def bridgeClasspath(runDepClasspath: Seq[PathRef], classes: PathRef) =
    (runDepClasspath :+ classes).map(_.path).mkString(File.pathSeparator)
  def testArgs = T{
    val mapping = Map(
      "MILL_SCALAJS_BRIDGE_0_6" -> bridgeClasspath(jsbridges("0.6").runDepClasspath(), jsbridges("0.6").compile().classes),
      "MILL_SCALAJS_BRIDGE_1_0" -> bridgeClasspath(jsbridges("1.0").runDepClasspath(), jsbridges("1.0").compile().classes)
    )
    for((k, v) <- mapping.toSeq) yield s"-D$k=$v"
  }

}

val assemblyProjects = Seq(ScalaPlugin, ScalaJSPlugin)

def assemblyClasspath = mill.define.Task.traverse(assemblyProjects)(_.assemblyClasspath)

def publishBridges(credentials: String, gpgPassphrase: String) = T.command {
  mill.define.Task.traverse(bridges.items)(_._2.publish(credentials, gpgPassphrase))
}

def assemblyBase(classpath: Seq[Path], extraArgs: String)
                (implicit ctx: mill.util.Ctx.DestCtx) = {
  createAssembly(
    classpath,
    prependShellScript =
      "#!/usr/bin/env sh\n" +
      s"""exec java $extraArgs $$JAVA_OPTS -cp "$$0" mill.Main "$$@" """
  )
}

def devAssembly = T{
  assemblyBase(assemblyClasspath().flatten, (ScalaPlugin.testArgs() ++ ScalaJSPlugin.testArgs()).mkString(" "))
}

def releaseAssembly = T{
  assemblyBase(assemblyClasspath().flatten, "")
}

def idea() = T.command{ mill.scalaplugin.GenIdea() }