Skip to content

Commit ab9075e

Browse files
authored
Merge pull request #3372 from horothesun/patch-1
`scala-cli`: `using dep` and `using test.dep` directives support
2 parents 0765eec + fb3c668 commit ab9075e

File tree

3 files changed

+166
-4
lines changed

3 files changed

+166
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Thanks goes to these wonderful people for contributing to Scala Steward:
140140
* [miguelpuyol](https://github.com/miguelpuyol)
141141
* [nafg](https://github.com/nafg)
142142
* [Nabil Abdel-Hafeez](https://github.com/987Nabil)
143+
* [Nicola Di Pol](https://github.com/horothesun)
143144
* [Ondra Pelech](https://github.com/sideeffffect)
144145
* [Pavel Shapkin](https://github.com/psttf)
145146
* [Philippus Baalman](https://github.com/Philippus)

modules/core/src/main/scala/org/scalasteward/core/edit/update/Selector.scala

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ object Selector {
7070
versionPositions
7171
.collect { case p: DependencyDef => p }
7272
.filter {
73-
case p: MillDependency => scalaCliUsingLib.matcher(p.before).matches() || !p.isCommented
74-
case p: SbtDependency => !p.isCommented && !p.before.toLowerCase.contains("previous")
75-
case _ => true
73+
case p: MillDependency =>
74+
scalaCliUsingLib.matcher(p.before).matches() ||
75+
scalaCliUsingDep.matcher(p.before).matches() ||
76+
scalaCliUsingTestDep.matcher(p.before).matches() ||
77+
!p.isCommented
78+
case p: SbtDependency => !p.isCommented && !p.before.toLowerCase.contains("previous")
79+
case _ => true
7680
}
7781
.filter { p =>
7882
val artifactIdNames = Set(p.artifactId, p.artifactId.takeWhile(_ =!= '_'))
@@ -81,9 +85,15 @@ object Selector {
8185
}
8286
}
8387

84-
private def scalaCliUsingLib: Pattern =
88+
private val scalaCliUsingLib: Pattern =
8589
Pattern.compile("""//>\s+using\s+lib\s+""")
8690

91+
private val scalaCliUsingDep: Pattern =
92+
Pattern.compile("""//>\s+using\s+dep\s+""")
93+
94+
private val scalaCliUsingTestDep: Pattern =
95+
Pattern.compile("""//>\s+using\s+test\.dep\s+""")
96+
8797
private def scalaValInDependencyDefPositions(
8898
versionPositions: List[VersionPosition],
8999
modulePositions: List[ModulePosition]

modules/core/src/test/scala/org/scalasteward/core/edit/EditAlgTest.scala

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,155 @@ class EditAlgTest extends FunSuite {
134134
case Log(_) => false
135135
})
136136
}
137+
138+
test("applyUpdate for scala-cli using lib") {
139+
val repo = Repo("edit-alg", "test-4")
140+
val data = RepoData(repo, dummyRepoCache, RepoConfig.empty)
141+
val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
142+
val mainSc = repoDir / "main.sc"
143+
val update = ("org.typelevel".g % "cats-effect".a % "3.5.2" %> "3.5.4").single
144+
145+
val state = MockState.empty
146+
.copy(execCommands = true)
147+
.initGitRepo(
148+
repoDir,
149+
mainSc ->
150+
"""
151+
|//> using scala "3.4.2"
152+
|//> using jvm "temurin:21"
153+
|//> using lib "org.typelevel::cats-effect:3.5.2"
154+
|println("Hello!")
155+
|""".stripMargin
156+
)
157+
.flatMap(editAlg.applyUpdate(data, update).runS)
158+
.unsafeRunSync()
159+
160+
val expected = MockState.empty.copy(
161+
execCommands = true,
162+
trace = Vector(
163+
Cmd.gitGrep(repoDir, update.currentVersion.value),
164+
Cmd("read", mainSc.pathAsString),
165+
Cmd.gitGrep(repoDir, update.groupId.value),
166+
Cmd("read", mainSc.pathAsString),
167+
Cmd("read", mainSc.pathAsString),
168+
Cmd("write", mainSc.pathAsString),
169+
Cmd.gitStatus(repoDir),
170+
Cmd.gitCommit(repoDir, "Update cats-effect to 3.5.4"),
171+
Cmd.gitLatestSha1(repoDir)
172+
),
173+
files = Map(
174+
mainSc ->
175+
"""
176+
|//> using scala "3.4.2"
177+
|//> using jvm "temurin:21"
178+
|//> using lib "org.typelevel::cats-effect:3.5.4"
179+
|println("Hello!")
180+
|""".stripMargin
181+
)
182+
)
183+
184+
assertEquals(state, expected)
185+
}
186+
187+
test("applyUpdate for scala-cli using dep") {
188+
val repo = Repo("edit-alg", "test-5")
189+
val data = RepoData(repo, dummyRepoCache, RepoConfig.empty)
190+
val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
191+
val mainSc = repoDir / "main.sc"
192+
val update = ("org.typelevel".g % "cats-effect".a % "3.5.2" %> "3.5.4").single
193+
194+
val state = MockState.empty
195+
.copy(execCommands = true)
196+
.initGitRepo(
197+
repoDir,
198+
mainSc ->
199+
"""
200+
|//> using scala "3.4.2"
201+
|//> using jvm "temurin:21"
202+
|//> using dep "org.typelevel::cats-effect:3.5.2"
203+
|//> using test.dep "org.scalameta::munit:1.0.0"
204+
|println("Hello!")
205+
|""".stripMargin
206+
)
207+
.flatMap(editAlg.applyUpdate(data, update).runS)
208+
.unsafeRunSync()
209+
210+
val expected = MockState.empty.copy(
211+
execCommands = true,
212+
trace = Vector(
213+
Cmd.gitGrep(repoDir, update.currentVersion.value),
214+
Cmd("read", mainSc.pathAsString),
215+
Cmd.gitGrep(repoDir, update.groupId.value),
216+
Cmd("read", mainSc.pathAsString),
217+
Cmd("read", mainSc.pathAsString),
218+
Cmd("write", mainSc.pathAsString),
219+
Cmd.gitStatus(repoDir),
220+
Cmd.gitCommit(repoDir, "Update cats-effect to 3.5.4"),
221+
Cmd.gitLatestSha1(repoDir)
222+
),
223+
files = Map(
224+
mainSc ->
225+
"""
226+
|//> using scala "3.4.2"
227+
|//> using jvm "temurin:21"
228+
|//> using dep "org.typelevel::cats-effect:3.5.4"
229+
|//> using test.dep "org.scalameta::munit:1.0.0"
230+
|println("Hello!")
231+
|""".stripMargin
232+
)
233+
)
234+
235+
assertEquals(state, expected)
236+
}
237+
238+
test("applyUpdate for scala-cli using test.dep") {
239+
val repo = Repo("edit-alg", "test-6")
240+
val data = RepoData(repo, dummyRepoCache, RepoConfig.empty)
241+
val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
242+
val mainSc = repoDir / "main.sc"
243+
val update = ("org.scalameta".g % "munit".a % "0.7.29" %> "1.0.0").single
244+
245+
val state = MockState.empty
246+
.copy(execCommands = true)
247+
.initGitRepo(
248+
repoDir,
249+
mainSc ->
250+
"""
251+
|//> using scala "3.4.2"
252+
|//> using jvm "temurin:21"
253+
|//> using dep "org.typelevel::cats-effect:3.5.4"
254+
|//> using test.dep "org.scalameta::munit:0.7.29"
255+
|println("Hello!")
256+
|""".stripMargin
257+
)
258+
.flatMap(editAlg.applyUpdate(data, update).runS)
259+
.unsafeRunSync()
260+
261+
val expected = MockState.empty.copy(
262+
execCommands = true,
263+
trace = Vector(
264+
Cmd.gitGrep(repoDir, update.currentVersion.value),
265+
Cmd("read", mainSc.pathAsString),
266+
Cmd.gitGrep(repoDir, update.groupId.value),
267+
Cmd("read", mainSc.pathAsString),
268+
Cmd("read", mainSc.pathAsString),
269+
Cmd("write", mainSc.pathAsString),
270+
Cmd.gitStatus(repoDir),
271+
Cmd.gitCommit(repoDir, "Update munit to 1.0.0"),
272+
Cmd.gitLatestSha1(repoDir)
273+
),
274+
files = Map(
275+
mainSc ->
276+
"""
277+
|//> using scala "3.4.2"
278+
|//> using jvm "temurin:21"
279+
|//> using dep "org.typelevel::cats-effect:3.5.4"
280+
|//> using test.dep "org.scalameta::munit:1.0.0"
281+
|println("Hello!")
282+
|""".stripMargin
283+
)
284+
)
285+
286+
assertEquals(state, expected)
287+
}
137288
}

0 commit comments

Comments
 (0)