summaryrefslogtreecommitdiff
path: root/scalalib/src/main/scala/mill/scalalib/MiscModule.scala
blob: 52a953379d2dce66522da333cd63abaa28b698ef (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
package mill
package scalalib

import mill.define.Cross.Resolver
import mill.define.{Cross, Task}
import mill.eval.{PathRef, Result}
import mill.util.Loose.Agg


trait SbtModule extends ScalaModule { outer =>
  override def sources = T.input{ Agg(PathRef(basePath / 'src / 'main / 'scala)) }
  override def resources = T.input{ Agg(PathRef(basePath / 'src / 'main / 'resources)) }
  trait Tests extends super.Tests {
    override def basePath = outer.basePath
    override def sources = T.input{ Agg(PathRef(basePath / 'src / 'test / 'scala)) }
    override def resources = T.input{ Agg(PathRef(basePath / 'src / 'test / 'resources)) }
  }
}

trait CrossSbtModule extends SbtModule { outer =>
  override def basePath = super.basePath / ammonite.ops.up
  implicit def crossSbtModuleResolver: Resolver[CrossSbtModule] = new Resolver[CrossSbtModule]{
    def resolve[V <: CrossSbtModule](c: Cross[V]): V = {
      crossScalaVersion.split('.')
        .inits
        .takeWhile(_.length > 1)
        .flatMap( prefix =>
          c.items.map(_._2).find(_.crossScalaVersion.split('.').startsWith(prefix))
        )
        .collectFirst{case x => x}
        .getOrElse(
          throw new Exception(
            s"Unable to find compatible cross version between $crossScalaVersion and "+
            c.items.map(_._2.crossScalaVersion).mkString(",")
          )
        )
    }
  }

  def crossScalaVersion: String
  def scalaVersion = crossScalaVersion
  override def sources = T.input{
    super.sources() ++
    crossScalaVersion.split('.').inits.filter(_.nonEmpty).map(_.mkString(".")).map{
      s => PathRef{ basePath / 'src / 'main / s"scala-$s" }
    }

  }
  override def resources = T.input{ Agg(PathRef(basePath / 'src / 'main / 'resources)) }
  trait Tests extends super.Tests {
    override def basePath = outer.basePath
    override def sources = T.input{
      super.sources() ++
      crossScalaVersion.split('.').inits.filter(_.nonEmpty).map(_.mkString(".")).map{
        s => PathRef{ basePath / 'src / 'test / s"scala-$s" }
      }
    }
    override def resources = T.input{ Agg(PathRef(basePath / 'src / 'test / 'resources)) }
  }
}