-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample App + Subcomponent Factories (#18)
_WIP_ Fixes #17 Fixes #8 --------- Co-authored-by: Drew Heavner <[email protected]>
- Loading branch information
Showing
58 changed files
with
2,550 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="Desktop App" type="Application" factoryName="Application"> | ||
<option name="ALTERNATIVE_JRE_PATH" value="azul-17" /> | ||
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" /> | ||
<option name="MAIN_CLASS_NAME" value="com.r0adkll.kimchi.restaurant.MainKt" /> | ||
<module name="kimchi.sample.desktopApp.main" /> | ||
<method v="2"> | ||
<option name="Make" enabled="true" /> | ||
</method> | ||
</configuration> | ||
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
compiler-utils/src/main/kotlin/com/r0adkll/kimchi/util/kotlinpoet/Functions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (C) 2024 r0adkll | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.r0adkll.kimchi.util.kotlinpoet | ||
|
||
import com.google.devtools.ksp.symbol.KSFunctionDeclaration | ||
import com.squareup.kotlinpoet.ParameterSpec | ||
|
||
fun KSFunctionDeclaration.parameterSpecs(): List<ParameterSpec> { | ||
return parameters.map { it.toParameterSpec() } | ||
} |
18 changes: 18 additions & 0 deletions
18
compiler-utils/src/main/kotlin/com/r0adkll/kimchi/util/kotlinpoet/Parameters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (C) 2024 r0adkll | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.r0adkll.kimchi.util.kotlinpoet | ||
|
||
import com.google.devtools.ksp.symbol.KSValueParameter | ||
import com.squareup.kotlinpoet.ParameterSpec | ||
import com.squareup.kotlinpoet.ksp.toAnnotationSpec | ||
import com.squareup.kotlinpoet.ksp.toTypeName | ||
|
||
fun KSValueParameter.toParameterSpec(): ParameterSpec { | ||
return ParameterSpec.builder(name!!.asString(), type.toTypeName()) | ||
.addAnnotations( | ||
annotations | ||
.map { it.toAnnotationSpec() } | ||
.toList(), | ||
) | ||
.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
compiler/src/main/kotlin/com/r0adkll/kimchi/util/ksp/SubcomponentDeclaration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (C) 2024 r0adkll | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.r0adkll.kimchi.util.ksp | ||
|
||
import com.google.devtools.ksp.KspExperimental | ||
import com.google.devtools.ksp.getDeclaredFunctions | ||
import com.google.devtools.ksp.isAnnotationPresent | ||
import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
import com.google.devtools.ksp.symbol.KSFunctionDeclaration | ||
import com.r0adkll.kimchi.annotations.ContributesSubcomponent | ||
import com.r0adkll.kimchi.util.buildFun | ||
import com.r0adkll.kimchi.util.kotlinpoet.parameterSpecs | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.ksp.toClassName | ||
import com.squareup.kotlinpoet.ksp.toTypeName | ||
|
||
/** | ||
* A custom overlay of [KSClassDeclaration] to provide a unified way of | ||
* accessing the specific components and assumptions of declarations | ||
* annotated with [com.r0adkll.kimchi.annotations.ContributesSubcomponent] | ||
*/ | ||
class SubcomponentDeclaration( | ||
private val clazz: KSClassDeclaration, | ||
) : KSClassDeclaration by clazz { | ||
|
||
val subcomponentSimpleName: String | ||
get() = "Merged${simpleName.asString()}" | ||
|
||
@OptIn(KspExperimental::class) | ||
val factoryClass: FactoryDeclaration by lazy { | ||
declarations | ||
.filterIsInstance<KSClassDeclaration>() | ||
.filter { it.isAnnotationPresent(ContributesSubcomponent.Factory::class) } | ||
.map { FactoryDeclaration(this, it) } | ||
.firstOrNull() | ||
?: throw IllegalStateException( | ||
"@ContributesSubcomponent must define a factory interface annotated with " + | ||
"@ContributesSubcomponent.Factory", | ||
) | ||
} | ||
|
||
fun createFactoryFunctionOverload(): FunSpec = with(factoryClass) { | ||
return FunSpec.buildFun(factoryFunction.simpleName.asString()) { | ||
addModifiers(KModifier.OVERRIDE) | ||
|
||
returns(factoryFunction.returnType!!.toTypeName()) | ||
|
||
val factoryParameters = factoryFunction.parameterSpecs() | ||
addParameters(factoryParameters) | ||
|
||
// Build the return statement constructing the expected merged subcomponent, including | ||
// parent. | ||
addStatement( | ||
"return %L.create(${factoryParameters.joinToString { "%L" }}" + | ||
"${if (factoryParameters.isNotEmpty()) ", " else ""}this)", | ||
subcomponentSimpleName, | ||
*factoryParameters.map { it.name }.toTypedArray(), | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* A custom overlay of [KSClassDeclaration] to provide a unified way of accessing | ||
* the specific components and assumptions of declarations annotated with | ||
* [com.r0adkll.kimchi.annotations.ContributesSubcomponent.Factory] | ||
*/ | ||
class FactoryDeclaration( | ||
private val subcomponent: SubcomponentDeclaration, | ||
private val clazz: KSClassDeclaration, | ||
) : KSClassDeclaration by clazz { | ||
|
||
init { | ||
require(isInterface) { | ||
"@ContributesSubcomponent.Factory annotated declarations must be an interface" | ||
} | ||
} | ||
|
||
val factoryFunction: KSFunctionDeclaration by lazy { | ||
getDeclaredFunctions() | ||
.singleOrNull() | ||
?.also { | ||
require(it.returnType != null) { "Factory methods are required to return their component" } | ||
require(it.returnType!!.toTypeName() == subcomponent.toClassName()) { | ||
"Factory methods are required to return their component" | ||
} | ||
} | ||
?: throw IllegalStateException( | ||
"@ContributeSubcomponent.Factory interfaces must only have a " + | ||
"single function declared", | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (C) 2024 r0adkll | ||
// SPDX-License-Identifier: Apache-2.0 | ||
plugins { | ||
alias(libs.plugins.androidApplication) | ||
alias(libs.plugins.kotlinAndroid) | ||
alias(libs.plugins.compose.compiler) | ||
alias(libs.plugins.compose.multiplatform) | ||
alias(libs.plugins.ksp) | ||
} | ||
|
||
android { | ||
namespace = "com.r0adkll.kimchi.restaurant.android" | ||
compileSdk = 34 | ||
|
||
defaultConfig { | ||
applicationId = "com.r0adkll.kimchi.restaurant.android" | ||
minSdk = 26 | ||
targetSdk = 34 | ||
versionCode = 1 | ||
versionName = "1.0" | ||
} | ||
|
||
buildFeatures { | ||
compose = true | ||
} | ||
|
||
packaging { | ||
resources { | ||
excludes += "/META-INF/{AL2.0,LGPL2.1}" | ||
} | ||
} | ||
|
||
buildTypes { | ||
getByName("release") { | ||
isMinifyEnabled = false | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
kotlinOptions { | ||
jvmTarget = "17" | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(projects.annotations) | ||
implementation(projects.sample.shared) | ||
|
||
implementation(libs.androidx.activity.compose) | ||
implementation(libs.circuit.runtime) | ||
implementation(libs.circuit.foundation) | ||
|
||
api(compose.foundation) | ||
api(compose.material) | ||
api(compose.material3) | ||
api(compose.materialIconsExtended) | ||
api(compose.animation) | ||
|
||
ksp(projects.compiler) | ||
ksp(libs.kotlininject.ksp) | ||
} |
Oops, something went wrong.