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.core.script
 
  19 import org.jetbrains.kotlin.script.util.LocalFilesResolver
 
  21 import kotlin.script.dependencies.ScriptContents
 
  22 import kotlin.script.dependencies.ScriptDependenciesResolver
 
  23 import kotlin.script.experimental.annotations.KotlinScript
 
  24 import kotlin.script.experimental.api.*
 
  25 import kotlin.script.experimental.jvm.JvmDependency
 
  26 import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
 
  27 import kotlin.script.experimental.jvm.jvm
 
  30 @KotlinScript(fileExtension = "kts",
 
  31         compilationConfiguration = ComponentScriptConfiguration::class)
 
  32 abstract class ComponentScript {
 
  36 object ComponentScriptConfiguration : ScriptCompilationConfiguration(
 
  38            // defaultImports(DependsOn::class, Repository::class)
 
  40                 dependenciesFromCurrentContext(
 
  44 //            refineConfiguration {
 
  45 //                onAnnotations(DependsOn::class, Repository::class, handler = ::configureLocalFileDepsOnAnnotations)
 
  51 private val resolver = LocalFilesResolver()
 
  53 fun configureLocalFileDepsOnAnnotations(context: ScriptConfigurationRefinementContext):
 
  54         ResultWithDiagnostics<ScriptCompilationConfiguration> {
 
  56     val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.takeIf { it.isNotEmpty() }
 
  57             ?: return context.compilationConfiguration.asSuccess()
 
  59     val scriptContents = object : ScriptContents {
 
  60         override val annotations: Iterable<Annotation> = annotations
 
  61         override val file: File? = null
 
  62         override val text: CharSequence? = null
 
  65     val diagnostics = arrayListOf<ScriptDiagnostic>()
 
  67     fun report(severity: ScriptDependenciesResolver.ReportSeverity, message: String, position: ScriptContents.Position?) {
 
  72         val newDepsFromResolver = resolver.resolve(scriptContents, emptyMap(), ::report, null).get()
 
  73                 ?: return context.compilationConfiguration.asSuccess(diagnostics)
 
  75         val resolvedClasspath = newDepsFromResolver.classpath.toList().takeIf { it.isNotEmpty() }
 
  76                 ?: return context.compilationConfiguration.asSuccess(diagnostics)
 
  78         ScriptCompilationConfiguration(context.compilationConfiguration) {
 
  79             dependencies.append(JvmDependency(resolvedClasspath))
 
  81         }.asSuccess(diagnostics)
 
  83     } catch (e: Throwable) {
 
  84         ResultWithDiagnostics.Failure(*diagnostics.toTypedArray(), e.asDiagnostics())