Search in sources :

Example 1 with MessageCollector

use of org.jetbrains.kotlin.cli.common.messages.MessageCollector 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;
}
Also used : MainCallParameters(org.jetbrains.kotlin.js.facade.MainCallParameters) JsConfig(org.jetbrains.kotlin.js.config.JsConfig) AnalyzerWithCompilerReport(org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport) K2JSTranslator(org.jetbrains.kotlin.js.facade.K2JSTranslator) LibrarySourcesConfig(org.jetbrains.kotlin.js.config.LibrarySourcesConfig) JsAnalysisResult(org.jetbrains.kotlin.js.analyzer.JsAnalysisResult) AnalysisResult(org.jetbrains.kotlin.analyzer.AnalysisResult) Project(com.intellij.openapi.project.Project) JsAnalysisResult(org.jetbrains.kotlin.js.analyzer.JsAnalysisResult) OutputFileCollection(org.jetbrains.kotlin.backend.common.output.OutputFileCollection) KotlinCoreEnvironment(org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment) MessageCollector(org.jetbrains.kotlin.cli.common.messages.MessageCollector) KtFile(org.jetbrains.kotlin.psi.KtFile) TranslationResult(org.jetbrains.kotlin.js.facade.TranslationResult) VirtualFile(com.intellij.openapi.vfs.VirtualFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with MessageCollector

use of org.jetbrains.kotlin.cli.common.messages.MessageCollector in project kotlin by JetBrains.

the class K2JSCompiler method setupPlatformSpecificArgumentsAndServices.

@Override
protected void setupPlatformSpecificArgumentsAndServices(@NotNull CompilerConfiguration configuration, @NotNull K2JSCompilerArguments arguments, @NotNull Services services) {
    MessageCollector messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY);
    if (arguments.target != null) {
        assert arguments.target == "v5" : "Unsupported ECMA version: " + arguments.target;
    }
    configuration.put(JSConfigurationKeys.TARGET, EcmaVersion.defaultVersion());
    if (arguments.sourceMap) {
        configuration.put(JSConfigurationKeys.SOURCE_MAP, true);
    }
    if (arguments.metaInfo) {
        configuration.put(JSConfigurationKeys.META_INFO, true);
    }
    List<String> libraries = new SmartList<String>();
    if (!arguments.noStdlib) {
        libraries.add(0, PathUtil.getKotlinPathsForCompiler().getJsStdLibJarPath().getAbsolutePath());
    }
    if (arguments.libraries != null) {
        ContainerUtil.addAllNotNull(libraries, arguments.libraries.split(File.pathSeparator));
    }
    configuration.put(JSConfigurationKeys.LIBRARIES, libraries);
    String moduleKindName = arguments.moduleKind;
    ModuleKind moduleKind = moduleKindName != null ? moduleKindMap.get(moduleKindName) : ModuleKind.PLAIN;
    if (moduleKind == null) {
        messageCollector.report(CompilerMessageSeverity.ERROR, "Unknown module kind: " + moduleKindName + ". " + "Valid values are: plain, amd, commonjs, umd", CompilerMessageLocation.NO_LOCATION);
    }
    configuration.put(JSConfigurationKeys.MODULE_KIND, moduleKind);
}
Also used : ModuleKind(org.jetbrains.kotlin.serialization.js.ModuleKind) MessageCollector(org.jetbrains.kotlin.cli.common.messages.MessageCollector) SmartList(com.intellij.util.SmartList)

Example 3 with MessageCollector

use of org.jetbrains.kotlin.cli.common.messages.MessageCollector in project kotlin by JetBrains.

the class AbstractModuleXmlParserTest method doTest.

protected static void doTest(String xmlPath) throws IOException {
    File txtFile = new File(FileUtil.getNameWithoutExtension(xmlPath) + ".txt");
    ModuleScriptData result = ModuleXmlParser.parseModuleScript(xmlPath, new MessageCollector() {

        @Override
        public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location) {
            throw new AssertionError(MessageRenderer.PLAIN_FULL_PATHS.render(severity, message, location));
        }

        @Override
        public void clear() {
        // Do nothing
        }

        @Override
        public boolean hasErrors() {
            throw new UnsupportedOperationException();
        }
    });
    StringBuilder sb = new StringBuilder();
    for (Module module : result.getModules()) {
        sb.append(moduleToString(module)).append("\n");
    }
    String actual = sb.toString();
    if (!txtFile.exists()) {
        FileUtil.writeToFile(txtFile, actual);
        fail("Expected data file does not exist. A new file created: " + txtFile);
    }
    KotlinTestUtils.assertEqualsToFile(txtFile, actual);
}
Also used : ModuleScriptData(org.jetbrains.kotlin.cli.common.modules.ModuleScriptData) CompilerMessageSeverity(org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity) MessageCollector(org.jetbrains.kotlin.cli.common.messages.MessageCollector) Module(org.jetbrains.kotlin.modules.Module) File(java.io.File) CompilerMessageLocation(org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation)

Aggregations

MessageCollector (org.jetbrains.kotlin.cli.common.messages.MessageCollector)3 File (java.io.File)2 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 SmartList (com.intellij.util.SmartList)1 NotNull (org.jetbrains.annotations.NotNull)1 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)1 OutputFileCollection (org.jetbrains.kotlin.backend.common.output.OutputFileCollection)1 AnalyzerWithCompilerReport (org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport)1 CompilerMessageLocation (org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation)1 CompilerMessageSeverity (org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity)1 ModuleScriptData (org.jetbrains.kotlin.cli.common.modules.ModuleScriptData)1 KotlinCoreEnvironment (org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment)1 JsAnalysisResult (org.jetbrains.kotlin.js.analyzer.JsAnalysisResult)1 JsConfig (org.jetbrains.kotlin.js.config.JsConfig)1 LibrarySourcesConfig (org.jetbrains.kotlin.js.config.LibrarySourcesConfig)1 K2JSTranslator (org.jetbrains.kotlin.js.facade.K2JSTranslator)1 MainCallParameters (org.jetbrains.kotlin.js.facade.MainCallParameters)1 TranslationResult (org.jetbrains.kotlin.js.facade.TranslationResult)1 Module (org.jetbrains.kotlin.modules.Module)1