Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
Adding ability to set importing depth for targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Błażej Kardyś committed May 26, 2022
1 parent c932858 commit c221030
Show file tree
Hide file tree
Showing 23 changed files with 728 additions and 449 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.vavr.control.Try
import org.jetbrains.bsp.bazel.projectview.generator.sections.ProjectViewBazelPathSectionGenerator
import org.jetbrains.bsp.bazel.projectview.generator.sections.ProjectViewBuildFlagsSectionGenerator
import org.jetbrains.bsp.bazel.projectview.generator.sections.ProjectViewDebuggerAddressSectionGenerator
import org.jetbrains.bsp.bazel.projectview.generator.sections.ProjectViewImportDepthSectionGenerator
import org.jetbrains.bsp.bazel.projectview.generator.sections.ProjectViewJavaPathSectionGenerator
import org.jetbrains.bsp.bazel.projectview.generator.sections.ProjectViewTargetsSectionGenerator
import org.jetbrains.bsp.bazel.projectview.model.ProjectView
Expand All @@ -22,5 +23,6 @@ object DefaultProjectViewGenerator : ProjectViewGenerator {
ProjectViewDebuggerAddressSectionGenerator.generatePrettyString(projectView.debuggerAddress),
ProjectViewJavaPathSectionGenerator.generatePrettyString(projectView.javaPath),
ProjectViewBuildFlagsSectionGenerator.generatePrettyString(projectView.buildFlags),
ProjectViewImportDepthSectionGenerator.generatePrettyString(projectView.importDepth),
).joinToString(separator = "\n\n", postfix = "\n")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.bsp.bazel.projectview.generator.sections

import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBazelPathSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewDebuggerAddressSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewImportDepthSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewJavaPathSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewSingletonSection

Expand All @@ -23,3 +24,5 @@ object ProjectViewDebuggerAddressSectionGenerator :
ProjectViewSingletonSectionGenerator<ProjectViewDebuggerAddressSection>()

object ProjectViewBazelPathSectionGenerator : ProjectViewSingletonSectionGenerator<ProjectViewBazelPathSection>()

object ProjectViewImportDepthSectionGenerator : ProjectViewSingletonSectionGenerator<ProjectViewImportDepthSection>()
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBazelPathSe
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBuildFlagsSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewDebuggerAddressSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewExcludableListSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewImportDepthSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewJavaPathSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewListSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewSingletonSection
Expand All @@ -28,6 +29,8 @@ data class ProjectView constructor(
val javaPath: ProjectViewJavaPathSection?,
/** bazel flags added to all bazel command invocations */
val buildFlags: ProjectViewBuildFlagsSection?,
/** level of depth for importing inherited targets */
val importDepth: ProjectViewImportDepthSection?,
) {

class Builder constructor(
Expand All @@ -37,6 +40,7 @@ data class ProjectView constructor(
private val debuggerAddress: ProjectViewDebuggerAddressSection? = null,
private val javaPath: ProjectViewJavaPathSection? = null,
private val buildFlags: ProjectViewBuildFlagsSection? = null,
private val importDepth: ProjectViewImportDepthSection? = null,
) {

fun build(): Try<ProjectView> {
Expand All @@ -48,7 +52,8 @@ data class ProjectView constructor(
+ " bazel path: {},"
+ " debugger address: {},"
+ " java path: {},"
+ " build flags: {}.",
+ " build flags: {},"
+ " import depth: {}.",
imports,
targets,
bazelPath,
Expand All @@ -69,18 +74,21 @@ data class ProjectView constructor(
val debuggerAddress = combineDebuggerAddressSection(importedProjectViews)
val javaPath = combineJavaPathSection(importedProjectViews)
val buildFlags = combineBuildFlagsSection(importedProjectViews)
val importDepth = combineImportDepthSection(importedProjectViews)
log.debug(
"Building project view with combined"
+ " targets: {},"
+ " bazel path: {},"
+ " debugger address: {},"
+ " java path: {}.",
+ " java path: {},"
+ " import depth: {}.",
targets,
bazelPath,
debuggerAddress,
javaPath
javaPath,
importDepth
)
return ProjectView(targets, bazelPath, debuggerAddress, javaPath, buildFlags)
return ProjectView(targets, bazelPath, debuggerAddress, javaPath, buildFlags, importDepth)
}

private fun combineTargetsSection(importedProjectViews: List<ProjectView>): ProjectViewTargetsSection? {
Expand Down Expand Up @@ -153,6 +161,9 @@ data class ProjectView constructor(
private fun combineJavaPathSection(importedProjectViews: List<ProjectView>): ProjectViewJavaPathSection? =
javaPath ?: getLastImportedSingletonValue(importedProjectViews, ProjectView::javaPath)

private fun combineImportDepthSection(importedProjectViews: List<ProjectView>): ProjectViewImportDepthSection? =
importDepth ?: getLastImportedSingletonValue(importedProjectViews, ProjectView::importDepth)


private fun <T : ProjectViewSingletonSection<*>> getLastImportedSingletonValue(
importedProjectViews: List<ProjectView>, sectionGetter: (ProjectView) -> T?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ data class ProjectViewBazelPathSection(override val value: Path) :
const val SECTION_NAME = "bazel_path"
}
}

data class ProjectViewImportDepthSection(override val value: Int) :
ProjectViewSingletonSection<Int>(SECTION_NAME) {
companion object {
const val SECTION_NAME = "import_depth"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.jetbrains.bsp.bazel.projectview.model.ProjectView
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewBazelPathSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewBuildFlagsSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewDebuggerAddressSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewImportDepthSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewJavaPathSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.sections.ProjectViewTargetsSectionParser
import org.jetbrains.bsp.bazel.projectview.parser.splitter.ProjectViewRawSections
Expand Down Expand Up @@ -46,6 +47,7 @@ open class DefaultProjectViewParser : ProjectViewParser {
debuggerAddress = ProjectViewDebuggerAddressSectionParser.parse(rawSections),
javaPath = ProjectViewJavaPathSectionParser.parse(rawSections),
buildFlags = ProjectViewBuildFlagsSectionParser.parse(rawSections),
importDepth = ProjectViewImportDepthSectionParser.parse(rawSections),
).build()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.jetbrains.bsp.bazel.projectview.parser.sections

import org.apache.logging.log4j.LogManager
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBazelPathSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewDebuggerAddressSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewJavaPathSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewSingletonSection
import org.jetbrains.bsp.bazel.projectview.model.sections.*
import org.jetbrains.bsp.bazel.projectview.parser.splitter.ProjectViewRawSections
import java.nio.file.Path
import kotlin.io.path.Path
Expand Down Expand Up @@ -69,3 +66,13 @@ object ProjectViewJavaPathSectionParser :

override fun createInstance(value: Path): ProjectViewJavaPathSection = ProjectViewJavaPathSection(value)
}


object ProjectViewImportDepthSectionParser :
ProjectViewSingletonSectionParser<Int, ProjectViewImportDepthSection>(ProjectViewImportDepthSection.SECTION_NAME) {

override fun mapRawValue(rawValue: String): Int = rawValue.toInt()

override fun createInstance(value: Int): ProjectViewImportDepthSection =
ProjectViewImportDepthSection(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ package org.jetbrains.bsp.bazel.projectview.generator
import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import io.kotest.matchers.shouldBe
import org.jetbrains.bsp.bazel.projectview.model.ProjectView
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBazelPathSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBuildFlagsSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewDebuggerAddressSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewJavaPathSection
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewTargetsSection
import org.jetbrains.bsp.bazel.projectview.model.sections.*
import org.jetbrains.bsp.bazel.projectview.parser.DefaultProjectViewParser
import org.jetbrains.bsp.bazel.utils.dope.DopeTemp
import org.junit.jupiter.api.DisplayName
Expand All @@ -31,6 +27,7 @@ class DefaultProjectViewGeneratorTest {
debuggerAddress = null,
javaPath = null,
buildFlags = null,
importDepth = null,
)

// when
Expand Down Expand Up @@ -59,6 +56,7 @@ class DefaultProjectViewGeneratorTest {
debuggerAddress = null,
javaPath = null,
buildFlags = null,
importDepth = null,
)

// when
Expand Down Expand Up @@ -87,6 +85,7 @@ class DefaultProjectViewGeneratorTest {
debuggerAddress = null,
javaPath = null,
buildFlags = null,
importDepth = null,
)

// when
Expand All @@ -110,6 +109,7 @@ class DefaultProjectViewGeneratorTest {
debuggerAddress = ProjectViewDebuggerAddressSection("localhost:8000"),
javaPath = null,
buildFlags = null,
importDepth = null,
)

// when
Expand All @@ -133,6 +133,7 @@ class DefaultProjectViewGeneratorTest {
debuggerAddress = null,
javaPath = ProjectViewJavaPathSection(Paths.get("/path/to/java")),
buildFlags = null,
importDepth = null,
)

// when
Expand Down Expand Up @@ -162,6 +163,7 @@ class DefaultProjectViewGeneratorTest {
"--build_flag3=value3",
)
),
importDepth = null,
)

// when
Expand All @@ -179,6 +181,30 @@ class DefaultProjectViewGeneratorTest {
generatedString shouldBe expectedGeneratedString
}

@Test
fun `should return pretty string only with import depth flag for project view only with import depth flag`() {
// given
val projectView = ProjectView(
targets = null,
bazelPath = null,
debuggerAddress = null,
javaPath = null,
buildFlags = null,
importDepth = ProjectViewImportDepthSection(3),
)

// when
val generatedString = DefaultProjectViewGenerator.generatePrettyString(projectView)

// then
val expectedGeneratedString =
"""
import_depth: 3
""".trimIndent()
generatedString shouldBe expectedGeneratedString
}

@Test
fun `should return pretty string with project view for project view with empty list sections`() {
// given
Expand All @@ -188,6 +214,7 @@ class DefaultProjectViewGeneratorTest {
debuggerAddress = ProjectViewDebuggerAddressSection("localhost:8000"),
javaPath = ProjectViewJavaPathSection(Paths.get("/path/to/java")),
buildFlags = ProjectViewBuildFlagsSection(emptyList()),
importDepth = ProjectViewImportDepthSection(3),
)

// when
Expand All @@ -206,6 +233,8 @@ class DefaultProjectViewGeneratorTest {
build_flags:
import_depth: 3
""".trimIndent()
generatedString shouldBe expectedGeneratedString
}
Expand All @@ -231,6 +260,7 @@ class DefaultProjectViewGeneratorTest {
"--build_flag3=value3",
)
),
importDepth = null
)

// when
Expand Down Expand Up @@ -280,6 +310,7 @@ class DefaultProjectViewGeneratorTest {
"--build_flag3=value3",
)
),
importDepth = ProjectViewImportDepthSection(3),
)

// when
Expand All @@ -306,6 +337,8 @@ class DefaultProjectViewGeneratorTest {
--build_flag2=value2
--build_flag3=value3
import_depth: 3
""".trimIndent()
generatedString shouldBe expectedGeneratedString
}
Expand Down Expand Up @@ -342,6 +375,7 @@ class DefaultProjectViewGeneratorTest {
"--build_flag3=value3",
)
),
importDepth = ProjectViewImportDepthSection(3),
)

// when
Expand Down Expand Up @@ -370,6 +404,8 @@ class DefaultProjectViewGeneratorTest {
--build_flag2=value2
--build_flag3=value3
import_depth: 3
""".trimIndent()
Files.readString(filePath) shouldBe expectedFileContent
}
Expand Down Expand Up @@ -402,6 +438,7 @@ class DefaultProjectViewGeneratorTest {
"--build_flag3=value3",
)
),
importDepth = ProjectViewImportDepthSection(3),
)

// when
Expand Down Expand Up @@ -430,6 +467,8 @@ class DefaultProjectViewGeneratorTest {
--build_flag2=value2
--build_flag3=value3
import_depth: 3
""".trimIndent()
Files.readString(filePath) shouldBe expectedFileContent
}
Expand All @@ -445,6 +484,7 @@ class DefaultProjectViewGeneratorTest {
debuggerAddress = ProjectViewDebuggerAddressSection("localhost:8000"),
javaPath = ProjectViewJavaPathSection(Paths.get("/path/to/java")),
buildFlags = ProjectViewBuildFlagsSection(emptyList()),
importDepth = ProjectViewImportDepthSection(3),
)

val parser = DefaultProjectViewParser()
Expand All @@ -463,6 +503,7 @@ class DefaultProjectViewGeneratorTest {
debuggerAddress = ProjectViewDebuggerAddressSection("localhost:8000"),
javaPath = ProjectViewJavaPathSection(Paths.get("/path/to/java")),
buildFlags = null,
importDepth = ProjectViewImportDepthSection(3),
)
parsedProjectViewTry.get() shouldBe expectedProjectView
}
Expand Down Expand Up @@ -490,6 +531,7 @@ class DefaultProjectViewGeneratorTest {
"--build_flag3=value3",
)
),
importDepth = null,
)

val parser = DefaultProjectViewParser()
Expand Down Expand Up @@ -532,6 +574,7 @@ class DefaultProjectViewGeneratorTest {
"--build_flag3=value3",
)
),
importDepth = ProjectViewImportDepthSection(3),
)

val parser = DefaultProjectViewParser()
Expand Down
Loading

0 comments on commit c221030

Please sign in to comment.