Skip to content

Commit

Permalink
[feature][experimental] add flag ide_java_home_override to override…
Browse files Browse the repository at this point in the history
… the java home to work with local IDE.

- This flag is highly experimental and is only used for integrating purpose. The implementation may not be final.


Merge-request: BAZEL-MR-975
Merged-by: Xuan Son Trinh <xuanson.trinh@jetbrains.com>
  • Loading branch information
xuansontrinh authored and Space Team committed Mar 27, 2024
1 parent cd00ed5 commit c0de197
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 19 deletions.
Expand Up @@ -8,6 +8,7 @@ import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewDeriveTarge
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewDirectoriesSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewEnabledRulesSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewExcludableListSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewIdeJavaHomeOverrideSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewImportDepthSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewListSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewSingletonSection
Expand Down Expand Up @@ -35,6 +36,8 @@ data class ProjectView(
val importDepth: ProjectViewImportDepthSection?,
/** manually enabled rules to override the automatic rules detection mechanism */
val enabledRules: ProjectViewEnabledRulesSection?,
/** local java home path to override to use with IDE, e.g. IntelliJ IDEA */
val ideJavaHomeOverride: ProjectViewIdeJavaHomeOverrideSection?,
) {

data class Builder(
Expand All @@ -47,6 +50,7 @@ data class ProjectView(
private val deriveTargetsFromDirectories: ProjectViewDeriveTargetsFromDirectoriesSection? = null,
private val importDepth: ProjectViewImportDepthSection? = null,
private val enabledRules: ProjectViewEnabledRulesSection? = null,
private val ideJavaHomeOverride: ProjectViewIdeJavaHomeOverrideSection? = null,
) {

fun build(): ProjectView {
Expand All @@ -64,6 +68,7 @@ data class ProjectView(
val deriveTargetsFromDirectories = combineDeriveTargetFlagSection(importedProjectViews)
val importDepth = combineImportDepthSection(importedProjectViews)
val enabledRules = combineEnabledRulesSection(importedProjectViews)
val ideJavaHomeOverride = combineIdeJavaHomeOverrideSection(importedProjectViews)
log.debug(
"Building project view with combined"
+ " targets: {},"
Expand All @@ -73,7 +78,8 @@ data class ProjectView(
+ " directories: {},"
+ " deriveTargetsFlag: {}."
+ " import depth: {},"
+ " enabled rules: {},",
+ " enabled rules: {},"
+ " ideJavaHomeOverride: {},",
targets,
bazelBinary,
buildFlags,
Expand All @@ -82,6 +88,7 @@ data class ProjectView(
deriveTargetsFromDirectories,
importDepth,
enabledRules,
ideJavaHomeOverride,
)
return ProjectView(
targets,
Expand All @@ -92,6 +99,7 @@ data class ProjectView(
deriveTargetsFromDirectories,
importDepth,
enabledRules,
ideJavaHomeOverride,
)
}

Expand Down Expand Up @@ -202,6 +210,9 @@ data class ProjectView(
return createInstanceOfListSectionOrNull(rules, ::ProjectViewEnabledRulesSection)
}

private fun combineIdeJavaHomeOverrideSection(importedProjectViews: List<ProjectView>): ProjectViewIdeJavaHomeOverrideSection? =
ideJavaHomeOverride ?: getLastImportedSingletonValue(importedProjectViews, ProjectView::ideJavaHomeOverride)

private fun <T : ProjectViewSingletonSection<*>> getLastImportedSingletonValue(
importedProjectViews: List<ProjectView>, sectionGetter: (ProjectView) -> T?
): T? = importedProjectViews.mapNotNull(sectionGetter).lastOrNull()
Expand Down
Expand Up @@ -27,6 +27,13 @@ data class ProjectViewBuildManualTargetsSection(override val value: Boolean) :
}
}

data class ProjectViewIdeJavaHomeOverrideSection(override val value: Path) :
ProjectViewSingletonSection<Path>(SECTION_NAME) {
companion object {
const val SECTION_NAME = "ide_java_home_override"
}
}

data class ProjectViewImportDepthSection(override val value: Int) :
ProjectViewSingletonSection<Int>(SECTION_NAME) {
companion object {
Expand Down
Expand Up @@ -3,12 +3,14 @@ package org.jetbrains.bsp.bazel.projectview.parser
import org.apache.logging.log4j.LogManager
import org.jetbrains.bsp.bazel.commons.escapeNewLines
import org.jetbrains.bsp.bazel.projectview.model.ProjectView
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewIdeJavaHomeOverrideSection
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewBazelBinarySectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewBuildFlagsSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewBuildManualTargetsSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewDeriveTargetsFromDirectoriesSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewDirectoriesSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewEnabledRulesSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewIdeJavaHomeOverrideSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewImportDepthSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewTargetsSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.splitter.ProjectViewRawSections
Expand Down Expand Up @@ -40,6 +42,7 @@ open class DefaultProjectViewParser : ProjectViewParser {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSectionParser.parse(rawSections),
importDepth = ProjectViewImportDepthSectionParser.parse(rawSections),
enabledRules = ProjectViewEnabledRulesSectionParser.parse(rawSections),
ideJavaHomeOverride = ProjectViewIdeJavaHomeOverrideSectionParser.parse(rawSections),
).build()
}

Expand Down
Expand Up @@ -4,6 +4,7 @@ import org.apache.logging.log4j.LogManager
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBazelBinarySection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBuildManualTargetsSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewDeriveTargetsFromDirectoriesSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewIdeJavaHomeOverrideSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewImportDepthSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewSingletonSection
import org.jetbrains.bsp.bazel.projectview.parser.splitter.ProjectViewRawSections
Expand Down Expand Up @@ -78,3 +79,11 @@ object ProjectViewImportDepthSectionParser :
override fun createInstance(value: Int): ProjectViewImportDepthSection =
ProjectViewImportDepthSection(value)
}

object ProjectViewIdeJavaHomeOverrideSectionParser :
ProjectViewSingletonSectionParser<Path, ProjectViewIdeJavaHomeOverrideSection>(ProjectViewIdeJavaHomeOverrideSection.SECTION_NAME) {

override fun mapRawValue(rawValue: String): Path = Path(rawValue)

override fun createInstance(value: Path): ProjectViewIdeJavaHomeOverrideSection = ProjectViewIdeJavaHomeOverrideSection(value)
}
Expand Up @@ -43,6 +43,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -74,6 +75,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -105,6 +107,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -136,7 +139,8 @@ class DefaultProjectViewGeneratorTest {
directories = null,
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = null
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -176,6 +180,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -207,6 +212,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand All @@ -233,6 +239,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = null,
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand All @@ -259,6 +266,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand All @@ -285,6 +293,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -335,6 +344,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -395,6 +405,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -491,6 +502,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -571,6 +583,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)

// when
Expand Down Expand Up @@ -624,6 +637,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)

val parser = DefaultProjectViewParser()
Expand All @@ -642,6 +656,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)
parsedProjectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -672,6 +687,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)

val parser = DefaultProjectViewParser()
Expand Down Expand Up @@ -724,6 +740,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)

val parser = DefaultProjectViewParser()
Expand All @@ -750,6 +767,7 @@ class DefaultProjectViewGeneratorTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = ProjectViewEnabledRulesSection(listOf("rules_scala", "rules_jvm")),
ideJavaHomeOverride = null,
)

// when
Expand Down
Expand Up @@ -39,6 +39,7 @@ class ProjectViewBuilderTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -103,6 +104,7 @@ class ProjectViewBuilderTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(0),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -168,6 +170,7 @@ class ProjectViewBuilderTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(0),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -217,6 +220,7 @@ class ProjectViewBuilderTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(0),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -326,7 +330,8 @@ class ProjectViewBuilderTest {
),
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(false),
importDepth = ProjectViewImportDepthSection(2),
enabledRules = ProjectViewEnabledRulesSection(listOf("rules_scala"))
enabledRules = ProjectViewEnabledRulesSection(listOf("rules_scala")),
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -496,6 +501,7 @@ class ProjectViewBuilderTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -668,6 +674,7 @@ class ProjectViewBuilderTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down
Expand Up @@ -166,6 +166,7 @@ class DefaultProjectViewParserTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = null,
ideJavaHomeOverride = null,
)

projectView shouldBe expectedProjectView
Expand Down Expand Up @@ -210,6 +211,7 @@ class DefaultProjectViewParserTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(1),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -262,6 +264,7 @@ class DefaultProjectViewParserTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(1),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -312,6 +315,7 @@ class DefaultProjectViewParserTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(false),
importDepth = ProjectViewImportDepthSection(7),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -353,6 +357,7 @@ class DefaultProjectViewParserTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(8),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -410,6 +415,7 @@ class DefaultProjectViewParserTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(false),
importDepth = ProjectViewImportDepthSection(3),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand Down Expand Up @@ -472,6 +478,7 @@ class DefaultProjectViewParserTest {
deriveTargetsFromDirectories = ProjectViewDeriveTargetsFromDirectoriesSection(true),
importDepth = ProjectViewImportDepthSection(1),
enabledRules = null,
ideJavaHomeOverride = null,
)
projectView shouldBe expectedProjectView
}
Expand All @@ -496,6 +503,7 @@ class DefaultProjectViewParserTest {
deriveTargetsFromDirectories = null,
importDepth = null,
enabledRules = ProjectViewEnabledRulesSection(listOf("io_bazel_rules_scala", "rules_jvm", "rules_java")),
ideJavaHomeOverride = null,
)

projectView shouldBe expectedProjectView
Expand Down

0 comments on commit c0de197

Please sign in to comment.