forked from bazelbuild/rules_kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a kt_plugin rule (bazelbuild#308)
Creates a `kt_compiler_plugin` rule to allow kotlin compiler plugins to be specified, and then included with a `plugins=` attribute on kt_jvm_* jobs. This should support arbitrary kotlin compiler plugins, such as the android extensions, open-for-testing, and others. This does _not_ add an exported_plugins infrastructure (such as you get with java_plugins), and it does not change the special-case handling of kapt. It also doesn't do anything to resolve plugin inter-0compatibilities (e.g. the ABI plugin possibly needing information from the parcelizable, or other such interactions) The latter are issues yet to be figured out int he kotlinc plugin infrastructure, and we'll adapt this rule as appropriate. Also adds docs and examples.
- Loading branch information
1 parent
7b205f1
commit d89d6a3
Showing
27 changed files
with
376 additions
and
22 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,36 @@ | ||
load("//kotlin:kotlin.bzl", "kt_compiler_plugin", "kt_jvm_library") | ||
|
||
kt_compiler_plugin( | ||
name = "open_for_testing_plugin", | ||
id = "org.jetbrains.kotlin.allopen", | ||
options = { | ||
"annotation": "plugin.allopen.OpenForTesting", | ||
}, | ||
deps = [ | ||
"@com_github_jetbrains_kotlin//:allopen-compiler-plugin", | ||
], | ||
) | ||
|
||
kt_jvm_library( | ||
name = "open_for_testing", | ||
srcs = ["OpenForTesting.kt"], | ||
) | ||
|
||
kt_jvm_library( | ||
name = "user", | ||
srcs = ["User.kt"], | ||
plugins = [ | ||
":open_for_testing_plugin", | ||
], | ||
deps = [ | ||
":open_for_testing", | ||
], | ||
) | ||
|
||
kt_jvm_library( | ||
name = "user_is_open_test", | ||
srcs = ["UserIsOpenTest.kt"], | ||
deps = [ | ||
":user", | ||
], | ||
) |
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,3 @@ | ||
package plugin.allopen | ||
|
||
annotation class OpenForTesting |
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,9 @@ | ||
package plugin.allopen; | ||
|
||
import java.util.* | ||
|
||
@OpenForTesting | ||
data class User( | ||
val userId: UUID, | ||
val emails: String | ||
) |
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,5 @@ | ||
package plugin.allopen | ||
|
||
import java.util.* | ||
|
||
class Subclass : User(UUID.randomUUID(), "[email protected]") |
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,38 @@ | ||
load("//kotlin:kotlin.bzl", "kt_compiler_plugin", "kt_jvm_library", "kt_jvm_test") | ||
|
||
kt_compiler_plugin( | ||
name = "no_arg_plugin", | ||
id = "org.jetbrains.kotlin.noarg", | ||
options = { | ||
"annotation": "plugin.noarg.NoArgConstructor", | ||
}, | ||
deps = [ | ||
"@com_github_jetbrains_kotlin//:noarg-compiler-plugin", | ||
], | ||
) | ||
|
||
kt_jvm_library( | ||
name = "no_arg_constructor", | ||
srcs = ["NoArgConstructor.kt"], | ||
) | ||
|
||
kt_jvm_library( | ||
name = "user", | ||
srcs = ["User.kt"], | ||
plugins = [":no_arg_plugin"], | ||
deps = [ | ||
":no_arg_constructor", | ||
], | ||
) | ||
|
||
# The no-arg constructor that is generated cannot be compiled against, but should be discoverable at runtime. | ||
kt_jvm_test( | ||
name = "user_has_noarg_constructor_test", | ||
srcs = ["UserHasNoargConstructorTest.kt"], | ||
test_class = "plugin.noarg.UserHasNoargConstructorTest", | ||
deps = [ | ||
":user", | ||
"@com_github_jetbrains_kotlin//:kotlin-reflect", | ||
"@kotlin_rules_maven//:junit_junit", | ||
], | ||
) |
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,3 @@ | ||
package plugin.noarg | ||
|
||
annotation class NoArgConstructor |
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,9 @@ | ||
package plugin.noarg; | ||
|
||
import java.util.* | ||
|
||
@NoArgConstructor | ||
data class User( | ||
val userId: UUID, | ||
val emails: String | ||
) |
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,13 @@ | ||
package plugin.noarg | ||
|
||
import org.junit.* | ||
import java.lang.Exception | ||
|
||
class UserHasNoargConstructorTest { | ||
@Test | ||
fun userShouldHaveNoargConstructor() { | ||
if (User::class.java.constructors.none { it.parameters.isEmpty() }) { | ||
throw Exception("Expected an empty constructor to exist") | ||
} | ||
} | ||
} |
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,31 @@ | ||
load("//kotlin:kotlin.bzl", "kt_compiler_plugin", "kt_jvm_library") | ||
load("@rules_java//java:defs.bzl", "java_library") | ||
|
||
kt_compiler_plugin( | ||
name = "sam_with_receiver_plugin", | ||
id = "org.jetbrains.kotlin.samWithReceiver", | ||
options = { | ||
"annotation": "plugin.sam_with_receiver.SamWithReceiver", | ||
}, | ||
deps = [ | ||
"@com_github_jetbrains_kotlin//:sam-with-receiver-compiler-plugin", | ||
], | ||
) | ||
|
||
kt_jvm_library( | ||
name = "sam_with_receiver", | ||
srcs = ["SamWithReceiver.kt"], | ||
) | ||
|
||
java_library( | ||
name = "runner", | ||
srcs = ["Runner.java"], | ||
deps = [":sam_with_receiver"], | ||
) | ||
|
||
kt_jvm_library( | ||
name = "runner_test", | ||
srcs = ["RunnerTest.kt"], | ||
plugins = [":sam_with_receiver_plugin"], | ||
deps = [":runner"], | ||
) |
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,6 @@ | ||
package plugin.sam_with_receiver; | ||
|
||
@SamWithReceiver | ||
public interface Runner { | ||
void run(Double shouldBecomeThis); | ||
} |
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,5 @@ | ||
package plugin.sam_with_receiver | ||
|
||
val thisShouldWork = Runner { | ||
println(this.isFinite()) | ||
} |
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,3 @@ | ||
package plugin.sam_with_receiver | ||
|
||
annotation class SamWithReceiver |
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,21 @@ | ||
load( | ||
"//kotlin/internal:defs.bzl", | ||
_KtCompilerPluginInfo = "KtCompilerPluginInfo", | ||
) | ||
|
||
def plugins_to_classpaths(providers_list): | ||
flattened_files = [] | ||
for providers in providers_list: | ||
if _KtCompilerPluginInfo in providers: | ||
provider = providers[_KtCompilerPluginInfo] | ||
for e in provider.classpath: | ||
flattened_files.append(e) | ||
return flattened_files | ||
|
||
def plugins_to_options(providers_list): | ||
kt_compiler_plugin_providers = [providers[_KtCompilerPluginInfo] for providers in providers_list if _KtCompilerPluginInfo in providers] | ||
flattened_options = [] | ||
for provider in kt_compiler_plugin_providers: | ||
for option in provider.options: | ||
flattened_options.append("%s:%s" % (option.id, option.value)) | ||
return flattened_options |
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
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
Oops, something went wrong.