Skip to content

Instantly share code, notes, and snippets.

View adibfara's full-sized avatar

Adib Faramarzi adibfara

View GitHub Profile
@adibfara
adibfara / sample-do-once-without-contracts.kt
Last active September 5, 2018 21:48
Kotlin contracts
/**
* @author Adib Faramarzi (adibfara@gmail.com)
*/
fun createOnce(runFunction: ()-> Unit) {
runFunction()
}
fun getKotlinVersion(): Float{
val kotlinVersion: Float
// add kotlin gradle plugin (v1.3 and above)
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3"
}
}
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
/**
* @author Adib Faramarzi (adibfara@gmail.com)
*/
@ExperimentalContracts
fun createOnce(runFunction: ()-> Unit) {
contract {
/**
* @author Adib Faramarzi (adibfara@gmail.com)
*/
fun Any?.isAValidName():Boolean{
return this!=null && this is String && this.length > 3
}
fun getName():String? { ... }
fun testUserName(){
@adibfara
adibfara / KSP Setup.kt
Last active May 5, 2022 14:17
Setup Guide for KSP
// to help the IDE recognize the KSP generated files, add the following to your app's build.gradle
android {
kotlin {
sourceSets.debug {
kotlin.srcDirs += 'build/generated/ksp/debug/kotlin'
}
sourceSets.release {
kotlin.srcDirs += 'build/generated/ksp/release/kotlin'
}
}
@adibfara
adibfara / Listed Annotation.kt
Created May 5, 2022 07:56
Create Listed Annotation
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.SOURCE)
annotation class Listed(
val name: String,
)
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.KSAnnotated
class ListedProcessor(private val environment: SymbolProcessorEnvironment): SymbolProcessor {
override fun process(resolver: Resolver): List<KSAnnotated> {
return emptyList() // we'll return an empty list for now, since we haven't processed anything.
}
}
@adibfara
adibfara / SymbolProcessorProvider.kt
Created May 5, 2022 08:11
Adding a Service Loader
// Create the directories: yourmodule/src/main/resources/META-INF/services/
// Create a file named com.google.devtools.ksp.processing.SymbolProcessorProvider
// Inside the file, add the following line (and nothing more):
com.snaky.ksp.processor.provider.ListedProcessorProvider
// For a reference, you can check out here:
// https://github.com/adibfara/ListGen/blob/main/ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider
@adibfara
adibfara / build.gradle
Created May 5, 2022 08:31
Adding the generator to the application's build.gradle
// Inside your app's module, declare a dependency to your ksp module
dependencies {
implementation project(":ksp") // since you want to use your @Listed annotation
ksp project(":ksp") // to make KSP work
}
@adibfara
adibfara / ListedProcessor.kt
Created May 5, 2022 13:52
Simple Code Generator
internal class ListedProcessor(
private val environment: SymbolProcessorEnvironment,
) : SymbolProcessor {
private fun Resolver.findAnnotations(
kClass: KClass<*>,
) = getSymbolsWithAnnotation(
kClass.qualifiedName.toString())
.filterIsInstance<KSFunctionDeclaration>().filter {
it.parameters.isEmpty()