Skip to content

Commit 237a592

Browse files
bishaboshaKordyjan
authored andcommitted
refactor republishing to a plugin
[Cherry-picked b1cd484]
1 parent 10bd87f commit 237a592

File tree

2 files changed

+144
-113
lines changed

2 files changed

+144
-113
lines changed

project/Build.scala

Lines changed: 7 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import pl.project13.scala.sbt.JmhPlugin
1212
import pl.project13.scala.sbt.JmhPlugin.JmhKeys.Jmh
1313
import sbt.Package.ManifestAttributes
1414
import sbt.PublishBinPlugin.autoImport._
15+
import dotty.tools.sbtplugin.RepublishPlugin
16+
import dotty.tools.sbtplugin.RepublishPlugin.autoImport._
1517
import sbt.plugins.SbtPlugin
1618
import sbt.ScriptedPlugin.autoImport._
1719
import xerial.sbt.pack.PackPlugin
@@ -2112,120 +2114,14 @@ object Build {
21122114
)
21132115
)
21142116

2115-
lazy val DistCacheConfig = config("DistCacheConfig") extend Compile
2116-
2117-
val distModules = taskKey[Seq[(ModuleID, Map[Artifact, File])]]("fetch local artifacts for distribution.")
2118-
val distResolvedArtifacts = taskKey[Seq[ResolvedArtifacts]]("Resolve the dependencies for the distribution")
2119-
val distCaching = taskKey[File]("cache the dependencies for the distribution")
2120-
2121-
def evalPublishSteps(dependencies: Seq[ProjectReference]): Def.Initialize[Task[Seq[(ModuleID, Map[Artifact, File])]]] = {
2122-
val publishAllLocalBin = dependencies.map({ d => ((d / publishLocalBin / packagedArtifacts)) }).join
2123-
val resolveId = dependencies.map({ d => ((d / projectID)) }).join
2124-
Def.task {
2125-
val s = streams.value
2126-
val log = s.log
2127-
val published = publishAllLocalBin.value
2128-
val ids = resolveId.value
2129-
2130-
ids.zip(published)
2131-
}
2132-
}
2133-
2134-
case class SimpleModuleId(org: String, name: String, revision: String) {
2135-
override def toString = s"$org:$name:$revision"
2136-
}
2137-
case class ResolvedArtifacts(id: SimpleModuleId, jar: File, pom: File)
2138-
2139-
def commonDistSettings(dependencies: Seq[ClasspathDep[ProjectReference]]) = Seq(
2117+
lazy val commonDistSettings = Seq(
21402118
packMain := Map(),
21412119
publishArtifact := false,
21422120
packGenerateMakefile := false,
21432121
packArchiveName := "scala3-" + dottyVersion,
2144-
DistCacheConfig / distModules := {
2145-
evalPublishSteps(dependencies.map(_.project)).value
2146-
},
2147-
DistCacheConfig / distResolvedArtifacts := {
2148-
val localArtifactIds = (DistCacheConfig / distModules).value
2149-
val report = (thisProjectRef / updateFull).value
2150-
2151-
val found = mutable.Map.empty[SimpleModuleId, ResolvedArtifacts]
2152-
val evicted = mutable.Set.empty[SimpleModuleId]
2153-
2154-
localArtifactIds.foreach({ case (id, as) =>
2155-
val simpleId = {
2156-
val name0 = id.crossVersion match {
2157-
case _: CrossVersion.Binary =>
2158-
// projectID does not add binary suffix
2159-
(id.name + "_3").ensuring(!id.name.endsWith("_3") && id.revision.startsWith("3."))
2160-
case _ => id.name
2161-
}
2162-
SimpleModuleId(id.organization, name0, id.revision)
2163-
}
2164-
var jarOrNull: File = null
2165-
var pomOrNull: File = null
2166-
as.foreach({ case (a, f) =>
2167-
if (a.`type` == "jar") {
2168-
jarOrNull = f
2169-
} else if (a.`type` == "pom") {
2170-
pomOrNull = f
2171-
}
2172-
})
2173-
assert(jarOrNull != null, s"Could not find jar for ${id}")
2174-
assert(pomOrNull != null, s"Could not find pom for ${id}")
2175-
evicted += simpleId.copy(revision = simpleId.revision + "-nonbootstrapped")
2176-
found(simpleId) = ResolvedArtifacts(simpleId, jarOrNull, pomOrNull)
2177-
})
2178-
2179-
report.allModuleReports.foreach { mr =>
2180-
val simpleId = {
2181-
val id = mr.module
2182-
SimpleModuleId(id.organization, id.name, id.revision)
2183-
}
2184-
2185-
if (!found.contains(simpleId) && !evicted(simpleId)) {
2186-
var jarOrNull: File = null
2187-
var pomOrNull: File = null
2188-
mr.artifacts.foreach({ case (a, f) =>
2189-
if (a.`type` == "jar" || a.`type` == "bundle") {
2190-
jarOrNull = f
2191-
} else if (a.`type` == "pom") {
2192-
pomOrNull = f
2193-
}
2194-
})
2195-
assert(jarOrNull != null, s"Could not find jar for ${simpleId}")
2196-
if (pomOrNull == null) {
2197-
val jarPath = jarOrNull.toPath
2198-
// we found the jar, so assume we can resolve a sibling pom file
2199-
val pomPath = jarPath.resolveSibling(jarPath.getFileName.toString.stripSuffix(".jar") + ".pom")
2200-
assert(Files.exists(pomPath), s"Could not find pom for ${simpleId}")
2201-
pomOrNull = pomPath.toFile
2202-
}
2203-
found(simpleId) = ResolvedArtifacts(simpleId, jarOrNull, pomOrNull)
2204-
}
2205-
2206-
}
2207-
found.values.toSeq
2208-
},
2209-
DistCacheConfig / distCaching := {
2210-
val resolved = (DistCacheConfig / distResolvedArtifacts).value
2211-
val targetDir = target.value
2212-
val cacheDir = targetDir / "local-repo"
2213-
val mavenRepo = cacheDir / "maven2"
2214-
IO.createDirectory(mavenRepo)
2215-
resolved.foreach { ra =>
2216-
val jar = ra.jar
2217-
val pom = ra.pom
2218-
2219-
val pathElems = ra.id.org.split('.').toVector :+ ra.id.name :+ ra.id.revision
2220-
val artifactDir = pathElems.foldLeft(mavenRepo)(_ / _)
2221-
IO.createDirectory(artifactDir)
2222-
IO.copyFile(jar, artifactDir / jar.getName)
2223-
IO.copyFile(pom, artifactDir / pom.getName)
2224-
}
2225-
cacheDir
2226-
},
2122+
republishRepo := target.value / "local-repo",
22272123
Compile / pack := {
2228-
val localRepo = (DistCacheConfig / distCaching).value
2124+
val localRepo = republishClasspath.value // republish all artifacts to local repo
22292125
(Compile / pack).value
22302126
}
22312127
)
@@ -2363,7 +2259,9 @@ object Build {
23632259

23642260
def asDist(implicit mode: Mode): Project = project.
23652261
enablePlugins(PackPlugin).
2262+
enablePlugins(RepublishPlugin).
23662263
withCommonSettings.
2264+
settings(commonDistSettings).
23672265
dependsOn(
23682266
`scala3-interfaces`,
23692267
dottyCompiler,
@@ -2374,14 +2272,10 @@ object Build {
23742272
scaladoc,
23752273
`scala3-sbt-bridge`, // for scala-cli
23762274
).
2377-
withDepSettings(commonDistSettings).
23782275
bootstrappedSettings(
23792276
target := baseDirectory.value / "target" // override setting in commonBootstrappedSettings
23802277
)
23812278

2382-
def withDepSettings(f: Seq[ClasspathDep[ProjectReference]] => Seq[Setting[?]]): Project =
2383-
project.settings(f(project.dependencies))
2384-
23852279
def withCommonSettings(implicit mode: Mode): Project = project.settings(mode match {
23862280
case NonBootstrapped => commonNonBootstrappedSettings
23872281
case Bootstrapped => commonBootstrappedSettings

project/RepublishPlugin.scala

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package dotty.tools.sbtplugin
2+
3+
import sbt._
4+
import xerial.sbt.pack.PackPlugin
5+
import sbt.Keys._
6+
import sbt.AutoPlugin
7+
import sbt.PublishBinPlugin
8+
import sbt.PublishBinPlugin.autoImport._
9+
10+
import scala.collection.mutable
11+
import java.nio.file.Files
12+
13+
/** This local plugin provides ways of publishing a project classpath and library dependencies to
14+
* .a local repository */
15+
object RepublishPlugin extends AutoPlugin {
16+
override def trigger = allRequirements
17+
override def requires = super.requires && PublishBinPlugin && PackPlugin
18+
19+
object autoImport {
20+
val republishProjectRefs = taskKey[Seq[ProjectRef]]("fetch the classpath deps from the project.")
21+
val republishLocalResolved = taskKey[Seq[ResolvedArtifacts]]("resolve local artifacts for distribution.")
22+
val republishAllResolved = taskKey[Seq[ResolvedArtifacts]]("Resolve the dependencies for the distribution")
23+
val republishClasspath = taskKey[File]("cache the dependencies for the distribution")
24+
val republishRepo = settingKey[File]("the location to store the republished artifacts.")
25+
}
26+
27+
import autoImport._
28+
29+
case class SimpleModuleId(org: String, name: String, revision: String) {
30+
override def toString = s"$org:$name:$revision"
31+
}
32+
case class ResolvedArtifacts(id: SimpleModuleId, jar: File, pom: File)
33+
34+
override val projectSettings: Seq[Def.Setting[_]] = Def.settings(
35+
republishLocalResolved / republishProjectRefs := {
36+
val proj = thisProjectRef.value
37+
val deps = buildDependencies.value
38+
39+
deps.classpathRefs(proj)
40+
},
41+
republishLocalResolved := Def.taskDyn {
42+
val deps = (republishLocalResolved / republishProjectRefs).value
43+
val publishAllLocalBin = deps.map({ d => ((d / publishLocalBin / packagedArtifacts)) }).join
44+
val resolveId = deps.map({ d => ((d / projectID)) }).join
45+
Def.task {
46+
val s = streams.value
47+
val log = s.log
48+
val published = publishAllLocalBin.value
49+
val ids = resolveId.value
50+
51+
ids.zip(published).map({ case (id, as) =>
52+
val simpleId = {
53+
val name0 = id.crossVersion match {
54+
case _: CrossVersion.Binary =>
55+
// projectID does not add binary suffix
56+
(id.name + "_3").ensuring(!id.name.endsWith("_3") && id.revision.startsWith("3."))
57+
case _ => id.name
58+
}
59+
SimpleModuleId(id.organization, name0, id.revision)
60+
}
61+
var jarOrNull: File = null
62+
var pomOrNull: File = null
63+
as.foreach({ case (a, f) =>
64+
if (a.`type` == "jar") {
65+
jarOrNull = f
66+
} else if (a.`type` == "pom") {
67+
pomOrNull = f
68+
}
69+
})
70+
assert(jarOrNull != null, s"Could not find jar for ${id}")
71+
assert(pomOrNull != null, s"Could not find pom for ${id}")
72+
ResolvedArtifacts(simpleId, jarOrNull, pomOrNull)
73+
})
74+
}
75+
}.value,
76+
republishAllResolved := {
77+
val localResolved = republishLocalResolved.value
78+
val report = (thisProjectRef / updateFull).value
79+
80+
val found = mutable.Map.empty[SimpleModuleId, ResolvedArtifacts]
81+
val evicted = mutable.Set.empty[SimpleModuleId]
82+
83+
localResolved.foreach({ resolved =>
84+
val simpleId = resolved.id
85+
evicted += simpleId.copy(revision = simpleId.revision + "-nonbootstrapped")
86+
found(simpleId) = resolved
87+
})
88+
89+
report.allModuleReports.foreach { mr =>
90+
val simpleId = {
91+
val id = mr.module
92+
SimpleModuleId(id.organization, id.name, id.revision)
93+
}
94+
95+
if (!found.contains(simpleId) && !evicted(simpleId)) {
96+
var jarOrNull: File = null
97+
var pomOrNull: File = null
98+
mr.artifacts.foreach({ case (a, f) =>
99+
if (a.`type` == "jar" || a.`type` == "bundle") {
100+
jarOrNull = f
101+
} else if (a.`type` == "pom") {
102+
pomOrNull = f
103+
}
104+
})
105+
assert(jarOrNull != null, s"Could not find jar for ${simpleId}")
106+
if (pomOrNull == null) {
107+
val jarPath = jarOrNull.toPath
108+
// we found the jar, so assume we can resolve a sibling pom file
109+
val pomPath = jarPath.resolveSibling(jarPath.getFileName.toString.stripSuffix(".jar") + ".pom")
110+
assert(Files.exists(pomPath), s"Could not find pom for ${simpleId}")
111+
pomOrNull = pomPath.toFile
112+
}
113+
found(simpleId) = ResolvedArtifacts(simpleId, jarOrNull, pomOrNull)
114+
}
115+
116+
}
117+
found.values.toSeq
118+
},
119+
republishClasspath := {
120+
val resolved = republishAllResolved.value
121+
val cacheDir = republishRepo.value
122+
val mavenRepo = cacheDir / "maven2"
123+
IO.createDirectory(mavenRepo)
124+
resolved.foreach { ra =>
125+
val jar = ra.jar
126+
val pom = ra.pom
127+
128+
val pathElems = ra.id.org.split('.').toVector :+ ra.id.name :+ ra.id.revision
129+
val artifactDir = pathElems.foldLeft(mavenRepo)(_ / _)
130+
IO.createDirectory(artifactDir)
131+
IO.copyFile(jar, artifactDir / jar.getName)
132+
IO.copyFile(pom, artifactDir / pom.getName)
133+
}
134+
cacheDir
135+
}
136+
)
137+
}

0 commit comments

Comments
 (0)