forked from com-lihaoyi/mill
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
748 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mill.main.initializers | ||
|
||
import mill.api.Logger | ||
import mill.eval.Evaluator | ||
import os.Path | ||
|
||
object MavenInit { | ||
def apply(workspace: Path, rootPom: Path, log: Logger, evaluator: Evaluator, args: String*): Unit = { | ||
maven.JavaModule.readFrom(rootPom) match { | ||
case Left(error) => log.error(error) | ||
case Right(module) => | ||
log.info(module.render) | ||
os.write(workspace / "build.mill", module.render) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package mill.main.initializers.maven | ||
|
||
import scala.xml.Node | ||
|
||
/** Dependency for `dependencies` section of POM. */ | ||
private[maven] case class Dependency( | ||
groupId: TemplatedString, artifactId: TemplatedString, version: Option[TemplatedString], | ||
type_ : Option[DependencyType], scope: DependencyScope | ||
) { | ||
override def toString: String = | ||
s"Dependency(groupId=$groupId, artifactId=$artifactId, version=$version, type=$type_, scope=$scope)" | ||
|
||
def asDependencyManagement(properties: PropertyMap): Either[String, String] = { | ||
for { | ||
groupId <- groupId.resolve(properties).left.map(err => s"Could not resolve groupId for $this: $err") | ||
artifactId <- artifactId.resolve(properties).left.map(err => s"Could not resolve artifactId for $this: $err") | ||
} yield s"$groupId:$artifactId" | ||
} | ||
|
||
def render(properties: PropertyMap): String = { | ||
val versionStr = version match { | ||
case Some(version) => version.renderForInsideString(TemplatedString.ScopeName.Properties) | ||
case None => | ||
TemplatedString(s"$${${asDependencyManagement(properties)}") | ||
.renderForInsideString(TemplatedString.ScopeName.DependencyManagement) | ||
} | ||
|
||
val str = s"${groupId.renderForInsideString(TemplatedString.ScopeName.Properties)}:${ | ||
artifactId.renderForInsideString(TemplatedString.ScopeName.Properties)}:$versionStr" | ||
s"ivy${str.renderAsString}" | ||
} | ||
} | ||
private[maven] object Dependency { | ||
def parse(xml: Node): Either[String, Dependency] = { | ||
for { | ||
groupId <- readSingleText(xml, "groupId").map(TemplatedString.apply) | ||
artifactId <- readSingleText(xml, "artifactId").map(TemplatedString.apply) | ||
version <- readMaybeSingleText(xml, "version").map(_.map(TemplatedString.apply)) | ||
type_ <- readMaybeSingleText(xml, "type").map(_.map(DependencyType.parse)) | ||
scope <- readMaybeSingleText(xml, "scope").flatMap { opt => | ||
opt | ||
.map(DependencyScope.parse(_).left.map(err => s"$err for $xml")) | ||
.getOrElse(Right(DependencyScope.Compile)) | ||
} | ||
} yield apply(groupId = groupId, artifactId = artifactId, version = version, type_ = type_, scope = scope) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
main/src/mill/main/initializers/maven/DependencyScope.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package mill.main.initializers.maven | ||
|
||
/** https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#dependency-scope */ | ||
private[maven] sealed trait DependencyScope { | ||
def render: String = this match { | ||
case DependencyScope.Compile => "compile" | ||
case DependencyScope.Provided => "provided" | ||
case DependencyScope.Runtime => "runtime" | ||
case DependencyScope.Test => "test" | ||
case DependencyScope.System => "system" | ||
case DependencyScope.Import => "import" | ||
} | ||
} | ||
private[maven] object DependencyScope { | ||
case object Compile extends DependencyScope | ||
|
||
/** This is much like compile, but indicates you expect the JDK or a container to provide the dependency at | ||
* runtime. */ | ||
case object Provided extends DependencyScope | ||
|
||
/** This scope indicates that the dependency is not required for compilation, but is for execution. Maven includes | ||
* a dependency with this scope in the runtime and test classpaths, but not the compile classpath. */ | ||
case object Runtime extends DependencyScope | ||
|
||
case object Test extends DependencyScope | ||
|
||
/** This scope is similar to provided except that you have to provide the JAR which contains it explicitly. | ||
* The artifact is always available and is not looked up in a repository. */ | ||
case object System extends DependencyScope | ||
|
||
case object Import extends DependencyScope | ||
|
||
def parse(str: String): Either[String, DependencyScope] = str match { | ||
case "compile" => Right(Compile) | ||
case "provided" => Right(Provided) | ||
case "runtime" => Right(Runtime) | ||
case "test" => Right(Test) | ||
case "system" => Right(System) | ||
case "import" => Right(Import) | ||
case _ => Left(s"Unknown dependency scope: '$str'") | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
main/src/mill/main/initializers/maven/DependencyType.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mill.main.initializers.maven | ||
|
||
private[maven] sealed trait DependencyType | ||
private[maven] object DependencyType { | ||
case object POM extends DependencyType | ||
case class Other(value: String) extends DependencyType | ||
|
||
def parse(str: String): DependencyType = str match { | ||
case "pom" => POM | ||
case _ => Other(str) | ||
} | ||
} |
Oops, something went wrong.