use of org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot in project kotlin by JetBrains.
the class ExecuteKotlinScriptMojo method executeScriptFile.
private void executeScriptFile(File scriptFile) throws MojoExecutionException {
initCompiler();
Disposable rootDisposable = Disposer.newDisposable();
try {
MavenPluginLogMessageCollector messageCollector = new MavenPluginLogMessageCollector(getLog());
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
List<File> deps = new ArrayList<File>();
deps.addAll(PathUtil.getJdkClassesRoots());
deps.addAll(getDependenciesForScript());
for (File item : deps) {
if (item.exists()) {
configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, new JvmClasspathRoot(item));
getLog().debug("Adding to classpath: " + item.getAbsolutePath());
} else {
getLog().debug("Skipping non-existing dependency: " + item.getAbsolutePath());
}
}
configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, new KotlinSourceRoot(scriptFile.getAbsolutePath()));
configuration.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME);
K2JVMCompiler.Companion.configureScriptDefinitions(scriptTemplates.toArray(new String[scriptTemplates.size()]), configuration, messageCollector, new HashMap<String, Object>());
KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
GenerationState state = KotlinToJVMBytecodeCompiler.INSTANCE.analyzeAndGenerate(environment);
if (state == null) {
throw new ScriptExecutionException(scriptFile, "compile error");
}
GeneratedClassLoader classLoader = new GeneratedClassLoader(state.getFactory(), getClass().getClassLoader());
KtScript script = environment.getSourceFiles().get(0).getScript();
FqName nameForScript = script.getFqName();
try {
Class<?> klass = classLoader.loadClass(nameForScript.asString());
ExecuteKotlinScriptMojo.INSTANCE = this;
if (ReflectionUtilKt.tryConstructClassFromStringArgs(klass, scriptArguments) == null)
throw new ScriptExecutionException(scriptFile, "unable to construct script");
} catch (ClassNotFoundException e) {
throw new ScriptExecutionException(scriptFile, "internal error", e);
}
} finally {
rootDisposable.dispose();
ExecuteKotlinScriptMojo.INSTANCE = null;
}
}
Aggregations