Skip to content

Commit

Permalink
Add ignoredConfigs in SubmitInput
Browse files Browse the repository at this point in the history
  • Loading branch information
adpi2 committed Jan 6, 2023
1 parent cf68df0 commit 2214c94
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
7 changes: 7 additions & 0 deletions sbt-plugin/src/main/contraband/input.contra
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ type SubmitInput {
## The name of module is composed of the name of the project and its binary version.
## Example: foo_2.13
ignoredModules: [String]

## A set of sbt configurations to ignore.
## Examples:
## - "test" to ignore the test dependencies
## - "scala-doc-tool" to ignore the scaladoc dependencies
## - "scala-tool" to ignore the compiler dependencies
ignoredConfigs: [String]
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,20 @@ object GithubDependencyGraphPlugin extends AutoPlugin {

private def includeProject(projectRef: ProjectRef, state: State, logger: Logger): Boolean = {
val ignoredModules = state.attributes(githubSubmitInputKey).ignoredModules
val scalaVersion = state.setting(projectRef / Keys.artifactName / Keys.scalaVersion)
val scalaBinaryVersion = state.setting(projectRef / Keys.artifactName / Keys.scalaBinaryVersion)
val projectID = state.setting(projectRef / Keys.projectID)
val moduleName = CrossVersion(scalaVersion, scalaBinaryVersion).apply(projectID).name
val moduleName = getModuleName(projectRef, state)
val ignored = ignoredModules.contains(moduleName)
if (!ignored) logger.info(s"Including dependency graph of $moduleName")
else logger.info(s"Excluding dependency graph of $moduleName")
!ignored
}

private def getModuleName(projectRef: ProjectRef, state: State): String = {
val scalaVersion = state.setting(projectRef / Keys.artifactName / Keys.scalaVersion)
val scalaBinaryVersion = state.setting(projectRef / Keys.artifactName / Keys.scalaBinaryVersion)
val projectID = state.setting(projectRef / Keys.projectID)
CrossVersion(scalaVersion, scalaBinaryVersion).apply(projectID).name
}

private def manifestTask: Def.Initialize[Task[Option[Manifest]]] = Def.task {
// updateFull is needed to have information about callers and reconstruct dependency tree
val reportResult = Keys.updateFull.result.value
Expand All @@ -108,17 +112,26 @@ object GithubDependencyGraphPlugin extends AutoPlugin {
val allDirectDependencies = Keys.allDependencies.value
val baseDirectory = Keys.baseDirectory.value
val logger = Keys.streams.value.log
val onResolveFailure = Keys.state.value.get(githubSubmitInputKey).flatMap(_.onResolveFailure)
val input = Keys.state.value.get(githubSubmitInputKey)

val onResolveFailure = input.flatMap(_.onResolveFailure)
val ignoredConfigs = input.toSeq.flatMap(_.ignoredConfigs).toSet
val moduleName = crossVersion(projectID).name

def getReference(module: ModuleID): String =
crossVersion(module)
.withConfigurations(None)
.withExtraAttributes(Map.empty)
.toString

def includeConfig(config: ConfigRef): Boolean =
if (ignoredConfigs.contains(config.name)) {
logger.info(s"Excluding config ${config.name} of ${moduleName} from its dependency graph")
false
} else true

reportResult match {
case Inc(cause) =>
val moduleName = crossVersion(projectID).name
val message = s"Failed to resolve the dependencies of $moduleName"
onResolveFailure match {
case Some(OnFailure.warning) =>
Expand All @@ -132,9 +145,9 @@ object GithubDependencyGraphPlugin extends AutoPlugin {
val alreadySeen = mutable.Set[String]()
val moduleReports = mutable.Buffer[(ModuleReport, ConfigRef)]()
val allDependencies = mutable.Buffer[(String, String)]()

for {
configReport <- report.configurations
if includeConfig(configReport.configuration)
moduleReport <- configReport.modules
moduleRef = getReference(moduleReport.module)
if !moduleReport.evicted && !alreadySeen.contains(moduleRef)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import ch.epfl.scala.githubapi.DependencyRelationship
import ch.epfl.scala.githubapi.DependencyScope
import ch.epfl.scala.githubapi.Manifest
import ch.epfl.scala.SubmitInput
import sjsonnew.shaded.scalajson.ast.unsafe.JString

val checkScaladoc = taskKey[Unit]("Check scaladoc_3 is in the manifest ")
val ignoreScaladoc = taskKey[StateTransform]("Ignore the scala-doc-tool in the submit input")
val checkIgnoreScaladoc = taskKey[Unit]("Check scaladoc_3 is absent in the manifest")

inThisBuild(
Seq(
organization := "ch.epfl.scala",
version := "1.2.0-SNAPSHOT",
scalaVersion := "3.2.1"
)
)

Global / ignoreScaladoc := {
val input = SubmitInput(None, Vector.empty, ignoredConfigs = Vector("scala-doc-tool"))
StateTransform(state => state.put(githubSubmitInputKey, input))
}

lazy val p1 = project
.in(file("p1"))
.settings(
checkScaladoc := {
val manifest = githubDependencyManifest.value.get
checkDependency(manifest, "org.scala-lang:scaladoc_3:3.2.1")(
expectedRelationship = DependencyRelationship.direct,
expectedScope = DependencyScope.development,
expectedConfig = "scala-doc-tool"
)
},
checkIgnoreScaladoc := {
val manifest = githubDependencyManifest.value.get
val suspicious = manifest.resolved.keys.filter(dep => dep.contains("scaladoc_3"))
assert(suspicious.isEmpty, s"The manifest should not contain scaladoc_3, found ${suspicious.mkString(", ")}")
}
)

def checkDependency(manifest: Manifest, name: String)(
expectedRelationship: DependencyRelationship = DependencyRelationship.direct,
expectedScope: DependencyScope = DependencyScope.runtime,
expectedConfig: String = "compile",
expectedDeps: Seq[String] = Seq.empty
): Unit = {
val node = manifest.resolved(name)
assert(node.package_url.startsWith("pkg:maven/"), s"Wrong package_url for node $name: ${node.package_url}")
assert(node.relationship.contains(expectedRelationship), s"Wrong relationship for node $name: ${node.relationship}")
assert(node.scope.contains(expectedScope), s"Wrong scope for node $name: ${node.scope}")
val configurations = node.metadata.get("config").collect { case JString(c) => c }
assert(configurations.contains(expectedConfig), s"Wrong config in metadata for node $name: $configurations")
expectedDeps.foreach(d => assert(node.dependencies.contains(d), s"missing dependency $d in node $name"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
val pluginVersion = sys.props("plugin.version")

addSbtPlugin("ch.epfl.scala" % "sbt-github-dependency-submission" % pluginVersion)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
> p1 / checkScaladoc
> Global / ignoreScaladoc
> p1 / checkIgnoreScaladoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class JsonProtocolTests extends FunSuite {
import ch.epfl.scala.JsonProtocol._
val raw = Parser.parseUnsafe("{}")
val obtained = Converter.fromJson[SubmitInput](raw).get
val expected = SubmitInput(None, Vector.empty)
val expected = SubmitInput(None, Vector.empty, Vector.empty)
assertEquals(obtained, expected)
}

test("decode input with onResolveFailure: warning") {
import ch.epfl.scala.JsonProtocol._
val raw = Parser.parseUnsafe("""{"onResolveFailure": "warning"}""")
val obtained = Converter.fromJson[SubmitInput](raw).get
val expected = SubmitInput(Some(OnFailure.warning), Vector.empty)
val expected = SubmitInput(Some(OnFailure.warning), Vector.empty, Vector.empty)
assertEquals(obtained, expected)
}
}

0 comments on commit 2214c94

Please sign in to comment.