Skip to content

Commit 03ebfd9

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 63090b7 commit 03ebfd9

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

@@ -2099,17 +2101,128 @@ object Build {
20992101
)
21002102
)
21012103

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

21102222
lazy val dist = project.asDist(Bootstrapped)
21112223
.settings(
21122224
packResourceDir += (baseDirectory.value / "bin" -> "bin"),
2225+
packResourceDir += (target.value / "local-repo" -> "local"),
21132226
)
21142227

21152228
private def customMimaReportBinaryIssues(issueFilterLocation: String) = mimaReportBinaryIssues := {
@@ -2240,12 +2353,24 @@ object Build {
22402353
def asDist(implicit mode: Mode): Project = project.
22412354
enablePlugins(PackPlugin).
22422355
withCommonSettings.
2243-
dependsOn(`scala3-interfaces`, dottyCompiler, dottyLibrary, tastyCore, `scala3-staging`, `scala3-tasty-inspector`, scaladoc).
2244-
settings(commonDistSettings).
2356+
dependsOn(
2357+
`scala3-interfaces`,
2358+
dottyCompiler,
2359+
dottyLibrary,
2360+
tastyCore,
2361+
`scala3-staging`,
2362+
`scala3-tasty-inspector`,
2363+
scaladoc,
2364+
`scala3-sbt-bridge`, // for scala-cli
2365+
).
2366+
withDepSettings(commonDistSettings).
22452367
bootstrappedSettings(
22462368
target := baseDirectory.value / "target" // override setting in commonBootstrappedSettings
22472369
)
22482370

2371+
def withDepSettings(f: Seq[ClasspathDep[ProjectReference]] => Seq[Setting[?]]): Project =
2372+
project.settings(f(project.dependencies))
2373+
22492374
def withCommonSettings(implicit mode: Mode): Project = project.settings(mode match {
22502375
case NonBootstrapped => commonNonBootstrappedSettings
22512376
case Bootstrapped => commonBootstrappedSettings

0 commit comments

Comments
 (0)