@@ -26,6 +26,8 @@ import sbttastymima.TastyMiMaPlugin
26
26
import sbttastymima .TastyMiMaPlugin .autoImport ._
27
27
28
28
import scala .util .Properties .isJavaAtLeast
29
+ import scala .collection .mutable
30
+
29
31
import org .portablescala .sbtplatformdeps .PlatformDepsPlugin .autoImport ._
30
32
import org .scalajs .linker .interface .{ModuleInitializer , StandardConfig }
31
33
@@ -2102,17 +2104,128 @@ object Build {
2102
2104
)
2103
2105
)
2104
2106
2105
- lazy val commonDistSettings = Seq (
2107
+ lazy val DistCacheConfig = config(" DistCacheConfig" ) extend Compile
2108
+
2109
+ val distModules = taskKey[Seq [(ModuleID , Map [Artifact , File ])]](" fetch local artifacts for distribution." )
2110
+ val distResolvedArtifacts = taskKey[Seq [ResolvedArtifacts ]](" Resolve the dependencies for the distribution" )
2111
+ val distCaching = taskKey[File ](" cache the dependencies for the distribution" )
2112
+
2113
+ def evalPublishSteps (dependencies : Seq [ProjectReference ]): Def .Initialize [Task [Seq [(ModuleID , Map [Artifact , File ])]]] = {
2114
+ val publishAllLocalBin = dependencies.map({ d => ((d / publishLocalBin / packagedArtifacts)) }).join
2115
+ val resolveId = dependencies.map({ d => ((d / projectID)) }).join
2116
+ Def .task {
2117
+ val s = streams.value
2118
+ val log = s.log
2119
+ val published = publishAllLocalBin.value
2120
+ val ids = resolveId.value
2121
+
2122
+ ids.zip(published)
2123
+ }
2124
+ }
2125
+
2126
+ case class SimpleModuleId (org : String , name : String , revision : String ) {
2127
+ override def toString = s " $org: $name: $revision"
2128
+ }
2129
+ case class ResolvedArtifacts (id : SimpleModuleId , jar : File , pom : File )
2130
+
2131
+ def commonDistSettings (dependencies : Seq [ClasspathDep [ProjectReference ]]) = Seq (
2106
2132
packMain := Map (),
2107
2133
publishArtifact := false ,
2108
2134
packGenerateMakefile := false ,
2109
- packExpandedClasspath := true ,
2110
- packArchiveName := " scala3-" + dottyVersion
2135
+ packArchiveName := " scala3-" + dottyVersion,
2136
+ DistCacheConfig / distModules := {
2137
+ evalPublishSteps(dependencies.map(_.project)).value
2138
+ },
2139
+ DistCacheConfig / distResolvedArtifacts := {
2140
+ val localArtifactIds = (DistCacheConfig / distModules).value
2141
+ val report = (thisProjectRef / updateFull).value
2142
+
2143
+ val found = mutable.Map .empty[SimpleModuleId , ResolvedArtifacts ]
2144
+ val evicted = mutable.Set .empty[SimpleModuleId ]
2145
+
2146
+ localArtifactIds.foreach({ case (id, as) =>
2147
+ val simpleId = {
2148
+ val name0 = id.crossVersion match {
2149
+ case _ : CrossVersion .Binary =>
2150
+ // projectID does not add binary suffix
2151
+ (id.name + " _3" ).ensuring(! id.name.endsWith(" _3" ) && id.revision.startsWith(" 3." ))
2152
+ case _ => id.name
2153
+ }
2154
+ SimpleModuleId (id.organization, name0, id.revision)
2155
+ }
2156
+ var jarOrNull : File = null
2157
+ var pomOrNull : File = null
2158
+ as.foreach({ case (a, f) =>
2159
+ if (a.`type` == " jar" ) {
2160
+ jarOrNull = f
2161
+ } else if (a.`type` == " pom" ) {
2162
+ pomOrNull = f
2163
+ }
2164
+ })
2165
+ assert(jarOrNull != null , s " Could not find jar for ${id}" )
2166
+ assert(pomOrNull != null , s " Could not find pom for ${id}" )
2167
+ evicted += simpleId.copy(revision = simpleId.revision + " -nonbootstrapped" )
2168
+ found(simpleId) = ResolvedArtifacts (simpleId, jarOrNull, pomOrNull)
2169
+ })
2170
+
2171
+ report.allModuleReports.foreach { mr =>
2172
+ val simpleId = {
2173
+ val id = mr.module
2174
+ SimpleModuleId (id.organization, id.name, id.revision)
2175
+ }
2176
+
2177
+ if (! found.contains(simpleId) && ! evicted(simpleId)) {
2178
+ var jarOrNull : File = null
2179
+ var pomOrNull : File = null
2180
+ mr.artifacts.foreach({ case (a, f) =>
2181
+ if (a.`type` == " jar" || a.`type` == " bundle" ) {
2182
+ jarOrNull = f
2183
+ } else if (a.`type` == " pom" ) {
2184
+ pomOrNull = f
2185
+ }
2186
+ })
2187
+ assert(jarOrNull != null , s " Could not find jar for ${simpleId}" )
2188
+ if (pomOrNull == null ) {
2189
+ val jarPath = jarOrNull.toPath
2190
+ // we found the jar, so assume we can resolve a sibling pom file
2191
+ val pomPath = jarPath.resolveSibling(jarPath.getFileName.toString.stripSuffix(" .jar" ) + " .pom" )
2192
+ assert(Files .exists(pomPath), s " Could not find pom for ${simpleId}" )
2193
+ pomOrNull = pomPath.toFile
2194
+ }
2195
+ found(simpleId) = ResolvedArtifacts (simpleId, jarOrNull, pomOrNull)
2196
+ }
2197
+
2198
+ }
2199
+ found.values.toSeq
2200
+ },
2201
+ DistCacheConfig / distCaching := {
2202
+ val resolved = (DistCacheConfig / distResolvedArtifacts).value
2203
+ val targetDir = target.value
2204
+ val cacheDir = targetDir / " local-repo"
2205
+ val mavenRepo = cacheDir / " maven2"
2206
+ IO .createDirectory(mavenRepo)
2207
+ resolved.foreach { ra =>
2208
+ val jar = ra.jar
2209
+ val pom = ra.pom
2210
+
2211
+ val pathElems = ra.id.org.split('.' ).toVector :+ ra.id.name :+ ra.id.revision
2212
+ val artifactDir = pathElems.foldLeft(mavenRepo)(_ / _)
2213
+ IO .createDirectory(artifactDir)
2214
+ IO .copyFile(jar, artifactDir / jar.getName)
2215
+ IO .copyFile(pom, artifactDir / pom.getName)
2216
+ }
2217
+ cacheDir
2218
+ },
2219
+ Compile / pack := {
2220
+ val localRepo = (DistCacheConfig / distCaching).value
2221
+ (Compile / pack).value
2222
+ }
2111
2223
)
2112
2224
2113
2225
lazy val dist = project.asDist(Bootstrapped )
2114
2226
.settings(
2115
2227
packResourceDir += (baseDirectory.value / " bin" -> " bin" ),
2228
+ packResourceDir += (target.value / " local-repo" -> " local" ),
2116
2229
)
2117
2230
2118
2231
private def customMimaReportBinaryIssues (issueFilterLocation : String ) = mimaReportBinaryIssues := {
@@ -2243,12 +2356,24 @@ object Build {
2243
2356
def asDist (implicit mode : Mode ): Project = project.
2244
2357
enablePlugins(PackPlugin ).
2245
2358
withCommonSettings.
2246
- dependsOn(`scala3-interfaces`, dottyCompiler, dottyLibrary, tastyCore, `scala3-staging`, `scala3-tasty-inspector`, scaladoc).
2247
- settings(commonDistSettings).
2359
+ dependsOn(
2360
+ `scala3-interfaces`,
2361
+ dottyCompiler,
2362
+ dottyLibrary,
2363
+ tastyCore,
2364
+ `scala3-staging`,
2365
+ `scala3-tasty-inspector`,
2366
+ scaladoc,
2367
+ `scala3-sbt-bridge`, // for scala-cli
2368
+ ).
2369
+ withDepSettings(commonDistSettings).
2248
2370
bootstrappedSettings(
2249
2371
target := baseDirectory.value / " target" // override setting in commonBootstrappedSettings
2250
2372
)
2251
2373
2374
+ def withDepSettings (f : Seq [ClasspathDep [ProjectReference ]] => Seq [Setting [? ]]): Project =
2375
+ project.settings(f(project.dependencies))
2376
+
2252
2377
def withCommonSettings (implicit mode : Mode ): Project = project.settings(mode match {
2253
2378
case NonBootstrapped => commonNonBootstrappedSettings
2254
2379
case Bootstrapped => commonBootstrappedSettings
0 commit comments