Skip to content
Merged
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 @@ -18,6 +18,8 @@ package software.purpledragon.sbt.lock

import sbt.Keys._
import sbt._
import sbt.internal.util.ManagedLogger
import software.purpledragon.sbt.lock.DependencyLockUpdateMode._
import software.purpledragon.sbt.lock.model.{DependencyLockFile, LockFileMatches}

object DependencyLockPlugin extends AutoPlugin {
Expand All @@ -29,12 +31,20 @@ object DependencyLockPlugin extends AutoPlugin {
val dependencyLockRead = taskKey[Option[DependencyLockFile]]("read dependencies from lock file")

val dependencyLockCheck = taskKey[Unit]("check if dependency lock is up to date")

val DependencyLockUpdateMode: software.purpledragon.sbt.lock.DependencyLockUpdateMode.type =
software.purpledragon.sbt.lock.DependencyLockUpdateMode
val dependencyLockAutoCheck = settingKey[DependencyLockUpdateMode]("automatically check lock file after update")
}

import autoImport._

// task names to skip auto-check if we're inside of
private val PluginTasks = Seq("dependencyLockWrite", "dependencyLockCheck", "dependencyLockRead")

override def projectSettings: Seq[Def.Setting[_]] = Seq(
dependencyLockFile := baseDirectory.value / "build.sbt.lock",
dependencyLockAutoCheck := DependencyLockUpdateMode.WarnOnError,
dependencyLockWrite := {
val dest = dependencyLockFile.value
val updateReport = update.value
Expand All @@ -49,8 +59,8 @@ object DependencyLockPlugin extends AutoPlugin {
deps
},
dependencyLockCheck := {
val logger = streams.value.log
val updateReport = update.value
val logger: ManagedLogger = streams.value.log
val updateReport: UpdateReport = update.value

val currentFile = dependencyLockRead.value.getOrElse(sys.error("no lock file"))
val updatedFile = DependencyUtils.resolve(updateReport, thisProject.value.configurations.map(_.toConfigRef))
Expand All @@ -63,6 +73,47 @@ object DependencyLockPlugin extends AutoPlugin {
logger.warn(changes.toShortReport)
sys.error(changes.toLongReport)
}
}
},
update := Def.taskDyn {
val report = update.value
val logger = streams.value.log


// check to see if the current command/task is one of our internal ones
val skipCheck = state.value.currentCommand.map(_.commandLine).exists(PluginTasks.contains)
val checkMode = dependencyLockAutoCheck.value

if (checkMode != DependencyLockUpdateMode.CheckDisabled && !skipCheck) {
logger.debug("Automatically checking lockfile")

dependencyLockRead.value match {
case Some(currentFile) =>
val updatedFile = DependencyUtils.resolve(report, thisProject.value.configurations.map(_.toConfigRef))

val changes = currentFile.findChanges(updatedFile)

(changes, checkMode) match {
case (LockFileMatches, _) =>
// check passed
case (_, WarnOnError) =>
logger.warn("Dependency lock file is outdated - please run `dependencyLockCheck` for details")
case (_, FailOnError) =>
logger.error("Dependency lock file is outdated")
sys.error(changes.toLongReport)

case _ =>
// scenario shouldn't happen - failed check, but we're not checking...
}

case None =>
logger.warn("no lockfile found - please run dependencyLockWrite")
}
}

// return the original report
Def.task {
report
}
}.value
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019 Michael Stringer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package software.purpledragon.sbt.lock

object DependencyLockUpdateMode extends Enumeration {
type DependencyLockUpdateMode = Value

val CheckDisabled, WarnOnError, FailOnError = Value
}
9 changes: 9 additions & 0 deletions src/sbt-test/update/disabled-differences/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
scalaVersion := "2.12.10"

libraryDependencies ++= Seq(
"org.apache.commons" % "commons-lang3" % "3.9",
// lock file has 3.0.8
"org.scalatest" %% "scalatest" % "3.0.7" % Test,
)

dependencyLockAutoCheck := DependencyLockUpdateMode.CheckDisabled
101 changes: 101 additions & 0 deletions src/sbt-test/update/disabled-differences/build.sbt.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"lockVersion" : 1,
"timestamp" : "2019-11-21T18:42:01.200Z",
"configurations" : [
"compile",
"optional",
"provided",
"runtime",
"test"
],
"dependencies" : [
{
"org" : "org.apache.commons",
"name" : "commons-lang3",
"version" : "3.9",
"artifacts" : [
{
"name" : "commons-lang3.jar",
"hash" : "sha1:0122c7cee69b53ed4a7681c03d4ee4c0e2765da5"
}
],
"configurations" : [
"test",
"compile",
"runtime"
]
},
{
"org" : "org.scala-lang",
"name" : "scala-library",
"version" : "2.12.10",
"artifacts" : [
{
"name" : "scala-library.jar",
"hash" : "sha1:3509860bc2e5b3da001ed45aca94ffbe5694dbda"
}
],
"configurations" : [
"test",
"compile",
"runtime"
]
},
{
"org" : "org.scala-lang",
"name" : "scala-reflect",
"version" : "2.12.10",
"artifacts" : [
{
"name" : "scala-reflect.jar",
"hash" : "sha1:14cb7beb516cd8e07716133668c427792122c926"
}
],
"configurations" : [
"test"
]
},
{
"org" : "org.scala-lang.modules",
"name" : "scala-xml_2.12",
"version" : "1.2.0",
"artifacts" : [
{
"name" : "scala-xml_2.12.jar",
"hash" : "sha1:5d38ac30beb8420dd395c0af447ba412158965e6"
}
],
"configurations" : [
"test"
]
},
{
"org" : "org.scalactic",
"name" : "scalactic_2.12",
"version" : "3.0.8",
"artifacts" : [
{
"name" : "scalactic_2.12.jar",
"hash" : "sha1:b50559dfc4a691c1089f9c8812e1d6fd17f80277"
}
],
"configurations" : [
"test"
]
},
{
"org" : "org.scalatest",
"name" : "scalatest_2.12",
"version" : "3.0.8",
"artifacts" : [
{
"name" : "scalatest_2.12.jar",
"hash" : "sha1:8493ffa579676977b810a7a9fdc23af9d3c8af7f"
}
],
"configurations" : [
"test"
]
}
]
}
7 changes: 7 additions & 0 deletions src/sbt-test/update/disabled-differences/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
val pluginVersion = System.getProperty("plugin.version")
if (pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("software.purpledragon" % "sbt-dependency-lock" % pluginVersion)
}
1 change: 1 addition & 0 deletions src/sbt-test/update/disabled-differences/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> update
19 changes: 19 additions & 0 deletions src/sbt-test/update/disabled-same/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
scalaVersion := "2.12.10"

libraryDependencies ++= Seq(
"org.apache.commons" % "commons-lang3" % "3.9",
"org.scalatest" %% "scalatest" % "3.0.8" % Test,
)

dependencyLockAutoCheck := DependencyLockUpdateMode.CheckDisabled

val checkLog = taskKey[Unit]("checks the contents of the log")

checkLog := {
val lastLog = BuiltinCommands.lastLogFile(state.value).get
val last = IO.read(lastLog)

if (last.contains("Dependency lock file is outdated")) {
sys.error("check contained warning")
}
}
101 changes: 101 additions & 0 deletions src/sbt-test/update/disabled-same/build.sbt.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"lockVersion" : 1,
"timestamp" : "2019-11-21T18:42:01.200Z",
"configurations" : [
"compile",
"optional",
"provided",
"runtime",
"test"
],
"dependencies" : [
{
"org" : "org.apache.commons",
"name" : "commons-lang3",
"version" : "3.9",
"artifacts" : [
{
"name" : "commons-lang3.jar",
"hash" : "sha1:0122c7cee69b53ed4a7681c03d4ee4c0e2765da5"
}
],
"configurations" : [
"test",
"compile",
"runtime"
]
},
{
"org" : "org.scala-lang",
"name" : "scala-library",
"version" : "2.12.10",
"artifacts" : [
{
"name" : "scala-library.jar",
"hash" : "sha1:3509860bc2e5b3da001ed45aca94ffbe5694dbda"
}
],
"configurations" : [
"test",
"compile",
"runtime"
]
},
{
"org" : "org.scala-lang",
"name" : "scala-reflect",
"version" : "2.12.10",
"artifacts" : [
{
"name" : "scala-reflect.jar",
"hash" : "sha1:14cb7beb516cd8e07716133668c427792122c926"
}
],
"configurations" : [
"test"
]
},
{
"org" : "org.scala-lang.modules",
"name" : "scala-xml_2.12",
"version" : "1.2.0",
"artifacts" : [
{
"name" : "scala-xml_2.12.jar",
"hash" : "sha1:5d38ac30beb8420dd395c0af447ba412158965e6"
}
],
"configurations" : [
"test"
]
},
{
"org" : "org.scalactic",
"name" : "scalactic_2.12",
"version" : "3.0.8",
"artifacts" : [
{
"name" : "scalactic_2.12.jar",
"hash" : "sha1:b50559dfc4a691c1089f9c8812e1d6fd17f80277"
}
],
"configurations" : [
"test"
]
},
{
"org" : "org.scalatest",
"name" : "scalatest_2.12",
"version" : "3.0.8",
"artifacts" : [
{
"name" : "scalatest_2.12.jar",
"hash" : "sha1:8493ffa579676977b810a7a9fdc23af9d3c8af7f"
}
],
"configurations" : [
"test"
]
}
]
}
7 changes: 7 additions & 0 deletions src/sbt-test/update/disabled-same/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
val pluginVersion = System.getProperty("plugin.version")
if (pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("software.purpledragon" % "sbt-dependency-lock" % pluginVersion)
}
2 changes: 2 additions & 0 deletions src/sbt-test/update/disabled-same/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> update
> checkLog
9 changes: 9 additions & 0 deletions src/sbt-test/update/fail-differences/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
scalaVersion := "2.12.10"

libraryDependencies ++= Seq(
"org.apache.commons" % "commons-lang3" % "3.9",
// lock file has 3.0.8
"org.scalatest" %% "scalatest" % "3.0.7" % Test,
)

dependencyLockAutoCheck := DependencyLockUpdateMode.FailOnError
Loading