Skip to content

Commit

Permalink
1.0.3: Support internal visibility modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Oct 25, 2024
1 parent d77c050 commit 2a06a4f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ To generate a translations class, create an instance of `TranslationsClass`, pas
- `bundle`, the corresponding bundle name.
- `className`, the name used by the generated class.
- `classPackage`, the package containing the generated class.
- `publicVisibility`, whether to generate objects with the `public` visibility modifier.
Defaults to `true`, will use `internal` if set to `false`.

If you need to access the KotlinPoet `FileSpec` object, access it via the `spec` property.
Otherwise, use the `writeTo(File)` function to write the generated class to a given directory, including the package
Expand Down Expand Up @@ -60,6 +62,7 @@ The tool will generate this class within a directory tree matching the given pac

- `-c, --class-name = Translations` - Name for the generated root class.
- `-e, --encoding = UTF-8` - Encoding used to read the input properties file.
- `-in, --internal` - Generate objects with the `internal` visibility modifier instead of `public`.
- `-o, --output-dir = ./output` - Output directory for the generated files.

### Other Parameters
Expand Down
7 changes: 7 additions & 0 deletions changes/1.0.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# i18n tools 1.0.2

This is what I get for developing on a work night!

## Translations Class Generator

**CLI:** Fix the main class in the manifest, which IntelliJ messed up somehow. It works now, I promise!
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ kotlin.incremental=true
org.gradle.jvmargs=-XX:MaxMetaspaceSize=1024m
org.gradle.parallel=true

projectVersion=1.0.2
projectVersion=1.0.3
11 changes: 11 additions & 0 deletions i18n-generator/src/main/kotlin/dev/kordex/i18n/generator/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public fun main(vararg args: String) {
)
}

spec.addOption<Boolean>("-in", "--internal") {
paramLabel("INTERNAL")
defaultValue("false")

description(
"Generate objects with internal visibility, rather than public."
)
}

val commandLine = CommandLine(spec)

commandLine.setExecutionStrategy(::run)
Expand All @@ -99,6 +108,7 @@ private fun run(result: CommandLine.ParseResult): Int {
val inputFile: File = result.matchedOption("i").getValue()
val classPackage: String = result.matchedOption("p").getValue()
val encoding: String = result.matchedOptionValue("e", "UTF-8")
val internal: Boolean = result.matchedOptionValue("in", false)

val className: String = result.matchedOptionValue("c", "Translations")
val outputDir: File = result.matchedOptionValue("o", File("output"))
Expand Down Expand Up @@ -135,6 +145,7 @@ private fun run(result: CommandLine.ParseResult): Int {
allProps = props,
className = className,
classPackage = classPackage,
publicVisibility = !internal
)

translationsClass.writeTo(outputDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.hanggrian.kotlinpoet.buildFileSpec
import com.hanggrian.kotlinpoet.buildPropertySpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import java.io.File
import java.util.Properties
Expand All @@ -20,13 +21,20 @@ public class TranslationsClass(
public val allProps: Properties,
public val bundle: String,
public val className: String,
public val publicVisibility: Boolean = true,

classPackage: String
) {
public val visibility: KModifier = if (publicVisibility) {
KModifier.PUBLIC
} else {
KModifier.INTERNAL
}
public val allKeys: List<String> = allProps.toList().map { (left, _) -> left.toString() }

public val spec: FileSpec = buildFileSpec(classPackage, className) {
types.addObject(className) {
addModifiers(visibility)
bundle()

addKeys(allKeys, allProps, className)
Expand All @@ -39,6 +47,7 @@ public class TranslationsClass(

public fun key(name: String, value: String, property: String, translationsClassName: String): PropertySpec =
buildPropertySpec(name.replace("-", "_"), ClassName("dev.kordex.core.i18n.types", "Key")) {
addModifiers(visibility)
setInitializer("Key(%S)\n.withBundle(%L.bundle)", value, translationsClassName)

property.lines().forEach {
Expand Down Expand Up @@ -79,6 +88,7 @@ public class TranslationsClass(
.joinToString("")

types.addObject(objName) {
addModifiers(visibility)
addKeys(v, props, translationsClassName, keyName)
}
}
Expand All @@ -88,6 +98,7 @@ public class TranslationsClass(
public fun TypeSpecBuilder.bundle() {
properties.add(
buildPropertySpec("bundle", ClassName("dev.kordex.core.i18n.types", "Bundle")) {
addModifiers(visibility)
setInitializer("Bundle(%S)", bundle)
}
)
Expand Down

0 comments on commit 2a06a4f

Please sign in to comment.