Skip to content

Commit 7524d59

Browse files
authored
improvement: Bump using directives to 1.0.0 (#2076)
* improvement: Bump using directives to 1.0.0 This includes the changes to simplify using directives: - remove the possibility of nesting from API - allow to omit commas in lists of values - don't allow multiline comments - remove multiline strings - remove require and @require syntax support - allow values without quotes - remove @using - remove multiline directives * chore: Replace usages of quotes and comas, which are no longer needed * documentation: Update using directives documentation to felect recent changes * bugfix: Fix last compilation errors * chore: Update some of the tests to use the new syntax * bugfix: Remove or change tests that no longer make sense * chore: Run scalafix and update reference docs * chore: Handle empty using directives
1 parent c078147 commit 7524d59

File tree

78 files changed

+431
-784
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+431
-784
lines changed

gifs/scenarios/demo.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if [[ -z "${ASCIINEMA_REC}" ]]; then
1212
# Warm up scala-cli
1313
echo "println(1)" | scala-cli -
1414
cat <<EOF > demo.test.scala |
15-
//> using dep "org.scalameta::munit:0.7.29"
15+
//> using dep org.scalameta::munit:0.7.29
1616
EOF
1717
scala-cli test demo.test.scala
1818
# or do other preparation (e.g. create code)
@@ -56,7 +56,7 @@ EOF
5656
clearConsole
5757

5858
cat <<EOF | updateFile demo.test.scala
59-
//> using dep "org.scalameta::munit:0.7.29"
59+
//> using dep org.scalameta::munit:0.7.29
6060
6161
class demoTest extends munit.FunSuite {
6262
test("test nice args") {

gifs/scenarios/learning_curve.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ else
1919

2020
# Put your stuff here
2121
cat <<EOF | updateFile Hello.scala
22-
//> using scala "3.0.2"
22+
//> using scala 3.0.2
2323
2424
@main def hello() = println("Hello world from Scala CLI")
2525
EOF
@@ -31,7 +31,7 @@ EOF
3131
clearConsole
3232

3333
cat <<EOF | updateFile Hello.scala
34-
//> using scala "2.13.6"
34+
//> using scala 2.13.6
3535
3636
object Hello extends App {
3737
println("Hello world from Scala CLI")

gifs/scenarios/powerful_scripts.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
1010

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

2121
# Put your stuff here
2222
cat <<EOF | updateFile stat.sc
23-
//> using dep "com.lihaoyi::os-lib:0.9.1"
24-
//> using dep "com.lihaoyi::pprint:0.8.1"
23+
//> using dep com.lihaoyi::os-lib:0.9.1
24+
//> using dep com.lihaoyi::pprint:0.8.1
2525
import pprint._
2626
import os._
2727

gifs/scenarios/projects.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ else
2121

2222
# Put your stuff here
2323
cat <<EOF | updateFile Post.scala
24-
//> using dep "com.softwaremill.sttp.client3::core:3.3.18"
24+
//> using dep com.softwaremill.sttp.client3::core:3.3.18
2525
import sttp.client3._
2626
2727
// https://sttp.softwaremill.com/en/latest/quickstart.html

gifs/scenarios/scripting.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Hello World from file
2424
EOF
2525

2626
cat <<EOF | updateFile script.sc
27-
//> using dep "com.lihaoyi::os-lib::0.9.1"
27+
//> using dep com.lihaoyi::os-lib::0.9.1
2828
2929
val filePath = os.pwd / "file"
3030
val fileContent = os.read(filePath)

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ object CrossSources {
310310
} yield mainClass
311311

312312
val pathsWithDirectivePositions
313-
: Seq[(WithBuildRequirements[(os.Path, os.RelPath)], Option[DirectivesPositions])] =
313+
: Seq[(WithBuildRequirements[(os.Path, os.RelPath)], Option[Position.File])] =
314314
preprocessedSources.collect {
315315
case d: PreprocessedSource.OnDisk =>
316316
val baseReqs0 = baseReqs(d.scopePath)
@@ -320,7 +320,7 @@ object CrossSources {
320320
) -> d.directivesPositions
321321
}
322322
val inMemoryWithDirectivePositions
323-
: Seq[(WithBuildRequirements[Sources.InMemory], Option[DirectivesPositions])] =
323+
: Seq[(WithBuildRequirements[Sources.InMemory], Option[Position.File])] =
324324
preprocessedSources.collect {
325325
case m: PreprocessedSource.InMemory =>
326326
val baseReqs0 = baseReqs(m.scopePath)
@@ -330,7 +330,7 @@ object CrossSources {
330330
) -> m.directivesPositions
331331
}
332332
val unwrappedScriptsWithDirectivePositions
333-
: Seq[(WithBuildRequirements[Sources.UnwrappedScript], Option[DirectivesPositions])] =
333+
: Seq[(WithBuildRequirements[Sources.UnwrappedScript], Option[Position.File])] =
334334
preprocessedSources.collect {
335335
case m: PreprocessedSource.UnwrappedScript =>
336336
val baseReqs0 = baseReqs(m.scopePath)
@@ -347,10 +347,8 @@ object CrossSources {
347347
WithBuildRequirements(BuildRequirements(), _)
348348
)
349349

350-
lazy val allPathsWithDirectivesByScope: Map[Scope, Seq[(os.Path, DirectivesPositions)]] =
351-
(pathsWithDirectivePositions ++
352-
inMemoryWithDirectivePositions ++
353-
unwrappedScriptsWithDirectivePositions)
350+
lazy val allPathsWithDirectivesByScope: Map[Scope, Seq[(os.Path, Position.File)]] =
351+
(pathsWithDirectivePositions ++ inMemoryWithDirectivePositions ++ unwrappedScriptsWithDirectivePositions)
354352
.flatMap { (withBuildRequirements, directivesPositions) =>
355353
val scope = withBuildRequirements.scopedValue(Scope.Main).scope
356354
val path: os.Path = withBuildRequirements.value match
@@ -384,7 +382,7 @@ object CrossSources {
384382
.foreach { (_, directivesPositions) =>
385383
logger.diagnostic(
386384
s"Using directives detected in multiple files. It is recommended to keep them centralized in the $projectFilePath file.",
387-
positions = Seq(directivesPositions.all.maxBy(_.endPos._1))
385+
positions = Seq(directivesPositions)
388386
)
389387
}
390388
}

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

Lines changed: 12 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package scala.build.preprocessing
22

3-
import com.virtuslab.using_directives.config.Settings
4-
import com.virtuslab.using_directives.custom.model.{
5-
UsingDirectiveKind,
6-
UsingDirectiveSyntax,
7-
UsingDirectives
8-
}
3+
import com.virtuslab.using_directives.UsingDirectivesProcessor
4+
import com.virtuslab.using_directives.custom.model.UsingDirectives
95
import com.virtuslab.using_directives.custom.utils.ast.{UsingDef, UsingDefs}
10-
import com.virtuslab.using_directives.{Context, UsingDirectivesProcessor}
116

127
import scala.annotation.targetName
138
import scala.build.errors.*
@@ -19,22 +14,13 @@ import scala.jdk.CollectionConverters.*
1914

2015
case class ExtractedDirectives(
2116
directives: Seq[StrictDirective],
22-
positions: Option[DirectivesPositions]
17+
positions: Option[Position.File]
2318
) {
2419
@targetName("append")
2520
def ++(other: ExtractedDirectives): ExtractedDirectives =
2621
ExtractedDirectives(directives ++ other.directives, positions)
2722
}
2823

29-
case class DirectivesPositions(
30-
codeDirectives: Position.File,
31-
specialCommentDirectives: Position.File,
32-
plainCommentDirectives: Position.File
33-
) {
34-
def all: Seq[Position.File] =
35-
Seq(codeDirectives, specialCommentDirectives, plainCommentDirectives)
36-
}
37-
3824
object ExtractedDirectives {
3925

4026
def empty: ExtractedDirectives = ExtractedDirectives(Seq.empty, None)
@@ -55,14 +41,8 @@ object ExtractedDirectives {
5541
else
5642
errors += diag
5743
}
58-
val processor = {
59-
val settings = new Settings
60-
settings.setAllowStartWithoutAt(true)
61-
settings.setAllowRequire(false)
62-
val context = new Context(reporter, settings)
63-
new UsingDirectivesProcessor(context)
64-
}
65-
val all = processor.extract(contentChars, true, true).asScala
44+
val processor = new UsingDirectivesProcessor(reporter)
45+
val allDirectives = processor.extract(contentChars).asScala
6646
val malformedDirectiveErrors =
6747
errors.map(diag => new MalformedDirectiveError(diag.message, diag.positions)).toSeq
6848
val maybeCompositeMalformedDirectiveError =
@@ -71,65 +51,22 @@ object ExtractedDirectives {
7151
else None
7252
if (malformedDirectiveErrors.isEmpty || maybeCompositeMalformedDirectiveError.isEmpty) {
7353

74-
def byKind(kind: UsingDirectiveKind) = all.find(_.getKind == kind).get
75-
76-
val codeDirectives = byKind(UsingDirectiveKind.Code)
77-
val specialCommentDirectives = byKind(UsingDirectiveKind.SpecialComment)
78-
val plainCommentDirectives = byKind(UsingDirectiveKind.PlainComment)
79-
80-
val directivesPositionsOpt =
81-
if (
82-
codeDirectives.containsTargetDirectivesOnly &&
83-
specialCommentDirectives.containsTargetDirectivesOnly &&
84-
plainCommentDirectives.containsTargetDirectivesOnly
85-
)
54+
val directivesOpt = allDirectives.headOption
55+
val directivesPositionOpt = directivesOpt match {
56+
case Some(directives) if directives.containsTargetDirectivesOnly =>
8657
None
87-
else
88-
Some(DirectivesPositions(
89-
codeDirectives.getPosition(path),
90-
specialCommentDirectives.getPosition(path),
91-
plainCommentDirectives.getPosition(path)
92-
))
93-
94-
def reportWarning(msg: String, values: Seq[UsingDef], before: Boolean = true): Unit =
95-
values.foreach { v =>
96-
val astPos = v.getPosition
97-
val (start, end) =
98-
if (before) (0, astPos.getColumn)
99-
else (astPos.getColumn, astPos.getColumn + v.getSyntax.getKeyword.length)
100-
val position = Position.File(path, (astPos.getLine, start), (astPos.getLine, end))
101-
logger.diagnostic(msg, positions = Seq(position))
102-
}
103-
104-
if (codeDirectives.nonEmpty) {
105-
val msg =
106-
"This using directive is ignored. Only using directives starting with //> are supported."
107-
reportWarning(msg, getDirectives(codeDirectives))
58+
case Some(directives) => Some(directives.getPosition(path))
59+
case None => None
10860
}
10961

110-
if (plainCommentDirectives.nonEmpty) {
111-
val msg =
112-
s"This using directive is ignored. $changeToSpecialCommentMsg"
113-
reportWarning(msg, getDirectives(plainCommentDirectives))
114-
}
115-
116-
val usedDirectives = specialCommentDirectives
117-
118-
// All using directives should use just `using` keyword, no @using or require
119-
reportWarning(
120-
"Deprecated using directive syntax, please use keyword `using`.",
121-
getDirectives(specialCommentDirectives).filter(_.getSyntax != UsingDirectiveSyntax.Using),
122-
before = false
123-
)
124-
125-
val flattened = usedDirectives.getFlattenedMap.asScala.toSeq
62+
val flattened = directivesOpt.map(_.getFlattenedMap.asScala.toSeq).getOrElse(Seq.empty)
12663
val strictDirectives =
12764
flattened.map {
12865
case (k, l) =>
12966
StrictDirective(k.getPath.asScala.mkString("."), l.asScala.toSeq)
13067
}
13168

132-
Right(ExtractedDirectives(strictDirectives, directivesPositionsOpt))
69+
Right(ExtractedDirectives(strictDirectives, directivesPositionOpt))
13370
}
13471
else
13572
maybeCompositeMalformedDirectiveError match {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package scala.build.preprocessing
22

3-
import com.virtuslab.using_directives.custom.model.UsingDirectiveKind
43
import coursier.cache.ArchiveCache
54
import coursier.util.Task
65

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package scala.build.preprocessing
22

3-
import com.virtuslab.using_directives.custom.model.UsingDirectiveKind
4-
53
import scala.build.EitherCps.{either, value}
64
import scala.build.Logger
75
import scala.build.errors.BuildException

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package scala.build.preprocessing
22

3+
import scala.build.Position
34
import scala.build.internal.CodeWrapper
45
import scala.build.options.{BuildOptions, BuildRequirements, WithBuildRequirements}
56

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

1213
def scopedRequirements: Seq[Scoped[BuildRequirements]]
1314
def scopePath: ScopePath
14-
def directivesPositions: Option[DirectivesPositions]
15+
def directivesPositions: Option[Position.File]
1516
}
1617

1718
object PreprocessedSource {
@@ -23,7 +24,7 @@ object PreprocessedSource {
2324
requirements: Option[BuildRequirements],
2425
scopedRequirements: Seq[Scoped[BuildRequirements]],
2526
mainClassOpt: Option[String],
26-
directivesPositions: Option[DirectivesPositions]
27+
directivesPositions: Option[Position.File]
2728
) extends PreprocessedSource {
2829
def scopePath: ScopePath =
2930
ScopePath.fromPath(path)
@@ -39,7 +40,7 @@ object PreprocessedSource {
3940
scopedRequirements: Seq[Scoped[BuildRequirements]],
4041
mainClassOpt: Option[String],
4142
scopePath: ScopePath,
42-
directivesPositions: Option[DirectivesPositions]
43+
directivesPositions: Option[Position.File]
4344
) extends PreprocessedSource {
4445
def reportingPath: Either[String, os.Path] =
4546
originalPath.map(_._2)
@@ -54,7 +55,7 @@ object PreprocessedSource {
5455
scopedRequirements: Seq[Scoped[BuildRequirements]],
5556
mainClassOpt: Option[String],
5657
scopePath: ScopePath,
57-
directivesPositions: Option[DirectivesPositions],
58+
directivesPositions: Option[Position.File],
5859
wrapScriptFun: CodeWrapper => (String, Int)
5960
) extends PreprocessedSource
6061

0 commit comments

Comments
 (0)