From bc75e619f5d725dc52ad653fb704da48bd59df9e Mon Sep 17 00:00:00 2001
From: Adrian Legaspi <legaspi.adrianking@gmail.com>
Date: Mon, 29 Apr 2024 20:33:14 +0300
Subject: [PATCH] Improving CLI tooltip and usage

Update README.md
---
 .bsp/sbt.json                                 |  1 -
 .gitignore                                    |  2 +-
 README.md                                     | 29 +++++++++++--
 build.sbt                                     |  6 ++-
 .../scala/com/skyline/warlangmod/App.scala    | 15 +++++++
 .../com/skyline/warlangmod/Language.scala     |  1 -
 .../scala/com/skyline/warlangmod/Main.scala   | 41 +++++++++++--------
 .../scala/com/skyline/warlangmod/Output.scala | 11 +++--
 .../skyline/warlangmod/ParsingService.scala   | 20 +++++----
 .../com/skyline/warlangmod/Renderable.scala   |  3 ++
 .../TranslationOverwriteService.scala         |  5 +--
 .../ParsingServiceInstanceSpec.scala          | 20 +++++++++
 12 files changed, 115 insertions(+), 39 deletions(-)
 delete mode 100644 .bsp/sbt.json
 create mode 100644 src/main/scala/com/skyline/warlangmod/App.scala
 create mode 100644 src/test/scala/com/skyline/warlangmod/ParsingServiceInstanceSpec.scala

diff --git a/.bsp/sbt.json b/.bsp/sbt.json
deleted file mode 100644
index 7be5c37..0000000
--- a/.bsp/sbt.json
+++ /dev/null
@@ -1 +0,0 @@
-{"name":"sbt","version":"1.9.7","bspVersion":"2.1.0-M1","languages":["scala"],"argv":["C:\\Program Files\\Java\\jdk-17.0.3.1/bin/java","-Xms100m","-Xmx100m","-classpath","C:/Users/marcu/AppData/Roaming/JetBrains/IdeaIC2022.1/plugins/Scala/launcher/sbt-launch.jar","-Dsbt.script=C:\\Users\\marcu\\AppData\\Local\\Coursier\\data\\bin\\sbt.bat","xsbt.boot.Boot","-bsp"]}
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index 4b34ba5..e42364d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,4 +7,4 @@ target/
 */target
 **/target
 .bsp/*
-.bst/**/*
+.bsp/**/*
diff --git a/README.md b/README.md
index 3cc6ab6..75008e9 100644
--- a/README.md
+++ b/README.md
@@ -5,11 +5,34 @@ An updater for lang files for War Thunder, written in Scala
 
 You will need your existing Units.csv file and the updated version file, renamed to UnitsN.csv, something you can get from Gszabi99's repository [here](https://github.com/gszabi99/War-Thunder-Datamine).
 <br>
-Run the file like so: 
+Help file
+```shell
+$ java -jar WarThunder-Lang-Updater-0.1.0-SNAPSHOT.jar --help
+
+Usage: WarThunder Translation Updater [--input <string>] [--original <string>] [--output <string>]
+
+A tool to update the translation files for WarThunder
+
+Options and flags:
+    --help
+        Display this help text.
+    --input <string>, -i <string>
+        The input file to be updated, defaults to units.csv when not provided
+    --original <string>, -r <string>
+        The original file to be updated, defaults to unitsN.csv when not provided
+    --output <string>, -o <string>
+        The output file to be written to, defaults unitsMod.csv when not provided
 ```
-run 'file path 1' 'file path 2'
+
+Run the JAR file with the following command:
+```shell
+$ java -jar WarThunder-Lang-Updater-0.1.0-SNAPSHOT.jar --input <file path 1> --original <file path 2> --output <file path 3>
 ```
-where file path 1 and 2 are your old and new version files respectively, it can be your absolute or relative path
+
+Where `input` file is your old translations file and `original` is the translation file from the most recent WarThunder Client, it can be your absolute or relative path
+
+The `output` file is the file that will be written to, it can be absolute or relative path
+
 
 ## Can you use this for any other file other than Units.csv?
 Some of the localization files currently do not work with this program. So far I've confirmed it to work with units and units_weaponry.
diff --git a/build.sbt b/build.sbt
index c82c903..6f396cf 100644
--- a/build.sbt
+++ b/build.sbt
@@ -1,6 +1,6 @@
 ThisBuild / version := "0.1.0-SNAPSHOT"
 
-ThisBuild / scalaVersion := "2.13.11"
+ThisBuild / scalaVersion := "2.13.13"
 
 ThisBuild / organization := "com.skyline.warlangmod"
 
@@ -8,4 +8,8 @@ lazy val root = (project in file("."))
   .settings(
     name := "WarThunder Language Updater",
     assembly / mainClass := Some("com.skyline.warlangmod.Main"),
+    libraryDependencies ++= Seq(
+      "com.monovore" %% "decline" % "2.4.1",
+      "org.scalameta" %% "munit" % "0.7.29" % Test,
+    ),
   )
diff --git a/src/main/scala/com/skyline/warlangmod/App.scala b/src/main/scala/com/skyline/warlangmod/App.scala
new file mode 100644
index 0000000..e392f67
--- /dev/null
+++ b/src/main/scala/com/skyline/warlangmod/App.scala
@@ -0,0 +1,15 @@
+package com.skyline.warlangmod
+
+import java.io.FileWriter
+
+object App {
+  def run(inputFileName: String, originalFile: String, outputFileName: String): Unit = {
+    val parsingService = ParsingService.instance
+    val translationOverwrite = TranslationOverwriteService.instance
+    val outputService = Output.instance
+    val moddedTranslations = parsingService.translations(inputFileName)
+    val originalUpdatedTranslations = parsingService.translations(originalFile)
+    val updatedModdedFile = translationOverwrite.overwrite(originalUpdatedTranslations, moddedTranslations)
+    outputService.write(updatedModdedFile, outputFileName)
+  }
+}
diff --git a/src/main/scala/com/skyline/warlangmod/Language.scala b/src/main/scala/com/skyline/warlangmod/Language.scala
index 2af3d10..bc4e0a9 100644
--- a/src/main/scala/com/skyline/warlangmod/Language.scala
+++ b/src/main/scala/com/skyline/warlangmod/Language.scala
@@ -1,6 +1,5 @@
 package com.skyline.warlangmod
 
-import scala.collection.mutable.ArrayBuffer
 import scala.io.BufferedSource
 import scala.util.Try
 
diff --git a/src/main/scala/com/skyline/warlangmod/Main.scala b/src/main/scala/com/skyline/warlangmod/Main.scala
index aea8fbb..7ae2ac3 100644
--- a/src/main/scala/com/skyline/warlangmod/Main.scala
+++ b/src/main/scala/com/skyline/warlangmod/Main.scala
@@ -1,22 +1,31 @@
 package com.skyline.warlangmod
 
-import java.io.FileWriter
+import com.monovore.decline._
 
-object Main {
-  def main(args: Array[String]): Unit = {
+import cats.implicits._
 
-    if(args.length < 2)
-      System.exit(1) // No input file
+object Main extends CommandApp(
+  name = "WarThunder Translation Updater",
+  header = "A tool to update the translation files for WarThunder ",
+  main = {
+    val defaultInFile = "units.csv"
+    val defaultOriginalFile = "unitsN.csv"
+    val defaultOutFile = "unitsMod.csv"
+    val originalFile = Opts.option[String](
+      long = "original",
+      short = "r",
+      help = s"The original file to be updated, defaults to $defaultOriginalFile when not provided").withDefault(defaultOriginalFile)
+    val inFile = Opts.option[String](
+      long = "input",
+      short = "i",
+      help = s"The input file to be updated, defaults to $defaultInFile when not provided").withDefault(defaultInFile)
+    val outFile = Opts.option[String](
+      long = "output",
+      short = "o",
+      help = s"The output file to be written to, defaults $defaultOutFile when not provided").withDefault(defaultOutFile)
 
-    val parsingService                = ParsingService()
-    val translationOverwriter         = TranslationOverwriteService()
-    val outputService                 = Output()
-
-    val moddedTranslations            = parsingService.translations(args(0))
-    val originalUpdatedTranslations   = parsingService.translations(args(1))
-
-    val uptodateModdedTranslationFile = translationOverwriter.overwrite(originalUpdatedTranslations, moddedTranslations)
-
-    outputService.write(uptodateModdedTranslationFile)
+    (inFile, originalFile, outFile).mapN {
+      (inputFileName, originalFileName, outputFileName) => App.run(inputFileName, originalFileName, outputFileName)
+    }
   }
-}
+)
\ No newline at end of file
diff --git a/src/main/scala/com/skyline/warlangmod/Output.scala b/src/main/scala/com/skyline/warlangmod/Output.scala
index 276a76e..463d8c4 100644
--- a/src/main/scala/com/skyline/warlangmod/Output.scala
+++ b/src/main/scala/com/skyline/warlangmod/Output.scala
@@ -1,14 +1,17 @@
 package com.skyline.warlangmod
 
+/**
+ *  For writing the translations to a file
+ */
 trait Output {
-    def write(file: Translations): Unit
+    def write(file: Translations, outputFileName: String): Unit
 }
 
 object Output {
-  def apply(): Output = new Output {
+  lazy val instance: Output = new Output {
     import java.io.FileWriter
-    override def write(file: Translations): Unit = {
-      val updatedFile = new FileWriter("UnitsMod.csv")
+    override def write(file: Translations, outputFileName: String): Unit = {
+      val updatedFile = new FileWriter(outputFileName)
       updatedFile.write(file.render())
       updatedFile.close()
     }
diff --git a/src/main/scala/com/skyline/warlangmod/ParsingService.scala b/src/main/scala/com/skyline/warlangmod/ParsingService.scala
index 271a2d7..d13d6a1 100644
--- a/src/main/scala/com/skyline/warlangmod/ParsingService.scala
+++ b/src/main/scala/com/skyline/warlangmod/ParsingService.scala
@@ -1,20 +1,22 @@
 package com.skyline.warlangmod
 
+import scala.io.Source
+
+/**
+ * For parsing language/translation files of WarThunder
+ */
+
 trait ParsingService {
   def translations(filename: String): Translations
 }
 
 object ParsingService {
 
-  def apply(): ParsingService = new ParsingService {
-    import scala.io.Source
-    override def translations(filename: String): Translations = {
-      val langFile     = Source.fromFile(filename)
-      val translations = Language.parse(langFile)
-      langFile.close()
-      translations
-    }
-
+  lazy val instance: ParsingService = (filename: String) => {
+    val langFile = Source.fromFile(filename)
+    val translations = Language.parse(langFile)
+    langFile.close()
+    translations
   }
 
 }
diff --git a/src/main/scala/com/skyline/warlangmod/Renderable.scala b/src/main/scala/com/skyline/warlangmod/Renderable.scala
index 276807e..ad4160c 100644
--- a/src/main/scala/com/skyline/warlangmod/Renderable.scala
+++ b/src/main/scala/com/skyline/warlangmod/Renderable.scala
@@ -1,5 +1,8 @@
 package com.skyline.warlangmod
 
+/**
+ * A trait for objects that can be rendered as a string.
+ */
 trait Renderable {
   def render(): String
 }
diff --git a/src/main/scala/com/skyline/warlangmod/TranslationOverwriteService.scala b/src/main/scala/com/skyline/warlangmod/TranslationOverwriteService.scala
index 28c8d87..8d2c715 100644
--- a/src/main/scala/com/skyline/warlangmod/TranslationOverwriteService.scala
+++ b/src/main/scala/com/skyline/warlangmod/TranslationOverwriteService.scala
@@ -5,7 +5,7 @@ trait TranslationOverwriteService {
 }
 
 object TranslationOverwriteService {
-  def apply(): TranslationOverwriteService = new TranslationOverwriteService {
+  lazy val instance: TranslationOverwriteService = new TranslationOverwriteService {
 
     private type ObjectName = String
 
@@ -13,8 +13,7 @@ object TranslationOverwriteService {
       val moddedMap  = toMap(modded)
       val updated = original.languages.map { language =>
         // headOption because we only need the first translation which is english
-        // Fuck the french btw
-        val overwrittenLanguageTranslation = moddedMap.get(language.objName).getOrElse(language.translation)
+        val overwrittenLanguageTranslation = moddedMap.getOrElse(language.objName, language.translation)
         language.copy(translation = overwrittenLanguageTranslation)
       }
       
diff --git a/src/test/scala/com/skyline/warlangmod/ParsingServiceInstanceSpec.scala b/src/test/scala/com/skyline/warlangmod/ParsingServiceInstanceSpec.scala
new file mode 100644
index 0000000..876fe51
--- /dev/null
+++ b/src/test/scala/com/skyline/warlangmod/ParsingServiceInstanceSpec.scala
@@ -0,0 +1,20 @@
+package com.skyline.warlangmod
+
+import java.io.FileWriter
+import java.nio.file.Files
+
+class ParsingServiceInstanceSpec extends munit.FunSuite {
+  test("ParsingServiceInstance should parse a WarThunder Translation file format") {
+    val translations = Translations(
+      languages = Vector(
+        Language("something", "good")
+      )
+    )
+    val tmpFile = Files.createTempFile("ParsingServiceInstanceTestFile", null)
+    val writer = new FileWriter(tmpFile.toFile)
+    writer.write(translations.render())
+    writer.close()
+    val parsedTranslation = ParsingService.instance.translations(tmpFile.toAbsolutePath.toString)
+    assertEquals(parsedTranslation, translations)
+  }
+}