Make script infrastructure providers optional

This commit is contained in:
Ilya Chernikov
2019-02-04 14:49:51 +01:00
parent 0732b48701
commit 89fc7eba95
10 changed files with 51 additions and 40 deletions
@@ -139,6 +139,10 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
?: return COMPILATION_ERROR ?: return COMPILATION_ERROR
val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(environment.project) val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(environment.project)
if (scriptDefinitionProvider == null) {
messageCollector.report(ERROR, "Unable to process the script, scripting plugin is not configured")
return COMPILATION_ERROR
}
val scriptFile = File(sourcePath) val scriptFile = File(sourcePath)
if (scriptFile.isDirectory || !scriptDefinitionProvider.isScript(scriptFile.name)) { if (scriptFile.isDirectory || !scriptDefinitionProvider.isScript(scriptFile.name)) {
val extensionHint = val extensionHint =
@@ -36,37 +36,39 @@ fun collectScriptsCompilationDependencies(
var remainingSources = initialSources var remainingSources = initialSources
val knownSourcePaths = initialSources.mapNotNullTo(HashSet()) { it.virtualFile?.path } val knownSourcePaths = initialSources.mapNotNullTo(HashSet()) { it.virtualFile?.path }
val importsProvider = ScriptDependenciesProvider.getInstance(project) val importsProvider = ScriptDependenciesProvider.getInstance(project)
while (true) { if (importsProvider != null) {
val newRemainingSources = ArrayList<KtFile>() while (true) {
for (source in remainingSources) { val newRemainingSources = ArrayList<KtFile>()
val dependencies = importsProvider.getScriptDependencies(source) for (source in remainingSources) {
if (dependencies != null) { val dependencies = importsProvider.getScriptDependencies(source)
collectedClassPath.addAll(dependencies.classpath) if (dependencies != null) {
collectedClassPath.addAll(dependencies.classpath)
val sourceDependenciesRoots = dependencies.scripts.map { val sourceDependenciesRoots = dependencies.scripts.map {
KotlinSourceRoot(it.path, false) KotlinSourceRoot(it.path, false)
} }
val sourceDependencies = val sourceDependencies =
KotlinCoreEnvironment.createSourceFilesFromSourceRoots( KotlinCoreEnvironment.createSourceFilesFromSourceRoots(
configuration, project, sourceDependenciesRoots, configuration, project, sourceDependenciesRoots,
// TODO: consider receiving and using precise location from the resolver in the future // TODO: consider receiving and using precise location from the resolver in the future
source.virtualFile?.path?.let { CompilerMessageLocation.create(it) } source.virtualFile?.path?.let { CompilerMessageLocation.create(it) }
) )
if (sourceDependencies.isNotEmpty()) { if (sourceDependencies.isNotEmpty()) {
collectedSourceDependencies.add(ScriptsCompilationDependencies.SourceDependencies(source, sourceDependencies)) collectedSourceDependencies.add(ScriptsCompilationDependencies.SourceDependencies(source, sourceDependencies))
val newSources = sourceDependencies.filterNot { knownSourcePaths.contains(it.virtualFile.path) } val newSources = sourceDependencies.filterNot { knownSourcePaths.contains(it.virtualFile.path) }
for (newSource in newSources) { for (newSource in newSources) {
collectedSources.add(newSource) collectedSources.add(newSource)
newRemainingSources.add(newSource) newRemainingSources.add(newSource)
knownSourcePaths.add(newSource.virtualFile.path) knownSourcePaths.add(newSource.virtualFile.path)
}
} }
} }
} }
} if (newRemainingSources.isEmpty()) break
if (newRemainingSources.isEmpty()) break else {
else { remainingSources = newRemainingSources
remainingSources = newRemainingSources }
} }
} }
return ScriptsCompilationDependencies( return ScriptsCompilationDependencies(
@@ -73,7 +73,7 @@ open class GenericReplCompiler(
Pair(compilerState.lastLineState!!.psiFile, compilerState.lastLineState!!.errorHolder) Pair(compilerState.lastLineState!!.psiFile, compilerState.lastLineState!!.errorHolder)
} }
val newDependencies = ScriptDependenciesProvider.getInstance(checker.environment.project).getScriptDependencies(psiFile) val newDependencies = ScriptDependenciesProvider.getInstance(checker.environment.project)?.getScriptDependencies(psiFile)
var classpathAddendum: List<File>? = null var classpathAddendum: List<File>? = null
if (compilerState.lastDependencies != newDependencies) { if (compilerState.lastDependencies != newDependencies) {
compilerState.lastDependencies = newDependencies compilerState.lastDependencies = newDependencies
@@ -179,7 +179,7 @@ class FileScopeFactory(
private fun createDefaultImportResolversForFile(): DefaultImportResolvers { private fun createDefaultImportResolversForFile(): DefaultImportResolvers {
val extraImports = file.takeIf { it.isScript() }?.let { ktFile -> val extraImports = file.takeIf { it.isScript() }?.let { ktFile ->
val scriptDependencies = ScriptDependenciesProvider.getInstance(ktFile.project).getScriptDependencies(ktFile.originalFile) val scriptDependencies = ScriptDependenciesProvider.getInstance(ktFile.project)?.getScriptDependencies(ktFile.originalFile)
scriptDependencies?.imports?.map { DefaultImportImpl(ImportPath.fromString(it)) } scriptDependencies?.imports?.map { DefaultImportImpl(ImportPath.fromString(it)) }
}.orEmpty() }.orEmpty()
@@ -178,7 +178,7 @@ class LazyScriptDescriptor(
val res = ArrayList<ClassDescriptor>() val res = ArrayList<ClassDescriptor>()
val importedScriptsFiles = ScriptDependenciesProvider.getInstance(scriptInfo.script.project) val importedScriptsFiles = ScriptDependenciesProvider.getInstance(scriptInfo.script.project)
.getScriptDependencies(scriptInfo.script.containingKtFile)?.scripts ?.getScriptDependencies(scriptInfo.script.containingKtFile)?.scripts
if (importedScriptsFiles != null) { if (importedScriptsFiles != null) {
val findImportedScriptDescriptor = ImportedScriptDescriptorsFinder() val findImportedScriptDescriptor = ImportedScriptDescriptorsFinder()
importedScriptsFiles.mapNotNullTo(res) { importedScriptsFiles.mapNotNullTo(res) {
@@ -30,6 +30,9 @@ import java.util.List;
public class KtScript extends KtNamedDeclarationStub<KotlinScriptStub> implements KtDeclarationContainer { public class KtScript extends KtNamedDeclarationStub<KotlinScriptStub> implements KtDeclarationContainer {
public KotlinScriptDefinition getKotlinScriptDefinition() { public KotlinScriptDefinition getKotlinScriptDefinition() {
ScriptDefinitionProvider definitionsProvider = ScriptDefinitionProvider.Companion.getInstance(getProject()); ScriptDefinitionProvider definitionsProvider = ScriptDefinitionProvider.Companion.getInstance(getProject());
if (definitionsProvider == null) {
throw new IllegalStateException("Unable to use KtScript: ScriptDefinitionProvider is not configured.");
}
KotlinScriptDefinition definition = definitionsProvider.findScriptDefinition(getContainingKtFile().getName()); KotlinScriptDefinition definition = definitionsProvider.findScriptDefinition(getContainingKtFile().getName());
return definition != null ? definition : definitionsProvider.getDefaultScriptDefinition(); return definition != null ? definition : definitionsProvider.getDefaultScriptDefinition();
} }
@@ -37,7 +37,7 @@ interface ScriptDefinitionProvider {
fun getKnownFilenameExtensions(): Sequence<String> fun getKnownFilenameExtensions(): Sequence<String>
companion object { companion object {
fun getInstance(project: Project): ScriptDefinitionProvider = fun getInstance(project: Project): ScriptDefinitionProvider? =
ServiceManager.getService(project, ScriptDefinitionProvider::class.java) ServiceManager.getService(project, ScriptDefinitionProvider::class.java)
} }
} }
@@ -51,7 +51,7 @@ fun findScriptDefinition(file: VirtualFile, project: Project): KotlinScriptDefin
return null return null
} }
val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(project) val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(project) ?: return null
val psiFile = PsiManager.getInstance(project).findFile(file) val psiFile = PsiManager.getInstance(project).findFile(file)
if (psiFile != null) { if (psiFile != null) {
if (psiFile !is KtFile) return null if (psiFile !is KtFile) return null
@@ -27,7 +27,7 @@ interface ScriptDependenciesProvider {
fun getScriptDependencies(file: PsiFile) = getScriptDependencies(file.virtualFile ?: file.originalFile.virtualFile) fun getScriptDependencies(file: PsiFile) = getScriptDependencies(file.virtualFile ?: file.originalFile.virtualFile)
companion object { companion object {
fun getInstance(project: Project): ScriptDependenciesProvider = fun getInstance(project: Project): ScriptDependenciesProvider? =
ServiceManager.getService(project, ScriptDependenciesProvider::class.java) ServiceManager.getService(project, ScriptDependenciesProvider::class.java)
} }
} }
@@ -439,14 +439,16 @@ public abstract class CodegenTestCase extends KtUsefulTestCase {
ScriptDependenciesProvider externalImportsProvider = ScriptDependenciesProvider externalImportsProvider =
ScriptDependenciesProvider.Companion.getInstance(myEnvironment.getProject()); ScriptDependenciesProvider.Companion.getInstance(myEnvironment.getProject());
myEnvironment.getSourceFiles().forEach( if (externalImportsProvider != null) {
file -> { myEnvironment.getSourceFiles().forEach(
ScriptDependencies dependencies = externalImportsProvider.getScriptDependencies(file); file -> {
if (dependencies != null) { ScriptDependencies dependencies = externalImportsProvider.getScriptDependencies(file);
files.addAll(dependencies.getClasspath()); if (dependencies != null) {
files.addAll(dependencies.getClasspath());
}
} }
} );
); }
try { try {
URL[] result = new URL[files.size()]; URL[] result = new URL[files.size()];
@@ -65,7 +65,7 @@ class ImportInsertHelperImpl(private val project: Project) : ImportInsertHelper(
val allDefaultImports = platform.getDefaultImports(languageVersionSettings, includeLowPriorityImports = true) val allDefaultImports = platform.getDefaultImports(languageVersionSettings, includeLowPriorityImports = true)
val scriptExtraImports = contextFile.takeIf { it.isScript() }?.let { ktFile -> val scriptExtraImports = contextFile.takeIf { it.isScript() }?.let { ktFile ->
val scriptDependencies = ScriptDependenciesProvider.getInstance(ktFile.project).getScriptDependencies(ktFile.originalFile) val scriptDependencies = ScriptDependenciesProvider.getInstance(ktFile.project)?.getScriptDependencies(ktFile.originalFile)
scriptDependencies?.imports?.map { ImportPath.fromString(it) } scriptDependencies?.imports?.map { ImportPath.fromString(it) }
}.orEmpty() }.orEmpty()