Skip to content

Commit

Permalink
AERD-10 || Support deprecating strings (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
scana authored Mar 3, 2022
1 parent 7a8e56e commit 12a41a7
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Plugin for downloading and uploading translations from <a href="https://www.ones
</p>

<p align="center">
<img src="https://img.shields.io/badge/version-1.1.0-blue.svg">
<img src="https://img.shields.io/badge/version-1.2.0-blue.svg">
</p>

## Installation

**app/build.gradle.kts**
```kotlin
plugins {
id("co.brainly.onesky") version "1.1.0"
id("co.brainly.onesky") version "1.2.0"
}

// ...
Expand All @@ -35,6 +35,12 @@ configure<OneSkyPluginExtension> {
| **downloadTranslations** | Downloads all of available translations (including not finished ones) |
| **uploadTranslations** | Uploads base translation files |

Add `-Pdeprecate-strings` if you would like to deprecate strings on OneSky which are not present in `sourceStringFiles`.

```bash
# deprecates removed strings on OneSky
./gradlew sample:uploadTranslations -Pdeprecate-strings
```
## Releasing

See the release instructions [here](HOW_TO_RELEASE.md).
Expand All @@ -46,7 +52,7 @@ See the release instructions [here](HOW_TO_RELEASE.md).
./gradlew clean plugin:build plugin:publishToMavenLocal

# run a sample
./gradlew sample:translationsProgress --refresh-dependencies
./gradlew sample:translationsProgress
```

## License
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
apply(plugin = "org.jlleitschuh.gradle.ktlint")

group = "co.brainly"
version = "1.1.0"
version = "1.2.0"

gradlePlugin {
plugins {
Expand Down
4 changes: 3 additions & 1 deletion plugin/src/main/java/co/brainly/onesky/OneSkyPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.brainly.onesky

import co.brainly.onesky.client.ONESKY_API_URL
import co.brainly.onesky.task.DEPRECATE_STRINGS_FLAG
import co.brainly.onesky.task.DownloadTranslationsTask
import co.brainly.onesky.task.TranslationsProgressTask
import co.brainly.onesky.task.UploadTranslationTask
Expand Down Expand Up @@ -45,7 +46,8 @@ class OneSkyPlugin : Plugin<Project> {
description = "Displays current progress from OneSky, skips already finished translations"
}

tasks.register("uploadTranslations", UploadTranslationTask::class.java, extension)
val deprecateStrings = project.hasProperty(DEPRECATE_STRINGS_FLAG)
tasks.register("uploadTranslations", UploadTranslationTask::class.java, extension, deprecateStrings)
.configure {
group = Constants.TASK_GROUP
description = "Uploads base \"sourceStringFiles\" to OneSky"
Expand Down
10 changes: 8 additions & 2 deletions plugin/src/main/java/co/brainly/onesky/client/OneSkyApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,20 @@ class OneSkyApiClient(
return fetch(request)
}

fun uploadTranslation(projectId: Int, file: File): Result<String> {
fun uploadTranslation(projectId: Int, file: File, deprecateStrings: Boolean): Result<String> {
val isKeepingAllStrings = if (deprecateStrings) {
"false"
} else {
"true"
}

val url = baseUrl.newBuilder()
.addPathSegment("projects")
.addPathSegment(projectId.toString())
.addPathSegment("files")
.addAuthParams(apiKey, apiSecret)
.addQueryParameter("file_format", "ANDROID_XML")
.addQueryParameter("is_keeping_all_strings", "true")
.addQueryParameter("is_keeping_all_strings", isKeepingAllStrings)
.build()

val body = MultipartBody.Builder(boundary = "onesky-gradle-plugin-file")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import org.slf4j.LoggerFactory
import java.io.File
import javax.inject.Inject

const val DEPRECATE_STRINGS_FLAG = "deprecate-strings"

open class UploadTranslationTask @Inject constructor(
extension: OneSkyPluginExtension
extension: OneSkyPluginExtension,
private val deprecateStrings: Boolean
) : DefaultTask() {

private val projectId = extension.projectId
Expand All @@ -35,6 +38,10 @@ open class UploadTranslationTask @Inject constructor(
fun run() {
progressLogger.start("Uploading translations", "")

if (deprecateStrings) {
logger.warn("Deprecating strings on OneSky which are not found in base files.")
}

files.forEachIndexed { index, filename ->
progressLogger.progress(
"$filename (${index + 1}/${files.size})"
Expand All @@ -44,7 +51,11 @@ open class UploadTranslationTask @Inject constructor(

logger.warn(baseTranslationFile.absolutePath)

val result = client.uploadTranslation(projectId, baseTranslationFile)
val result = client.uploadTranslation(
projectId,
baseTranslationFile,
deprecateStrings = deprecateStrings
)
result.handle(
onSuccess = { /*do nothing*/ },
onFailure = { error -> onUploadFailure(filename, error) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class OneSkyApiClientTest {
val file = File.createTempFile("onesky", ".tmp")
file.writeText("Hello OneSky Gradle Plugin")

client.uploadTranslation(projectId, file)
client.uploadTranslation(projectId, file, deprecateStrings = false)

val request = server.takeRequest()

Expand Down Expand Up @@ -137,4 +137,42 @@ class OneSkyApiClientTest {
request.body.readByteString().utf8()
)
}

@Test
fun `uploads a translation file and deprecate old strings`() {
val file = File.createTempFile("onesky", ".tmp")
file.writeText("Hello OneSky Gradle Plugin")

client.uploadTranslation(projectId, file, deprecateStrings = true)

val request = server.takeRequest()

assertEquals(
"/projects/41994/files?api_key=my-api-key&timestamp=12" +
"&dev_hash=28dac32cc9ee8ab264d35087653be23e&file_format=ANDROID_XML&is_keeping_all_strings=false",
request.path
)

assertEquals(
"POST",
request.method
)

assertEquals(
"""
--onesky-gradle-plugin-file
Content-Disposition: form-data; name="file"; filename="${file.name}"
Content-Type: application/octet-stream
Content-Length: 26
Hello OneSky Gradle Plugin
--onesky-gradle-plugin-file--
""".trimIndent().replace(
"\n",
"\r\n"
), // to avoid conflicts with OkHttp
request.body.readByteString().utf8()
)
}
}
2 changes: 1 addition & 1 deletion sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {
plugins {
id("com.android.application")
kotlin("android")
id("co.brainly.onesky") version "1.1.0"
id("co.brainly.onesky") version "1.2.0"
}

android {
Expand Down

0 comments on commit 12a41a7

Please sign in to comment.