From e77ebc57ad8deb0fca957543627b214a52b6e708 Mon Sep 17 00:00:00 2001 From: Ott <49874532+ottx96@users.noreply.github.com> Date: Thu, 25 Mar 2021 16:39:43 +0100 Subject: [PATCH] Fixing missing resources in native image (#23) * Update README.md * Moved template files to source code * Updated README.md * Bugfixes Fixed resources in native image Fixed input/output of files Added parameter -R (=repository), instead of -d Co-authored-by: Manuel Ott --- README.md | 34 ++-- build.gradle | 2 +- .../kotlin/com/github/ottx96/Entrypoint.kt | 21 +- .../ottx96/generate/DifferenceGenerator.kt | 180 ++++++++++++++---- .../github/ottx96/parse/DifferenceParser.kt | 1 + src/main/resources/application.yml | 2 +- .../template/html/comparison.template | 4 - .../resources/template/html/html.template | 118 ------------ src/main/resources/template/html/row.template | 6 - 9 files changed, 176 insertions(+), 192 deletions(-) delete mode 100644 src/main/resources/template/html/comparison.template delete mode 100644 src/main/resources/template/html/html.template delete mode 100644 src/main/resources/template/html/row.template diff --git a/README.md b/README.md index 348dafa..0f29a87 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ ## A simple `git diff` to html converter This Project is meant to be run as CLI Application. -I'll provide a `Dockerfile` to build your own images using `GraalVM native images`. +I'll provide a `Dockerfile` to build your own images using [GraalVM native images](https://www.graalvm.org/). Also, I'll provide another shellscript, which creates a container with a running webserver. -So you can directly see the formatted diff locally. +This way, you can directly see the formatted diff locally. [![Gradle Build](https://github.com/ottx96/diffview-git/actions/workflows/shadow-jar.yml/badge.svg)](https://github.com/ottx96/diffview-git/actions/workflows/shadow-jar.yml) [![Native Image](https://github.com/ottx96/diffview-git/actions/workflows/native-image.yml/badge.svg)](https://github.com/ottx96/diffview-git/actions/workflows/native-image.yml) @@ -12,14 +12,10 @@ So you can directly see the formatted diff locally. ## Usage ```text -Usage: diffview [-hvV] [--debug] [-d[=]] - [--no-original-extension[=]] [-o - [=]] ... +Usage: diffview [-hvV] [--debug] [--no-original-extension + [=]] [-o[=]] [-R + [=]] ... ... The file whose history/diffviews to generate. - -d, --directory-in[=] - Sets the directory root to read from. - Has to be inside of a valid git repository. - Default: --debug Sets the output to debug. -h, --help Show this help message and exit. --no-original-extension[=] @@ -27,11 +23,15 @@ Usage: diffview [-hvV] [--debug] [-d[=]] e.g.: README.md --> README.html instead of README.md.html or build.gradle --> build.html Default: false - -o, --directory-out[=] + -o, --directory-out[=] Sets the directory to output .html files to. Files wll be created as [file name].html e.g.: README.md.html Default: diffview-generated/ + -R, --repository, --directory-in[=] + Sets the directory root to read from. + Has to be inside of a valid git repository. + Default: -v, --verbose Sets the output to verbose. -V, --version Print version information and exit. ``` @@ -67,21 +67,21 @@ Example: ![image](https://user-images.githubusercontent.com/49874532/112373883-bceb4e80-8ce1-11eb-946f-f65cc3075a85.png) ## Building from Source -If you want to create the Binaries from Source, here you go! +If you want to create the binaries from source, here you go! #### Build using Java Run `gradlew assemble` inside of the repository. -This will generate the Libraries (.jar) insiddde of the `build/` folder. +This will generate the libraries (.jar) inside of the `build/` folder. #### Build as Native Image (GraalVM) -After compiling the libraries (.jar), you can use GraalVM's native-image tool to generate a native image. +After compiling the libraries (.jar), you can use [GraalVM's native-image tool](https://www.graalvm.org/reference-manual/native-image/) to generate a native image. Run `native-image -cp "application.jar:libs/*.jar:resources/*" com.github.ottx96.Entrypoint` inside of the folder `build/layers`. #### Build using Docker Run `docker build -t "diffview:latest" .` or `gradlew buildDockerImage` inside of the repository. ## Roadmap -- [ ] Add Paramter to limit the count of commits to display -- [ ] Add Paramter to enlargen the displayed length of the file -- [ ] Directly Serve the HTTP via Web Server (netty?) after converting the history -- [ ] Add Parameter to auto-stop the Web Server (javascript / time limit) +- [ ] Add parameter to limit the count of commits to display +- [ ] Add parameter to enlargen the displayed length of the file +- [ ] Directly serve the HTTP via web server (netty?) after converting the history +- [ ] Add parameter to auto-stop the seb server (javascript / time limit) diff --git a/build.gradle b/build.gradle index e9a6b9d..b667be4 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { id("com.github.johnrengelman.shadow") version "6.1.0" } -version = "1.0.0" +version = "1.0.1" group = "com.github.ottx96" repositories { diff --git a/src/main/kotlin/com/github/ottx96/Entrypoint.kt b/src/main/kotlin/com/github/ottx96/Entrypoint.kt index ef243e8..6eafcf7 100644 --- a/src/main/kotlin/com/github/ottx96/Entrypoint.kt +++ b/src/main/kotlin/com/github/ottx96/Entrypoint.kt @@ -41,41 +41,40 @@ class Entrypoint : Runnable { arity = "0..1") var omitOriginalExtensions: Boolean = false - - @Option(names = ["-d", "--directory-in"], description = ["Sets the directory root to read from.", "Has to be inside of a valid git repository."], + @Option(names = ["-R", "--repository", "--directory-in"], description = ["Sets the directory root to read from.", "Has to be inside of a valid git repository."], showDefaultValue = Help.Visibility.ALWAYS, defaultValue = "", arity = "0..1") - lateinit var inputDirectory: File + lateinit var repository: File @Option(names = ["-o", "--directory-out"], description = ["Sets the directory to output .html files to.", "Files wll be created as [file name].html", "e.g.: README.md.html"], showDefaultValue = Help.Visibility.ALWAYS, defaultValue = "diffview-generated/", arity = "0..1") - lateinit var outputDirectory: File + lateinit var outputDir: File @Parameters(index = "0", description = ["The file whose history/diffviews to generate."], arity = "1..*") lateinit var files: List override fun run() { - outputDirectory.mkdirs() + outputDir.mkdirs() verbose { (Styles.ITALIC withColor Colors.BLUE).println(""" Debug: $debug Verbose: $verbose - Input directory: ${inputDirectory.absolutePath} - Output directory: ${outputDirectory.absolutePath} + Input directory: ${repository.absolutePath} + Output directory: ${outputDir.absolutePath} Files: ${files.joinToString()} """.trimIndent()) } - files.forEach { + files.map { File("${repository.absolutePath}/$it") }.forEach { if(it.isDirectory) { (Styles.BOLD withColor Colors.RED).println("Skipping folder ${it.absolutePath}. Only files are allowed!") return@forEach } (Styles.BOLD withColor Colors.MAGENTA).println("Processing file ${it.absolutePath} ..") - val output = ShellCommandExecutor(it, inputDirectory).execute() - val views = DifferenceParser(it.relativeTo(inputDirectory).toString().replace('\\', '/'), output).parse() - val target = File("${outputDirectory.absolutePath}/${if(omitOriginalExtensions)it.nameWithoutExtension else it}.html") + val output = ShellCommandExecutor(it, repository).execute() + val views = DifferenceParser(it.toString().replace('\\', '/'), output).parse() + val target = File("${outputDir.absolutePath}/${if(omitOriginalExtensions)it.nameWithoutExtension else it.name}.html") (Styles.BOLD withColor Colors.MAGENTA).println("Writing output file to ${target.absolutePath} ..") DifferenceGenerator(views).generate(target) } diff --git a/src/main/kotlin/com/github/ottx96/generate/DifferenceGenerator.kt b/src/main/kotlin/com/github/ottx96/generate/DifferenceGenerator.kt index 2530269..7ec9cfb 100644 --- a/src/main/kotlin/com/github/ottx96/generate/DifferenceGenerator.kt +++ b/src/main/kotlin/com/github/ottx96/generate/DifferenceGenerator.kt @@ -7,6 +7,7 @@ import com.github.ottx96.logging.Styles import com.github.ottx96.parse.DifferenceView import java.io.File import java.io.IOException +import java.io.OutputStreamWriter enum class GenerationFormat { HTML @@ -18,12 +19,6 @@ enum class RowType(val classname: String, val icon: String) { class DifferenceGenerator(val data: MutableList) { - companion object { - val TEMPLATE_HTML = File("src/main/resources/template/html/html.template") - val TEMPLATE_COMPARISON = File("src/main/resources/template/html/comparison.template") - val TEMPLATE_ROW = File("src/main/resources/template/html/row.template") - } - fun generate(output: File, format: GenerationFormat = GenerationFormat.HTML): File { when (format) { GenerationFormat.HTML -> return generateHTML(output) @@ -31,11 +26,6 @@ class DifferenceGenerator(val data: MutableList) { } private fun generateHTML(output: File): File { - // read template files - val rowTemplate = TEMPLATE_ROW.readText() - val comparisonTemplate = TEMPLATE_COMPARISON.readText() - val htmlTemplate = TEMPLATE_HTML.readText() - // create comparisons val comparisons = mutableListOf() @@ -68,32 +58,31 @@ class DifferenceGenerator(val data: MutableList) { val type = if (new?.marked == true) RowType.ADDED else if (old?.marked == true) RowType.REMOVED else RowType.NEUTRAL - tableRows += rowTemplate - .replace("@@line.class@@", type.classname) - .replace("@@line.icon@@", type.icon) - .replace("@@line.number.old@@", it.old.getLineNumber(data)) - .replace("@@line.number.new@@", it.new.getLineNumber(data)) - .replace( - "@@line.content@@", - data.value.replace(" ", " ") - .replace("<", "<") - .replace(">", ">") - ) + tableRows += """ + + ${type.icon} + ${it.old.getLineNumber(data)} + ${it.new.getLineNumber(data)} + ${data.value.replace(" ", " ").replace(">", ">").replace("<", "<")} + + """.trimIndent() } tables += tableRows } readCommits[it.commit] = true - comparisons += comparisonTemplate - .replace("@@file.name@@", it.name) - .replace("@@file.commit@@", it.commit) - .replace("@@tables.start@@", tables.joinToString(separator = "\n
\n
\n"){ - """ - | - | ${it.joinToString("\n")} - | - |
- """.trimMargin() - }) + comparisons += """ +
+

${it.name} (${it.commit})

+ ${tables.joinToString(separator = "\n
\n
\n"){ + """ + | + | ${it.joinToString(separator = "\n")} + | + |
+ """.trimMargin() + }} +
+ """.trimIndent() } // create html @@ -102,12 +91,135 @@ class DifferenceGenerator(val data: MutableList) { output.createNewFile() } output.writer().use { - it.write(htmlTemplate.replace("@@comparisons.start@@", comparisons.joinToString(separator = "\n"))) + it.write(createFromTemplate(comparisons)) } if (!output.exists() || output.length() == 0L) throw IOException() return output } + private fun createFromTemplate(comparisons: MutableList): String { + return """ + + + + + Diffview (Git) by ottx96 + + + + + +
+ ${comparisons.joinToString(separator = "\n")} +
+ + + """.trimIndent() + } + private fun DifferenceView.FileHunk.getLineNumber(other: DifferenceView.FileHunk.IdentifiableLine): String { val i = this.lines.indexOf(other) return if (i == -1) "" else "${i + this.range.first}" diff --git a/src/main/kotlin/com/github/ottx96/parse/DifferenceParser.kt b/src/main/kotlin/com/github/ottx96/parse/DifferenceParser.kt index 499d41b..2fb0499 100644 --- a/src/main/kotlin/com/github/ottx96/parse/DifferenceParser.kt +++ b/src/main/kotlin/com/github/ottx96/parse/DifferenceParser.kt @@ -18,6 +18,7 @@ class DifferenceParser(private val name: String, private val input: String) { entries.forEachIndexed { idx, commit -> var lines = commit.lines().dropWhile { !it.matches(Regex("""@@ -\d.*\d.*@@.*""")) } do { + if(lines.isEmpty()) continue var hunkID = lines[0] lines = lines.drop(1) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a3857c0..2e1e319 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,3 +1,3 @@ micronaut: application: - name: "diffview-git" + name: "diffview" diff --git a/src/main/resources/template/html/comparison.template b/src/main/resources/template/html/comparison.template deleted file mode 100644 index 1568ed9..0000000 --- a/src/main/resources/template/html/comparison.template +++ /dev/null @@ -1,4 +0,0 @@ -
-

@@file.name@@ (@@file.commit@@)

- @@tables.start@@ -
\ No newline at end of file diff --git a/src/main/resources/template/html/html.template b/src/main/resources/template/html/html.template deleted file mode 100644 index 0f39fb0..0000000 --- a/src/main/resources/template/html/html.template +++ /dev/null @@ -1,118 +0,0 @@ - - - - - Diffview (Git) by ottx96 - - - - - -
- @@comparisons.start@@ -
- - \ No newline at end of file diff --git a/src/main/resources/template/html/row.template b/src/main/resources/template/html/row.template deleted file mode 100644 index 927ad79..0000000 --- a/src/main/resources/template/html/row.template +++ /dev/null @@ -1,6 +0,0 @@ - - @@line.icon@@ - @@line.number.old@@ - @@line.number.new@@ - @@line.content@@ - \ No newline at end of file