Skip to content

Commit 55cb158

Browse files
authored
sbt 2 (#25)
1 parent 53609bb commit 55cb158

26 files changed

Lines changed: 412 additions & 121 deletions

File tree

‎build.sbt‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ val Versions = new {
3232
val Scala213 = "2.13.17"
3333
val Scala212 = "2.12.21"
3434
val Scala3 = "3.3.7"
35-
val Scala3Next = "3.8.3"
35+
val Scala3Next = "3.8.4"
3636
val scalaVersions = Seq(Scala3, Scala212, Scala213)
3737
val scala3Next = Seq(Scala3Next)
3838
val munit = "1.3.0"
3939
val upickle = "4.4.3"
40+
41+
val Sbt1 = "1.12.12"
42+
val Sbt2 = "2.0.0"
43+
val Sbt1Scala = Scala212
44+
val Sbt2Scala = "3.8.4"
4045
}
4146

4247
lazy val root: Project = project
@@ -123,16 +128,37 @@ lazy val snapshotsBuildtime = projectMatrix
123128

124129
lazy val snapshotsSbtPlugin = projectMatrix
125130
.dependsOn(snapshotsBuildtime)
126-
.jvmPlatform(Seq(Versions.Scala212))
131+
.enablePlugins(ScriptedPlugin, SbtPlugin)
132+
.jvmPlatform(Seq(Versions.Sbt1Scala, Versions.Sbt2Scala))
127133
.in(file("modules/snapshots-sbt-plugin"))
128134
.settings(
129-
sbtPlugin := true,
130-
name := "sbt-snapshots",
135+
(pluginCrossBuild / sbtVersion) := {
136+
scalaBinaryVersion.value match {
137+
case "2.12" => Versions.Sbt1
138+
case _ => Versions.Sbt2
139+
}
140+
},
141+
scalacOptions ++= {
142+
scalaBinaryVersion.value match {
143+
case "2.12" => "-Xsource:3" :: Nil
144+
case _ => Nil
145+
}
146+
},
147+
sbtTestDirectory := {
148+
scalaBinaryVersion.value match {
149+
case "2.12" => (sourceDirectory).value / "sbt-test"
150+
case _ => (sourceDirectory).value / "sbt-test-sbt2"
151+
}
152+
},
131153
scriptedLaunchOpts := {
132154
scriptedLaunchOpts.value ++
133155
Seq("-Xmx1024M", "-Dplugin.version=" + version.value)
134156
},
135-
scriptedBufferLog := false,
157+
scriptedBufferLog := false
158+
)
159+
.settings(
160+
sbtPlugin := true,
161+
name := "sbt-snapshots",
136162
publishLocal := publishLocal
137163
.dependsOn(
138164
snapshotsBuildtime.jvm(Versions.Scala212) / publishLocal,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2024 Anton Sviridov
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.indoorvivants.snapshots.sbtplugin
18+
19+
import sbt._
20+
21+
import Keys._
22+
23+
private[sbtplugin] object Compat {
24+
import SnapshotsPlugin.autoImport.snapshotsAddRuntimeDependency
25+
val addRuntimeDep = Seq(
26+
libraryDependencies ++= {
27+
if (snapshotsAddRuntimeDependency.value) {
28+
val cross = crossVersion.value match {
29+
case b: Binary => b.prefix + scalaBinaryVersion.value
30+
case _ => scalaBinaryVersion.value
31+
}
32+
33+
Seq(
34+
"com.indoorvivants.snapshots" % s"snapshots-runtime_$cross" % BuildInfo.version % Test
35+
)
36+
} else Seq.empty
37+
}
38+
)
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2024 Anton Sviridov
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.indoorvivants.snapshots.sbtplugin
18+
19+
import sbt.*
20+
21+
import Keys.*
22+
23+
private[sbtplugin] object Compat:
24+
import SnapshotsPlugin.autoImport.snapshotsAddRuntimeDependency
25+
val addRuntimeDep = Seq(
26+
libraryDependencies ++= {
27+
if snapshotsAddRuntimeDependency.value then
28+
val cross = platform.value match
29+
case "jvm" => scalaBinaryVersion.value
30+
case other => other + "_" + scalaBinaryVersion.value
31+
32+
Seq(
33+
"com.indoorvivants.snapshots" % s"snapshots-runtime_$cross" % BuildInfo.version % Test
34+
)
35+
else Seq.empty
36+
}
37+
)
38+
end Compat

‎modules/snapshots-sbt-plugin/src/main/scala/SnapshotsPlugin.scala‎

Lines changed: 104 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -80,134 +80,123 @@ object SnapshotsPlugin extends AutoPlugin {
8080
)
8181

8282
override def projectSettings: Seq[Setting[?]] =
83-
Seq(
84-
libraryDependencies ++= {
85-
if (snapshotsAddRuntimeDependency.value) {
86-
val cross = crossVersion.value match {
87-
case b: Binary => b.prefix + scalaBinaryVersion.value
88-
case _ => scalaBinaryVersion.value
83+
Compat.addRuntimeDep ++
84+
Seq(
85+
snapshotsProjectIdentifier := thisProject.value.id,
86+
snapshotsAddRuntimeDependency := true,
87+
snapshotsIntegrations := Seq.empty,
88+
snapshotsForceOverwrite := false,
89+
snapshotsLocation := sourceDirectory.value / "snapshots",
90+
snapshotsTemporaryDirectory := (
91+
Test / managedResourceDirectories
92+
).value.head / "snapshots-tmp",
93+
snapshotsCheck := Def
94+
.task {
95+
SnapshotsBuild.checkSnapshots(
96+
snapshotsTemporaryDirectory.value,
97+
snapshotsProjectIdentifier.value,
98+
SnapshotAction.Interactive
99+
)
89100
}
90-
91-
Seq(
92-
"com.indoorvivants.snapshots" % s"snapshots-runtime_$cross" % BuildInfo.version % Test
93-
)
94-
} else Seq.empty
95-
},
96-
snapshotsProjectIdentifier := thisProject.value.id,
97-
snapshotsAddRuntimeDependency := true,
98-
snapshotsIntegrations := Seq.empty,
99-
snapshotsForceOverwrite := false,
100-
snapshotsLocation := sourceDirectory.value / "snapshots",
101-
snapshotsTemporaryDirectory := (
102-
Test / managedResourceDirectories
103-
).value.head / "snapshots-tmp",
104-
snapshotsCheck := Def
105-
.task {
106-
SnapshotsBuild.checkSnapshots(
107-
snapshotsTemporaryDirectory.value,
108-
snapshotsProjectIdentifier.value,
109-
SnapshotAction.Interactive
110-
)
111-
}
112-
.tag(snapshotsTag)
113-
.value,
114-
snapshotsAcceptAll := Def
115-
.task {
116-
SnapshotsBuild.checkSnapshots(
117-
snapshotsTemporaryDirectory.value,
118-
snapshotsProjectIdentifier.value,
119-
SnapshotAction.Accept
120-
)
121-
}
122-
.tag(snapshotsTag)
123-
.value,
124-
snapshotsDiscardAll := Def
125-
.task {
126-
SnapshotsBuild.checkSnapshots(
127-
snapshotsTemporaryDirectory.value,
128-
snapshotsProjectIdentifier.value,
129-
SnapshotAction.Discard
130-
)
131-
}
132-
.tag(snapshotsTag)
133-
.value,
134-
Test / sourceGenerators += Def.task {
135-
val dest = (Test / managedSourceDirectories).value.head
136-
137-
val sources = SnapshotsBuild.generateSources(
138-
projectId = snapshotsProjectIdentifier.value,
139-
packageName = snapshotsPackageName.value,
140-
snapshotsDestination =
141-
snapshotsLocation.value / snapshotsProjectIdentifier.value,
142-
sourceDestination = dest / "Snapshots.scala",
143-
tmpLocation = snapshotsTemporaryDirectory.value,
144-
forceOverwrite = snapshotsForceOverwrite.value
145-
)
146-
147-
val integrations = snapshotsIntegrations.value.flatMap { integ =>
148-
SnapshotsBuild.generateIntegrationSources(
149-
dest / s"${integ}Integration.scala",
150-
integ,
151-
snapshotsPackageName.value
101+
.tag(snapshotsTag)
102+
.value,
103+
snapshotsAcceptAll := Def
104+
.task {
105+
SnapshotsBuild.checkSnapshots(
106+
snapshotsTemporaryDirectory.value,
107+
snapshotsProjectIdentifier.value,
108+
SnapshotAction.Accept
109+
)
110+
}
111+
.tag(snapshotsTag)
112+
.value,
113+
snapshotsDiscardAll := Def
114+
.task {
115+
SnapshotsBuild.checkSnapshots(
116+
snapshotsTemporaryDirectory.value,
117+
snapshotsProjectIdentifier.value,
118+
SnapshotAction.Discard
119+
)
120+
}
121+
.tag(snapshotsTag)
122+
.value,
123+
Test / sourceGenerators += Def.task {
124+
val dest = (Test / managedSourceDirectories).value.head
125+
126+
val sources = SnapshotsBuild.generateSources(
127+
projectId = snapshotsProjectIdentifier.value,
128+
packageName = snapshotsPackageName.value,
129+
snapshotsDestination =
130+
snapshotsLocation.value / snapshotsProjectIdentifier.value,
131+
sourceDestination = dest / "Snapshots.scala",
132+
tmpLocation = snapshotsTemporaryDirectory.value,
133+
forceOverwrite = snapshotsForceOverwrite.value
152134
)
153-
}
154-
155-
sources ++ integrations
156-
},
157-
snapshotsMigrate := Def
158-
.inputTask {
159-
import complete.DefaultParsers._
160-
161-
val migrations = Seq("0.0.6").map(_.trim.toLowerCase())
162-
val args: Seq[String] = spaceDelimited("<arg>").parsed
163-
val migration = args.headOption
164-
.map(_.trim.toLowerCase())
165-
.filter(migrations.contains(_))
166-
.getOrElse(
167-
sys.error(
168-
s"Expected 1 argument to the task, which is migration name, one of: [${migrations.mkString(", ")}]"
169-
)
135+
136+
val integrations = snapshotsIntegrations.value.flatMap { integ =>
137+
SnapshotsBuild.generateIntegrationSources(
138+
dest / s"${integ}Integration.scala",
139+
integ,
140+
snapshotsPackageName.value
170141
)
142+
}
171143

172-
val log = sLog.value
144+
sources ++ integrations
145+
},
146+
snapshotsMigrate := Def
147+
.inputTask {
148+
import complete.DefaultParsers._
149+
150+
val migrations = Seq("0.0.6").map(_.trim.toLowerCase())
151+
val args: Seq[String] = spaceDelimited("<arg>").parsed
152+
val migration = args.headOption
153+
.map(_.trim.toLowerCase())
154+
.filter(migrations.contains(_))
155+
.getOrElse(
156+
sys.error(
157+
s"Expected 1 argument to the task, which is migration name, one of: [${migrations.mkString(", ")}]"
158+
)
159+
)
173160

174-
migration match {
175-
case "0.0.6" =>
176-
val oldLocation =
177-
(Test / resourceDirectory).value / "snapshots" / snapshotsProjectIdentifier.value
178-
val newLocation =
179-
snapshotsLocation.value / snapshotsProjectIdentifier.value
161+
val log = sLog.value
180162

181-
if (oldLocation.exists() && oldLocation.isDirectory()) {
182-
val snapshots = IO.listFiles(oldLocation)
183-
log.info(
184-
s"Found ${snapshots.size} snapshots in old location [$oldLocation]. Will migrate them to new location - [$newLocation]"
185-
)
163+
migration match {
164+
case "0.0.6" =>
165+
val oldLocation =
166+
(Test / resourceDirectory).value / "snapshots" / snapshotsProjectIdentifier.value
167+
val newLocation =
168+
snapshotsLocation.value / snapshotsProjectIdentifier.value
186169

187-
IO.createDirectory(newLocation)
170+
if (oldLocation.exists() && oldLocation.isDirectory()) {
171+
val snapshots = IO.listFiles(oldLocation)
172+
log.info(
173+
s"Found ${snapshots.size} snapshots in old location [$oldLocation]. Will migrate them to new location - [$newLocation]"
174+
)
188175

189-
snapshots.foreach { file =>
190-
IO.move(file, newLocation / file.name)
191-
}
176+
IO.createDirectory(newLocation)
192177

193-
log.info(s"Migrated ${snapshots.size} files")
178+
snapshots.foreach { file =>
179+
IO.move(file, newLocation / file.name)
180+
}
194181

195-
val remaining = IO.listFiles(oldLocation).size
182+
log.info(s"Migrated ${snapshots.size} files")
196183

197-
if (remaining == 0) {
198-
log.info(s"[$oldLocation] is now empty and can be removed")
184+
val remaining = IO.listFiles(oldLocation).size
185+
186+
if (remaining == 0) {
187+
log.info(s"[$oldLocation] is now empty and can be removed")
188+
}
189+
190+
} else {
191+
sLog.value.warn(
192+
s"Tried to find old snapshots in [$oldLocation] but it doesn't seem to exist or is not a directory. Doing nothing"
193+
)
199194
}
200195

201-
} else {
202-
sLog.value.warn(
203-
s"Tried to find old snapshots in [$oldLocation] but it doesn't seem to exist or is not a directory. Doing nothing"
204-
)
205-
}
196+
}
206197

207198
}
208-
209-
}
210-
.tag(snapshotsTag)
211-
.evaluated
212-
)
199+
.tag(snapshotsTag)
200+
.evaluated
201+
)
213202
}

0 commit comments

Comments
 (0)