Skip to content

Commit bed324e

Browse files
committed
Refactor sources default main class override
- it is only to be used by scripts - rename relevant fieds to imply the purpose better
1 parent bdd649e commit bed324e

File tree

8 files changed

+26
-41
lines changed

8 files changed

+26
-41
lines changed

modules/build/src/main/scala/scala/build/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object Build {
5757
MainClass.find(output)
5858
def retainedMainClass: Either[MainClassError, String] = {
5959
lazy val foundMainClasses0 = foundMainClasses()
60-
val defaultMainClassOpt = sources.mainClass
60+
val defaultMainClassOpt = sources.defaultMainClass
6161
.filter(name => foundMainClasses0.contains(name))
6262
def foundMainClass =
6363
if (foundMainClasses0.isEmpty) Left(new NoMainClassFoundError)

modules/build/src/main/scala/scala/build/CrossSources.scala

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import scala.build.preprocessing._
99
final case class CrossSources(
1010
paths: Seq[HasBuildRequirements[(os.Path, os.RelPath)]],
1111
inMemory: Seq[HasBuildRequirements[Sources.InMemory]],
12-
mainClass: Option[String],
12+
defaultMainClass: Option[String],
1313
resourceDirs: Seq[HasBuildRequirements[os.Path]],
1414
buildOptions: Seq[HasBuildRequirements[BuildOptions]]
1515
) {
@@ -49,7 +49,7 @@ final case class CrossSources(
4949
.flatMap(_.withScalaVersion(retainedScalaVersion).toSeq)
5050
.flatMap(_.withPlatform(platform.value).toSeq)
5151
.map(_.scopedValue(defaultScope)),
52-
mainClass,
52+
defaultMainClass,
5353
resourceDirs
5454
.flatMap(_.withScalaVersion(retainedScalaVersion).toSeq)
5555
.flatMap(_.withPlatform(platform.value).toSeq)
@@ -132,11 +132,8 @@ object CrossSources {
132132
)
133133
}
134134

135-
val mainClassOpt = for {
136-
mainClassPath <- inputs.mainClassElement.map {
137-
case sf: Inputs.SourceFile => ScopePath.fromPath(sf.path).path
138-
case vsf: Inputs.VirtualScalaFile => vsf.scopePath.path
139-
}
135+
val defaultMainClassOpt = for {
136+
mainClassPath <- inputs.defaultMainClassElement.map(s => ScopePath.fromPath(s.path).path)
140137
processedMainClass <- preprocessedSources.find(_.scopePath.path == mainClassPath)
141138
mainClass <- processedMainClass.mainClassOpt
142139
} yield mainClass
@@ -165,6 +162,6 @@ object CrossSources {
165162
HasBuildRequirements(BuildRequirements(), _)
166163
)
167164

168-
CrossSources(paths, inMemory, mainClassOpt, resourceDirs, buildOptions)
165+
CrossSources(paths, inMemory, defaultMainClassOpt, resourceDirs, buildOptions)
169166
}
170167
}

modules/build/src/main/scala/scala/build/Inputs.scala

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import scala.util.matching.Regex
1616

1717
final case class Inputs(
1818
elements: Seq[Inputs.Element],
19-
mainClassElement: Option[Inputs.SingleElement],
19+
defaultMainClassElement: Option[Inputs.Script],
2020
workspace: os.Path,
2121
baseProjectName: String,
2222
mayAppendHash: Boolean,
@@ -279,14 +279,11 @@ object Inputs {
279279
case _: ResourceDirectory => true
280280
case _: Virtual => true
281281
}
282-
val mainClassElemOpt = validElems
283-
.collectFirst {
284-
case f: SourceFile => f
285-
case vsf: VirtualScalaFile => vsf
286-
}
282+
// only on-disk scripts need a main class override
283+
val defaultMainClassElemOpt = validElems.collectFirst { case script: Script => script }
287284
Inputs(
288285
updatedElems,
289-
mainClassElemOpt,
286+
defaultMainClassElemOpt,
290287
workspace,
291288
baseProjectName,
292289
mayAppendHash = needsHash,
@@ -297,15 +294,14 @@ object Inputs {
297294
private val githubGistsArchiveRegex: Regex =
298295
s""":\\/\\/gist\\.github\\.com\\/[^\\/]*?\\/[^\\/]*$$""".r
299296

300-
private def resolve(path: String, content: Array[Byte]): Element = {
301-
val wrapperPath =
302-
os.sub / path.split("/").last
303-
297+
private def resolve(path: String, content: Array[Byte]): Element =
304298
if (path.endsWith(".scala")) VirtualScalaFile(content, path)
305299
else if (path.endsWith(".java")) VirtualJavaFile(content, path)
306-
else if (path.endsWith(".sc")) VirtualScript(content, path, wrapperPath)
300+
else if (path.endsWith(".sc")) {
301+
val wrapperPath = os.sub / path.split("/").last
302+
VirtualScript(content, path, wrapperPath)
303+
}
307304
else VirtualData(content, path)
308-
}
309305

310306
private def resolveZipArchive(content: Array[Byte]): Seq[Element] = {
311307
val zipInputStream = new ZipInputStream(new ByteArrayInputStream(content))
@@ -429,7 +425,7 @@ object Inputs {
429425
def empty(workspace: os.Path): Inputs =
430426
Inputs(
431427
elements = Nil,
432-
mainClassElement = None,
428+
defaultMainClassElement = None,
433429
workspace = workspace,
434430
baseProjectName = "project",
435431
mayAppendHash = true,

modules/build/src/main/scala/scala/build/ScopedSources.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import scala.build.options.{BuildOptions, HasScope, Scope}
55
final case class ScopedSources(
66
paths: Seq[HasScope[(os.Path, os.RelPath)]],
77
inMemory: Seq[HasScope[Sources.InMemory]],
8-
mainClass: Option[String],
8+
defaultMainClass: Option[String],
99
resourceDirs: Seq[HasScope[os.Path]],
1010
buildOptions: Seq[HasScope[BuildOptions]]
1111
) {
1212
def sources(scope: Scope, baseOptions: BuildOptions): Sources =
1313
Sources(
1414
paths.flatMap(_.valueFor(scope).toSeq),
1515
inMemory.flatMap(_.valueFor(scope).toSeq),
16-
mainClass,
16+
defaultMainClass,
1717
resourceDirs.flatMap(_.valueFor(scope).toSeq),
1818
buildOptions
1919
.flatMap(_.valueFor(scope).toSeq)

modules/build/src/main/scala/scala/build/Sources.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import scala.build.preprocessing._
77
final case class Sources(
88
paths: Seq[(os.Path, os.RelPath)],
99
inMemory: Seq[Sources.InMemory],
10-
mainClass: Option[String],
10+
defaultMainClass: Option[String],
1111
resourceDirs: Seq[os.Path],
1212
buildOptions: BuildOptions
1313
) {

modules/build/src/main/scala/scala/build/preprocessing/ScalaPreprocessor.scala

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.nio.charset.StandardCharsets
99
import scala.build.EitherCps.{either, value}
1010
import scala.build.Ops._
1111
import scala.build.errors._
12-
import scala.build.internal.{AmmUtil, Util}
12+
import scala.build.internal.Util
1313
import scala.build.options.{BuildOptions, BuildRequirements, ClassPathOptions, ShadowingSeq}
1414
import scala.build.preprocessing.directives._
1515
import scala.build.{Inputs, Logger, Position, Positioned}
@@ -72,16 +72,12 @@ case object ScalaPreprocessor extends Preprocessor {
7272
): Option[Either[BuildException, Seq[PreprocessedSource]]] =
7373
input match {
7474
case f: Inputs.ScalaFile =>
75-
val inferredClsName = {
76-
val (pkg, wrapper) = AmmUtil.pathToPackageWrapper(f.subPath)
77-
(pkg :+ wrapper).map(_.raw).mkString(".")
78-
}
7975
val res = either {
8076
val content = value(PreprocessingUtil.maybeRead(f.path))
8177
val scopePath = ScopePath.fromPath(f.path)
8278
val source = value(process(content, Right(f.path), scopePath / os.up, logger)) match {
8379
case None =>
84-
PreprocessedSource.OnDisk(f.path, None, None, Nil, Some(inferredClsName))
80+
PreprocessedSource.OnDisk(f.path, None, None, Nil, None)
8581
case Some(ProcessingOutput(
8682
requirements,
8783
scopedRequirements,
@@ -96,7 +92,7 @@ case object ScalaPreprocessor extends Preprocessor {
9692
Some(options),
9793
Some(requirements),
9894
scopedRequirements,
99-
Some(inferredClsName),
95+
None,
10096
scopePath
10197
)
10298
case Some(ProcessingOutput(requirements, scopedRequirements, options, None)) =>
@@ -105,7 +101,7 @@ case object ScalaPreprocessor extends Preprocessor {
105101
Some(options),
106102
Some(requirements),
107103
scopedRequirements,
108-
Some(inferredClsName)
104+
None
109105
)
110106
}
111107
Seq(source)
@@ -115,10 +111,6 @@ case object ScalaPreprocessor extends Preprocessor {
115111
case v: Inputs.VirtualScalaFile =>
116112
val res = either {
117113
val relPath = if (v.isStdin) os.sub / "stdin.scala" else v.subPath
118-
val className = {
119-
val (pkg, wrapper) = AmmUtil.pathToPackageWrapper(relPath)
120-
(pkg :+ wrapper).map(_.raw).mkString(".")
121-
}
122114
val content = new String(v.content, StandardCharsets.UTF_8)
123115
val (requirements, scopedRequirements, options, updatedContentOpt) =
124116
value(
@@ -135,7 +127,7 @@ case object ScalaPreprocessor extends Preprocessor {
135127
options = Some(options),
136128
requirements = Some(requirements),
137129
scopedRequirements,
138-
mainClassOpt = Some(className),
130+
mainClassOpt = None,
139131
scopePath = v.scopePath
140132
)
141133
Seq(s)

modules/cli/src/main/scala/scala/cli/commands/Package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ object Package extends ScalaCommand[PackageOptions] {
183183

184184
val dest = outputOpt
185185
.orElse {
186-
build.sources.mainClass
186+
build.sources.defaultMainClass
187187
.map(n => n.drop(n.lastIndexOf('.') + 1))
188188
.map(_.stripSuffix("_sc"))
189189
.map(_ + extension)

modules/cli/src/main/scala/scala/cli/packaging/Library.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object Library {
3333
manifest.getMainAttributes.put(JarAttributes.Name.MANIFEST_VERSION, "1.0")
3434

3535
if (hasActualManifest)
36-
for (mainClass <- mainClassOpt.orElse(build.sources.mainClass) if mainClass.nonEmpty)
36+
for (mainClass <- mainClassOpt.orElse(build.sources.defaultMainClass) if mainClass.nonEmpty)
3737
manifest.getMainAttributes.put(JarAttributes.Name.MAIN_CLASS, mainClass)
3838

3939
var zos: ZipOutputStream = null

0 commit comments

Comments
 (0)