Skip to content

Commit

Permalink
Path check smart default
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Nov 30, 2021
1 parent 7c73063 commit 2c10622
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/main/kotlin/com/cognifide/gradle/common/CommonPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,9 @@ class CommonPlugin : Plugin<Project> {
override fun apply(project: Project) = project.using {
val common = extensions.create(CommonExtension.NAME, CommonExtension::class.java, this)

if (common.prop.boolean("common.path.check") ?: pathCheckDefault(project)) {
val rootPath = FileUtil.systemPath(project.projectDir.absolutePath)
val sanitizedPath = FileUtil.systemPath(FileUtil.sanitizePath(rootPath))
if (sanitizedPath != rootPath) {
throw CommonException(
"Project path contains problematic characters!\n" +
"Shell scripts could run improperly with such paths.\n" +
"Consider relocating project - updating the path from '$rootPath' to '$sanitizedPath'."
)
}
if (common.prop.boolean("common.path.check") ?: checkPathEnabled(project)) {
checkPath(project)
}

if (common.prop.boolean("common.plugin.base") != false) {
plugins.apply(BasePlugin::class.java)
}
Expand All @@ -32,14 +23,38 @@ class CommonPlugin : Plugin<Project> {
}
}

/**
* Detect project paths that may be a root cause of hard to investigate later problems.
*/
private fun checkPath(project: Project) {
val rootPath = FileUtil.systemPath(project.projectDir.absolutePath)
val sanitizedPath = FileUtil.systemPath(FileUtil.sanitizePath(rootPath))
if (sanitizedPath != rootPath) {
throw CommonException(
"Project path contains problematic characters!\n" +
"Shell scripts could run improperly with such paths.\n" +
"Consider relocating project - updating the path from '$rootPath' to '$sanitizedPath'."
)
}
}

/**
* Exceptionally, disable path checking in some circumstances.
* Some CI/CD systems like Jenkins may allow spaces in paths.
*/
private fun pathCheckDefault(project: Project): Boolean {
val normalizedPath = Formats.normalizePath(project.projectDir.absolutePath)
val excludedPathFragments = listOf("/jenkins/")
return excludedPathFragments.none { normalizedPath.contains(it) }
private fun checkPathEnabled(project: Project): Boolean {
val envVars = listOf("JENKINS_HOME")
if (envVars.any { System.getenv(it).isNotBlank() }) {
return false
}

val projectPath = Formats.normalizePath(project.projectDir.absolutePath)
val pathFragments = listOf("/jenkins/")
if (pathFragments.any { projectPath.contains(it) }) {
return false
}

return true
}

companion object {
Expand Down

0 comments on commit 2c10622

Please sign in to comment.