Skip to content

Commit

Permalink
Setup compiler plugin that makes API overloads internal
Browse files Browse the repository at this point in the history
  • Loading branch information
koperagen committed Dec 16, 2024
1 parent c7b1004 commit 291371c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ public annotation class ScopeProperty

@Target(AnnotationTarget.FUNCTION)
internal annotation class Check

/**
* One of the design goals of the library is typed access to columns.
* That's why all operations that have "column" parameters have 4 overloads: https://kotlin.github.io/dataframe/apilevels.html
* In Kotlin Notebook and in Gradle project with the compiler plugin, Column Accessors API and KProperties API become redundant and
* clutter API scope of DataFrame. This annotation indicates such functions so that they can be excluded from public API
*/
@Target(AnnotationTarget.FUNCTION)
internal annotation class AccessApiOverload
4 changes: 4 additions & 0 deletions plugins/public-api-modifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## :plugins:public-api-modifier

This plugin makes annotated functions internal.
Helps to produce two artifacts from the same code: one with full API, one with reduced API.
25 changes: 25 additions & 0 deletions plugins/public-api-modifier/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktlint)
}

group = "org.jetbrains.kotlinx.dataframe"

repositories {
mavenCentral()
}

dependencies {
compileOnly(libs.kotlin.compiler)
}

sourceSets {
main {
java.setSrcDirs(listOf("src"))
resources.setSrcDirs(listOf("resources"))
}
test {
java.setSrcDirs(listOf("tests", "tests-gen"))
resources.setSrcDirs(listOf("testResources"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
# Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
#

org.jetbrains.kotlinx.dataframe.PublicApiModifierRegistrar
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.jetbrains.kotlinx.dataframe

import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.copy
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.declarations.FirDeclarationStatus
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar
import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrarAdapter
import org.jetbrains.kotlin.fir.extensions.FirStatusTransformerExtension
import org.jetbrains.kotlin.fir.resolve.fqName
import org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol
import org.jetbrains.kotlin.name.FqName

@OptIn(ExperimentalCompilerApi::class)
class PublicApiModifierRegistrar : CompilerPluginRegistrar() {
override val supportsK2: Boolean = true

override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
FirExtensionRegistrarAdapter.registerExtension(Extensions())
}
}

class Extensions : FirExtensionRegistrar() {
override fun ExtensionRegistrarContext.configurePlugin() {
+::StatusTransformer
}
}

class StatusTransformer(session: FirSession) : FirStatusTransformerExtension(session) {
companion object {
val apiOverload = FqName("org.jetbrains.kotlinx.dataframe.annotations.AccessApiOverload")
}

override fun needTransformStatus(declaration: FirDeclaration): Boolean =
declaration.annotations.any {
it.fqName(session) == apiOverload
}

override fun transformStatus(
status: FirDeclarationStatus,
function: FirSimpleFunction,
containingClass: FirClassLikeSymbol<*>?,
isLocal: Boolean,
): FirDeclarationStatus = status.copy(visibility = Visibilities.Internal)
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ include("dataframe-excel")
include("core")
include("dataframe-openapi-generator")
include("dataframe-geo")
include("plugins:public-api-modifier")

0 comments on commit 291371c

Please sign in to comment.