From 798ecaf7afda9671ab5ae16113c4e6462d4840aa Mon Sep 17 00:00:00 2001 From: JingZhang Chen Date: Mon, 29 Jan 2024 20:15:02 +0800 Subject: [PATCH] optimized code and code fmt and fix compile Signed-off-by: JingZhang Chen --- build.sbt | 35 +++++++++------- .../github/roiocam/DependWalkerPlugin.scala | 40 +++++++++++++------ 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/build.sbt b/build.sbt index 767642f..bc8d912 100644 --- a/build.sbt +++ b/build.sbt @@ -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 @@ -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" @@ -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 @@ -113,4 +120,4 @@ ThisBuild / scmInfo := Some( ) enablePlugins(SbtPlugin) -scriptedBufferLog := false \ No newline at end of file +scriptedBufferLog := false diff --git a/src/main/scala/io/github/roiocam/DependWalkerPlugin.scala b/src/main/scala/io/github/roiocam/DependWalkerPlugin.scala index b28fe35..a533c45 100644 --- a/src/main/scala/io/github/roiocam/DependWalkerPlugin.scala +++ b/src/main/scala/io/github/roiocam/DependWalkerPlugin.scala @@ -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, @@ -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 => @@ -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( @@ -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})" ) } }