From e593cd179ec4067fa3b999a749aa33cde8913c09 Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Fri, 10 Sep 2021 18:09:34 +0100 Subject: [PATCH 1/2] Create the Danger main instance only once --- .../main/kotlin/systems/danger/kotlin/MainScript.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainScript.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainScript.kt index 17ca22ed..6b4fede9 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainScript.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainScript.kt @@ -31,12 +31,14 @@ inline fun danger(args: Array, block: DangerDSL.() -> Unit) = Danger(arg * @return a new [DangerDSL] descriptor */ fun Danger(args: Array): DangerDSL { - val argsCount = args.count() + if (dangerRunner == null) { + val argsCount = args.count() - val jsonInputFilePath = args[argsCount - 2] - val jsonOutputPath = args[argsCount - 1] + val jsonInputFilePath = args[argsCount - 2] + val jsonOutputPath = args[argsCount - 1] - dangerRunner = MainDangerRunner(jsonInputFilePath, jsonOutputPath) + dangerRunner = MainDangerRunner(jsonInputFilePath, jsonOutputPath) + } return dangerRunner.danger } From 03ea31b123ba6570170c02dc2708a91460f1ec52 Mon Sep 17 00:00:00 2001 From: Franco Meloni Date: Sat, 11 Sep 2021 10:43:10 +0100 Subject: [PATCH 2/2] Use optionals instead of lateinit --- .../systems/danger/kotlin/MainDangerRunner.kt | 3 +- .../systems/danger/kotlin/MainScript.kt | 31 ++++++++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainDangerRunner.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainDangerRunner.kt index 5b5cbbe9..7ec4742d 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainDangerRunner.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainDangerRunner.kt @@ -19,7 +19,6 @@ import java.util.concurrent.atomic.AtomicReference * @param jsonOutputPath the output json file path used to publish the danger results on your Pull Request */ internal class MainDangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: FilePath) : DangerContext { - private val jsonOutputFile: File = File(jsonOutputPath) val danger: DangerDSL = JsonParser.decodeJson(jsonInputFilePath).danger @@ -86,7 +85,7 @@ internal class MainDangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: Fil } override fun suggest(code: String, file: FilePath, line: Int) { - if (dangerRunner.danger.onGitHub) { + if (runnerInstance.danger.onGitHub) { val message = "```suggestion\n $code \n```" markdown(Violation(message, file, line)) } else { diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainScript.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainScript.kt index 6b4fede9..38b49dca 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainScript.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/MainScript.kt @@ -3,7 +3,16 @@ package systems.danger.kotlin import systems.danger.kotlin.models.danger.DangerDSL import systems.danger.kotlin.models.git.FilePath -internal lateinit var dangerRunner: MainDangerRunner +internal var dangerRunner: MainDangerRunner? = null + +internal val runnerInstance: MainDangerRunner + get() { + if(dangerRunner != null) { + return dangerRunner!! + } else { + throw IllegalArgumentException("Danger must be initialised before accessing it") + } + } /** * Syntactic sugar that allows you to work with a [DangerDSL] descriptor in a single Danger block. @@ -40,7 +49,7 @@ fun Danger(args: Array): DangerDSL { dangerRunner = MainDangerRunner(jsonInputFilePath, jsonOutputPath) } - return dangerRunner.danger + return dangerRunner!!.danger } /** @@ -49,7 +58,7 @@ fun Danger(args: Array): DangerDSL { * @param message the standard message */ fun message(message: String) = - dangerRunner.message(message) + runnerInstance.message(message) /** * Adds an inline message message to the Danger report @@ -59,7 +68,7 @@ fun message(message: String) = * @param line the line number into the target file */ fun message(message: String, file: FilePath, line: Int) = - dangerRunner.message(message, file, line) + runnerInstance.message(message, file, line) /** * Adds an inline markdown message to the Danger report @@ -67,7 +76,7 @@ fun message(message: String, file: FilePath, line: Int) = * @param message the markdown formatted message */ fun markdown(message: String) = - dangerRunner.markdown(message) + runnerInstance.markdown(message) /** * Adds an inline markdown message to the Danger report @@ -77,7 +86,7 @@ fun markdown(message: String) = * @param line the line number into the target file */ fun markdown(message: String, file: FilePath, line: Int) = - dangerRunner.markdown(message, file, line) + runnerInstance.markdown(message, file, line) /** * Adds an inline warning message to the Danger report @@ -85,7 +94,7 @@ fun markdown(message: String, file: FilePath, line: Int) = * @param message the warning message */ fun warn(message: String) = - dangerRunner.warn(message) + runnerInstance.warn(message) /** * Adds an inline warning message to the Danger report @@ -95,7 +104,7 @@ fun warn(message: String) = * @param line the line number into the target file */ fun warn(message: String, file: FilePath, line: Int) = - dangerRunner.warn(message, file, line) + runnerInstance.warn(message, file, line) /** * Adds an inline fail message to the Danger report @@ -103,7 +112,7 @@ fun warn(message: String, file: FilePath, line: Int) = * @param message the fail message */ fun fail(message: String) = - dangerRunner.fail(message) + runnerInstance.fail(message) /** * Adds an inline fail message to the Danger report @@ -113,7 +122,7 @@ fun fail(message: String) = * @param line the line number into the target file */ fun fail(message: String, file: FilePath, line: Int) = - dangerRunner.fail(message, file, line) + runnerInstance.fail(message, file, line) /** * Adds an inline suggested code message to the Danger report @@ -123,4 +132,4 @@ fun fail(message: String, file: FilePath, line: Int) = * @param line the line number into the target file */ fun suggest(code: String, file: FilePath, line: Int) = - dangerRunner.suggest(code, file, line) + runnerInstance.suggest(code, file, line)