Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade gatling-enterprise-plugin-commons 1.9.0-M5 #150

Merged
merged 1 commit into from
Jan 26, 2024
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
5 changes: 2 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ sbtPlugin := true
githubPath := "gatling/gatling-sbt-plugin"

libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.17" % Test,
"org.zeroturnaround" % "zt-zip" % "1.16",
"io.gatling" % "gatling-enterprise-plugin-commons" % "1.8.0"
"org.scalatest" %% "scalatest" % "3.2.17" % Test,
"io.gatling" % "gatling-enterprise-plugin-commons" % "1.9.0-M5"
)

scriptedLaunchOpts := {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ package io.gatling.sbt.settings.gatling

import java.io.File

import scala.collection.JavaConverters._

import io.gatling.plugin.io.PluginLogger
import io.gatling.plugin.pkg.EnterprisePackager
import io.gatling.sbt.settings.gatling.EnterpriseUtils.InitializeTask
import io.gatling.sbt.utils._

import sbt._
import sbt.{ Configuration, Def, IntegrationTest, ModuleDescriptorConfiguration, Test }
import sbt.Keys._

object TaskEnterprisePackage {
private val SbtPackagerName = "sbt"
}

class TaskEnterprisePackage(config: Configuration) {
private val moduleDescriptorConfig = Def.task {
val logger = streams.value.log
Expand All @@ -38,7 +47,7 @@ class TaskEnterprisePackage(config: Configuration) {
val buildEnterprisePackage: InitializeTask[File] = Def.task {
val moduleDescriptor = moduleDescriptorConfig.value

val DependenciesAnalysisResult(gatlingVersion, dependencies) = DependenciesAnalyzer.analyze(
val DependenciesAnalysisResult(gatlingDependencies, nonGatlingDependencies) = DependenciesAnalyzer.analyze(
dependencyResolution.value,
updateConfiguration.value,
(update / unresolvedWarningConfiguration).value,
Expand All @@ -49,10 +58,33 @@ class TaskEnterprisePackage(config: Configuration) {

val classesDirectories = (config / fullClasspath).value.map(_.data).filter(_.isDirectory)

val jarName = s"${moduleName.value}-gatling-enterprise-${version.value}"
val pluginLogger: PluginLogger = new PluginLogger {
private val logger = streams.value.log

override def info(s: String): Unit = logger.info(s)

override def error(s: String): Unit = logger.error(s)
}

FatJar
.packageFatJar(moduleDescriptor.module, classesDirectories, gatlingVersion, dependencies, target.value, jarName)
val rootModule = moduleDescriptor.module

target.value.mkdirs()
val packageFile = target.value / s"${moduleName.value}-gatling-enterprise-${version.value}.jar"

new EnterprisePackager(pluginLogger)
.createEnterprisePackage(
classesDirectories.asJava,
gatlingDependencies.asJava,
nonGatlingDependencies.asJava,
rootModule.organization,
rootModule.name,
rootModule.revision,
TaskEnterprisePackage.SbtPackagerName,
getClass.getPackage.getImplementationVersion,
packageFile
)

packageFile
}

val legacyPackageEnterpriseJar: InitializeTask[File] = Def.sequential(
Expand All @@ -70,5 +102,4 @@ class TaskEnterprisePackage(config: Configuration) {
},
buildEnterprisePackage
)

}
59 changes: 32 additions & 27 deletions src/main/scala/io/gatling/sbt/utils/DependenciesAnalyzer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/

package io.gatling.sbt.utils
import java.io.File

import scala.annotation.tailrec

import io.gatling.plugin.pkg.Dependency

import sbt.librarymanagement._
import sbt.util.Logger

Expand All @@ -29,28 +30,22 @@ object ArtifactWithoutVersion {

case class ArtifactWithoutVersion(organization: String, name: String)

case class DependenciesAnalysisResult(gatlingVersion: String, nonGatlingDependencies: Vector[File])
case class DependenciesAnalysisResult(gatlingDependencies: Set[Dependency], nonGatlingDependencies: Set[Dependency])

object DependenciesAnalyzer {
private final case class Exclusion(organization: String, name: Option[String] = None)
private object Exclusion {
val All = Seq(
private val All = Array(
Exclusion("io.gatling"),
Exclusion("io.gatling.highcharts"),
Exclusion("io.gatling.frontline"),
// having multiple slf4-j back-ends on the classpath is an issue
Exclusion("ch.qos.logback"),
// scala-library and scala-reflect are always direct dependencies
Exclusion("org.scala-lang", Some("scala-library")),
Exclusion("org.scala-lang", Some("scala-reflect")),
Exclusion("io.netty", Some("netty-all")),
Exclusion("io.netty", Some("netty-resolver-dns-classes-macos")),
Exclusion("io.netty", Some("netty-resolver-dns-native-macos"))
Exclusion("org.scala-lang", Some("scala-reflect"))
)
}

private def exclude(dep: ArtifactWithoutVersion): Boolean =
Exclusion.All.exists(exclusion => exclusion.organization == dep.organization && exclusion.name.forall(_ == dep.name))
def exclude(dep: ArtifactWithoutVersion): Boolean =
Exclusion.All.exists(exclusion => exclusion.organization == dep.organization && exclusion.name.forall(_ == dep.name))
}

def analyze(
resolution: DependencyResolution,
Expand All @@ -64,27 +59,37 @@ object DependenciesAnalyzer {

val updateReport = resolution
.update(moduleDescriptor, updateConfig, unresolvedWarningConfig, logger)
.getOrElse(throw new IllegalStateException("Cannot build a fatjar with unresolved dependencies"))
.getOrElse(throw new IllegalStateException("Cannot build a package with unresolved dependencies"))

val configurationReport = updateReport
.configuration(ConfigRef.configToConfigRef(config))
.getOrElse(throw new IllegalStateException(s"Could not find a report for configuration $config"))

val gatlingVersion = configurationReport.modules
.map(_.module)
.collectFirst { case module if module.organization == "io.gatling" && module.name.startsWith("gatling-app") => module.revision }
.getOrElse(throw new IllegalArgumentException("Couldn't locate Gatling libraries in the classpath"))

val callers = moduleCallers(configurationReport.modules)
val excludedDeps = configurationReport.modules.filterNot(isTransitiveGatlingDependency(_, callers))

val nonGatlingDependencies = excludedDeps
.flatMap(_.artifacts)
.collect { case (artifact, file) if artifact.`type` == Artifact.DefaultType || artifact.`type` == "bundle" => file }
val (gatlingModules, nonGatlingModules) = configurationReport.modules.toSet.partition(isTransitiveGatlingDependency(_, callers))

DependenciesAnalysisResult(gatlingVersion, nonGatlingDependencies)
DependenciesAnalysisResult(toDependencies(gatlingModules), toDependencies(nonGatlingModules))
}

private def toDependencies(moduleReports: Set[ModuleReport]): Set[Dependency] =
for {
module <- moduleReports
(artifact, file) <- module.artifacts
if isJar(artifact) || isBundle(artifact)
} yield new Dependency(
module.module.organization,
module.module.name,
module.module.revision,
file
)

private def isBundle(artifact: Artifact) =
artifact.`type` == "bundle"

private def isJar(artifact: Artifact) =
artifact.`type` == Artifact.DefaultType

private def moduleCallers(reports: Vector[ModuleReport]): Map[ArtifactWithoutVersion, List[ArtifactWithoutVersion]] =
reports
.map(report => ArtifactWithoutVersion(report.module) -> report.callers.map(caller => ArtifactWithoutVersion(caller.caller)))
Expand All @@ -97,9 +102,9 @@ object DependenciesAnalyzer {
@tailrec
def isTransitiveGatlingDependencyRec(toCheck: List[ArtifactWithoutVersion]): Boolean =
toCheck match {
case Nil => false
case dep :: _ if exclude(dep) => true
case dep :: rest => isTransitiveGatlingDependencyRec(callers.getOrElse(dep, Nil) ::: rest)
case Nil => false
case dep :: _ if Exclusion.exclude(dep) => true
case dep :: rest => isTransitiveGatlingDependencyRec(callers.getOrElse(dep, Nil) ::: rest)
}

isTransitiveGatlingDependencyRec(List(ArtifactWithoutVersion(report.module.withConfigurations(None))))
Expand Down
104 changes: 0 additions & 104 deletions src/main/scala/io/gatling/sbt/utils/FatJar.scala

This file was deleted.