Skip to content

Commit

Permalink
Require basePath when setting up environment
Browse files Browse the repository at this point in the history
This is a prerequisite for fixing detekt#3728
  • Loading branch information
3flex committed Jul 29, 2023
1 parent 51e6065 commit 342872e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.beust.jcommander.Parameter
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.config.LanguageVersion
import java.nio.file.Path
import kotlin.io.path.Path

class CliArgs {

Expand Down Expand Up @@ -92,7 +93,7 @@ class CliArgs {
"File paths in console output and txt report are not affected and remain as absolute paths.",
converter = PathConverter::class
)
var basePath: Path? = null
var basePath: Path = Path(System.getProperty("user.dir"))

@Parameter(
names = ["--disable-default-rulesets", "-dd"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class KtTreeCompiler(
private val compiler: KtCompiler = KtCompiler(settings.environment)
) {

private val basePath: Path? = projectSpec.basePath
private val basePath: Path = projectSpec.basePath
private val pathFilters: PathFilters? =
PathFilters.of(projectSpec.includes.toList(), projectSpec.excludes.toList())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal fun <R> ProcessingSpec.withSettings(execute: ProcessingSettings.() -> R
register(DETEKT_BASELINE_CREATION_KEY, baselineSpec.shouldCreateDuringAnalysis)
register(MONITOR_PROPERTY_KEY, monitor)
register(DETEKT_OUTPUT_REPORT_PATHS_KEY, reportsSpec.reports)
projectSpec.basePath?.let { register(DETEKT_OUTPUT_REPORT_BASE_PATH_KEY, it) }
register(DETEKT_OUTPUT_REPORT_BASE_PATH_KEY, projectSpec.basePath)
}
}
val result = settings.use { execute(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ open class KtCompiler(

protected val psiFileFactory = KtPsiFactory(environment.project, markGenerated = false)

fun compile(basePath: Path?, path: Path): KtFile {
fun compile(basePath: Path, path: Path): KtFile {
require(path.isRegularFile()) { "Given sub path ($path) should be a regular file!" }
val content = path.readText()
return createKtFile(content, basePath, path)
}

fun createKtFile(content: String, basePath: Path?, path: Path): KtFile {
fun createKtFile(content: String, basePath: Path, path: Path): KtFile {
require(path.isRegularFile()) { "Given sub path ($path) should be a regular file!" }

val normalizedAbsolutePath = path.absolute().normalize()
Expand All @@ -40,11 +40,9 @@ open class KtCompiler(
return psiFile.apply {
this.absolutePath = normalizedAbsolutePath
this.lineSeparator = lineSeparator
val normalizedBasePath = basePath?.absolute()?.normalize()
normalizedBasePath?.relativize(normalizedAbsolutePath)?.let { relativePath ->
this.basePath = normalizedBasePath.absolute()
this.relativePath = relativePath
}
val normalizedBasePath = basePath.absolute().normalize()
this.basePath = normalizedBasePath.absolute()
this.relativePath = normalizedBasePath.relativize(normalizedAbsolutePath)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface ProjectSpec {
/**
* A base path to relativize paths. Mostly used for generating path in the output or report.
*/
val basePath: Path?
val basePath: Path

/**
* Paths to analyze. Works with files and directories.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package io.github.detekt.tooling.dsl

import io.github.detekt.tooling.api.spec.ProjectSpec
import java.nio.file.Path
import kotlin.io.path.Path

@ProcessingModelDsl
class ProjectSpecBuilder : Builder<ProjectSpec> {

var basePath: Path? = null
var basePath: Path = Path("")
var inputPaths: Collection<Path> = emptyList()
var excludes: Collection<String> = emptyList()
var includes: Collection<String> = emptyList()
Expand All @@ -15,7 +16,7 @@ class ProjectSpecBuilder : Builder<ProjectSpec> {
}

private data class ProjectModel(
override val basePath: Path?,
override val basePath: Path,
override val inputPaths: Collection<Path>,
override val excludes: Collection<String>,
override val includes: Collection<String>
Expand Down

0 comments on commit 342872e

Please sign in to comment.