Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable tldr to format the output as a json #20

Merged
merged 4 commits into from
Jul 4, 2023
Merged

Conversation

MarcoSignoretto
Copy link
Contributor

This patch adds the capability to get the tldr output in json format.

Comment on lines +1 to +13
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

[*.{kt,kts,gradle}]
indent_style = space
indent_size = 2
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_packages_to_use_import_on_demand =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding it here to avoid people having to manually configure it

@@ -115,7 +85,7 @@ private fun partitionDifferences(
removedVersion != null -> {
mutableRemovedMap.remove(artifact)
upgrades.add(
VersionedDependency(artifact, "$version, (changed from $removedVersion)"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing it to store the otherVersion in field of VersionedDependency to decouple printing logic from business logic

@@ -133,20 +103,4 @@ private fun partitionDifferences(
)
}

private fun StringBuilder.writeList(title: String, list: List<VersionedDependency>, newLineBeforeTitle: Boolean = false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just moved to the printer.kt file

@@ -65,7 +65,7 @@ fun upgradeEffects(old: String, new: String, collapseKeys: List<String>): String
if (index > 0) {
appendLine()
}
append("Changing ${effects.first.artifact} to ${effects.first.version} will also change:")
append("Changing ${effects.first.artifact} to ${effects.first.version}${effects.first.alternativeVersion?.let { ", (changed from $it)"}} will also change:")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to stay backward compatible and print on the same way as before


private val old by argument("old.txt").path(mustExist = true)
private val new by argument("new.txt").path(mustExist = true)

override fun run() {
val oldContents = old.readText()
val newContents = new.readText()
print(tldr(oldContents, newContents, collapse))
print(tldr(oldContents, newContents), collapse, outputType = output.toOutputType())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this print is the new print, it allows to decouple the output of tldr function and the printing logic

Comment on lines +27 to +34
private fun writeJson(versionDifferences: VersionDifferences, collapse: List<String>): String = buildString {
val (added, removed, upgraded) = versionDifferences
append("{")
writeJsonObject("new_dependencies", added, trailingComma = true)
writeJsonObject("removed_dependencies", removed, trailingComma = true)
writeJsonObject("upgraded_dependencies", collapseDependencies(upgraded, collapse))
append("}")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the new function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add any third party since this json is quite simple and adding a third party dependency doesn't add much value for this use case

Copy link
Contributor

@ahmedre ahmedre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this!

This patch adds the capability to get the tldr output in json format.
@MarcoSignoretto MarcoSignoretto marked this pull request as ready for review July 4, 2023 12:57
@ahmedre ahmedre merged commit 953a153 into main Jul 4, 2023
@ahmedre ahmedre deleted the feature/json-output branch July 4, 2023 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants