Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: Bump using directives to 1.0.0 #2076

Merged
merged 8 commits into from May 9, 2023
4 changes: 2 additions & 2 deletions gifs/scenarios/demo.sh
Expand Up @@ -12,7 +12,7 @@ if [[ -z "${ASCIINEMA_REC}" ]]; then
# Warm up scala-cli
echo "println(1)" | scala-cli -
cat <<EOF > demo.test.scala |
//> using dep "org.scalameta::munit:0.7.29"
//> using dep org.scalameta::munit:0.7.29
EOF
scala-cli test demo.test.scala
# or do other preparation (e.g. create code)
Expand Down Expand Up @@ -56,7 +56,7 @@ EOF
clearConsole

cat <<EOF | updateFile demo.test.scala
//> using dep "org.scalameta::munit:0.7.29"
//> using dep org.scalameta::munit:0.7.29

class demoTest extends munit.FunSuite {
test("test nice args") {
Expand Down
4 changes: 2 additions & 2 deletions gifs/scenarios/learning_curve.sh
Expand Up @@ -19,7 +19,7 @@ else

# Put your stuff here
cat <<EOF | updateFile Hello.scala
//> using scala "3.0.2"
//> using scala 3.0.2

@main def hello() = println("Hello world from Scala CLI")
EOF
Expand All @@ -31,7 +31,7 @@ EOF
clearConsole

cat <<EOF | updateFile Hello.scala
//> using scala "2.13.6"
//> using scala 2.13.6

object Hello extends App {
println("Hello world from Scala CLI")
Expand Down
8 changes: 4 additions & 4 deletions gifs/scenarios/powerful_scripts.sh
Expand Up @@ -10,8 +10,8 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)

if [[ -z "${ASCIINEMA_REC}" ]]; then
# Warm up scala-cli
echo '//> using dep "com.lihaoyi::os-lib:0.9.1"' | scala-cli -
echo '//> using dep "com.lihaoyi::pprint:0.8.1"' | scala-cli -
echo '//> using dep com.lihaoyi::os-lib:0.9.1' | scala-cli -
echo '//> using dep com.lihaoyi::pprint:0.8.1' | scala-cli -
# or do other preparation (e.g. create code)
else
. $SCRIPT_DIR/../demo-magic.sh
Expand All @@ -20,8 +20,8 @@ else

# Put your stuff here
cat <<EOF | updateFile stat.sc
//> using dep "com.lihaoyi::os-lib:0.9.1"
//> using dep "com.lihaoyi::pprint:0.8.1"
//> using dep com.lihaoyi::os-lib:0.9.1
//> using dep com.lihaoyi::pprint:0.8.1
import pprint._
import os._

Expand Down
2 changes: 1 addition & 1 deletion gifs/scenarios/projects.sh
Expand Up @@ -21,7 +21,7 @@ else

# Put your stuff here
cat <<EOF | updateFile Post.scala
//> using dep "com.softwaremill.sttp.client3::core:3.3.18"
//> using dep com.softwaremill.sttp.client3::core:3.3.18
import sttp.client3._

// https://sttp.softwaremill.com/en/latest/quickstart.html
Expand Down
2 changes: 1 addition & 1 deletion gifs/scenarios/scripting.sh
Expand Up @@ -24,7 +24,7 @@ Hello World from file
EOF

cat <<EOF | updateFile script.sc
//> using dep "com.lihaoyi::os-lib::0.9.1"
//> using dep com.lihaoyi::os-lib::0.9.1

val filePath = os.pwd / "file"
val fileContent = os.read(filePath)
Expand Down
14 changes: 6 additions & 8 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Expand Up @@ -310,7 +310,7 @@ object CrossSources {
} yield mainClass

val pathsWithDirectivePositions
: Seq[(WithBuildRequirements[(os.Path, os.RelPath)], Option[DirectivesPositions])] =
: Seq[(WithBuildRequirements[(os.Path, os.RelPath)], Option[Position.File])] =
preprocessedSources.collect {
case d: PreprocessedSource.OnDisk =>
val baseReqs0 = baseReqs(d.scopePath)
Expand All @@ -320,7 +320,7 @@ object CrossSources {
) -> d.directivesPositions
}
val inMemoryWithDirectivePositions
: Seq[(WithBuildRequirements[Sources.InMemory], Option[DirectivesPositions])] =
: Seq[(WithBuildRequirements[Sources.InMemory], Option[Position.File])] =
preprocessedSources.collect {
case m: PreprocessedSource.InMemory =>
val baseReqs0 = baseReqs(m.scopePath)
Expand All @@ -330,7 +330,7 @@ object CrossSources {
) -> m.directivesPositions
}
val unwrappedScriptsWithDirectivePositions
: Seq[(WithBuildRequirements[Sources.UnwrappedScript], Option[DirectivesPositions])] =
: Seq[(WithBuildRequirements[Sources.UnwrappedScript], Option[Position.File])] =
preprocessedSources.collect {
case m: PreprocessedSource.UnwrappedScript =>
val baseReqs0 = baseReqs(m.scopePath)
Expand All @@ -347,10 +347,8 @@ object CrossSources {
WithBuildRequirements(BuildRequirements(), _)
)

lazy val allPathsWithDirectivesByScope: Map[Scope, Seq[(os.Path, DirectivesPositions)]] =
(pathsWithDirectivePositions ++
inMemoryWithDirectivePositions ++
unwrappedScriptsWithDirectivePositions)
lazy val allPathsWithDirectivesByScope: Map[Scope, Seq[(os.Path, Position.File)]] =
(pathsWithDirectivePositions ++ inMemoryWithDirectivePositions ++ unwrappedScriptsWithDirectivePositions)
.flatMap { (withBuildRequirements, directivesPositions) =>
val scope = withBuildRequirements.scopedValue(Scope.Main).scope
val path: os.Path = withBuildRequirements.value match
Expand Down Expand Up @@ -384,7 +382,7 @@ object CrossSources {
.foreach { (_, directivesPositions) =>
logger.diagnostic(
s"Using directives detected in multiple files. It is recommended to keep them centralized in the $projectFilePath file.",
positions = Seq(directivesPositions.all.maxBy(_.endPos._1))
positions = Seq(directivesPositions)
)
}
}
Expand Down
@@ -1,13 +1,8 @@
package scala.build.preprocessing

import com.virtuslab.using_directives.config.Settings
import com.virtuslab.using_directives.custom.model.{
UsingDirectiveKind,
UsingDirectiveSyntax,
UsingDirectives
}
import com.virtuslab.using_directives.UsingDirectivesProcessor
import com.virtuslab.using_directives.custom.model.UsingDirectives
import com.virtuslab.using_directives.custom.utils.ast.{UsingDef, UsingDefs}
import com.virtuslab.using_directives.{Context, UsingDirectivesProcessor}

import scala.annotation.targetName
import scala.build.errors.*
Expand All @@ -19,22 +14,13 @@ import scala.jdk.CollectionConverters.*

case class ExtractedDirectives(
directives: Seq[StrictDirective],
positions: Option[DirectivesPositions]
positions: Option[Position.File]
) {
@targetName("append")
def ++(other: ExtractedDirectives): ExtractedDirectives =
ExtractedDirectives(directives ++ other.directives, positions)
}

case class DirectivesPositions(
codeDirectives: Position.File,
specialCommentDirectives: Position.File,
plainCommentDirectives: Position.File
) {
def all: Seq[Position.File] =
Seq(codeDirectives, specialCommentDirectives, plainCommentDirectives)
}

object ExtractedDirectives {

def empty: ExtractedDirectives = ExtractedDirectives(Seq.empty, None)
Expand All @@ -55,14 +41,8 @@ object ExtractedDirectives {
else
errors += diag
}
val processor = {
val settings = new Settings
settings.setAllowStartWithoutAt(true)
settings.setAllowRequire(false)
val context = new Context(reporter, settings)
new UsingDirectivesProcessor(context)
}
val all = processor.extract(contentChars, true, true).asScala
val processor = new UsingDirectivesProcessor(reporter)
val allDirectives = processor.extract(contentChars).asScala
val malformedDirectiveErrors =
errors.map(diag => new MalformedDirectiveError(diag.message, diag.positions)).toSeq
val maybeCompositeMalformedDirectiveError =
Expand All @@ -71,65 +51,22 @@ object ExtractedDirectives {
else None
if (malformedDirectiveErrors.isEmpty || maybeCompositeMalformedDirectiveError.isEmpty) {

def byKind(kind: UsingDirectiveKind) = all.find(_.getKind == kind).get

val codeDirectives = byKind(UsingDirectiveKind.Code)
val specialCommentDirectives = byKind(UsingDirectiveKind.SpecialComment)
val plainCommentDirectives = byKind(UsingDirectiveKind.PlainComment)

val directivesPositionsOpt =
if (
codeDirectives.containsTargetDirectivesOnly &&
specialCommentDirectives.containsTargetDirectivesOnly &&
plainCommentDirectives.containsTargetDirectivesOnly
)
val directivesOpt = allDirectives.headOption
val directivesPositionOpt = directivesOpt match {
case Some(directives) if directives.containsTargetDirectivesOnly =>
None
else
Some(DirectivesPositions(
codeDirectives.getPosition(path),
specialCommentDirectives.getPosition(path),
plainCommentDirectives.getPosition(path)
))

def reportWarning(msg: String, values: Seq[UsingDef], before: Boolean = true): Unit =
values.foreach { v =>
val astPos = v.getPosition
val (start, end) =
if (before) (0, astPos.getColumn)
else (astPos.getColumn, astPos.getColumn + v.getSyntax.getKeyword.length)
val position = Position.File(path, (astPos.getLine, start), (astPos.getLine, end))
logger.diagnostic(msg, positions = Seq(position))
}

if (codeDirectives.nonEmpty) {
val msg =
"This using directive is ignored. Only using directives starting with //> are supported."
reportWarning(msg, getDirectives(codeDirectives))
case Some(directives) => Some(directives.getPosition(path))
case None => None
}

if (plainCommentDirectives.nonEmpty) {
val msg =
s"This using directive is ignored. $changeToSpecialCommentMsg"
reportWarning(msg, getDirectives(plainCommentDirectives))
}

val usedDirectives = specialCommentDirectives

// All using directives should use just `using` keyword, no @using or require
reportWarning(
"Deprecated using directive syntax, please use keyword `using`.",
getDirectives(specialCommentDirectives).filter(_.getSyntax != UsingDirectiveSyntax.Using),
before = false
)

val flattened = usedDirectives.getFlattenedMap.asScala.toSeq
val flattened = directivesOpt.map(_.getFlattenedMap.asScala.toSeq).getOrElse(Seq.empty)
val strictDirectives =
flattened.map {
case (k, l) =>
StrictDirective(k.getPath.asScala.mkString("."), l.asScala.toSeq)
}

Right(ExtractedDirectives(strictDirectives, directivesPositionsOpt))
Right(ExtractedDirectives(strictDirectives, directivesPositionOpt))
}
else
maybeCompositeMalformedDirectiveError match {
Expand Down
@@ -1,6 +1,5 @@
package scala.build.preprocessing

import com.virtuslab.using_directives.custom.model.UsingDirectiveKind
import coursier.cache.ArchiveCache
import coursier.util.Task

Expand Down
@@ -1,7 +1,5 @@
package scala.build.preprocessing

import com.virtuslab.using_directives.custom.model.UsingDirectiveKind

import scala.build.EitherCps.{either, value}
import scala.build.Logger
import scala.build.errors.BuildException
Expand Down
@@ -1,5 +1,6 @@
package scala.build.preprocessing

import scala.build.Position
import scala.build.internal.CodeWrapper
import scala.build.options.{BuildOptions, BuildRequirements, WithBuildRequirements}

Expand All @@ -11,7 +12,7 @@ sealed abstract class PreprocessedSource extends Product with Serializable {

def scopedRequirements: Seq[Scoped[BuildRequirements]]
def scopePath: ScopePath
def directivesPositions: Option[DirectivesPositions]
def directivesPositions: Option[Position.File]
}

object PreprocessedSource {
Expand All @@ -23,7 +24,7 @@ object PreprocessedSource {
requirements: Option[BuildRequirements],
scopedRequirements: Seq[Scoped[BuildRequirements]],
mainClassOpt: Option[String],
directivesPositions: Option[DirectivesPositions]
directivesPositions: Option[Position.File]
) extends PreprocessedSource {
def scopePath: ScopePath =
ScopePath.fromPath(path)
Expand All @@ -39,7 +40,7 @@ object PreprocessedSource {
scopedRequirements: Seq[Scoped[BuildRequirements]],
mainClassOpt: Option[String],
scopePath: ScopePath,
directivesPositions: Option[DirectivesPositions]
directivesPositions: Option[Position.File]
) extends PreprocessedSource {
def reportingPath: Either[String, os.Path] =
originalPath.map(_._2)
Expand All @@ -54,7 +55,7 @@ object PreprocessedSource {
scopedRequirements: Seq[Scoped[BuildRequirements]],
mainClassOpt: Option[String],
scopePath: ScopePath,
directivesPositions: Option[DirectivesPositions],
directivesPositions: Option[Position.File],
wrapScriptFun: CodeWrapper => (String, Int)
) extends PreprocessedSource

Expand Down
@@ -1,6 +1,5 @@
package scala.build.preprocessing

import com.virtuslab.using_directives.custom.model.UsingDirectiveKind
import dependency.AnyDependency
import dependency.parser.DependencyParser

Expand Down Expand Up @@ -33,7 +32,7 @@ case object ScalaPreprocessor extends Preprocessor {
opts: BuildOptions,
optsWithReqs: List[WithBuildRequirements[BuildOptions]],
updatedContent: Option[String],
directivesPositions: Option[DirectivesPositions]
directivesPositions: Option[Position.File]
)

object ProcessingOutput {
Expand Down Expand Up @@ -129,7 +128,7 @@ case object ScalaPreprocessor extends Preprocessor {
options: BuildOptions,
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
updatedContentOpt: Option[String],
directivesPositions: Option[DirectivesPositions]
directivesPositions: Option[Position.File]
) =
value(
process(
Expand Down
@@ -1,10 +1,6 @@
package scala.build.preprocessing

import com.virtuslab.using_directives.custom.model.{
UsingDirectiveKind,
UsingDirectiveSyntax,
UsingDirectives
}
import com.virtuslab.using_directives.custom.model.UsingDirectives
import com.virtuslab.using_directives.custom.utils.ast.UsingDefs

import scala.build.Position
Expand Down
@@ -1,15 +1,16 @@
package scala.build.preprocessing.directives

import scala.build.Position
import scala.build.options.{BuildOptions, BuildRequirements, WithBuildRequirements}
import scala.build.preprocessing.{DirectivesPositions, Scoped}
import scala.build.preprocessing.Scoped

case class PreprocessedDirectives(
globalReqs: BuildRequirements,
globalUsings: BuildOptions,
usingsWithReqs: List[WithBuildRequirements[BuildOptions]],
scopedReqs: Seq[Scoped[BuildRequirements]],
strippedContent: Option[String],
directivesPositions: Option[DirectivesPositions]
directivesPositions: Option[Position.File]
) {
def isEmpty: Boolean = globalReqs == BuildRequirements.monoid.zero &&
globalUsings == BuildOptions.monoid.zero &&
Expand Down