Skip to content

Commit 420e245

Browse files
committed
Resolve artefacts to a local repo:
assemble a map of artefacts in maven format to both the local artefacts and library dependencies. Write them to dist/target/local-repo/maven2. Copy the local-repo to dist/target/pack/local. TODO: - evaluate how to remove lib dir in pack, only resolve from repo
1 parent 4d291fb commit 420e245

File tree

3 files changed

+148
-6
lines changed

3 files changed

+148
-6
lines changed

dist/bin/cli-common

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ function onExit() {
2424
[[ "$saved_stty" != "" ]] && restoreSttySettings
2525
exit $scala_exit_status
2626
}
27+
28+
declare -a scala_args
29+
30+
addScala () {
31+
scala_args+=("'$1'")
32+
}

dist/bin/scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,20 @@ if [ -z "$SCALA_VERSION" ]; then
4444
exit 1
4545
fi
4646

47+
MVN_REPOSITORY="file://$PROG_HOME/local/maven2"
48+
49+
# escape all script arguments
50+
while [[ $# -gt 0 ]]; do
51+
addScala "$1"
52+
shift
53+
done
54+
4755
# exec here would prevent onExit from being called, leaving terminal in unusable state
4856
[ -z "${ConEmuPID-}" -o -n "${cygwin-}" ] && export MSYSTEM= PWD= # workaround for #12405
49-
eval "\"$SCALA_CLI_LAUNCHER\"" "--cli-default-scala-version \"$SCALA_VERSION\"" "$@"
57+
eval "\"$SCALA_CLI_LAUNCHER\"" \
58+
"--cli-default-scala-version \"$SCALA_VERSION\"" \
59+
"-r \"$MVN_REPOSITORY\"" \
60+
"${scala_args[@]}"
5061
scala_exit_status=$?
5162

5263
onExit

project/Build.scala

Lines changed: 130 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import sbttastymima.TastyMiMaPlugin
2626
import sbttastymima.TastyMiMaPlugin.autoImport._
2727

2828
import scala.util.Properties.isJavaAtLeast
29+
import scala.collection.mutable
30+
2931
import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._
3032
import org.scalajs.linker.interface.{ModuleInitializer, StandardConfig}
3133

@@ -2102,17 +2104,128 @@ object Build {
21022104
)
21032105
)
21042106

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(
21062132
packMain := Map(),
21072133
publishArtifact := false,
21082134
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+
}
21112223
)
21122224

21132225
lazy val dist = project.asDist(Bootstrapped)
21142226
.settings(
21152227
packResourceDir += (baseDirectory.value / "bin" -> "bin"),
2228+
packResourceDir += (target.value / "local-repo" -> "local"),
21162229
)
21172230

21182231
private def customMimaReportBinaryIssues(issueFilterLocation: String) = mimaReportBinaryIssues := {
@@ -2243,12 +2356,24 @@ object Build {
22432356
def asDist(implicit mode: Mode): Project = project.
22442357
enablePlugins(PackPlugin).
22452358
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).
22482370
bootstrappedSettings(
22492371
target := baseDirectory.value / "target" // override setting in commonBootstrappedSettings
22502372
)
22512373

2374+
def withDepSettings(f: Seq[ClasspathDep[ProjectReference]] => Seq[Setting[?]]): Project =
2375+
project.settings(f(project.dependencies))
2376+
22522377
def withCommonSettings(implicit mode: Mode): Project = project.settings(mode match {
22532378
case NonBootstrapped => commonNonBootstrappedSettings
22542379
case Bootstrapped => commonBootstrappedSettings

0 commit comments

Comments
 (0)