-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to use kdiff as cli application
- Loading branch information
1 parent
2629f3c
commit 4a0a017
Showing
11 changed files
with
210 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changes for gradle-kdiff plugin | ||
|
||
## 2023-10-10 / 0.1.0 | ||
|
||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
plugins { | ||
id("com.lovelysystems.gradle") version ("1.12.0") | ||
} | ||
|
||
lovely { | ||
gitProject() | ||
} | ||
|
||
subprojects { | ||
version = rootProject.version | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
plugins { | ||
application | ||
kotlin("jvm") version "1.9.0" | ||
id("com.github.johnrengelman.shadow") version "8.1.1" | ||
} | ||
|
||
application { | ||
applicationName = "kdiff" | ||
} | ||
|
||
tasks.shadowDistZip { | ||
archiveBaseName.set("kdiff") | ||
archiveVersion.set(project.version.toString()) | ||
archiveClassifier.set("") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation("com.github.ajalt.clikt:clikt:4.2.1") | ||
implementation("io.github.java-diff-utils:java-diff-utils:4.12") | ||
} | ||
|
||
application { | ||
mainClass = "at.mibe.kdiff.ApplicationKt" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package at.mibe.kdiff | ||
|
||
fun main(args: Array<String>) = KDiffCommand().main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package at.mibe.kdiff | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.parameters.arguments.argument | ||
import com.github.ajalt.clikt.parameters.arguments.help | ||
import com.github.ajalt.clikt.parameters.options.default | ||
import com.github.ajalt.clikt.parameters.options.help | ||
import com.github.ajalt.clikt.parameters.options.option | ||
import com.github.ajalt.clikt.parameters.types.file | ||
import com.github.ajalt.clikt.parameters.types.path | ||
import java.io.File | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.div | ||
import kotlin.io.path.pathString | ||
|
||
class KDiffCommand : CliktCommand(name = "kdiff") { | ||
|
||
private val kpath: Path by argument("path").path(mustExist = true).help("The path to the Kustomization file") | ||
private val remoteBranch: String by option("-b", "--branch").help("Remote branch to diff against").default("master") | ||
private val remoteDirOverride: String by option("-r", "--remote-dir").help("Root directory of the remote branch to diff against") | ||
.default("") | ||
private val kustomize: File by option("--kustomize").file(mustExist = true).help("Path to the kustomize executable") | ||
.default(File("kustomize")) | ||
|
||
override fun run() { | ||
// clone the git repo | ||
val remoteRepoDir = cloneGitRepo(gitOriginUrl(), Path("/tmp/kdiff")) | ||
checkoutBranch(remoteRepoDir, remoteBranch) | ||
|
||
val remoteRepoExecDir = if (remoteDirOverride.isNotEmpty()) { | ||
remoteRepoDir / remoteDirOverride | ||
} else { | ||
remoteRepoDir | ||
} | ||
|
||
// run kustomize on the cloned remote branch | ||
// suppress error output because we don't care about the exit code | ||
val branch1Output = execCmd(kustomize.path, "build", (remoteRepoExecDir / kpath).pathString) { "" } | ||
|
||
// run kustomize on the local branch | ||
// suppress error output because we don't care about the exit code | ||
val branch2Output = execCmd(kustomize.path, "build", kpath.pathString) { "" } | ||
|
||
val diffs = findTextDifferences(branch1Output, branch2Output) | ||
if (diffs.deltas.isNotEmpty()) { | ||
val inlineDiff = generateInlineDiff(branch1Output, diffs) | ||
println(inlineDiff) | ||
} else { | ||
println("No differences found.") | ||
} | ||
} | ||
|
||
private fun cloneGitRepo(originUrl: String, dest: Path): Path { | ||
// Store target directory into a variable to avoid project reference in the configuration cache | ||
val repoPath = dest / Path(originUrl) | ||
Files.createDirectories(repoPath) | ||
|
||
execCmd("git", "clone", originUrl, ".", dir = repoPath.toFile()) { "Git clone failed" } | ||
return repoPath | ||
} | ||
|
||
private fun checkoutBranch(repoPath: Path, branch: String) { | ||
execCmd("git", "checkout", branch, dir = repoPath.toFile()) | ||
execCmd("git", "pull", "origin", branch, dir = repoPath.toFile()) | ||
} | ||
|
||
private fun gitOriginUrl(): String = execCmd("git", "remote", "get-url", "origin") | ||
|
||
private fun execCmd( | ||
vararg args: String, | ||
dir: File? = null, | ||
onError: ((String) -> String)? = null | ||
): String { | ||
val cmd = args.toList() | ||
val stdoutFile = kotlin.io.path.createTempFile().toFile() | ||
val stderrFile = kotlin.io.path.createTempFile().toFile() | ||
|
||
try { | ||
val proc = ProcessBuilder(cmd) | ||
.directory(dir) | ||
.redirectOutput(stdoutFile) | ||
.redirectError(stderrFile) | ||
.start() | ||
proc.waitFor(10, TimeUnit.SECONDS) | ||
val stdout = stdoutFile.readText().trim() | ||
val stderr = stderrFile.readText().trim() | ||
return if (proc.exitValue() == 0) { | ||
stdout | ||
} else { | ||
onError?.invoke(stderr) ?: throw RuntimeException( | ||
"command failed: ${cmd.joinToString(" ")}\n$stderr" | ||
) | ||
} | ||
} finally { | ||
stderrFile.delete() | ||
stdoutFile.delete() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 51 additions & 6 deletions
57
plugin/src/main/kotlin/at/mibe/gradle/kdiff/GradleKDiffPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,80 @@ | ||
package at.mibe.gradle.kdiff | ||
|
||
import de.undercouch.gradle.tasks.download.Download | ||
import org.gradle.api.Project | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.file.RelativePath | ||
import org.gradle.api.tasks.Copy | ||
import org.gradle.api.tasks.Exec | ||
|
||
@Suppress("unused") | ||
class GradleKDiffPlugin : Plugin<Project> { | ||
|
||
companion object { | ||
const val KDIFF_GROUP = "kdiff" | ||
} | ||
|
||
override fun apply(project: Project) { | ||
val kDiffLocation = project.layout.buildDirectory.dir("kdiff").get() | ||
|
||
// Apply the third-party plugin | ||
project.apply { action -> | ||
action.plugin("de.undercouch.download") | ||
} | ||
|
||
project.tasks.register("kDiffVersion") { | ||
it.group = KDIFF_GROUP | ||
it.description = "Prints the kDiff version" | ||
|
||
it.doLast { | ||
println("GradleKDiffPlugin version: ${project.version}") | ||
} | ||
} | ||
|
||
project.tasks.register("downloadKustomize", Download::class.java) { | ||
val downloadKustomize = project.tasks.register("downloadKustomize", Download::class.java) { | ||
it.group = KDIFF_GROUP | ||
it.description = "Download kustomize CLI" | ||
|
||
it.src("https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh") | ||
it.dest(project.layout.buildDirectory) | ||
it.dest(kDiffLocation.dir("bin")) | ||
it.overwrite(false) | ||
} | ||
|
||
project.tasks.register("installKustomize", Exec::class.java) { | ||
it.dependsOn("downloadKustomize") | ||
it.workingDir(project.layout.buildDirectory) | ||
it.group = KDIFF_GROUP | ||
it.description = "Install kustomize CLI" | ||
|
||
it.onlyIf { !kDiffLocation.asFile.resolve("bin/kustomize").exists() } | ||
it.dependsOn(downloadKustomize) | ||
|
||
it.workingDir(kDiffLocation.dir("bin")) | ||
// only install kustomize if it is not already installed | ||
it.commandLine("bash", "install_kustomize.sh") | ||
} | ||
|
||
project.tasks.register("kDiff", KDiffTask::class.java) | ||
val downloadKDiff = project.tasks.register("downloadKDiff", Download::class.java) { | ||
it.group = KDIFF_GROUP | ||
it.description = "Download kDiff CLI" | ||
|
||
it.src("https://github.com/mikethebeer/gradle-kdiff/releases/download/${project.version}/kdiff-${project.version}.zip") | ||
it.dest(project.layout.buildDirectory.file("kdiff.zip")) | ||
it.overwrite(false) | ||
} | ||
|
||
project.tasks.register("installKDiff", Copy::class.java) { | ||
it.group = KDIFF_GROUP | ||
it.description = "Install kDiff CLI" | ||
|
||
it.dependsOn(downloadKDiff) | ||
|
||
it.from(project.zipTree(downloadKDiff.get().dest)) { f -> | ||
f.include("kdiff-*/**") | ||
f.eachFile { cd -> | ||
cd.relativePath = RelativePath(true, *cd.relativePath.segments.drop(1).toTypedArray()) | ||
} | ||
f.includeEmptyDirs = false | ||
} | ||
it.into(kDiffLocation) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
rootProject.name = "gradle-kdiff" | ||
include("plugin") | ||
include("plugin", "cli") |