Skip to content

Commit

Permalink
Rework and publish Gradle connector plugin
Browse files Browse the repository at this point in the history
The connector plugin version is read from the gradle.properties file and added dynamically into all relevant places.
  • Loading branch information
ryru committed Sep 8, 2024
1 parent ac83288 commit cabd960
Show file tree
Hide file tree
Showing 27 changed files with 184 additions and 172 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/publishConnectorPlugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Publish Connector Plugin to Gradle Plugin Platform

on:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:

- name: Check out project
uses: actions/checkout@v3

- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
architecture: x64

- name: Set up Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5

- name: Build and publish connector plugin
run: ./gradlew publishPlugins
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ individual modules with the help of [Mermaid](https://mermaid.js.org/).
graph TD
vd2a57d(app) -->|implementation| va74ad8(core)
vd2a57d(app) -->|implementation| v1bc49d(importer)
v80f88a(dependency-plugin) -->|implementation| v8ebf3d(dependency-model)
va02543(connector-plugin) -->|implementation| v91bd8e(connector-model)
v1bc49d(importer) -->|implementation| v91bd8e(connector-model)
v1bc49d(importer) -->|implementation| va74ad8(core)
v1bc49d(importer) -->|implementation| v8ebf3d(dependency-model)
```

Features:
Expand Down Expand Up @@ -78,7 +78,7 @@ Download and setup this project:
git clone https://github.com/ryru/dependency-graph-analyser.git
cd dependency-graph-analyser/
./gradlew clean install
./gradlew :dependency-plugin:publishToMavenLocal
./gradlew :connector-plugin:publishToMavenLocal
```

1. Download the GitHub code repository
Expand All @@ -96,6 +96,7 @@ Get an overview of this project by running `dga .`:
Analyse project "dependency-graph-analyser"
5 modules
5 dependency configurations (1 unique dependency configurations)
```

### Create a Mermaid Chart
Expand All @@ -112,9 +113,9 @@ Analyse project "dependency-graph-analyser"
graph TD
vd2a57d(app) -->|implementation| va74ad8(core)
vd2a57d(app) -->|implementation| v1bc49d(importer)
v80f88a(dependency-plugin) -->|implementation| v8ebf3d(dependency-model)
va02543(connector-plugin) -->|implementation| v91bd8e(connector-model)
v1bc49d(importer) -->|implementation| v91bd8e(connector-model)
v1bc49d(importer) -->|implementation| va74ad8(core)
v1bc49d(importer) -->|implementation| v8ebf3d(dependency-model)
```

Expand All @@ -126,7 +127,7 @@ containing the dependencies originating from the module `app`:
```
./app/build/install/dga/bin/dga . -o app --mermaid-graph
Analyse project "dependency-graph-analyser"
Analyse project "dependency-graph-analyser"
5 modules
5 dependency configurations (1 unique dependency configurations)
Expand All @@ -137,7 +138,7 @@ Applying filter on data results in:
graph TD
vd2a57d(app) -->|implementation| va74ad8(core)
vd2a57d(app) -->|implementation| v1bc49d(importer)
```

- Use `--transitive` to also include transitive dependencies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ plugins {

description = "Data model of the analysed Gradle project"

group = "ch.addere.dga.plugin"
version = "0.1.0"
group = "ch.addere.dga.connectormodel"
version = property("connectorPluginVersion").toString()

publishing {
repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.addere.dga.dependencymodel
package ch.addere.dga.connectormodel

interface ConfigurationModel {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.addere.dga.dependencymodel
package ch.addere.dga.connectormodel

import java.io.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.addere.dga.dependencymodel
package ch.addere.dga.connectormodel

/**
* Represents a Gradle dependency.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.addere.dga.dependencymodel
package ch.addere.dga.connectormodel

import java.io.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.addere.dga.dependencymodel
package ch.addere.dga.connectormodel

interface DependencyModel {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.addere.dga.dependencymodel
package ch.addere.dga.connectormodel

import java.io.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.addere.dga.dependencymodel
package ch.addere.dga.connectormodel

/**
* Represents a Gradle project module. These modules usually have a Gradle build file.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.addere.dga.dependencymodel
package ch.addere.dga.connectormodel

import java.io.Serializable

Expand Down
49 changes: 49 additions & 0 deletions connector-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
plugins {
id("ch.addere.dga.kotlin-library-conventions")
`java-gradle-plugin`
`maven-publish`
id("com.gradle.plugin-publish") version "1.2.1"
}

description = "Gradle plugin that is injected into to be analysed Gradle projects"

group = "ch.addere.dga.connectorplugin"
version = property("connectorPluginVersion").toString()

dependencies {
implementation(project(":connector-model"))
}

gradlePlugin {
website = "https://github.com/ryru/dependency-graph-analyser"
vcsUrl = "https://github.com/ryru/dependency-graph-analyser.git"
plugins {
create("gradleDgaPlugin") {
id = "ch.addere.dga.connectorplugin"
displayName = "DGA Connector Plugin"
description =
"Small helper plugin that is injected into existing Gradle projects to analyse the dependency structure."
tags = listOf("dependencyAnalyser", "gradleBuild", "mermaid")
implementationClass = "ch.addere.dga.connectorplugin.ConnectorPlugin.kt"
}
}
}

publishing {
repositories {
maven {
name = "pluginRepo"
url = uri(layout.projectDirectory.dir("../build/pluginRepo"))
}

publications {
create<MavenPublication>("maven") {
from(components["kotlin"])
}
}
}
}

tasks.withType<PublishToMavenLocal> {
dependsOn(tasks.getByPath(":connector-model:publishToMavenLocal"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ch.addere.dga.connectorplugin

import ch.addere.dga.connectormodel.ConfigurationModel
import ch.addere.dga.connectormodel.ConfigurationModelData
import ch.addere.dga.connectormodel.Dependency
import ch.addere.dga.connectormodel.DependencyImpl
import ch.addere.dga.connectormodel.DependencyModel
import ch.addere.dga.connectormodel.DependencyModelImpl
import ch.addere.dga.connectormodel.ProjectModule
import ch.addere.dga.connectormodel.ProjectModuleImpl
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.tooling.provider.model.ToolingModelBuilder
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import javax.inject.Inject

class ConnectorPlugin @Inject constructor(private val registry: ToolingModelBuilderRegistry) :
Plugin<Project> {

override fun apply(target: Project) {
registry.register(DgaModelBuilder)
}

object DgaModelBuilder : ToolingModelBuilder {

override fun canBuild(modelName: String): Boolean {
return modelName == DependencyModel::class.java.name
}

override fun buildAll(modelName: String, project: Project): DependencyModel {
val projectName = project.name
val projectModules = project.subprojects.map { ProjectModuleImpl(it.name) }.toSet()
val moduleDependencies = extractModuleDependencies(project, projectModules)
return DependencyModelImpl(projectName, projectModules, moduleDependencies)
}

private fun extractModuleDependencies(
project: Project,
modules: Set<ProjectModule>
): Map<String, List<ConfigurationModel>> = project.allprojects
.associate { it.name to extractConfiguration(it.configurations, modules) }

private fun extractConfiguration(
configurationContainer: ConfigurationContainer,
modules: Set<ProjectModule>
): List<ConfigurationModel> = configurationContainer
.flatMap { configurationToDependencies(it, modules) }
.toList()

private fun configurationToDependencies(
configuration: Configuration,
modules: Set<ProjectModule>
): List<ConfigurationModel> =
configuration.dependencies
.map { DependencyImpl(it.name, it.group, it.version) }
.filter { isDependencyWithinProjectModules(modules, it) }
.map { ConfigurationModelData(configuration.name, it, ProjectModuleImpl(it.name)) }
.toList()

private fun isDependencyWithinProjectModules(
modules: Set<ProjectModule>,
dependency: Dependency
): Boolean =
modules.map { it.projectModule }.contains(dependency.name)
}
}
33 changes: 0 additions & 33 deletions dependency-plugin/build.gradle.kts

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit cabd960

Please sign in to comment.