7e9e868823f0685a702f0aedf6703295d9a1d82f
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-scripts / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / scripts / BluePrintCompilerProxy.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.scripts
18
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
33 import java.io.File
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
38
39 open class BluePrintsCompilerProxy(private val hostConfiguration: ScriptingHostConfiguration) : KJvmCompilerProxy {
40
41     private val log = LoggerFactory.getLogger(BluePrintsCompilerProxy::class.java)!!
42
43     override fun compile(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration)
44             : ResultWithDiagnostics<CompiledScript<*>> {
45
46         val messageCollector = ScriptDiagnosticsMessageCollector()
47
48         fun failure(vararg diagnostics: ScriptDiagnostic): ResultWithDiagnostics.Failure =
49                 ResultWithDiagnostics.Failure(*messageCollector.diagnostics.toTypedArray(), *diagnostics)
50
51         // Compile the Code
52         try {
53
54             log.trace("Scripting Host Configuration : $hostConfiguration")
55
56             setIdeaIoUseFallback()
57
58             val blueprintSourceCode = script as BluePrintSourceCode
59
60             val compiledJarFile = blueprintSourceCode.targetJarFile
61
62             if (!compiledJarFile.exists() || blueprintSourceCode.regenerate) {
63
64                 var environment: KotlinCoreEnvironment? = null
65
66                 val rootDisposable = Disposer.newDisposable()
67
68                 val compilerConfiguration = CompilerConfiguration().apply {
69
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)
74
75                     // Load Current Class loader to Compilation Class loader
76                     val currentClassLoader = classpathFromClasspathProperty()
77                     currentClassLoader?.forEach {
78                         add(CLIConfigurationKeys.CONTENT_ROOTS, JvmClasspathRoot(it))
79                     }
80
81                     // Add all Kotlin Sources
82                     addKotlinSourceRoots(blueprintSourceCode.blueprintKotlinSources)
83
84                     languageVersionSettings = LanguageVersionSettingsImpl(
85                             LanguageVersion.LATEST_STABLE, ApiVersion.LATEST_STABLE, mapOf(AnalysisFlags.skipMetadataVersionCheck to true)
86                     )
87                 }
88
89                 //log.info("Executing with compiler configuration : $compilerConfiguration")
90
91                 environment = KotlinCoreEnvironment.createForProduction(rootDisposable, compilerConfiguration,
92                         EnvironmentConfigFiles.JVM_CONFIG_FILES)
93
94                 // Compile Kotlin Sources
95                 val compiled = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment)
96
97                 log.info("Generated jar(${compiledJarFile.absolutePath}) status : $compiled}")
98
99                 val analyzerWithCompilerReport = AnalyzerWithCompilerReport(messageCollector,
100                         environment.configuration.languageVersionSettings)
101
102                 if (analyzerWithCompilerReport.hasErrors()) {
103                     return failure()
104                 }
105             }
106
107             val res = BluePrintCompiledScript<File>(scriptCompilationConfiguration, compiledJarFile)
108
109             return ResultWithDiagnostics.Success(res, messageCollector.diagnostics)
110
111         } catch (ex: Throwable) {
112             return failure(ex.asDiagnostics())
113         }
114     }
115 }
116
117 class ScriptDiagnosticsMessageCollector : MessageCollector {
118
119     private val _diagnostics = arrayListOf<ScriptDiagnostic>()
120
121     val diagnostics: List<ScriptDiagnostic> get() = _diagnostics
122
123     override fun clear() {
124         _diagnostics.clear()
125     }
126
127     override fun hasErrors(): Boolean =
128             _diagnostics.any { it.severity == ScriptDiagnostic.Severity.ERROR }
129
130
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
139             else -> null
140         }
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))
145             }
146             _diagnostics.add(ScriptDiagnostic(message, mappedSeverity, location?.path, mappedLocation))
147         }
148     }
149 }
150