Skip to content

Instantly share code, notes, and snippets.

View bnorm's full-sized avatar
🐢

Brian Norman bnorm

🐢
View GitHub Profile
class IrPluginTest {
@Test
fun `IR plugin success`() {
val result = compile(
sourceFile = SourceFile.kotlin(
"main.kt", """
fun main() {
println(debug())
}
fun debug() = "Hello, World!"
tasks.named("compileKotlin") { dependsOn("syncSource") }
tasks.register<Sync>("syncSource") {
from(project(":kotlin-ir-plugin").sourceSets.main.get().allSource)
into("src/main/kotlin")
filter {
// Replace shadowed imports from plugin module
when (it) {
"import org.jetbrains.kotlin.com.intellij.mock.MockProject" -> "import com.intellij.mock.MockProject"
else -> it
}
class TemplateIrGenerationExtension(
private val messageCollector: MessageCollector,
private val string: String,
private val file: String
) : IrGenerationExtension {
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
messageCollector.report(CompilerMessageSeverity.INFO, "Argument 'string' = $string")
messageCollector.report(CompilerMessageSeverity.INFO, "Argument 'file' = $file")
}
}
@AutoService(ComponentRegistrar::class)
class TemplateComponentRegistrar(
private val defaultString: String,
private val defaultFile: String,
) : ComponentRegistrar {
@Suppress("unused") // Used by service loader
constructor() : this(
defaultString = "Hello, World!",
defaultFile = "file.txt"
@AutoService(CommandLineProcessor::class)
class TemplateCommandLineProcessor : CommandLineProcessor {
companion object {
private const val OPTION_STRING = "string"
private const val OPTION_FILE = "file"
val ARG_STRING = CompilerConfigurationKey<String>(OPTION_STRING)
val ARG_FILE = CompilerConfigurationKey<String>(OPTION_FILE)
}
@bnorm
bnorm / TemplateGradlePlugin.kt
Created November 21, 2020 18:50
Writing Your Second Kotlin Compiler Plugin - Part 1
class TemplateGradlePlugin : KotlinCompilerPluginSupportPlugin {
override fun apply(target: Project): Unit = with(target) {
extensions.create("template", TemplateGradleExtension::class.java)
}
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean = true
override fun getCompilerPluginId(): String = BuildConfig.KOTLIN_PLUGIN_ID
override fun getPluginArtifact(): SubpluginArtifact = SubpluginArtifact(
@bnorm
bnorm / buildconfig-configuration.kt
Last active November 21, 2020 18:49
Writing Your Second Kotlin Compiler Plugin
buildConfig {
val project = project(":kotlin-ir-plugin")
packageName(project.group.toString())
buildConfigField("String", "KOTLIN_PLUGIN_ID", "\"${rootProject.extra["kotlin_plugin_id"]}\"")
buildConfigField("String", "KOTLIN_PLUGIN_GROUP", "\"${project.group}\"")
buildConfigField("String", "KOTLIN_PLUGIN_NAME", "\"${project.name}\"")
buildConfigField("String", "KOTLIN_PLUGIN_VERSION", "\"${project.version}\"")
}
@bnorm
bnorm / websocket.kt
Last active September 29, 2020 15:50
Structured OkHttp WebSockets
interface WebSocketSession {
val incoming: ReceiveChannel<String>
val outgoing: SendChannel<String>
}
suspend fun <R> OkHttpClient.newWebSocket(
request: Request,
block: suspend WebSocketSession.() -> R
): R = coroutineScope {
val incoming = Channel<String>()
@bnorm
bnorm / OkioAsyncChannels.kt
Created May 22, 2018 18:15
Okio based Kotlin suspension extension functions for Java asynchronous NIO channels
import kotlinx.coroutines.experimental.nio.aRead
import kotlinx.coroutines.experimental.nio.aWrite
import okio.Buffer
import java.nio.ByteBuffer
import java.nio.channels.AsynchronousFileChannel
import java.nio.channels.AsynchronousSocketChannel
import java.util.concurrent.TimeUnit
suspend fun AsynchronousSocketChannel.aRead(
buffer: Buffer,
@bnorm
bnorm / Ciphers.java
Created February 14, 2018 21:36
Example CipherSource and CipherSink
import java.io.IOException;
import java.net.ProtocolException;
import java.security.GeneralSecurityException;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import okio.Buffer;
import okio.BufferedSink;