Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --doc, --sources, and --transitive options to publishLocal command #3512

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 57 additions & 19 deletions scalalib/src/mill/scalalib/PublishModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,35 +124,73 @@ trait PublishModule extends JavaModule { outer =>
* @param localIvyRepo The local ivy repository.
* If not defined, the default resolution is used (probably `$HOME/.ivy2/local`).
*/
def publishLocal(localIvyRepo: String = null): define.Command[Unit] = T.command {
publishLocalTask(T.task {
Option(localIvyRepo).map(os.Path(_, T.workspace))
})()
def publishLocal(
localIvyRepo: String = null,
sources: Boolean = true,
doc: Boolean = true,
transitive: Boolean = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the default for transitive should be false. Also, it should include compileModuleDeps as well, when true.

Copy link
Contributor Author

@alexarchambault alexarchambault Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that transitive should default to false. I ran into issues with it and some publishLocal tasks aggregation via T.traverse done elsewhere (because of duplicated artifacts it seems). Although starting from a blank slate, a default to true would have been nicer IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About compileModuleDeps, I'm not sure. My aim with --transitive is to allow users to use something they published straightaway. So the standard dependencies need to be published too, but the compile-only ones aren't needed. Users can resolve the published module fine without publishing the compile-only dependencies.

): define.Command[Unit] = T.command {
publishLocalTask(
T.task {
Option(localIvyRepo).map(os.Path(_, T.workspace))
},
sources,
doc,
transitive
)()
Result.Success(())
}

/**
* Publish artifacts the local ivy repository.
*/
def publishLocalCached: T[Seq[PathRef]] = T {
publishLocalTask(T.task(None))().map(p => PathRef(p).withRevalidateOnce)
val res = publishLocalTask(
T.task(None),
sources = true,
doc = true,
transitive = true
)()
res.map(p => PathRef(p).withRevalidateOnce)
}

private def publishLocalTask(localIvyRepo: Task[Option[os.Path]]): Task[Seq[Path]] = T.task {
val publisher = localIvyRepo() match {
case None => LocalIvyPublisher
case Some(path) => new LocalIvyPublisher(path)
private def publishLocalTask(
localIvyRepo: Task[Option[os.Path]],
sources: Boolean,
doc: Boolean,
transitive: Boolean
): Task[Seq[Path]] =
if (transitive) {
val publishTransitiveModuleDeps = transitiveModuleDeps.collect {
case p: PublishModule => p
}
Target.traverse(publishTransitiveModuleDeps) { publishMod =>
publishMod.publishLocalTask(localIvyRepo, sources, doc, transitive = false)
}.map(_.flatten)
} else {
val sourcesJarOpt =
if (sources) T.task(Some(sourceJar()))
else T.task(None)
val docJarOpt =
if (doc) T.task(Some(docJar()))
else T.task(None)

T.task {
val publisher = localIvyRepo() match {
case None => LocalIvyPublisher
case Some(path) => new LocalIvyPublisher(path)
}
publisher.publishLocal(
jar = jar().path,
sourcesJarOpt = sourcesJarOpt().map(_.path),
docJarOpt = docJarOpt().map(_.path),
pom = pom().path,
ivy = ivy().path,
artifact = artifactMetadata(),
extras = extraPublish()
)
}
}
publisher.publishLocal(
jar = jar().path,
sourcesJar = sourceJar().path,
docJar = docJar().path,
pom = pom().path,
ivy = ivy().path,
artifact = artifactMetadata(),
extras = extraPublish()
)
}

/**
* Publish artifacts to a local Maven repository.
Expand Down
39 changes: 24 additions & 15 deletions scalalib/src/mill/scalalib/publish/LocalIvyPublisher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class LocalIvyPublisher(localIvyRepo: os.Path) {
ivy: os.Path,
artifact: Artifact,
extras: Seq[PublishInfo]
)(implicit ctx: Ctx.Log): Unit = publishLocal(jar, sourcesJar, docJar, pom, ivy, artifact, extras)
)(implicit ctx: Ctx.Log): Unit =
publishLocal(jar, Some(sourcesJar), Some(docJar), pom, ivy, artifact, extras)

def publishLocal(
jar: os.Path,
sourcesJar: os.Path,
docJar: os.Path,
sourcesJarOpt: Option[os.Path],
docJarOpt: Option[os.Path],
pom: os.Path,
ivy: os.Path,
artifact: Artifact,
Expand All @@ -28,18 +29,26 @@ class LocalIvyPublisher(localIvyRepo: os.Path) {
ctx.log.info(s"Publishing ${artifact} to ivy repo ${localIvyRepo}")
val releaseDir = localIvyRepo / artifact.group / artifact.id / artifact.version

val toCopy: Seq[(os.Path, os.Path)] = Seq(
jar -> releaseDir / "jars" / s"${artifact.id}.jar",
sourcesJar -> releaseDir / "srcs" / s"${artifact.id}-sources.jar",
docJar -> releaseDir / "docs" / s"${artifact.id}-javadoc.jar",
pom -> releaseDir / "poms" / s"${artifact.id}.pom",
ivy -> releaseDir / "ivys" / "ivy.xml"
) ++ extras.map { entry =>
(
entry.file.path,
releaseDir / s"${entry.ivyType}s" / s"${artifact.id}${entry.classifierPart}.${entry.ext}"
)
}
val sourcesToCopy = sourcesJarOpt
.toSeq
.map(sourcesJar => sourcesJar -> releaseDir / "srcs" / s"${artifact.id}-sources.jar")
val docToCopy = docJarOpt
.toSeq
.map(docJar => docJar -> releaseDir / "docs" / s"${artifact.id}-javadoc.jar")
val toCopy: Seq[(os.Path, os.Path)] =
Seq(jar -> releaseDir / "jars" / s"${artifact.id}.jar") ++
sourcesToCopy ++
docToCopy ++
Seq(
pom -> releaseDir / "poms" / s"${artifact.id}.pom",
ivy -> releaseDir / "ivys" / "ivy.xml"
) ++
extras.map { entry =>
(
entry.file.path,
releaseDir / s"${entry.ivyType}s" / s"${artifact.id}${entry.classifierPart}.${entry.ext}"
)
}

toCopy.map {
case (from, to) =>
Expand Down
Loading