Skip to content

Commit

Permalink
Add native support for (de-)serialization of *.po files (can be used …
Browse files Browse the repository at this point in the history
…from both Java and Javascript)
  • Loading branch information
floscher committed Oct 18, 2020
1 parent dddeeca commit d368c88
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 100 deletions.
64 changes: 8 additions & 56 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
image: registry.gitlab.com/josm/docker-library/openjdk-8-git:latest
image: registry.gitlab.com/josm/docker-library/openjdk-11-git:latest

before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
Expand All @@ -19,36 +19,9 @@ assemble:
paths:
- build/

assemble on java11:
stage: build
image: registry.gitlab.com/josm/docker-library/openjdk-11-git:latest
script:
- ./gradlew --stacktrace jar
artifacts:
paths:
- build/

assemble on java12:
stage: build
image: registry.gitlab.com/josm/docker-library/openjdk-12-git:latest
script:
- ./gradlew --stacktrace jar
artifacts:
paths:
- build/

assemble on java13:
stage: build
image: registry.gitlab.com/josm/docker-library/openjdk-13-git:latest
script:
- ./gradlew --stacktrace jar
artifacts:
paths:
- build/

test:
stage: test
image: registry.gitlab.com/josm/docker-library/openjdk-8-josmplugin
image: registry.gitlab.com/josm/docker-library/openjdk-11-josmplugin
script:
- ./gradlew --stacktrace --info build :dokkaHtmlMultimodule :jacocoTestReport
artifacts:
Expand All @@ -61,38 +34,15 @@ test:
needs:
- assemble

test on java11:
stage: test
image: registry.gitlab.com/josm/docker-library/openjdk-11-josmplugin:latest
script:
- ./gradlew --stacktrace --info build
needs:
- assemble on java11

test on java12:
stage: test
image: registry.gitlab.com/josm/docker-library/openjdk-12-josmplugin:latest
script:
- ./gradlew --stacktrace --info build
needs:
- assemble on java12

test on java13:
stage: test
image: registry.gitlab.com/josm/docker-library/openjdk-13-josmplugin:latest
script:
- ./gradlew --stacktrace --info build
needs:
- assemble on java13

publish to / Gradle Plugin Portal:
stage: deploy
environment:
name: maven / Gradle Plugin Portal
url: https://plugins.gradle.org/plugin/org.openstreetmap.josm
script:
- ./gradlew -Pgradle.publish.secret="$GRADLE_PUBLISH_SECRET" -Pgradle.publish.key="$GRADLE_PUBLISH_KEY" publishPlugins
needs: [ "test", "test on java11", "test on java12", "test on java13" ]
needs:
- test
only:
- tags@floscher/gradle-josm-plugin

Expand All @@ -104,7 +54,8 @@ publish to / GitLab.com Maven:
script:
- ./gradlew publishAllPublicationsToGitlabRepository
- ./gradlew releaseToGitlab
needs: [ "test", "test on java11", "test on java12", "test on java13" ]
needs:
- test
only:
- tags@floscher/gradle-josm-plugin

Expand Down Expand Up @@ -138,6 +89,7 @@ publish to / pages branch:
git config user.email "incoming+floscher/[email protected]"
git commit -a -m "Update Maven repository and KDoc documentation to $longVersion"
git push origin pages
needs: [ "test", "test on java11", "test on java12", "test on java13" ]
needs:
- test
only:
- tags@floscher/gradle-josm-plugin
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.dokka.Platform
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.openstreetmap.josm.gradle.plugin.GitDescriber
import org.openstreetmap.josm.gradle.plugin.api.gitlab.gitlabRepository
Expand All @@ -9,7 +10,6 @@ import org.openstreetmap.josm.gradle.plugin.logPublishedMavenArtifacts
import org.openstreetmap.josm.gradle.plugin.logSkippedTasks
import org.openstreetmap.josm.gradle.plugin.logTaskDuration
import java.net.URL
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension

plugins {
id("org.jetbrains.dokka")
Expand All @@ -31,8 +31,8 @@ gradle.projectsEvaluated {
dependsOn(testTasks)

additionalClassDirs(project(":i18n").tasks.getByName<KotlinCompile>("compileKotlinJvm").destinationDir)
additionalSourceDirs(project(":i18n").extensions.getByType(KotlinMultiplatformExtension::class).sourceSets.getByName("commonMain").kotlin.sourceDirectories)
additionalSourceDirs(project(":i18n").extensions.getByType(KotlinMultiplatformExtension::class).sourceSets.getByName("jvmMain").kotlin.sourceDirectories)
additionalSourceDirs(project(":i18n").extensions.getByType(KotlinProjectExtension::class).sourceSets.getByName("commonMain").kotlin.sourceDirectories)
additionalSourceDirs(project(":i18n").extensions.getByType(KotlinProjectExtension::class).sourceSets.getByName("jvmMain").kotlin.sourceDirectories)

sourceSets(*
setOf(":dogfood", ":langconv", ":plugin")
Expand All @@ -50,7 +50,7 @@ gradle.taskGraph.logTaskDuration()
logSkippedTasks()

allprojects {
group = "org.openstreetmap.josm"
group = "org.openstreetmap.josm" + if (name != "plugin") ".gradle-josm-plugin" else ""
version = GitDescriber(rootProject.projectDir).describe(trimLeading = true)

repositories.jcenter()
Expand Down
1 change: 1 addition & 0 deletions i18n/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
}

kotlin {
explicitApiWarning()
js().browser()
jvm()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ data class FourBytes(val a: Byte, val b: Byte, val c: Byte, val d: Byte) {
* @param uintValue the 32 bit unsigned integer value
* @param bigEndian if true, the number is encoded in big-endian byte order, otherwise in little-endian
*/
constructor(uintValue: kotlin.UInt, bigEndian: Boolean): this(
constructor(uintValue: UInt, bigEndian: Boolean): this(
uintValue.shr(24).and(0xFFu).toUByte().toByte(),
uintValue.shr(16).and(0xFFu).toUByte().toByte(),
uintValue.shr(8).and(0xFFu).toUByte().toByte(),
Expand All @@ -45,7 +45,7 @@ data class FourBytes(val a: Byte, val b: Byte, val c: Byte, val d: Byte) {
* @return the [kotlin.UInt] value (unsigned 32 bit) represented by the four bytes [a], [b], [c] and [d], respecting the given byte order.
*/
@ExperimentalUnsignedTypes
fun getUIntValue(bigEndian: Boolean): kotlin.UInt =
fun getUIntValue(bigEndian: Boolean): UInt =
if (bigEndian) {
// Big endian: "Beginning at the big end", first byte is most significant
FourBytes(d, c, b, a).getUIntValue(!bigEndian)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.openstreetmap.josm.gradle.plugin.i18n.io


internal val GETTEXT_CONTENT_TYPE_UTF8: String = "Content-Type: text/plain; charset=UTF-8"
internal val GETTEXT_EMPTY_HEADER: Pair<MsgId, MsgStr> = MsgId(MsgStr("")) to MsgStr("")
internal val GETTEXT_DEFAULT_HEADER: Pair<MsgId, MsgStr> = MsgId(MsgStr("")) to MsgStr("$GETTEXT_CONTENT_TYPE_UTF8\n")

/**
* If the [msgid] is just the empty string, returns a modified [msgstr] that contains the UTF-8 content type.
* Otherwise returns the given [msgstr] unmodified.
*/
internal fun Map<MsgId, MsgStr>.ensureUtf8EncodingInHeaderEntry(): List<Pair<MsgId, MsgStr>> =
mapOf(GETTEXT_EMPTY_HEADER).plus(this).map { (key, value) ->
if (key == MsgId(MsgStr(""))) {
key to MsgStr(
value.strings.mapIndexed { index, str ->
if (index == 0) {
str.lines()
.filter { !it.startsWith("Content-Type:") && !it.isEmpty() }
.plus(GETTEXT_CONTENT_TYPE_UTF8)
.joinToString("\n", "", "\n")
} else {
str
}
}
)
} else key to value
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.openstreetmap.josm.gradle.plugin.i18n.io

/**
* Represents a file format for i18n that allows for encoding strings and their translations into a [ByteArray]
* and decoding them again from such a [ByteArray].
*/
public interface I18nFileFormat {
/**
* Encode the [translations] given as parameter to the [I18nFileFormat]
* @param translations the translations that should be encoded
* @return the encoded bytes
*/
public fun encodeToByteArray(translations: Map<MsgId, MsgStr>): ByteArray
public fun decodeToTranslations(bytes: ByteArray): Map<MsgId, MsgStr>
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ data class MsgStr(val strings: List<String>) {
* @param singularString the singular version of the translatable string
* @param pluralStrings the plural versions of the translatable string
*/
constructor(singularString: String, vararg pluralStrings: String): this(listOf(singularString, *pluralStrings))
constructor(singularString: String, vararg pluralStrings: String): this(listOf(singularString).plus(pluralStrings))
init {
require(strings.isNotEmpty()){"A MsgStr has to consist of at least one string!"}
}
Expand Down
Loading

0 comments on commit d368c88

Please sign in to comment.