Skip to content

Commit

Permalink
Merge pull request #4 from ooni/tooling
Browse files Browse the repository at this point in the history
Tooling: CI + Lints
  • Loading branch information
sdsantos authored Jul 29, 2024
2 parents ae0d1ee + 017a04c commit fa8885e
Show file tree
Hide file tree
Showing 26 changed files with 302 additions and 187 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]

indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
13 changes: 13 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: 'Setup'
description: 'Setup Java, checkout and setup gradle'
runs:
using: "composite"
steps:
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: Gradle cache
uses: gradle/actions/setup-gradle@v3
69 changes: 69 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Validate
on:
push:
branches:
- master
pull_request:

jobs:
build:
name: Build
runs-on: macos-latest

strategy:
matrix:
type: [Debug, Release]

steps:
- uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup

- name: Build Android
run: ./gradlew assemble${{ matrix.type }}

- name: Build iOS
run: ./gradlew link${{ matrix.type }}FrameworkIosSimulatorArm64

android-lint:
name: Android Lint
runs-on: macos-latest
needs: [ build ]

steps:
- uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup

- name: Run lint
run: ./gradlew lint

- name: Uploads test reports
uses: actions/upload-artifact@v4
if: failure()
with:
name: android-lint-report
path: composeApp/build/reports/

kotlin-lint:
name: Kotlin Lint
runs-on: macos-latest
needs: [ build ]

steps:
- uses: actions/checkout@v4

- name: Setup
uses: ./.github/actions/setup

- name: Run lint
run: ./gradlew ktlintCheck

- name: Uploads test reports
uses: actions/upload-artifact@v4
if: failure()
with:
name: android-lint-report
path: composeApp/build/reports/ktlint/
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ plugins {
alias(libs.plugins.jetbrainsComposeCompiler) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
alias(libs.plugins.cocoapods) apply false
alias(libs.plugins.ktlint) apply false
}
22 changes: 21 additions & 1 deletion composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
alias(libs.plugins.jetbrainsComposeCompiler)
alias(libs.plugins.cocoapods)
alias(libs.plugins.kotlinSerialization)
alias(libs.plugins.ktlint)
}

kotlin {
Expand All @@ -32,11 +33,12 @@ kotlin {
framework {
baseName = "composeApp"
isStatic = true
binaryOption("bundleId", "composeApp")
}

podfile = project.file("../iosApp/Podfile")
}

sourceSets {
androidMain.dependencies {
implementation(libs.compose.ui.tooling.preview)
Expand All @@ -53,6 +55,10 @@ kotlin {
implementation(compose.components.uiToolingPreview)
implementation(libs.kotlin.serialization)
}

all {
languageSettings.optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
}
}

@OptIn(ExperimentalKotlinGradlePluginApi::class)
Expand Down Expand Up @@ -96,4 +102,18 @@ android {
dependencies {
debugImplementation(libs.compose.ui.tooling)
}
android {
lint {
warningsAsErrors = true
disable += "AndroidGradlePluginVersion"
}
}
}

ktlint {
filter {
exclude("**/generated/**")
include("**/kotlin/**")
}
additionalEditorconfig.put("ktlint_function_naming_ignore_when_annotated_with", "Composable")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class AndroidOonimkallBridge : OonimkallBridge {
override fun waitForNextEvent() = task.waitForNextEvent()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package org.ooni.probe

import android.app.Application

class AndroidApplication : Application()
class AndroidApplication : Application()
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
63 changes: 33 additions & 30 deletions composeApp/src/commonMain/kotlin/org/ooni/engine/Engine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,32 @@ import kotlin.math.roundToInt
class Engine(
private val bridge: OonimkallBridge,
private val json: Json,
private val baseFilePath: String
private val baseFilePath: String,
) {
fun startTask(taskSettings: TaskSettings): Flow<TaskEvent> =
channelFlow {
val finalSettings =
taskSettings.copy(
stateDir = baseFilePath,
tunnelDir = baseFilePath,
tempDir = baseFilePath,
assetsDir = baseFilePath,
)

fun startTask(taskSettings: TaskSettings): Flow<TaskEvent> = channelFlow {
val finalSettings = taskSettings.copy(
stateDir = baseFilePath,
tunnelDir = baseFilePath,
tempDir = baseFilePath,
assetsDir = baseFilePath,
)

val task = bridge.startTask(json.encodeToString(finalSettings))
val task = bridge.startTask(json.encodeToString(finalSettings))

while (!task.isDone()) {
val eventJson = task.waitForNextEvent()
val eventResult = json.decodeFromString<EventResult>(eventJson)
eventResult.toTaskEvent()?.let { send(it) }
}
while (!task.isDone()) {
val eventJson = task.waitForNextEvent()
val eventResult = json.decodeFromString<EventResult>(eventJson)
eventResult.toTaskEvent()?.let { send(it) }
}

invokeOnClose {
if (it is CancellationException) {
task.interrupt()
invokeOnClose {
if (it is CancellationException) {
task.interrupt()
}
}
}
}

private fun EventResult.toTaskEvent(): TaskEvent? =
when (key) {
Expand All @@ -46,25 +47,27 @@ class Engine(
value?.percentage?.let { percentageValue ->
TaskEvent.Progress(
percentage = (percentageValue * 100.0).roundToInt(),
message = value?.message
message = value?.message,
)
}

"log" -> value?.message?.let { message ->
TaskEvent.Log(
level = value?.log_level,
message = message
)
}
"log" ->
value?.message?.let { message ->
TaskEvent.Log(
level = value?.logLevel,
message = message,
)
}

"status.report_create" -> value?.report_id?.let {
TaskEvent.ReportCreate(reportId = it)
}
"status.report_create" ->
value?.reportId?.let {
TaskEvent.ReportCreate(reportId = it)
}

"task_terminated" -> TaskEvent.TaskTerminated

"failure.startup" -> TaskEvent.FailureStartup(message = value?.failure)

else -> null
}
}
}
55 changes: 45 additions & 10 deletions composeApp/src/commonMain/kotlin/org/ooni/engine/EventResult.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
package org.ooni.engine

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
class EventResult {
@SerialName("key")
var key: String? = null

@SerialName("value")
var value: Value? = null

@Serializable
class Value {
@SerialName("key")
var key: Double = 0.0
var log_level: String? = null

@SerialName("log_level")
var logLevel: String? = null

@SerialName("message")
var message: String? = null

@SerialName("percentage")
var percentage: Double = 0.0
var json_str: String? = null

@SerialName("json_str")
var jsonStr: String? = null

@SerialName("idx")
var idx: Int = 0
var report_id: String? = null
var probe_ip: String? = null
var probe_asn: String? = null
var probe_cc: String? = null
var probe_network_name: String? = null
var downloaded_kb: Double = 0.0
var uploaded_kb: Double = 0.0

@SerialName("report_id")
var reportId: String? = null

@SerialName("probe_ip")
var probeIp: String? = null

@SerialName("probe_asn")
var probeAsn: String? = null

@SerialName("probe_cc")
var probeCc: String? = null

@SerialName("probe_network_name")
var probeNetworkName: String? = null

@SerialName("downloaded_kb")
var downloadedKb: Double = 0.0

@SerialName("uploaded_kb")
var uploadedKb: Double = 0.0

@SerialName("input")
var input: String? = null

@SerialName("failure")
var failure: String? = null
var orig_key: String? = null

@SerialName("orig_key")
var origKey: String? = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ interface OonimkallBridge {

interface Task {
fun interrupt()

fun isDone(): Boolean

fun waitForNextEvent(): String
}
}
}
Loading

0 comments on commit fa8885e

Please sign in to comment.