Skip to content

Commit

Permalink
refactor(script): Migrate from deprecated constructorArgs to proper…
Browse files Browse the repository at this point in the history
…ties

This is a preparation for the migration to Kotlin 2.0.0 in order to work
around [1].

[1]: https://youtrack.jetbrains.com/issue/KT-68336

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed May 22, 2024
1 parent 8e2817f commit 8451714
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
17 changes: 14 additions & 3 deletions evaluator/src/main/kotlin/Evaluator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ package org.ossreviewtoolkit.evaluator

import java.time.Instant

import kotlin.script.experimental.api.KotlinType
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
import kotlin.script.experimental.api.constructorArgs
import kotlin.script.experimental.api.providedProperties
import kotlin.script.experimental.api.scriptsInstancesSharing
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate

Expand All @@ -42,10 +43,20 @@ class Evaluator(
licenseClassifications: LicenseClassifications = LicenseClassifications(),
time: Instant = Instant.now()
) : ScriptRunner() {
override val compConfig = createJvmCompilationConfigurationFromTemplate<RulesScriptTemplate>()
private val customProperties = mapOf(
"ortResult" to ortResult,
"licenseInfoResolver" to licenseInfoResolver,
"resolutionProvider" to resolutionProvider,
"licenseClassifications" to licenseClassifications,
"time" to time
)

override val compConfig = createJvmCompilationConfigurationFromTemplate<RulesScriptTemplate> {
providedProperties(customProperties.mapValues { (_, v) -> KotlinType(v::class) })
}

override val evalConfig = ScriptEvaluationConfiguration {
constructorArgs(ortResult, licenseInfoResolver, resolutionProvider, licenseClassifications, time)
providedProperties(customProperties)
scriptsInstancesSharing(true)
}

Expand Down
14 changes: 1 addition & 13 deletions evaluator/src/main/kotlin/RulesScriptTemplate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,19 @@

package org.ossreviewtoolkit.evaluator

import java.time.Instant

import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.ScriptCompilationConfiguration
import kotlin.script.experimental.api.defaultImports

import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.RuleViolation
import org.ossreviewtoolkit.model.licenses.LicenseClassifications
import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver
import org.ossreviewtoolkit.model.utils.ResolutionProvider
import org.ossreviewtoolkit.utils.scripting.OrtScriptCompilationConfiguration

@KotlinScript(
displayName = "ORT Evaluator Rules Script",
fileExtension = "rules.kts",
compilationConfiguration = RulesScriptCompilationConfiguration::class
)
open class RulesScriptTemplate(
val ortResult: OrtResult,
val licenseInfoResolver: LicenseInfoResolver,
val resolutionProvider: ResolutionProvider,
val licenseClassifications: LicenseClassifications,
val time: Instant
) {
open class RulesScriptTemplate {
val ruleViolations = mutableListOf<RuleViolation>()
}

Expand Down
5 changes: 1 addition & 4 deletions notifier/src/main/kotlin/NotificationsScriptTemplate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.ScriptCompilationConfiguration
import kotlin.script.experimental.api.defaultImports

import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.utils.scripting.OrtScriptCompilationConfiguration

@KotlinScript(
displayName = "ORT Notifications Script",
fileExtension = "notifications.kts",
compilationConfiguration = NotificationsScriptCompilationConfiguration::class
)
open class NotificationsScriptTemplate(
val ortResult: OrtResult
)
open class NotificationsScriptTemplate

class NotificationsScriptCompilationConfiguration : ScriptCompilationConfiguration(
OrtScriptCompilationConfiguration(),
Expand Down
7 changes: 3 additions & 4 deletions notifier/src/main/kotlin/Notifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import java.time.Instant

import kotlin.script.experimental.api.KotlinType
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
import kotlin.script.experimental.api.constructorArgs
import kotlin.script.experimental.api.providedProperties
import kotlin.script.experimental.api.scriptsInstancesSharing
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
Expand All @@ -43,6 +42,8 @@ class Notifier(
resolutionProvider: ResolutionProvider = DefaultResolutionProvider()
) : ScriptRunner() {
private val customProperties = buildMap {
put("ortResult", ortResult)

config.mail?.let { put("mailClient", MailNotifier(it)) }
config.jira?.let { put("jiraClient", JiraNotifier(it)) }

Expand All @@ -54,10 +55,8 @@ class Notifier(
}

override val evalConfig = ScriptEvaluationConfiguration {
constructorArgs(ortResult)
scriptsInstancesSharing(true)

providedProperties(customProperties)
scriptsInstancesSharing(true)
}

fun run(script: String): NotifierRun {
Expand Down
11 changes: 8 additions & 3 deletions reporter/src/main/kotlin/HowToFixTextProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

package org.ossreviewtoolkit.reporter

import kotlin.script.experimental.api.KotlinType
import kotlin.script.experimental.api.ResultValue
import kotlin.script.experimental.api.ScriptEvaluationConfiguration
import kotlin.script.experimental.api.constructorArgs
import kotlin.script.experimental.api.providedProperties
import kotlin.script.experimental.api.scriptsInstancesSharing
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate

Expand Down Expand Up @@ -54,10 +55,14 @@ fun interface HowToFixTextProvider {
}

private class HowToFixScriptRunner(ortResult: OrtResult) : ScriptRunner() {
override val compConfig = createJvmCompilationConfigurationFromTemplate<HowToFixTextProviderScriptTemplate>()
private val customProperties = mapOf("ortResult" to ortResult)

override val compConfig = createJvmCompilationConfigurationFromTemplate<HowToFixTextProviderScriptTemplate> {
providedProperties(customProperties.mapValues { (_, v) -> KotlinType(v::class) })
}

override val evalConfig = ScriptEvaluationConfiguration {
constructorArgs(ortResult)
providedProperties(customProperties)
scriptsInstancesSharing(true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.ScriptCompilationConfiguration
import kotlin.script.experimental.api.defaultImports

import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.utils.scripting.OrtScriptCompilationConfiguration

@KotlinScript(
displayName = "ORT How-To-Fix-Text-Provider Script",
fileExtension = "how-to-fix-text-provider.kts",
compilationConfiguration = HowToFixTextProviderScriptCompilationConfiguration::class
)
open class HowToFixTextProviderScriptTemplate(
val ortResult: OrtResult
)
open class HowToFixTextProviderScriptTemplate

class HowToFixTextProviderScriptCompilationConfiguration : ScriptCompilationConfiguration(
OrtScriptCompilationConfiguration(),
Expand Down

0 comments on commit 8451714

Please sign in to comment.