@@ -86,38 +86,9 @@ private class CompilationEnvironment(
86
86
configuration = KotlinCompilerConfiguration ().apply {
87
87
put(CommonConfigurationKeys .MODULE_NAME , JvmProtoBufUtil .DEFAULT_MODULE_NAME )
88
88
put(CLIConfigurationKeys .MESSAGE_COLLECTOR_KEY , LoggingMessageCollector )
89
- addJvmClasspathRoots(classPath.map { it.toFile() })
90
- // Setup script templates
91
- var scriptDefinitions: List <ScriptDefinition > = listOf ()
92
-
93
- if (classPath.any { GRADLE_DSL_DEPENDENCY_PATTERN .matches(it.fileName.toString()) }) {
94
- LOG .info(" Configuring Kotlin DSL script templates..." )
95
-
96
- val scriptTemplates = listOf (
97
- // "org.gradle.kotlin.dsl.KotlinInitScript",
98
- // "org.gradle.kotlin.dsl.KotlinSettingsScript",
99
- " org.gradle.kotlin.dsl.KotlinBuildScript"
100
- )
101
-
102
- try {
103
- // Load template classes
104
- val scriptClassLoader = URLClassLoader (classPath.map { it.toUri().toURL() }.toTypedArray())
105
- val fileClassPath = classPath.map { it.toFile() }
106
- val scriptHostConfig = ScriptingHostConfiguration (defaultJvmScriptingHostConfiguration) {
107
- configurationDependencies(JvmDependency (fileClassPath))
108
- }
109
- scriptDefinitions = scriptTemplates
110
- .map { object : ScriptDefinition .FromLegacy (defaultJvmScriptingHostConfiguration, KotlinScriptDefinition (scriptClassLoader.loadClass(it).kotlin)) {
111
- override val isDefault = true
112
- } }
113
- } catch (e: Exception ) {
114
- LOG .error(" Error while loading script template classes" )
115
- LOG .printStackTrace(e)
116
- }
117
- }
118
- addAll(ScriptingConfigurationKeys .SCRIPT_DEFINITIONS , scriptDefinitions)
119
- put(JVMConfigurationKeys .DISABLE_STANDARD_SCRIPT_DEFINITION , true )
120
89
add(ComponentRegistrar .PLUGIN_COMPONENT_REGISTRARS , ScriptingCompilerConfigurationComponentRegistrar ())
90
+ addJvmClasspathRoots(classPath.map { it.toFile() })
91
+ // addAll(ScriptingConfigurationKeys.SCRIPT_DEFINITIONS, scriptDefinitions)
121
92
},
122
93
configFiles = EnvironmentConfigFiles .JVM_CONFIG_FILES
123
94
)
@@ -129,6 +100,39 @@ private class CompilationEnvironment(
129
100
130
101
parser = KtPsiFactory (project)
131
102
scripts = ScriptDefinitionProvider .getInstance(project)!! as CliScriptDefinitionProvider
103
+
104
+ // Setup script templates
105
+ var scriptDefinitions: List <ScriptDefinition > = listOf ()
106
+
107
+ if (classPath.any { GRADLE_DSL_DEPENDENCY_PATTERN .matches(it.fileName.toString()) }) {
108
+ LOG .info(" Configuring Kotlin DSL script templates..." )
109
+
110
+ val scriptTemplates = listOf (
111
+ // "org.gradle.kotlin.dsl.KotlinInitScript",
112
+ // "org.gradle.kotlin.dsl.KotlinSettingsScript",
113
+ " org.gradle.kotlin.dsl.KotlinBuildScript"
114
+ )
115
+
116
+ try {
117
+ // Load template classes
118
+ val scriptClassLoader = URLClassLoader (classPath.map { it.toUri().toURL() }.toTypedArray())
119
+ val fileClassPath = classPath.map { it.toFile() }
120
+ val scriptHostConfig = ScriptingHostConfiguration (defaultJvmScriptingHostConfiguration) {
121
+ configurationDependencies(JvmDependency (fileClassPath))
122
+ }
123
+ scriptDefinitions = scriptTemplates
124
+ .map { object : ScriptDefinition .FromLegacy (defaultJvmScriptingHostConfiguration, KotlinScriptDefinition (scriptClassLoader.loadClass(it).kotlin)) {
125
+ override val isDefault = true
126
+ } }
127
+ } catch (e: Exception ) {
128
+ LOG .error(" Error while loading script template classes" )
129
+ LOG .printStackTrace(e)
130
+ }
131
+ }
132
+
133
+ LOG .info(" Definitions: ${scriptDefinitions.map { it?.legacyDefinition?.template?.simpleName }} " )
134
+ scripts.setScriptDefinitions(scriptDefinitions)
135
+ LOG .info(" Default def: ${scripts.getDefaultDefinition()?.legacyDefinition?.template?.simpleName} " )
132
136
}
133
137
134
138
fun updateConfiguration (config : CompilerConfiguration ) {
@@ -189,9 +193,6 @@ class Compiler(classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet(
189
193
private val buildScriptCompileEnvironment = buildScriptClassPath.takeIf { it.isNotEmpty() }?.let (::CompilationEnvironment )
190
194
private val compileLock = ReentrantLock () // TODO: Lock at file-level
191
195
192
- val psiFileFactory
193
- get() = PsiFileFactory .getInstance(defaultCompileEnvironment.environment.project)
194
-
195
196
companion object {
196
197
init {
197
198
setIdeaIoUseFallback()
@@ -211,26 +212,26 @@ class Compiler(classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet(
211
212
buildScriptCompileEnvironment?.updateConfiguration(config)
212
213
}
213
214
214
- fun createFile (content : String , file : Path = Paths .get("dummy.virtual.kt")): KtFile {
215
+ fun createFile (content : String , file : Path = Paths .get("dummy.virtual.kt"), kind : CompilationKind = CompilationKind . DEFAULT ): KtFile {
215
216
assert (! content.contains(' \r ' ))
216
217
217
- val new = psiFileFactory .createFileFromText(file.toString(), KotlinLanguage .INSTANCE , content, true , false ) as KtFile
218
+ val new = psiFileFactoryFor(kind) .createFileFromText(file.toString(), KotlinLanguage .INSTANCE , content, true , false ) as KtFile
218
219
assert (new.virtualFile != null )
219
220
220
221
return new
221
222
}
222
223
223
- fun createExpression (content : String , file : Path = Paths .get("dummy.virtual.kt")): KtExpression {
224
- val property = parseDeclaration(" val x = $content " , file) as KtProperty
224
+ fun createExpression (content : String , file : Path = Paths .get("dummy.virtual.kt"), kind : CompilationKind = CompilationKind . DEFAULT ): KtExpression {
225
+ val property = parseDeclaration(" val x = $content " , file, kind ) as KtProperty
225
226
226
227
return property.initializer!!
227
228
}
228
229
229
- fun createDeclaration (content : String , file : Path = Paths .get("dummy.virtual.kt")): KtDeclaration =
230
- parseDeclaration(content, file)
230
+ fun createDeclaration (content : String , file : Path = Paths .get("dummy.virtual.kt"), kind : CompilationKind = CompilationKind . DEFAULT ): KtDeclaration =
231
+ parseDeclaration(content, file, kind )
231
232
232
- private fun parseDeclaration (content : String , file : Path ): KtDeclaration {
233
- val parse = createFile(content, file)
233
+ private fun parseDeclaration (content : String , file : Path , kind : CompilationKind = CompilationKind . DEFAULT ): KtDeclaration {
234
+ val parse = createFile(content, file, kind )
234
235
val declarations = parse.declarations
235
236
236
237
assert (declarations.size == 1 ) { " ${declarations.size} declarations in $content " }
@@ -252,11 +253,14 @@ class Compiler(classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet(
252
253
CompilationKind .BUILD_SCRIPT -> buildScriptCompileEnvironment ? : defaultCompileEnvironment
253
254
}
254
255
256
+ fun psiFileFactoryFor (kind : CompilationKind ): PsiFileFactory =
257
+ PsiFileFactory .getInstance(compileEnvironmentFor(kind).environment.project)
258
+
255
259
fun compileFile (file : KtFile , sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind .DEFAULT ): Pair <BindingContext , ComponentProvider > =
256
- compileFiles(listOf (file), sourcePath, kind)
260
+ compileFiles(listOf (file), sourcePath, kind)
257
261
258
262
fun compileFiles (files : Collection <KtFile >, sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind .DEFAULT ): Pair <BindingContext , ComponentProvider > {
259
- files.forEach { LOG .info(" $it -> ScriptDefinition: ${it.findScriptDefinition()?.legacyDefinition?.template?.simpleName} " ) }
263
+ files.forEach { LOG .info(" $it -> ScriptDefinition: ${it.findScriptDefinition()?.legacyDefinition?.template?.simpleName} , provider: ${( ScriptDefinitionProvider .getInstance(buildScriptCompileEnvironment?.environment?.project !! ) as CliScriptDefinitionProvider ).getDefaultDefinition()?.legacyDefinition?.template?.simpleName} " ) }
260
264
compileLock.withLock {
261
265
val (container, trace) = compileEnvironmentFor(kind).createContainer(sourcePath)
262
266
val topDownAnalyzer = container.get<LazyTopDownAnalyzer >()
0 commit comments