Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Add new flag to specify a specific file to format
Browse files Browse the repository at this point in the history
Update README.md
Reword the exception message if no mod file is found
Add newline to end of files
  • Loading branch information
nappys-legacy committed Jun 23, 2020
1 parent 3807ccb commit 256c90d
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 29 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,41 @@ $ java -jar v2format.jar
$ java -jar v2format.jar --help
$ java -jar v2format.jar --config path/to/config
$ java -jar v2format.jar --folder path/to/mod
$ java -jar v2format.jar --file path/to/file
$ java -jar v2format.jar --file path/to/file --config path/to/config
$ java -jar v2format.jar --folder path/to/mod --file path/to/file
$ java -jar v2format.jar --folder path/to/mod --config path/to/config
$ java -jar v2format.jar --config path/to/config
$ java -jar v2format.jar --folder path/to/mod --file path/to/file --config path/to/config
```

### `--folder`

The `--folder` argument is by default the directory from which `v2format.jar` was executed.
The `--folder` argument is by default the directory from which `v2format.jar` was executed,
also known as the current directory.
The program will then search for a single `.mod` file and open up the mod based off the file results.
If no `.mod` file is found or multiple are found, the program exits.
In those cases, `--folder` should be used.

### `--file`

The `--file` argument is an optional argument and has no default value.
If the argument is provided, the program will still search for a `.mod` file and configuration in the same directory.
The `--folder` argument still applies to defining the mod and configuration.
If no `.mod` file is found, the program exits.
If the file passed in as an argument cannot be formatted by way of its extension or exclusion configuration, the program will exit.

Note that it is possible to use the `--folder` argument to indicate the mod to format while passing in an argument
to `--file` that is in a different directory.
It is important to know that excluding files is based off the relative path from the specified mod directory and not some pattern.

### `--config`

The `--config` argument is by default the file `v2format.config.json`.
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/v2/format/FileFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ class FileFormatter(private val modFolder: File) {
removeErrorListeners()
addErrorListener(listener)
}
}
}
41 changes: 30 additions & 11 deletions src/main/kotlin/v2/format/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,32 @@ import v2.format.config.Config
import java.io.File

class Main : CliktCommand(help = "A program formatter for Victoria 2 mods.") {
private val config by option(help = "Specifies the configuration file, otherwise it uses the file 'v2format.config.json'.")
private val folder by option(help = "Specifies the mod directory, otherwise it attempts to find the mod in the current directory")
.file(
mustExist = true,
canBeFile = true,
canBeDir = false,
mustBeReadable = true
canBeFile = false,
canBeDir = true,
mustBeReadable = true,
mustBeWritable = true
)

private val folder by option(help = "Specifies the mod directory, otherwise it attempts to find the mod in the current directory.")
private val file by option(help = "Specifies the file to format relative to the mod directory")
.file(
mustExist = true,
canBeFile = false,
canBeDir = true,
canBeFile = true,
canBeDir = false,
mustBeReadable = true,
mustBeWritable = true
)

private val config by option(help = "Specifies the configuration file, otherwise it uses the file 'v2format.config.json'")
.file(
mustExist = true,
canBeFile = true,
canBeDir = false,
mustBeReadable = true
)

init {
context {
helpFormatter = CliktHelpFormatter(showDefaultValues = true)
Expand All @@ -48,14 +57,24 @@ class Main : CliktCommand(help = "A program formatter for Victoria 2 mods.") {
try {
Config.loadConfig(modConfig)
} catch (e: Exception) {
throw RuntimeException("Config file ${modConfig.name} is incorrect. \n${e.message}")
val cleanMessage = e.message.orEmpty()
.replace(" You can enable 'JsonConfiguration.ignoreUnknownKeys' property to ignore unknown keys.", "")
throw RuntimeException("Config file ${modConfig.name} is incorrect. \n$cleanMessage")
}

val fileFormatter = FileFormatter(modFolder)

val modFormatter = ModFormatter(modFolder, fileFormatter)
modFormatter.format()

if (file != null) {
if (modFormatter.isFormattable(file!!)) {
fileFormatter.formatFile(file!!)
} else {
throw RuntimeException("File ${file!!.name} cannot be formatted due to its extension or exclusion configuration.")
}
} else {
modFormatter.format()
}
}
}

fun main(args: Array<String>) = Main().main(args)
fun main(args: Array<String>) = Main().main(args)
3 changes: 1 addition & 2 deletions src/main/kotlin/v2/format/ModFinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ModFinder {
val modFiles = file.listFiles { pathName -> pathName.extension == "mod" }!!

if (modFiles.isEmpty()) {
throw IOException("No .mod file in current directory found.")
throw IOException("No .mod file in directory found.")
}

if (modFiles.size > 1) {
Expand All @@ -29,4 +29,3 @@ class ModFinder {
return File(modFolderName)
}
}

6 changes: 3 additions & 3 deletions src/main/kotlin/v2/format/ModFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class ModFormatter(private val modFolder: File, private val fileFormatter: FileF

fun format() {
modFolder.walk()
.filter(::filterFile)
.filter(::isFormattable)
.forEach(fileFormatter::formatFile)
}

private fun filterFile(file: File) = with(file) {
fun isFormattable(file: File) = with(file) {
isFile && extension in goodExtensions && relativeTo(modFolder).path.asUnix() !in Config.excludeFiles
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/v2/format/Util.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package v2.format

fun String.asUnix(): String = replace('\\', '/')
fun String.asUnix(): String = replace('\\', '/')
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class ClausewitzErrorListener(private val file: File) : BaseErrorListener() {
) {
throw RuntimeException("${file.name}:$line:$charPositionInLine: error: $msg")
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/v2/format/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ object Config {
val levels = getLevels(key)
return configTree[levels]
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/v2/format/config/FormatOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ data class FormatOptions(
private val tab = " ".repeat(tabWidth)

fun indent(indentation: Int) = tab.repeat(indentation)
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/v2/format/config/FormatOptionsLayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ data class FormatOptionsLayer(
singleLineBlock ?: options.singleLineBlock,
bracketWraparound ?: options.bracketWraparound
)
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/v2/format/ModFinderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ internal class ModFinderTest {
val finder = ModFinder()
assert(!finder.getModFolder(file).exists())
}
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/v2/format/ModFormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ internal class ModFormatterTest {

return mock
}
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/v2/format/TestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ fun resetConfig() {
tree.set(tree.javaClass.kotlin.objectInstance, ConfigTree())

Config.excludeFiles = emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -751,4 +751,4 @@ level3 = { #comment3
private fun testFile(options: FormatOptions, original: File, test: File) {
testInput(ClausewitzParser::program, options, original.readText(), test.readText())
}
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/v2/format/config/ConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ internal class ConfigTest {

resetConfig()
}
}
}

0 comments on commit 256c90d

Please sign in to comment.