mirror of
https://github.com/tadfisher/gradle2nix.git
synced 2026-01-12 07:50:53 -05:00
Rewrite based on code from the GitHub Dependency Graph Gradle Plugin
This commit is contained in:
@@ -1,10 +1,22 @@
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
plugins {
|
||||
`embedded-kotlin`
|
||||
kotlin("kapt")
|
||||
id("org.jetbrains.kotlin.jvm")
|
||||
id("org.jetbrains.kotlin.plugin.serialization")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api("com.squareup.moshi:moshi:latest.release")
|
||||
kapt("com.squareup.moshi:moshi-kotlin-codegen:latest.release")
|
||||
implementation("net.swiftzer.semver:semver:latest.release")
|
||||
implementation(libs.serialization.json)
|
||||
implementation(libs.semver)
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
kotlin {
|
||||
compilerOptions {
|
||||
jvmTarget.set(JvmTarget.JVM_1_8)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
com.squareup.moshi:moshi:1.11.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.squareup.okio:okio:1.17.5=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
net.swiftzer.semver:semver:1.1.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.jetbrains.kotlin:kotlin-reflect:1.4.20=compileClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.4.20=compileClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.20=compileClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.20=compileClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.4.20=compileClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.jetbrains:annotations:13.0=compileClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
empty=
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.nixos.gradle2nix.dependencygraph.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class DependencyCoordinates(val group: String, val module: String, val version: String)
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.nixos.gradle2nix.dependencygraph.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* The source of a dependency declaration, representing where the direct dependency is declared,
|
||||
* or where the parent dependency is declared for transitive dependencies.
|
||||
* In most cases, this will be the project component that declares the dependency,
|
||||
* but may also be a Version Catalog or the build as a whole.
|
||||
* We attempt to map this to an actual source file location when building a dependency report.
|
||||
*/
|
||||
@Serializable
|
||||
data class DependencySource(val id: String, val path: String)
|
||||
@@ -1,139 +0,0 @@
|
||||
package org.nixos.gradle2nix
|
||||
|
||||
import com.squareup.moshi.JsonClass
|
||||
import net.swiftzer.semver.SemVer
|
||||
import java.io.Serializable
|
||||
import java.lang.IllegalArgumentException
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class DefaultBuild(
|
||||
override val gradle: DefaultGradle,
|
||||
override val settingsDependencies: List<DefaultArtifact>,
|
||||
override val pluginDependencies: List<DefaultArtifact>,
|
||||
override val rootProject: DefaultProject,
|
||||
override val includedBuilds: List<DefaultIncludedBuild>
|
||||
) : Build, Serializable {
|
||||
constructor(model: Build) : this(
|
||||
DefaultGradle(model.gradle),
|
||||
model.settingsDependencies.map(::DefaultArtifact),
|
||||
model.pluginDependencies.map(::DefaultArtifact),
|
||||
DefaultProject(model.rootProject),
|
||||
model.includedBuilds.map(::DefaultIncludedBuild)
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class DefaultIncludedBuild(
|
||||
override val name: String,
|
||||
override val projectDir: String
|
||||
) : IncludedBuild, Serializable {
|
||||
constructor(model: IncludedBuild) : this(
|
||||
model.name,
|
||||
model.projectDir
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class DefaultGradle(
|
||||
override val version: String,
|
||||
override val type: String,
|
||||
override val url: String,
|
||||
override val sha256: String,
|
||||
override val nativeVersion: String
|
||||
) : Gradle, Serializable {
|
||||
constructor(model: Gradle) : this(
|
||||
model.version,
|
||||
model.type,
|
||||
model.url,
|
||||
model.sha256,
|
||||
model.nativeVersion
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class DefaultProject(
|
||||
override val name: String,
|
||||
override val version: String,
|
||||
override val path: String,
|
||||
override val projectDir: String,
|
||||
override val buildscriptDependencies: List<DefaultArtifact>,
|
||||
override val projectDependencies: List<DefaultArtifact>,
|
||||
override val children: List<DefaultProject>
|
||||
) : Project, Serializable {
|
||||
constructor(model: Project) : this(
|
||||
model.name,
|
||||
model.version,
|
||||
model.path,
|
||||
model.projectDir,
|
||||
model.buildscriptDependencies.map(::DefaultArtifact),
|
||||
model.projectDependencies.map(::DefaultArtifact),
|
||||
model.children.map { DefaultProject(it) }
|
||||
)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class DefaultArtifact(
|
||||
override val id: DefaultArtifactIdentifier,
|
||||
override val name: String,
|
||||
override val path: String,
|
||||
override val timestamp: String? = null,
|
||||
override val build: Int? = null,
|
||||
override val urls: List<String>,
|
||||
override val sha256: String
|
||||
) : Artifact, Comparable<DefaultArtifact>, Serializable {
|
||||
constructor(model: Artifact) : this(
|
||||
DefaultArtifactIdentifier(model.id),
|
||||
model.name,
|
||||
model.path,
|
||||
model.timestamp,
|
||||
model.build,
|
||||
model.urls,
|
||||
model.sha256
|
||||
)
|
||||
|
||||
override fun toString() = id.toString()
|
||||
override fun compareTo(other: DefaultArtifact): Int = id.compareTo(other.id)
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class DefaultArtifactIdentifier(
|
||||
override val group: String,
|
||||
override val name: String,
|
||||
override val version: String,
|
||||
override val type: String,
|
||||
override val extension: String = type,
|
||||
override val classifier: String? = null
|
||||
) : ArtifactIdentifier, Comparable<DefaultArtifactIdentifier>, Serializable {
|
||||
constructor(model: ArtifactIdentifier) : this(
|
||||
model.group,
|
||||
model.name,
|
||||
model.version,
|
||||
model.type,
|
||||
model.extension,
|
||||
model.classifier
|
||||
)
|
||||
|
||||
@delegate:Transient
|
||||
private val semver: SemVer? by lazy {
|
||||
try {
|
||||
SemVer.parse(version)
|
||||
} catch (_: IllegalArgumentException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun compareTo(other: DefaultArtifactIdentifier): Int {
|
||||
return group.compareTo(other.group).takeUnless { it == 0 }
|
||||
?: name.compareTo(other.name).takeUnless { it == 0 }
|
||||
?: other.semver?.let { semver?.compareTo(it) }?.takeUnless { it == 0 }
|
||||
?: type.compareTo(other.type).takeUnless { it == 0 }
|
||||
?: other.classifier?.let { classifier?.compareTo(it) }?.takeUnless { it == 0 }
|
||||
?: 0
|
||||
}
|
||||
|
||||
override fun toString(): String = buildString {
|
||||
append("$group:$name:$version")
|
||||
if (classifier != null) append(":$classifier")
|
||||
append("@$type")
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package org.nixos.gradle2nix
|
||||
|
||||
interface Build {
|
||||
val gradle: Gradle
|
||||
val settingsDependencies: List<Artifact>
|
||||
val pluginDependencies: List<Artifact>
|
||||
val rootProject: Project
|
||||
val includedBuilds: List<IncludedBuild>
|
||||
}
|
||||
|
||||
interface IncludedBuild {
|
||||
val name: String
|
||||
val projectDir: String
|
||||
}
|
||||
|
||||
interface Gradle {
|
||||
val version: String
|
||||
val type: String
|
||||
val url: String
|
||||
val sha256: String
|
||||
val nativeVersion: String
|
||||
}
|
||||
|
||||
interface Project {
|
||||
val name: String
|
||||
val version: String
|
||||
val path: String
|
||||
val projectDir: String
|
||||
val buildscriptDependencies: List<Artifact>
|
||||
val projectDependencies: List<Artifact>
|
||||
val children: List<Project>
|
||||
}
|
||||
|
||||
interface Artifact {
|
||||
val id: ArtifactIdentifier
|
||||
val name: String
|
||||
val path: String
|
||||
val timestamp: String?
|
||||
val build: Int?
|
||||
val urls: List<String>
|
||||
val sha256: String
|
||||
}
|
||||
|
||||
interface ArtifactIdentifier {
|
||||
val group: String
|
||||
val name: String
|
||||
val version: String
|
||||
val type: String
|
||||
val extension: String
|
||||
val classifier: String?
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.nixos.gradle2nix
|
||||
|
||||
const val PARAM_INCLUDE_PROJECTS = "NIX_INCLUDE_PROJECTS"
|
||||
const val PARAM_INCLUDE_CONFIGURATIONS = "NIX_INCLUDE_CONFIGURATIONS"
|
||||
const val PARAM_REPORT_DIR = "NIX_REPORT_DIR"
|
||||
const val RESOLVE_PROJECT_TASK = "resolveProjectDependencies"
|
||||
const val RESOLVE_ALL_TASK = "resolveAllDependencies"
|
||||
20
model/src/main/kotlin/org/nixos/gradle2nix/Repository.kt
Normal file
20
model/src/main/kotlin/org/nixos/gradle2nix/Repository.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package org.nixos.gradle2nix.dependencygraph.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class Repository(
|
||||
val id: String,
|
||||
val type: Type,
|
||||
val name: String,
|
||||
val m2Compatible: Boolean,
|
||||
val metadataSources: List<String>,
|
||||
val metadataResources: List<String>,
|
||||
val artifactResources: List<String>,
|
||||
) {
|
||||
enum class Type {
|
||||
MAVEN,
|
||||
IVY,
|
||||
FLAT_DIR
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.nixos.gradle2nix.dependencygraph.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ResolvedConfiguration(
|
||||
val rootSource: DependencySource,
|
||||
val configurationName: String,
|
||||
val repositories: List<Repository> = emptyList(),
|
||||
val allDependencies: MutableList<ResolvedDependency> = mutableListOf()
|
||||
) {
|
||||
fun addDependency(component: ResolvedDependency) {
|
||||
allDependencies.add(component)
|
||||
}
|
||||
|
||||
fun hasDependency(componentId: String): Boolean {
|
||||
return allDependencies.any { it.id == componentId }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.nixos.gradle2nix.dependencygraph.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
// private const val DEFAULT_MAVEN_REPOSITORY_URL = "https://repo.maven.apache.org/maven2"
|
||||
|
||||
@Serializable
|
||||
data class ResolvedDependency(
|
||||
val id: String,
|
||||
val source: DependencySource,
|
||||
val direct: Boolean,
|
||||
val coordinates: DependencyCoordinates,
|
||||
val repository: String?,
|
||||
val dependencies: List<String>
|
||||
)
|
||||
//{
|
||||
// fun packageUrl() =
|
||||
// PackageURLBuilder
|
||||
// .aPackageURL()
|
||||
// .withType("maven")
|
||||
// .withNamespace(coordinates.group.ifEmpty { coordinates.module }) // TODO: This is a sign of broken mapping from component -> PURL
|
||||
// .withName(coordinates.module)
|
||||
// .withVersion(coordinates.version)
|
||||
// .also {
|
||||
// if (repositoryUrl != null && repositoryUrl != DEFAULT_MAVEN_REPOSITORY_URL) {
|
||||
// it.withQualifier("repository_url", repositoryUrl)
|
||||
// }
|
||||
// }
|
||||
// .build()
|
||||
// .toString()
|
||||
//
|
||||
//}
|
||||
Reference in New Issue
Block a user