Skip to content

Commit

Permalink
Setup plugin options and tidy up BUCK configuration
Browse files Browse the repository at this point in the history
Summary:
- Tidy up BUCK configuration. Invert the shaded target so that it depends on the unshaded one instead of the other way around
- Add plugin configs. Currently, we just have `ENABLED` which is false by default, so that the plugin is basically dormant for now
- Update existing dummy extension & test accordingly

Reviewed By: zielinskimz

Differential Revision: D64464563

fbshipit-source-id: 650766d438a3fe6078b0c57da6bef382a5792753
  • Loading branch information
kingsleyadio authored and facebook-github-bot committed Oct 21, 2024
1 parent f17c3dc commit 89cace8
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 24 deletions.
18 changes: 5 additions & 13 deletions litho-compiler-plugin/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,24 @@
# This source code is licensed under the Apache 2.0 license found in the
# LICENSE file in the root directory of this source tree.

load("@fbsource//third-party/java/jarjar:unshade.bzl", "unshade_kotlin_compiler_plugin_rule")
load("@fbsource//third-party/java/jarjar:unshade.bzl", "shade_kotlin_compiler_plugin_rule")
load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
load("@fbsource//tools/build_defs/features:native_feature_wrapper.bzl", "feature_native")
load("@fbsource//xplat/pfh/FBApp_UIFrameworks_Litho_Litho:DEFS.bzl", "FBApp_UIFrameworks_Litho_Litho")

oncall("litho_components_for_android")

fb_native.java_binary(
name = "litho-compiler-plugin-shaded",
blacklist = [
# It's probably possible to reduce this restriction to specific list of classes
"com.intellij",
"kotlinx.collections.immutable",
"org.jetbrains.kotlin.it.unimi.dsi.fastutil",
],
visibility = [
"PUBLIC",
],
name = "litho-compiler-plugin",
visibility = ["PUBLIC"],
deps = [
"//fbandroid/libraries/components/litho-compiler-plugin/compiler:litho-compiler",
],
)

feature_native.prebuilt_jar(
name = "litho-compiler-plugin",
binary_jar = unshade_kotlin_compiler_plugin_rule(":litho-compiler-plugin-shaded"),
name = "litho-compiler-plugin-shaded",
binary_jar = shade_kotlin_compiler_plugin_rule(":litho-compiler-plugin"),
feature = FBApp_UIFrameworks_Litho_Litho,
visibility = ["PUBLIC"],
)
9 changes: 5 additions & 4 deletions litho-compiler-plugin/compiler/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ feature_native.kotlin_library(
feature = FBApp_UIFrameworks_Litho_Litho,
labels = [],
provided_deps = [
"//third-party/kotlin:kotlin-compiler-embeddable",
"//third-party/kotlin:kotlin-compiler",
],
resources = glob(["resources/**"]),
resources_root = "resources",
tests = [
"//fbandroid/libraries/components/litho-compiler-plugin/compiler:litho-compiler-test",
":litho-compiler-test",
],
visibility = [
"PUBLIC",
Expand All @@ -34,12 +34,13 @@ fb_kotlin_test(
srcs = glob(["src/test/kotlin/**/*.kt"]),
deps = [
":litho-compiler",
"//fbandroid/libraries/components/litho-core/src/main/java/com/facebook/litho:litho",
"//fbandroid/third-party/java/assertj:assertj-core",
"//fbandroid/third-party/java/junit:junit",
"//third-party/kotlin:kotlin",
"//third-party/kotlin:kotlin-compiler-embeddable",
"//third-party/kotlin:kotlin-compiler",
"//third-party/kotlin:kotlin-reflect",
"//third-party/kotlin/kotlin-compile-testing:kotlin-compile-testing-k2-shaded",
"//third-party/kotlin/kotlin-compile-testing:kotlin-compile-testing",
"//third-party/kotlin/mockito-kotlin2:mockito-kotlin2",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.facebook.litho

import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
import org.jetbrains.kotlin.com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingTrace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@

package com.facebook.litho

import com.facebook.litho.common.LithoCompilerConfig
import com.facebook.litho.common.LithoCompilerConfig.Companion.ENABLED
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
import org.jetbrains.kotlin.compiler.plugin.CliOptionProcessingException
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.jetbrains.kotlin.config.CompilerConfiguration

@OptIn(ExperimentalCompilerApi::class)
class LithoCommandLineProcessor : CommandLineProcessor {
override val pluginId: String = "com.facebook.litho.compiler"
override val pluginOptions: Collection<AbstractCliOption> = emptyList()
override val pluginOptions: Collection<AbstractCliOption> = LithoCompilerConfig.options

override fun processOption(
option: AbstractCliOption,
value: String,
configuration: CompilerConfiguration
) {
when (option) {
ENABLED.cliOption -> configuration.put(ENABLED.configKey, value.toBoolean())
else -> throw CliOptionProcessingException("Unknown option: ${option.optionName}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.facebook.litho

import com.facebook.litho.common.LithoCompilerConfig
import com.facebook.litho.common.get
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
Expand All @@ -28,6 +30,7 @@ import org.jetbrains.kotlin.resolve.extensions.AnalysisHandlerExtension
class LithoComponentRegistrar : CompilerPluginRegistrar() {

override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
if (!configuration[LithoCompilerConfig.ENABLED]) return
val messageCollector =
configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.facebook.litho

import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtElement
Expand All @@ -37,7 +37,7 @@ class LithoFileVisitor(
}

override fun visitCallExpression(expression: KtCallExpression) {
if (expression.text.contains("test")) {
if (expression.calleeExpression?.text == "test") {
expression.report("Test function called")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.facebook.litho.common

import org.jetbrains.kotlin.compiler.plugin.CliOption
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey

class LithoCompilerConfig<T> private constructor(val cliOption: CliOption, val default: T?) {
init {
if (default == null) require(cliOption.required)
}

val configKey: CompilerConfigurationKey<T> = CompilerConfigurationKey.create(cliOption.optionName)

companion object {
val ENABLED: LithoCompilerConfig<Boolean> =
LithoCompilerConfig(
CliOption("enabled", "<true | false>", "whether plugin is enabled", false),
default = false)

val options: List<CliOption>
get() = listOf(ENABLED.cliOption)
}
}

operator fun <T> CompilerConfiguration.get(property: LithoCompilerConfig<T>): T {
return when (property.default) {
null -> getNotNull(property.configKey)
else -> get(property.configKey, property.default)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.facebook.litho

import com.tschuchort.compiletesting.CompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.PluginOption
import com.tschuchort.compiletesting.SourceFile
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
Expand All @@ -38,7 +39,7 @@ class LithoFileVisitorTest {

val result = compile(SourceFile.kotlin("Test.kt", file))

assertThat(KotlinCompilation.ExitCode.OK).isEqualTo(result.exitCode)
assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
}

@Test
Expand All @@ -56,7 +57,7 @@ class LithoFileVisitorTest {

val result = compile(SourceFile.kotlin("Test.kt", file))

assertThat(KotlinCompilation.ExitCode.COMPILATION_ERROR).isEqualTo(result.exitCode)
assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.COMPILATION_ERROR)
assertThat(result.messages).contains("Litho: Test function called")
}

Expand All @@ -68,7 +69,7 @@ class LithoFileVisitorTest {
compilerPluginRegistrars = listOf(LithoComponentRegistrar())
messageOutputStream = System.out
commandLineProcessors = listOf(LithoCommandLineProcessor())
pluginOptions = listOf()
pluginOptions = listOf(PluginOption("com.facebook.litho.compiler", "enabled", "true"))
languageVersion = "1.9"
supportsK2 = false
}
Expand Down

0 comments on commit 89cace8

Please sign in to comment.