f7bfb857c27416baebc11a9f5a7442a2cef59a6d
[ccsdk/cds.git] /
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.core.script
18
19 import org.jetbrains.kotlin.script.util.LocalFilesResolver
20 import java.io.File
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
28
29
30 @KotlinScript(fileExtension = "kts",
31         compilationConfiguration = ComponentScriptConfiguration::class)
32 abstract class ComponentScript {
33
34 }
35
36 object ComponentScriptConfiguration : ScriptCompilationConfiguration(
37         {
38            // defaultImports(DependsOn::class, Repository::class)
39             jvm {
40                 dependenciesFromCurrentContext(
41                         wholeClasspath = true
42                 )
43             }
44 //            refineConfiguration {
45 //                onAnnotations(DependsOn::class, Repository::class, handler = ::configureLocalFileDepsOnAnnotations)
46 //            }
47         }
48 )
49
50
51 private val resolver = LocalFilesResolver()
52
53 fun configureLocalFileDepsOnAnnotations(context: ScriptConfigurationRefinementContext):
54         ResultWithDiagnostics<ScriptCompilationConfiguration> {
55
56     val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.takeIf { it.isNotEmpty() }
57             ?: return context.compilationConfiguration.asSuccess()
58
59     val scriptContents = object : ScriptContents {
60         override val annotations: Iterable<Annotation> = annotations
61         override val file: File? = null
62         override val text: CharSequence? = null
63     }
64
65     val diagnostics = arrayListOf<ScriptDiagnostic>()
66
67     fun report(severity: ScriptDependenciesResolver.ReportSeverity, message: String, position: ScriptContents.Position?) {
68         //TODO
69     }
70
71     return try {
72         val newDepsFromResolver = resolver.resolve(scriptContents, emptyMap(), ::report, null).get()
73                 ?: return context.compilationConfiguration.asSuccess(diagnostics)
74
75         val resolvedClasspath = newDepsFromResolver.classpath.toList().takeIf { it.isNotEmpty() }
76                 ?: return context.compilationConfiguration.asSuccess(diagnostics)
77
78         ScriptCompilationConfiguration(context.compilationConfiguration) {
79             dependencies.append(JvmDependency(resolvedClasspath))
80
81         }.asSuccess(diagnostics)
82
83     } catch (e: Throwable) {
84         ResultWithDiagnostics.Failure(*diagnostics.toTypedArray(), e.asDiagnostics())
85     }
86 }