Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up static analysis checks #4

Merged
merged 5 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[*.{kt,kts}]

max_line_length = 130
indent_style = space
indent_size = 4

insert_final_newline = true
trim_trailing_whitespace = true

ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true

continuation_indent_size = 4
ktlint_standard_import-ordering = disabled
ij_kotlin_imports_layout = *,java.**,javax.**,kotlin.**,^
2 changes: 1 addition & 1 deletion .github/workflows/gradle-wrapper-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
validation:
name: Validation
name: Gradle Wrapper Validation
runs-on: ubuntu-latest
steps:
- name: Checkout latest code
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Check Pull Request
on:
pull_request:
branches:
- '*'

concurrency:
group: ${{ github.head_ref }}
cancel-in-progress: true

jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- name: Checkout latest code
uses: actions/checkout@v4
- name: Run check
run: ./gradlew --project-dir plugin-build check
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
uses: actions/checkout@v4
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
- name: Run check
run: ./gradlew --project-dir plugin-build check
- name: Publish on Plugin Portal
run: ./gradlew --project-dir plugin-build setupPublishSecrets publishPlugins
if: success()
42 changes: 42 additions & 0 deletions config/detekt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
build:
maxIssues: 5
excludeCorrectable: false

config:
validation: true
warningsAsErrors: false
checkExhaustiveness: false
# when writing own rules with new properties, exclude the property path e.g.: 'my_rule_set,.*>.*>[my_property]'
excludes: ''

naming:
active: true
MatchingDeclarationName:
active: false
FunctionNaming:
active: true
excludes: ['**/test/**']
functionPattern: '[a-z][a-zA-Z0-9]*'
excludeClassPattern: '$^'

style:
active: true
UnusedPrivateMember:
active: true
MaxLineLength:
active: true
maxLineLength: 130 # Keep aligned with .editorconfig file to prevent false positives
MagicNumber:
active: true
DestructuringDeclarationWithTooManyEntries:
active: true

complexity:
LongParameterList:
active: true
TooManyFunctions:
active: true
thresholdInInterfaces: 20
thresholdInClasses: 20
LongMethod:
active: true
6 changes: 5 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
kotlin = "1.9.20"
publish = "1.2.1"
kotlinx-serialization = "1.6.1"
detekt = "1.23.3"
ktlintGradle = "11.6.1"

[libraries]
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
gradle-pluginPublish = { id = "com.gradle.plugin-publish", version.ref = "publish" }
gradle-pluginPublish = { id = "com.gradle.plugin-publish", version.ref = "publish" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt"}
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlintGradle"}
14 changes: 0 additions & 14 deletions plugin-build/gradle/libs.versions.toml

This file was deleted.

36 changes: 35 additions & 1 deletion plugin-build/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import io.gitlab.arturbosch.detekt.Detekt
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.gradle.pluginPublish)
alias(libs.plugins.detekt)
alias(libs.plugins.ktlint)
}

group = property("GROUP").toString()
Expand Down Expand Up @@ -44,4 +49,33 @@ tasks.create("setupPublishSecrets") {
System.setProperty("gradle.publish.key", key)
System.setProperty("gradle.publish.secret", secret)
}
}
}

detekt {
ignoreFailures = false
source.setFrom(files(projectDir))
config.setFrom(files("$rootDir/../config/detekt.yml"))
buildUponDefaultConfig = true
}

tasks.withType<Detekt> {
reports {
sarif.required.set(false)
md.required.set(false)
txt.required.set(true)
xml.required.set(true)
}
}

ktlint {
ignoreFailures.set(false)
android.set(true)
outputToConsole.set(true)
reporters {
reporter(ReporterType.PLAIN)
reporter(ReporterType.CHECKSTYLE)
}
filter {
exclude("**/generated/**")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal object GoogleSheetParser {
response: SpreadsheetResponse,
sectionColumn: String?,
keyColumn: String,
languageMapping: List<LanguageMapping>
languageMapping: List<LanguageMapping>,
): List<SheetEntry> {
val rows = response.rows
check(rows.isNotEmpty()) { error("Provided spreadsheet is empty") }
Expand All @@ -30,7 +30,7 @@ internal object GoogleSheetParser {
}
check(parsedColumns.any { it is TableColumn.Translation }) {
"Google Sheet does not contain any columns specified in language mapping.\n" +
"Columns specified: ${languageMapping.map { it.columnName }.joinToString()}"
"Columns specified: ${languageMapping.map { it.columnName }.joinToString()}"
}

return parseEntries(rows.drop(1), parsedColumns)
Expand All @@ -40,9 +40,8 @@ internal object GoogleSheetParser {
row: TableRow,
sectionColumn: String?,
keyColumn: String,
languageMapping: List<LanguageMapping>
languageMapping: List<LanguageMapping>,
): List<TableColumn> = buildList {

// Find section column
row.indexOf(sectionColumn)
.takeIf { index -> index >= 0 }
Expand Down Expand Up @@ -88,11 +87,11 @@ internal object GoogleSheetParser {
column.locale to SheetEntry.PluralResource.Item(
key = key.pluralKeyValue(),
quantityModifier = key.pluralKeyModifier(),
value = columnValue
value = columnValue,
)
}
.filterNotNull()
.toMap()
.toMap(),
)
} else {
return@mapNotNull SheetEntry.PlainResource(
Expand All @@ -101,13 +100,13 @@ internal object GoogleSheetParser {
val columnValue = row[column] ?: return@map null
column.locale to SheetEntry.PlainResource.Item(
key = key,
value = columnValue
value = columnValue,
)
}
.filterNotNull()
.toMap()
.toMap(),
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal object ResourcesSerializer {
*/
fun serialize(
xmlElements: List<XmlElement>,
outputStream: OutputStream
outputStream: OutputStream,
) {
XmlWriter(outputStream).document {
xmlElements.forEach { xmlElement ->
Expand Down Expand Up @@ -84,4 +84,4 @@ private fun XmlWriter.writePluralResourceItem(quantityModifier: String, value: S
private fun XmlWriter.flushAndClose() {
flush()
close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal object SheetEntryAccumulator {
xmlEntries += XmlElement.PlainResource(
key = translation.key,
value = translation.value,
comments = commentsAccumulator.toList()
comments = commentsAccumulator.toList(),
)
commentsAccumulator.clear()
}
Expand All @@ -47,19 +47,19 @@ internal object SheetEntryAccumulator {
items = listOf(
XmlElement.PluralResource.Item(
translation.value,
translation.quantityModifier
)
translation.quantityModifier,
),
),
comments = commentsAccumulator.toList()
comments = commentsAccumulator.toList(),
)
commentsAccumulator.clear()
} else if (translation.key == pluralAccumulator.key) {
// Continue accumulating the same plural
pluralAccumulator = pluralAccumulator.copy(
items = pluralAccumulator.items + XmlElement.PluralResource.Item(
value = translation.value,
quantityModifier = translation.quantityModifier
)
quantityModifier = translation.quantityModifier,
),
)
}
}
Expand All @@ -84,4 +84,4 @@ internal object SheetEntryAccumulator {

return xmlEntries.toList()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ internal class XmlWriter(outputStream: OutputStream) {
fun close() {
writer.close()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ internal class GoogleSpreadsheetsApi {

return Json.decodeFromString(response)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ package app.futured.sheethappens.localizer.model
* The [Locale] maps specific column in Google Sheet identified by [columnName] to resource directory
* in mobile application.
*/
internal data class Locale(val columnName: String, val subdirectory: String)
internal data class Locale(val columnName: String, val subdirectory: String)
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ internal data class SpreadsheetResponse(
val majorDimension: String,

@SerialName("values")
val rows: List<List<String>>
)
val rows: List<List<String>>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ internal operator fun TableRow.get(column: TableColumn?): String? = column?.let

internal fun String.isPluralKey() = contains("##")
internal fun String.pluralKeyModifier() = substringAfter("{").replace("}", "").trim()
internal fun String.pluralKeyValue() = substringBefore("##")
internal fun String.pluralKeyValue() = substringBefore("##")
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed interface XmlElement {
data class PlainResource(
val key: String,
val value: String,
override val comments: List<String> = emptyList()
override val comments: List<String> = emptyList(),
) : XmlElement

/**
Expand All @@ -35,8 +35,8 @@ internal sealed interface XmlElement {
data class PluralResource(
val key: String,
val items: List<Item>,
override val comments: List<String> = emptyList()
override val comments: List<String> = emptyList(),
) : XmlElement {
data class Item(val value: String, val quantityModifier: String)
}
}
}
Loading