Add script config refinement before compilation into API
This commit is contained in:
@@ -90,13 +90,18 @@ val ScriptCompilationConfigurationKeys.compilerOptions by PropertiesCollection.k
|
|||||||
/**
|
/**
|
||||||
* The callback that will be called on the script compilation before parsing the script
|
* The callback that will be called on the script compilation before parsing the script
|
||||||
*/
|
*/
|
||||||
val ScriptCompilationConfigurationKeys.refineConfigurationBeforeParsing by PropertiesCollection.key<RefineConfigurationBeforeParsingData>()
|
val ScriptCompilationConfigurationKeys.refineConfigurationBeforeParsing by PropertiesCollection.key<RefineConfigurationUnconditionallyData>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The callback that will be called on the script compilation after parsing script file annotations
|
* The callback that will be called on the script compilation after parsing script file annotations
|
||||||
*/
|
*/
|
||||||
val ScriptCompilationConfigurationKeys.refineConfigurationOnAnnotations by PropertiesCollection.key<RefineConfigurationOnAnnotationsData>()
|
val ScriptCompilationConfigurationKeys.refineConfigurationOnAnnotations by PropertiesCollection.key<RefineConfigurationOnAnnotationsData>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The callback that will be called on the script compilation immediately before starting the compilation
|
||||||
|
*/
|
||||||
|
val ScriptCompilationConfigurationKeys.refineConfigurationBeforeCompiling by PropertiesCollection.key<RefineConfigurationUnconditionallyData>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of script fragments that should be compiled intead of the whole text
|
* The list of script fragments that should be compiled intead of the whole text
|
||||||
* (for use primary with the refinement callbacks)
|
* (for use primary with the refinement callbacks)
|
||||||
@@ -116,7 +121,7 @@ class RefineConfigurationBuilder : PropertiesCollection.Builder() {
|
|||||||
* @param handler the callback that will be called
|
* @param handler the callback that will be called
|
||||||
*/
|
*/
|
||||||
fun beforeParsing(handler: RefineScriptCompilationConfigurationHandler) {
|
fun beforeParsing(handler: RefineScriptCompilationConfigurationHandler) {
|
||||||
set(ScriptCompilationConfiguration.refineConfigurationBeforeParsing, RefineConfigurationBeforeParsingData(handler))
|
set(ScriptCompilationConfiguration.refineConfigurationBeforeParsing, RefineConfigurationUnconditionallyData(handler))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -125,6 +130,7 @@ class RefineConfigurationBuilder : PropertiesCollection.Builder() {
|
|||||||
* @param handler the callback that will be called
|
* @param handler the callback that will be called
|
||||||
*/
|
*/
|
||||||
fun onAnnotations(annotations: List<KotlinType>, handler: RefineScriptCompilationConfigurationHandler) {
|
fun onAnnotations(annotations: List<KotlinType>, handler: RefineScriptCompilationConfigurationHandler) {
|
||||||
|
// TODO: implement handlers composition
|
||||||
set(ScriptCompilationConfiguration.refineConfigurationOnAnnotations, RefineConfigurationOnAnnotationsData(annotations, handler))
|
set(ScriptCompilationConfiguration.refineConfigurationOnAnnotations, RefineConfigurationOnAnnotationsData(annotations, handler))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,7 +145,7 @@ class RefineConfigurationBuilder : PropertiesCollection.Builder() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The callback that will be called on the script compilation after parsing script file annotations
|
* The callback that will be called on the script compilation after parsing script file annotations
|
||||||
* @param annotations the list of annotations to trigger the callback on
|
* @param T the annotation to trigger the callback on
|
||||||
* @param handler the callback that will be called
|
* @param handler the callback that will be called
|
||||||
*/
|
*/
|
||||||
inline fun <reified T : Annotation> onAnnotations(noinline handler: RefineScriptCompilationConfigurationHandler) {
|
inline fun <reified T : Annotation> onAnnotations(noinline handler: RefineScriptCompilationConfigurationHandler) {
|
||||||
@@ -163,6 +169,14 @@ class RefineConfigurationBuilder : PropertiesCollection.Builder() {
|
|||||||
fun onAnnotations(annotations: Iterable<KClass<out Annotation>>, handler: RefineScriptCompilationConfigurationHandler) {
|
fun onAnnotations(annotations: Iterable<KClass<out Annotation>>, handler: RefineScriptCompilationConfigurationHandler) {
|
||||||
onAnnotations(annotations.map { KotlinType(it) }, handler)
|
onAnnotations(annotations.map { KotlinType(it) }, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The callback that will be called on the script compilation immediately before starting the compilation
|
||||||
|
* @param handler the callback that will be called
|
||||||
|
*/
|
||||||
|
fun beforeCompiling(handler: RefineScriptCompilationConfigurationHandler) {
|
||||||
|
set(ScriptCompilationConfiguration.refineConfigurationBeforeCompiling, RefineConfigurationUnconditionallyData(handler))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -171,7 +185,7 @@ class RefineConfigurationBuilder : PropertiesCollection.Builder() {
|
|||||||
typealias RefineScriptCompilationConfigurationHandler =
|
typealias RefineScriptCompilationConfigurationHandler =
|
||||||
(ScriptConfigurationRefinementContext) -> ResultWithDiagnostics<ScriptCompilationConfiguration>
|
(ScriptConfigurationRefinementContext) -> ResultWithDiagnostics<ScriptCompilationConfiguration>
|
||||||
|
|
||||||
class RefineConfigurationBeforeParsingData(
|
class RefineConfigurationUnconditionallyData(
|
||||||
val handler: RefineScriptCompilationConfigurationHandler
|
val handler: RefineScriptCompilationConfigurationHandler
|
||||||
) : Serializable
|
) : Serializable
|
||||||
|
|
||||||
|
|||||||
+32
-12
@@ -44,19 +44,22 @@ class BridgeDependenciesResolver(
|
|||||||
|
|
||||||
val defaultImports = scriptCompilationConfiguration[ScriptCompilationConfiguration.defaultImports]?.toList() ?: emptyList()
|
val defaultImports = scriptCompilationConfiguration[ScriptCompilationConfiguration.defaultImports]?.toList() ?: emptyList()
|
||||||
|
|
||||||
val refineFn = scriptCompilationConfiguration[ScriptCompilationConfiguration.refineConfigurationOnAnnotations]?.handler
|
fun ScriptCompilationConfiguration.toDependencies(classpath: List<File>): ScriptDependencies = ScriptDependencies(
|
||||||
?: return DependenciesResolver.ResolveResult.Success(
|
classpath = classpath,
|
||||||
ScriptDependencies(
|
sources = this[ScriptCompilationConfiguration.ide.dependenciesSources].toClassPathOrEmpty(),
|
||||||
classpath = oldClasspath,
|
imports = defaultImports
|
||||||
sources = scriptCompilationConfiguration[ScriptCompilationConfiguration.ide.dependenciesSources].toClassPathOrEmpty(),
|
|
||||||
imports = defaultImports
|
|
||||||
),
|
|
||||||
diagnostics
|
|
||||||
)
|
|
||||||
|
|
||||||
val refineResults = refineFn(
|
|
||||||
ScriptConfigurationRefinementContext(scriptContents.toScriptSource(), scriptCompilationConfiguration, processedScriptData)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
val refineResults = scriptCompilationConfiguration.refineWith(
|
||||||
|
scriptCompilationConfiguration[ScriptCompilationConfiguration.refineConfigurationOnAnnotations]?.handler,
|
||||||
|
scriptContents, processedScriptData
|
||||||
|
).onSuccess {
|
||||||
|
it.refineWith(
|
||||||
|
scriptCompilationConfiguration[ScriptCompilationConfiguration.refineConfigurationBeforeCompiling]?.handler,
|
||||||
|
scriptContents, processedScriptData
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
val refinedConfiguration = when (refineResults) {
|
val refinedConfiguration = when (refineResults) {
|
||||||
is ResultWithDiagnostics.Failure ->
|
is ResultWithDiagnostics.Failure ->
|
||||||
return DependenciesResolver.ResolveResult.Failure(refineResults.reports.mapScriptReportsToDiagnostics())
|
return DependenciesResolver.ResolveResult.Failure(refineResults.reports.mapScriptReportsToDiagnostics())
|
||||||
@@ -98,3 +101,20 @@ internal fun ScriptContents.toScriptSource(): SourceCode = when {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal fun List<ScriptDependency>?.toClassPathOrEmpty() = this?.flatMap { (it as JvmDependency).classpath } ?: emptyList()
|
internal fun List<ScriptDependency>?.toClassPathOrEmpty() = this?.flatMap { (it as JvmDependency).classpath } ?: emptyList()
|
||||||
|
|
||||||
|
internal fun List<SourceCode>?.toFilesOrEmpty() = this?.map {
|
||||||
|
val externalSource = it as? ExternalSourceCode
|
||||||
|
externalSource?.externalLocation?.toFile()
|
||||||
|
?: throw RuntimeException("Unsupported source in requireSources parameter - only local files are supported now (${externalSource?.externalLocation})")
|
||||||
|
} ?: emptyList()
|
||||||
|
|
||||||
|
fun ScriptCompilationConfiguration.refineWith(
|
||||||
|
handler: RefineScriptCompilationConfigurationHandler?, scriptContents: ScriptContents, processedScriptData: ScriptCollectedData
|
||||||
|
): ResultWithDiagnostics<ScriptCompilationConfiguration> {
|
||||||
|
|
||||||
|
if (handler == null) return this.asSuccess()
|
||||||
|
|
||||||
|
return handler(
|
||||||
|
ScriptConfigurationRefinementContext(scriptContents.toScriptSource(), this, processedScriptData)
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user