Skip to content

Commit

Permalink
optimized code and code fmt and fix compile
Browse files Browse the repository at this point in the history
Signed-off-by: JingZhang Chen <iRoiocam@gmail.com>
  • Loading branch information
Roiocam committed Jan 29, 2024
1 parent 3f6816f commit 798ecaf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
35 changes: 21 additions & 14 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "sbt-depend-walker"
organization := "io.github.roiocam"
description := "sbt plugin for walk on the dependency tree of Build"
description := "sbt plugin for walk on the dependency tree of Build"

sbtPlugin := true

Expand All @@ -16,19 +16,23 @@ scalacOptions ++= Seq(
ThisBuild / scalaVersion := "2.12.18"

// start ----------- sbt-ci-release
inThisBuild(List(
homepage := Some(url("https://github.com/roiocam/sbt-depend-walker")),
// Alternatively License.Apache2 see https://github.com/sbt/librarymanagement/blob/develop/core/src/main/scala/sbt/librarymanagement/License.scala
licenses := List("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")),
developers := List(
Developer(
id = "roiocam",
name = "Andy chen",
email = "iRoiocam@gmail.com",
url = url("https://github.com/roiocam")
inThisBuild(
List(
homepage := Some(url("https://github.com/roiocam/sbt-depend-walker")),
// Alternatively License.Apache2 see https://github.com/sbt/librarymanagement/blob/develop/core/src/main/scala/sbt/librarymanagement/License.scala
licenses := List(
"Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")
),
developers := List(
Developer(
id = "roiocam",
name = "Andy chen",
email = "iRoiocam@gmail.com",
url = url("https://github.com/roiocam")
)
)
)
))
)

sonatypeCredentialHost := "s01.oss.sonatype.org"
sonatypeRepository := "https://s01.oss.sonatype.org/service/local"
Expand Down Expand Up @@ -58,7 +62,10 @@ ThisBuild / dynver := {

// start ----------- sbt-github-actions
ThisBuild / githubWorkflowBuild := Seq(
WorkflowStep.Sbt(name = Some("Build project"), commands = List("test", "scripted"))
WorkflowStep.Sbt(
name = Some("Build project"),
commands = List("test", "scripted")
)
)
ThisBuild / githubWorkflowTargetTags ++= Seq("v*")
// publish snapshot and release
Expand Down Expand Up @@ -113,4 +120,4 @@ ThisBuild / scmInfo := Some(
)

enablePlugins(SbtPlugin)
scriptedBufferLog := false
scriptedBufferLog := false
40 changes: 27 additions & 13 deletions src/main/scala/io/github/roiocam/DependWalkerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ object DependWalkerPlugin extends AutoPlugin {

}

/** Used to match whether two ScopedKey are eligible
* @param currentKey
* @param executeTaskMatcher
* @param currentProject
* @return
*/
def scopedKeyIsMatch(
currentKey: ScopedKey[_],
executeTaskMatcher: ScopeKeyMatcher,
Expand All @@ -44,20 +50,21 @@ object DependWalkerPlugin extends AutoPlugin {
}
}

/** A method use to walking on the dependency tree.
* @param dependencies
* @param expectDependMatcher
* @param cMap
* @return
*/
def dependWalk(
dependencies: Set[_ <: ScopedKey[_]],
expectDependMatcher: ScopeKeyMatcher
)(implicit cMap: Map[Def.ScopedKey[_], Def.Flattened]): Boolean = {
val predicate: (ScopedKey[_], ScopedKey[_]) => Boolean = (sk, vk) =>
expectDependMatcher.mode match {
case CheckConfig => sk.scope.config == vk.scope.config
case CheckTask => sk.key == vk.key
case CheckBoth => sk.scope.config == vk.scope.config && sk.key == vk.key
case _ => false
}

)(implicit
cMap: Map[Def.ScopedKey[_], Def.Flattened],
currentProject: ProjectRef
): Boolean = {
dependencies.exists { sk =>
predicate(sk, expectDependMatcher.key) match {
scopedKeyIsMatch(sk, expectDependMatcher, currentProject) match {
case true => true
case false =>
cMap.get(sk).exists { flattened =>
Expand All @@ -71,11 +78,18 @@ object DependWalkerPlugin extends AutoPlugin {
}
}

/** Check dependency exist in the tree.
* @param currentProject
* check project
* @param walkTask
* job define
* @param cMap
* global project settings.
*/
def check(
currentProject: ProjectRef,
walkTask: WalkTask
)(implicit cMap: Map[Def.ScopedKey[_], Def.Flattened]): Unit = {

val executeTaskOnCurrentProject = cMap.collect {
case (currentKey, _)
if scopedKeyIsMatch(
Expand All @@ -91,13 +105,13 @@ object DependWalkerPlugin extends AutoPlugin {
case Some(c) => c.dependencies.toSet;
case None => Set.empty
}
if (dependWalk(depends, walkTask.expectDepend)) {
if (dependWalk(depends, walkTask.expectDepend)(cMap, currentProject)) {
println(
s"Depend verified in the tree of ${currentProject.project} / ${key.scope.config} / ${key.key}"
)
} else {
throw new Exception(
s"Expect depend (${walkTask.expectDepend.key.scope.config} / ${${walkTask.expectDepend.key.key}}) not in the dependency tree of (${walkTask.executeTask.key.scope.config} / ${walkTask.executeTask.key.key})"
s"Expect depend (${walkTask.expectDepend.key.scope.config} / ${walkTask.expectDepend.key.key}) not in the dependency tree of (${walkTask.executeTask.key.scope.config} / ${walkTask.executeTask.key.key})"
)
}
}
Expand Down

0 comments on commit 798ecaf

Please sign in to comment.