use of org.jetbrains.kotlin.js.facade.K2JSTranslator in project kotlin by JetBrains.
the class K2JSCompiler method doExecute.
@NotNull
@Override
protected ExitCode doExecute(@NotNull K2JSCompilerArguments arguments, @NotNull CompilerConfiguration configuration, @NotNull Disposable rootDisposable) {
final MessageCollector messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY);
if (arguments.freeArgs.isEmpty()) {
if (arguments.version) {
return OK;
}
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify at least one source file or directory", NO_LOCATION);
return COMPILATION_ERROR;
}
ContentRootsKt.addKotlinSourceRoots(configuration, arguments.freeArgs);
KotlinCoreEnvironment environmentForJS = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JS_CONFIG_FILES);
Project project = environmentForJS.getProject();
List<KtFile> sourcesFiles = environmentForJS.getSourceFiles();
environmentForJS.getConfiguration().put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage);
if (!checkKotlinPackageUsage(environmentForJS, sourcesFiles))
return ExitCode.COMPILATION_ERROR;
if (arguments.outputFile == null) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify output file via -output", CompilerMessageLocation.NO_LOCATION);
return ExitCode.COMPILATION_ERROR;
}
if (messageCollector.hasErrors()) {
return ExitCode.COMPILATION_ERROR;
}
if (sourcesFiles.isEmpty()) {
messageCollector.report(CompilerMessageSeverity.ERROR, "No source files", CompilerMessageLocation.NO_LOCATION);
return COMPILATION_ERROR;
}
if (arguments.verbose) {
reportCompiledSourcesList(messageCollector, sourcesFiles);
}
File outputFile = new File(arguments.outputFile);
configuration.put(CommonConfigurationKeys.MODULE_NAME, FileUtil.getNameWithoutExtension(outputFile));
JsConfig config = new LibrarySourcesConfig(project, configuration);
if (config.checkLibFilesAndReportErrors(new JsConfig.Reporter() {
@Override
public void error(@NotNull String message) {
messageCollector.report(CompilerMessageSeverity.ERROR, message, CompilerMessageLocation.NO_LOCATION);
}
@Override
public void warning(@NotNull String message) {
messageCollector.report(CompilerMessageSeverity.STRONG_WARNING, message, CompilerMessageLocation.NO_LOCATION);
}
})) {
return COMPILATION_ERROR;
}
AnalyzerWithCompilerReport analyzerWithCompilerReport = analyzeAndReportErrors(messageCollector, sourcesFiles, config);
if (analyzerWithCompilerReport.hasErrors()) {
return COMPILATION_ERROR;
}
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
AnalysisResult analysisResult = analyzerWithCompilerReport.getAnalysisResult();
assert analysisResult instanceof JsAnalysisResult : "analysisResult should be instance of JsAnalysisResult, but " + analysisResult;
JsAnalysisResult jsAnalysisResult = (JsAnalysisResult) analysisResult;
File outputPrefixFile = null;
if (arguments.outputPrefix != null) {
outputPrefixFile = new File(arguments.outputPrefix);
if (!outputPrefixFile.exists()) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Output prefix file '" + arguments.outputPrefix + "' not found", CompilerMessageLocation.NO_LOCATION);
return ExitCode.COMPILATION_ERROR;
}
}
File outputPostfixFile = null;
if (arguments.outputPostfix != null) {
outputPostfixFile = new File(arguments.outputPostfix);
if (!outputPostfixFile.exists()) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Output postfix file '" + arguments.outputPostfix + "' not found", CompilerMessageLocation.NO_LOCATION);
return ExitCode.COMPILATION_ERROR;
}
}
MainCallParameters mainCallParameters = createMainCallParameters(arguments.main);
TranslationResult translationResult;
K2JSTranslator translator = new K2JSTranslator(config);
try {
//noinspection unchecked
translationResult = translator.translate(sourcesFiles, mainCallParameters, jsAnalysisResult);
} catch (Exception e) {
throw ExceptionUtilsKt.rethrow(e);
}
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
AnalyzerWithCompilerReport.Companion.reportDiagnostics(translationResult.getDiagnostics(), messageCollector);
if (!(translationResult instanceof TranslationResult.Success))
return ExitCode.COMPILATION_ERROR;
TranslationResult.Success successResult = (TranslationResult.Success) translationResult;
OutputFileCollection outputFiles = successResult.getOutputFiles(outputFile, outputPrefixFile, outputPostfixFile);
if (outputFile.isDirectory()) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Cannot open output file '" + outputFile.getPath() + "': is a directory", CompilerMessageLocation.NO_LOCATION);
return ExitCode.COMPILATION_ERROR;
}
File outputDir = outputFile.getParentFile();
if (outputDir == null) {
outputDir = outputFile.getAbsoluteFile().getParentFile();
}
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
OutputUtilsKt.writeAll(outputFiles, outputDir, messageCollector);
return OK;
}
use of org.jetbrains.kotlin.js.facade.K2JSTranslator in project kotlin by JetBrains.
the class BasicTest method translateFiles.
private void translateFiles(@NotNull List<KtFile> jetFiles, @NotNull File outputFile, @NotNull MainCallParameters mainCallParameters, @NotNull JsConfig config) throws Exception {
K2JSTranslator translator = new K2JSTranslator(config);
TranslationResult translationResult = translator.translate(jetFiles, mainCallParameters);
if (!(translationResult instanceof TranslationResult.Success)) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintingMessageCollector collector = new PrintingMessageCollector(new PrintStream(outputStream), MessageRenderer.PLAIN_FULL_PATHS, true);
AnalyzerWithCompilerReport.Companion.reportDiagnostics(translationResult.getDiagnostics(), collector);
String messages = new String(outputStream.toByteArray(), "UTF-8");
throw new AssertionError("The following errors occurred compiling test:\n" + messages);
}
TranslationResult.Success successResult = (TranslationResult.Success) translationResult;
OutputFileCollection outputFiles = successResult.getOutputFiles(outputFile, getOutputPrefixFile(), getOutputPostfixFile());
File outputDir = outputFile.getParentFile();
assert outputDir != null : "Parent file for output file should not be null, outputFilePath: " + outputFile.getPath();
OutputUtilsKt.writeAllTo(outputFiles, outputDir);
processJsProgram(successResult.getProgram(), jetFiles);
}
Aggregations