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
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)
if (scriptFile.isDirectory || !scriptDefinitionProvider.isScript(scriptFile.name)) {
val extensionHint =
@@ -36,37 +36,39 @@ fun collectScriptsCompilationDependencies(
var remainingSources = initialSources
val knownSourcePaths = initialSources.mapNotNullTo(HashSet()) { it.virtualFile?.path }
val importsProvider = ScriptDependenciesProvider.getInstance(project)
while (true) {
val newRemainingSources = ArrayList<KtFile>()
for (source in remainingSources) {
val dependencies = importsProvider.getScriptDependencies(source)
if (dependencies != null) {
collectedClassPath.addAll(dependencies.classpath)
if (importsProvider != null) {
while (true) {
val newRemainingSources = ArrayList<KtFile>()
for (source in remainingSources) {
val dependencies = importsProvider.getScriptDependencies(source)
if (dependencies != null) {
collectedClassPath.addAll(dependencies.classpath)
val sourceDependenciesRoots = dependencies.scripts.map {
KotlinSourceRoot(it.path, false)
}
val sourceDependencies =
KotlinCoreEnvironment.createSourceFilesFromSourceRoots(
configuration, project, sourceDependenciesRoots,
// TODO: consider receiving and using precise location from the resolver in the future
source.virtualFile?.path?.let { CompilerMessageLocation.create(it) }
)
if (sourceDependencies.isNotEmpty()) {
collectedSourceDependencies.add(ScriptsCompilationDependencies.SourceDependencies(source, sourceDependencies))
val sourceDependenciesRoots = dependencies.scripts.map {
KotlinSourceRoot(it.path, false)
}
val sourceDependencies =
KotlinCoreEnvironment.createSourceFilesFromSourceRoots(
configuration, project, sourceDependenciesRoots,
// TODO: consider receiving and using precise location from the resolver in the future
source.virtualFile?.path?.let { CompilerMessageLocation.create(it) }
)
if (sourceDependencies.isNotEmpty()) {
collectedSourceDependencies.add(ScriptsCompilationDependencies.SourceDependencies(source, sourceDependencies))
val newSources = sourceDependencies.filterNot { knownSourcePaths.contains(it.virtualFile.path) }
for (newSource in newSources) {
collectedSources.add(newSource)
newRemainingSources.add(newSource)
knownSourcePaths.add(newSource.virtualFile.path)
val newSources = sourceDependencies.filterNot { knownSourcePaths.contains(it.virtualFile.path) }
for (newSource in newSources) {
collectedSources.add(newSource)
newRemainingSources.add(newSource)
knownSourcePaths.add(newSource.virtualFile.path)
}
}
}
}
}
if (newRemainingSources.isEmpty()) break
else {
remainingSources = newRemainingSources
if (newRemainingSources.isEmpty()) break
else {
remainingSources = newRemainingSources
}
}
}
return ScriptsCompilationDependencies(
@@ -73,7 +73,7 @@ open class GenericReplCompiler(
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
if (compilerState.lastDependencies != newDependencies) {
compilerState.lastDependencies = newDependencies
@@ -179,7 +179,7 @@ class FileScopeFactory(
private fun createDefaultImportResolversForFile(): DefaultImportResolvers {
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)) }
}.orEmpty()
@@ -178,7 +178,7 @@ class LazyScriptDescriptor(
val res = ArrayList<ClassDescriptor>()
val importedScriptsFiles = ScriptDependenciesProvider.getInstance(scriptInfo.script.project)
.getScriptDependencies(scriptInfo.script.containingKtFile)?.scripts
?.getScriptDependencies(scriptInfo.script.containingKtFile)?.scripts
if (importedScriptsFiles != null) {
val findImportedScriptDescriptor = ImportedScriptDescriptorsFinder()
importedScriptsFiles.mapNotNullTo(res) {
@@ -30,6 +30,9 @@ import java.util.List;
public class KtScript extends KtNamedDeclarationStub<KotlinScriptStub> implements KtDeclarationContainer {
public KotlinScriptDefinition getKotlinScriptDefinition() {
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());
return definition != null ? definition : definitionsProvider.getDefaultScriptDefinition();
}
@@ -37,7 +37,7 @@ interface ScriptDefinitionProvider {
fun getKnownFilenameExtensions(): Sequence<String>
companion object {
fun getInstance(project: Project): ScriptDefinitionProvider =
fun getInstance(project: Project): ScriptDefinitionProvider? =
ServiceManager.getService(project, ScriptDefinitionProvider::class.java)
}
}
@@ -51,7 +51,7 @@ fun findScriptDefinition(file: VirtualFile, project: Project): KotlinScriptDefin
return null
}
val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(project)
val scriptDefinitionProvider = ScriptDefinitionProvider.getInstance(project) ?: return null
val psiFile = PsiManager.getInstance(project).findFile(file)
if (psiFile != null) {
if (psiFile !is KtFile) return null
@@ -27,7 +27,7 @@ interface ScriptDependenciesProvider {
fun getScriptDependencies(file: PsiFile) = getScriptDependencies(file.virtualFile ?: file.originalFile.virtualFile)
companion object {
fun getInstance(project: Project): ScriptDependenciesProvider =
fun getInstance(project: Project): ScriptDependenciesProvider? =
ServiceManager.getService(project, ScriptDependenciesProvider::class.java)
}
}
@@ -439,14 +439,16 @@ public abstract class CodegenTestCase extends KtUsefulTestCase {
ScriptDependenciesProvider externalImportsProvider =
ScriptDependenciesProvider.Companion.getInstance(myEnvironment.getProject());
myEnvironment.getSourceFiles().forEach(
file -> {
ScriptDependencies dependencies = externalImportsProvider.getScriptDependencies(file);
if (dependencies != null) {
files.addAll(dependencies.getClasspath());
if (externalImportsProvider != null) {
myEnvironment.getSourceFiles().forEach(
file -> {
ScriptDependencies dependencies = externalImportsProvider.getScriptDependencies(file);
if (dependencies != null) {
files.addAll(dependencies.getClasspath());
}
}
}
);
);
}
try {
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 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) }
}.orEmpty()