This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] generating a custom file with extension imports based on pr…
…oject's external rules | #BAZEL-556 Done just to rerun the build edit changelog, add some aspect documentation Merge remote-tracking branch 'origin/master' into kasia/language-extension-file triple quote strings cr fixes Merge remote-tracking branch 'origin/master' into kasia/language-extension-file # Conflicts: # aspects/core.bzl # aspects/utils/utils.bzl Revert "asdf" This reverts commit fed1d73. asdf moved language enum to top level add a comment to the generated file rename of the file that invokes the query fetching external rules and creating an extension file based on the result rename LanguageExtensionManager to BazelBspEnvironmentManager test reformatting add test cleanup moved extensions to extension file that is replaced on each project reload concurrent builder map grouping by target id and choosing one info per target id + comment pr fixes fixed target info matching better matching of proto files by file name some logging adding '-general' to TargetInfo file name, TargetInfoReader with bugs generating files in extensions slightly formatted code yay tests work!! still need to prettify the code unified interface for extensions Merge-request: BAZEL-MR-442 Merged-by: Katarzyna Mielnik <katarzyna.anna.mielnik@jetbrains.com>
- Loading branch information
Showing
12 changed files
with
350 additions
and
21 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
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,2 @@ | ||
# This file is supposed to be overwritten with extensions relevant to the project. | ||
EXTENSIONS = [] |
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 @@ | ||
# Aspects | ||
|
||
## Extensions | ||
|
||
### The `extensions.bzl` file | ||
The server generates the `extensions.bzl` file on each sync according to a list of external rules relevant to the project. | ||
See `server/src/main/java/org/jetbrains/bsp/bazel/server/bsp/managers/BazelBspEnvironmentManager.kt` for the implementation. | ||
|
||
The file itself contains a declaration of functions invoked in `core.bzl`. Each function is supposed to provide language-specific data. |
9 changes: 6 additions & 3 deletions
9
server/src/main/java/org/jetbrains/bsp/bazel/server/bsp/managers/BUILD
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
87 changes: 87 additions & 0 deletions
87
...r/src/main/java/org/jetbrains/bsp/bazel/server/bsp/managers/BazelBspEnvironmentManager.kt
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,87 @@ | ||
package org.jetbrains.bsp.bazel.server.bsp.managers | ||
|
||
import org.eclipse.lsp4j.jsonrpc.CancelChecker | ||
import org.jetbrains.bsp.bazel.commons.Constants | ||
import org.jetbrains.bsp.bazel.server.bsp.utils.InternalAspectsResolver | ||
import java.nio.file.Paths | ||
import kotlin.io.path.writeText | ||
|
||
enum class Language(private val fileName: String, val functions: List<String>) { | ||
Java( | ||
"//aspects:rules/java/java_info.bzl", | ||
listOf("extract_java_info", "extract_java_toolchain", "extract_java_runtime") | ||
), | ||
Scala("//aspects:rules/scala/scala_info.bzl", listOf("extract_scala_info", "extract_scala_toolchain_info")), | ||
Cpp("//aspects:rules/cpp/cpp_info.bzl", listOf("extract_cpp_info")), | ||
Kotlin("//aspects:rules/kt/kt_info.bzl", listOf("extract_kotlin_info")), | ||
Python("//aspects:rules/python/python_info.bzl", listOf("extract_python_info")); | ||
|
||
fun toLoadStatement(): String = | ||
this.functions.joinToString( | ||
prefix = """load("${this.fileName}", """, | ||
separator = ", ", | ||
postfix = ")" | ||
) { "\"$it\"" } | ||
} | ||
|
||
class BazelBspEnvironmentManager( | ||
private val internalAspectsResolver: InternalAspectsResolver, | ||
private val bazelExternalRulesQuery: BazelExternalRulesQuery, | ||
) { | ||
|
||
fun generateLanguageExtensions(cancelChecker: CancelChecker) { | ||
val ruleNames = bazelExternalRulesQuery.fetchExternalRuleNames(cancelChecker) | ||
val languages = getProjectLanguages(ruleNames) | ||
val fileContent = prepareFileContent(languages) | ||
|
||
createNewExtensionsFile(fileContent) | ||
} | ||
|
||
private fun getProjectLanguages(allRuleNames: List<String>): List<Language> { | ||
fun checkForLanguage(languageRuleNames: List<String>, language: Language) = | ||
checkForLanguageRules(allRuleNames, languageRuleNames, language) | ||
|
||
return listOfNotNull( | ||
checkForLanguage(listOf("rules_python"), Language.Python), | ||
checkForLanguage(listOf("rules_cc"), Language.Cpp), | ||
checkForLanguage(listOf("io_bazel_rules_kotlin"), Language.Kotlin), | ||
checkForLanguage(listOf("io_bazel_rules_scala"), Language.Scala), | ||
checkForLanguage(listOf("rules_java"), Language.Java) | ||
) | ||
} | ||
|
||
private fun checkForLanguageRules( | ||
allRuleNames: List<String>, | ||
languageRuleNames: List<String>, | ||
language: Language | ||
): Language? = | ||
if (languageRuleNames.any { allRuleNames.contains(it) }) | ||
language | ||
else null | ||
|
||
private fun prepareFileContent(languages: List<Language>) = | ||
listOf( | ||
"# This is a generated file, do not edit it", | ||
createLoadStatementsString(languages), | ||
createExtensionListString(languages) | ||
).joinToString( | ||
separator = "\n", | ||
postfix = "\n" | ||
) | ||
|
||
private fun createLoadStatementsString(languages: List<Language>): String { | ||
val loadStatements = languages.map { it.toLoadStatement() } | ||
return loadStatements.joinToString(postfix = "\n", separator = "\n") | ||
} | ||
|
||
private fun createExtensionListString(languages: List<Language>): String { | ||
val functionNames = languages.flatMap { it.functions } | ||
return functionNames.joinToString(prefix = "EXTENSIONS = [\n", postfix = "\n]", separator = ",\n ") { "\t$it" } | ||
} | ||
|
||
private fun createNewExtensionsFile(fileContent: String) { | ||
val aspectsPath = Paths.get(internalAspectsResolver.bazelBspRoot, Constants.ASPECTS_ROOT) | ||
val file = aspectsPath.resolve("extensions.bzl") | ||
file.writeText(fileContent) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
server/src/main/java/org/jetbrains/bsp/bazel/server/bsp/managers/BazelExternalRulesQuery.kt
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,17 @@ | ||
package org.jetbrains.bsp.bazel.server.bsp.managers | ||
|
||
import org.eclipse.lsp4j.jsonrpc.CancelChecker | ||
import org.jetbrains.bsp.bazel.bazelrunner.BazelRunner | ||
|
||
interface BazelExternalRulesQuery { | ||
fun fetchExternalRuleNames(cancelChecker: CancelChecker): List<String> | ||
} | ||
|
||
class BazelExternalRulesQueryImpl(private val bazelRunner: BazelRunner) : BazelExternalRulesQuery { | ||
|
||
override fun fetchExternalRuleNames(cancelChecker: CancelChecker): List<String> = | ||
bazelRunner.commandBuilder().query().withArgument("//external:*").executeBazelCommand() | ||
.waitAndGetResult(cancelChecker, ensureAllOutputRead = true) | ||
.stdoutLines | ||
.mapNotNull { it.split(':').getOrNull(1) } | ||
} |
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
12 changes: 12 additions & 0 deletions
12
server/src/test/java/org/jetbrains/bsp/bazel/server/bsp/managers/BUILD
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,12 @@ | ||
load("@//rules/kotlin:junit5.bzl", "kt_test") | ||
|
||
kt_test( | ||
name = "BazelBspEnvironmentManagerTest", | ||
size = "small", | ||
src = "BazelBspEnvironmentManagerTest.kt", | ||
deps = [ | ||
"//commons", | ||
"//server/src/main/java/org/jetbrains/bsp/bazel/server/bsp/managers", | ||
"@maven//:org_eclipse_lsp4j_org_eclipse_lsp4j_jsonrpc", | ||
], | ||
) |
Oops, something went wrong.