2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.apps.controllerblueprints.scripts
19 import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
20 import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
21 import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoots
22 import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback
23 import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport
24 import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
25 import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
26 import org.jetbrains.kotlin.cli.common.messages.MessageCollector
27 import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
28 import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
29 import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler
30 import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot
31 import org.jetbrains.kotlin.config.*
32 import org.slf4j.LoggerFactory
34 import kotlin.script.experimental.api.*
35 import kotlin.script.experimental.host.ScriptingHostConfiguration
36 import kotlin.script.experimental.jvm.util.classpathFromClasspathProperty
37 import kotlin.script.experimental.jvmhost.KJvmCompilerProxy
39 open class BluePrintsCompilerProxy(private val hostConfiguration: ScriptingHostConfiguration) : KJvmCompilerProxy {
41 private val log = LoggerFactory.getLogger(BluePrintsCompilerProxy::class.java)!!
43 override fun compile(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration)
44 : ResultWithDiagnostics<CompiledScript<*>> {
46 val messageCollector = ScriptDiagnosticsMessageCollector()
48 fun failure(vararg diagnostics: ScriptDiagnostic): ResultWithDiagnostics.Failure =
49 ResultWithDiagnostics.Failure(*messageCollector.diagnostics.toTypedArray(), *diagnostics)
54 log.trace("Scripting Host Configuration : $hostConfiguration")
56 setIdeaIoUseFallback()
58 val blueprintSourceCode = script as BluePrintSourceCode
60 val compiledJarFile = blueprintSourceCode.targetJarFile
62 if (!compiledJarFile.exists() || blueprintSourceCode.regenerate) {
64 var environment: KotlinCoreEnvironment? = null
66 val rootDisposable = Disposer.newDisposable()
68 val compilerConfiguration = CompilerConfiguration().apply {
70 put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
71 put(CommonConfigurationKeys.MODULE_NAME, blueprintSourceCode.moduleName)
72 put(JVMConfigurationKeys.OUTPUT_JAR, compiledJarFile)
73 put(JVMConfigurationKeys.RETAIN_OUTPUT_IN_MEMORY, false)
75 // Load Current Class loader to Compilation Class loader
76 val currentClassLoader = classpathFromClasspathProperty()
77 currentClassLoader?.forEach {
78 add(CLIConfigurationKeys.CONTENT_ROOTS, JvmClasspathRoot(it))
81 // Add all Kotlin Sources
82 addKotlinSourceRoots(blueprintSourceCode.blueprintKotlinSources)
84 languageVersionSettings = LanguageVersionSettingsImpl(
85 LanguageVersion.LATEST_STABLE, ApiVersion.LATEST_STABLE, mapOf(AnalysisFlags.skipMetadataVersionCheck to true)
89 //log.info("Executing with compiler configuration : $compilerConfiguration")
91 environment = KotlinCoreEnvironment.createForProduction(rootDisposable, compilerConfiguration,
92 EnvironmentConfigFiles.JVM_CONFIG_FILES)
94 // Compile Kotlin Sources
95 val compiled = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment)
97 log.info("Generated jar(${compiledJarFile.absolutePath}) status : $compiled}")
99 val analyzerWithCompilerReport = AnalyzerWithCompilerReport(messageCollector,
100 environment.configuration.languageVersionSettings)
102 if (analyzerWithCompilerReport.hasErrors()) {
107 val res = BluePrintCompiledScript<File>(scriptCompilationConfiguration, compiledJarFile)
109 return ResultWithDiagnostics.Success(res, messageCollector.diagnostics)
111 } catch (ex: Throwable) {
112 return failure(ex.asDiagnostics())
117 class ScriptDiagnosticsMessageCollector : MessageCollector {
119 private val _diagnostics = arrayListOf<ScriptDiagnostic>()
121 val diagnostics: List<ScriptDiagnostic> get() = _diagnostics
123 override fun clear() {
127 override fun hasErrors(): Boolean =
128 _diagnostics.any { it.severity == ScriptDiagnostic.Severity.ERROR }
131 override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) {
132 val mappedSeverity = when (severity) {
133 CompilerMessageSeverity.EXCEPTION,
134 CompilerMessageSeverity.ERROR -> ScriptDiagnostic.Severity.ERROR
135 CompilerMessageSeverity.STRONG_WARNING,
136 CompilerMessageSeverity.WARNING -> ScriptDiagnostic.Severity.WARNING
137 CompilerMessageSeverity.INFO -> ScriptDiagnostic.Severity.INFO
138 CompilerMessageSeverity.LOGGING -> ScriptDiagnostic.Severity.DEBUG
141 if (mappedSeverity != null) {
142 val mappedLocation = location?.let {
143 if (it.line < 0 && it.column < 0) null // special location created by CompilerMessageLocation.create
144 else SourceCode.Location(SourceCode.Position(it.line, it.column))
146 _diagnostics.add(ScriptDiagnostic(message, mappedSeverity, location?.path, mappedLocation))