Skip to content

Commit

Permalink
Fixing missing resources in native image (#23)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ottx96 and Manuel Ott authored Mar 25, 2021
1 parent cc3300b commit e77ebc5
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 192 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
## 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)
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=ottx96/diffview-git)](https://dependabot.com)

## Usage
```text
Usage: diffview [-hvV] [--debug] [-d[=<inputDirectory>]]
[--no-original-extension[=<omitOriginalExtensions>]] [-o
[=<outputDirectory>]] <files>...
Usage: diffview [-hvV] [--debug] [--no-original-extension
[=<omitOriginalExtensions>]] [-o[=<outputDir>]] [-R
[=<repository>]] <files>...
<files>... The file whose history/diffviews to generate.
-d, --directory-in[=<inputDirectory>]
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[=<omitOriginalExtensions>]
Omits the original extension for output files.
e.g.: README.md --> README.html instead of README.md.html
or build.gradle --> build.html
Default: false
-o, --directory-out[=<outputDirectory>]
-o, --directory-out[=<outputDir>]
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[=<repository>]
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.
```
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 10 additions & 11 deletions src/main/kotlin/com/github/ottx96/Entrypoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<File>

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)
}
Expand Down
180 changes: 146 additions & 34 deletions src/main/kotlin/com/github/ottx96/generate/DifferenceGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,24 +19,13 @@ enum class RowType(val classname: String, val icon: String) {

class DifferenceGenerator(val data: MutableList<DifferenceView>) {

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)
}
}

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<String>()

Expand Down Expand Up @@ -68,32 +58,31 @@ class DifferenceGenerator(val data: MutableList<DifferenceView>) {
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(" ", "&nbsp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
)
tableRows += """
<tr class="${type.classname}">
<td class="line-icon">${type.icon}</td>
<td class="line-number">${it.old.getLineNumber(data)}</td>
<td class="line-number">${it.new.getLineNumber(data)}</td>
<td class="context">${data.value.replace(" ", "&nbsp;").replace(">", "&gt;").replace("<", "&lt;")}</td>
</tr>
""".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<hr>\n<hr>\n"){
"""<table>
| <tbody>
| ${it.joinToString("\n")}
| </tbody>
|</table>
""".trimMargin()
})
comparisons += """
<div class="comparison">
<h1>${it.name} (${it.commit})</h1>
${tables.joinToString(separator = "\n<hr>\n<hr>\n"){
"""<table>
| <tbody>
| ${it.joinToString(separator = "\n")}
| </tbody>
|</table>
""".trimMargin()
}}
</div>
""".trimIndent()
}

// create html
Expand All @@ -102,12 +91,135 @@ class DifferenceGenerator(val data: MutableList<DifferenceView>) {
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>): String {
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Diffview (Git) by ottx96</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
padding: 0;
overflow-x: hidden;
}
* {
font-family: 'Courier New', Courier, monospace;
font-size: 1rem;
color: #282c34;
}
.App {
background-color: #282c34;
min-height: 100vh;
max-width: 100%;
min-width: 100%;
}
.comparison {
padding-top: 3vh;
padding-bottom: 2vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: white;
}
h1 {
width: 100%;
margin-left: 6%;
color: white;
text-align: start;
font-family: 'Montserrat', sans-serif;
}
table {
background-color: white;
width: 95%;
display: flex;
justify-content: left;
align-items: center;
border-radius: 7px;
border: white 1px solid;
overflow: hidden;
}
tbody {
padding-top: 5px;
width: 100%;
display: flex;
flex-direction: column;
}
.row *, .row-created *, .row-removed * {
word-break: break-all;
}
.row {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(255, 255, 255, 0.8);
}
.row-created {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0, 255, 0, 0.35);
}
.row-removed {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(255, 0, 0, 0.32);
}
.line-number {
color: #282c34;
font-size: 0.7rem;
width: 60px;
text-align: center;
border-right: #282c34 1px solid;
}
.line-icon {
font-size: 0.4rem;
width: 60px;
text-align: center;
border-right: #282c34 1px solid;
}
.context, .created, .deleted {
width: 100%;
margin-left: 2%;
}
</style>
</head>
<body>
<div class="App">
${comparisons.joinToString(separator = "\n")}
</div>
</body>
</html>
""".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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
micronaut:
application:
name: "diffview-git"
name: "diffview"
4 changes: 0 additions & 4 deletions src/main/resources/template/html/comparison.template

This file was deleted.

Loading

0 comments on commit e77ebc5

Please sign in to comment.