Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.build.internal.Constants
import scala.build.options.{BuildOptions, Scope, SuppressWarningOptions}
import scala.build.preprocessing.directives.*
import scala.build.preprocessing.{ExtractedDirectives, SheBang}
import scala.build.{CrossSources, Logger, Position, Sources}
import scala.build.{Artifacts, CrossSources, Logger, Position, Sources}
import scala.cli.commands.util.CommandHelpers
import scala.util.chaining.scalaUtilChainingOps

Expand All @@ -33,25 +33,80 @@ object BuiltInRules extends CommandHelpers {
private val newLine: String = scala.build.internal.AmmUtil.lineSeparator

def runRules(
options: FixOptions,
inputs: Inputs,
buildOptions: BuildOptions,
logger: Logger
)(using ScalaCliInvokeData): Unit = {
val (mainSources, testSources) = getProjectSources(inputs, logger)
.left.map(CompositeBuildException(_))
.orExit(logger)

val sourcesCount =
mainSources.paths.length + mainSources.inMemory.length +
testSources.paths.length + testSources.inMemory.length
sourcesCount match
case 0 =>
logger.message("No sources to migrate directives from.")
logger.message("Nothing to do.")
case 1 =>
logger.message("No need to migrate directives for a single source file project.")
logger.message("Nothing to do.")
case _ => migrateDirectives(inputs, buildOptions, mainSources, testSources, logger)
if (options.enableBuiltInRules) {
val (mainSources, testSources) = getProjectSources(inputs, logger)
.left.map(CompositeBuildException(_))
.orExit(logger)

val sourcesCount =
mainSources.paths.length + mainSources.inMemory.length +
testSources.paths.length + testSources.inMemory.length
sourcesCount match
case 0 =>
logger.message("No sources to migrate directives from.")
logger.message("Nothing to do.")
case 1 =>
logger.message("No need to migrate directives for a single source file project.")
logger.message("Nothing to do.")
case _ => migrateDirectives(inputs, buildOptions, mainSources, testSources, logger)
}

if (options.checkUnusedDependencies || options.checkExplicitDependencies) {
val (crossSources, _) = CrossSources.forInputs(
inputs,
preprocessors = Sources.defaultPreprocessors(
buildOptions.archiveCache,
buildOptions.internal.javaClassNameVersionOpt,
() => buildOptions.javaHome().value.javaCommand
),
logger = logger,
suppressWarningOptions = buildOptions.suppressWarningOptions,
exclude = buildOptions.internal.exclude,
download = buildOptions.downloader
).orExit(logger)

val sharedOptions0 = crossSources.sharedOptions(buildOptions)
val sharedOptions = sharedOptions0.copy(
internal = sharedOptions0.internal.copy(keepResolution = true)
)
val scopedSources = crossSources.scopedSources(sharedOptions).orExit(logger)
val sources = scopedSources.sources(
Scope.Main,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A potential follow-up would be to respect the --test flag and if it's enabled, include the test scope in the analysis as well.
Doesn't have to be in this PR, can create a ticket to track it instead.

sharedOptions,
inputs.workspace,
logger
).orExit(logger)

val artifacts = sharedOptions.artifacts(logger, Scope.Main).orExit(logger)

val analysisResultEither = DependencyAnalyzer.analyzeDependencies(
sources,
sharedOptions,
artifacts,
logger
)

if (analysisResultEither.isRight) {
val analysisResult = analysisResultEither.toOption.get
if (options.checkUnusedDependencies)
DependencyAnalyzer.reportUnusedDependencies(analysisResult.unusedDependencies, logger)

if (options.checkExplicitDependencies)
DependencyAnalyzer.reportMissingDependencies(
analysisResult.missingExplicitDependencies,
logger
)
}
else
logger.error(
s"Dependency analysis failed: ${analysisResultEither.left.getOrElse("Unknown error")}"
)
}
}

private def migrateDirectives(
Expand Down
Loading