Search in sources :

Example 1 with ConversionService

use of com.intellij.conversion.ConversionService in project intellij-community by JetBrains.

the class InspectionApplication method run.

private void run() {
    File tmpDir = null;
    try {
        myProjectPath = myProjectPath.replace(File.separatorChar, '/');
        VirtualFile vfsProject = LocalFileSystem.getInstance().findFileByPath(myProjectPath);
        if (vfsProject == null) {
            logError(InspectionsBundle.message("inspection.application.file.cannot.be.found", myProjectPath));
            printHelp();
        }
        logMessage(1, InspectionsBundle.message("inspection.application.opening.project"));
        final ConversionService conversionService = ConversionService.getInstance();
        if (conversionService.convertSilently(myProjectPath, createConversionListener()).openingIsCanceled()) {
            gracefulExit();
            return;
        }
        myProject = ProjectUtil.openOrImport(myProjectPath, null, false);
        if (myProject == null) {
            logError("Unable to open project");
            gracefulExit();
            return;
        }
        ApplicationManager.getApplication().runWriteAction(() -> VirtualFileManager.getInstance().refreshWithoutFileWatcher(false));
        PatchProjectUtil.patchProject(myProject);
        logMessageLn(1, InspectionsBundle.message("inspection.done"));
        logMessage(1, InspectionsBundle.message("inspection.application.initializing.project"));
        InspectionProfileImpl inspectionProfile = loadInspectionProfile();
        if (inspectionProfile == null)
            return;
        final InspectionManagerEx im = (InspectionManagerEx) InspectionManager.getInstance(myProject);
        im.createNewGlobalContext(true).setExternalProfile(inspectionProfile);
        im.setProfile(inspectionProfile.getName());
        final AnalysisScope scope;
        if (mySourceDirectory == null) {
            final String scopeName = System.getProperty("idea.analyze.scope");
            final NamedScope namedScope = scopeName != null ? NamedScopesHolder.getScope(myProject, scopeName) : null;
            scope = namedScope != null ? new AnalysisScope(GlobalSearchScopesCore.filterScope(myProject, namedScope), myProject) : new AnalysisScope(myProject);
        } else {
            mySourceDirectory = mySourceDirectory.replace(File.separatorChar, '/');
            VirtualFile vfsDir = LocalFileSystem.getInstance().findFileByPath(mySourceDirectory);
            if (vfsDir == null) {
                logError(InspectionsBundle.message("inspection.application.directory.cannot.be.found", mySourceDirectory));
                printHelp();
            }
            PsiDirectory psiDirectory = PsiManager.getInstance(myProject).findDirectory(vfsDir);
            scope = new AnalysisScope(psiDirectory);
        }
        logMessageLn(1, InspectionsBundle.message("inspection.done"));
        if (!myRunWithEditorSettings) {
            logMessageLn(1, InspectionsBundle.message("inspection.application.chosen.profile.log.message", inspectionProfile.getName()));
        }
        InspectionsReportConverter reportConverter = getReportConverter(myOutputFormat);
        if (reportConverter == null && myOutputFormat != null && myOutputFormat.endsWith(".xsl")) {
            // xslt converter
            reportConverter = new XSLTReportConverter(myOutputFormat);
        }
        final String resultsDataPath;
        if (// use default xml converter(if null( or don't store default xml report in tmp dir
        (reportConverter == null || !reportConverter.useTmpDirForRawData()) && myOutPath != null) {
            // and don't use STDOUT stream
            resultsDataPath = myOutPath;
        } else {
            try {
                tmpDir = FileUtil.createTempDirectory("inspections", "data");
                resultsDataPath = tmpDir.getPath();
            } catch (IOException e) {
                LOG.error(e);
                System.err.println("Cannot create tmp directory.");
                System.exit(1);
                return;
            }
        }
        final List<File> inspectionsResults = new ArrayList<>();
        ProgressManager.getInstance().runProcess(() -> {
            if (!GlobalInspectionContextUtil.canRunInspections(myProject, false)) {
                gracefulExit();
                return;
            }
            im.createNewGlobalContext(true).launchInspectionsOffline(scope, resultsDataPath, myRunGlobalToolsOnly, inspectionsResults);
            logMessageLn(1, "\n" + InspectionsBundle.message("inspection.capitalized.done") + "\n");
            if (!myErrorCodeRequired) {
                closeProject();
            }
        }, new ProgressIndicatorBase() {

            private String lastPrefix = "";

            private int myLastPercent = -1;

            @Override
            public void setText(String text) {
                if (myVerboseLevel == 0)
                    return;
                if (myVerboseLevel == 1) {
                    String prefix = getPrefix(text);
                    if (prefix == null)
                        return;
                    if (prefix.equals(lastPrefix)) {
                        logMessage(1, ".");
                        return;
                    }
                    lastPrefix = prefix;
                    logMessageLn(1, "");
                    logMessageLn(1, prefix);
                    return;
                }
                if (myVerboseLevel == 3) {
                    if (!isIndeterminate() && getFraction() > 0) {
                        final int percent = (int) (getFraction() * 100);
                        if (myLastPercent == percent)
                            return;
                        String prefix = getPrefix(text);
                        myLastPercent = percent;
                        String msg = (prefix != null ? prefix : InspectionsBundle.message("inspection.display.name")) + " " + percent + "%";
                        logMessageLn(2, msg);
                    }
                    return;
                }
                logMessageLn(2, text);
            }
        });
        final String descriptionsFile = resultsDataPath + File.separatorChar + DESCRIPTIONS + XML_EXTENSION;
        describeInspections(descriptionsFile, myRunWithEditorSettings ? null : inspectionProfile.getName(), inspectionProfile);
        inspectionsResults.add(new File(descriptionsFile));
        // convert report
        if (reportConverter != null) {
            try {
                reportConverter.convert(resultsDataPath, myOutPath, im.createNewGlobalContext(true).getTools(), inspectionsResults);
            } catch (InspectionsReportConverter.ConversionException e) {
                logError("\n" + e.getMessage());
                printHelp();
            }
        }
    } catch (IOException e) {
        LOG.error(e);
        logError(e.getMessage());
        printHelp();
    } catch (Throwable e) {
        LOG.error(e);
        logError(e.getMessage());
        gracefulExit();
    } finally {
        // delete tmp dir
        if (tmpDir != null) {
            FileUtil.delete(tmpDir);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IOException(java.io.IOException) AnalysisScope(com.intellij.analysis.AnalysisScope) NamedScope(com.intellij.psi.search.scope.packageSet.NamedScope) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) ConversionService(com.intellij.conversion.ConversionService) PsiDirectory(com.intellij.psi.PsiDirectory) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

AnalysisScope (com.intellij.analysis.AnalysisScope)1 ConversionService (com.intellij.conversion.ConversionService)1 ProgressIndicatorBase (com.intellij.openapi.progress.util.ProgressIndicatorBase)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 NamedScope (com.intellij.psi.search.scope.packageSet.NamedScope)1 File (java.io.File)1 IOException (java.io.IOException)1