Search in sources :

Example 1 with ConversionRunner

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

the class ConvertProjectDialog method doOKAction.

@Override
protected void doOKAction() {
    final List<File> nonexistentFiles = myContext.getNonExistingModuleFiles();
    if (!nonexistentFiles.isEmpty() && !myNonExistingFilesMessageShown) {
        final String filesString = getFilesString(nonexistentFiles);
        final String message = IdeBundle.message("message.text.files.do.not.exist", filesString);
        final int res = Messages.showYesNoDialog(getContentPane(), message, IdeBundle.message("dialog.title.convert.project"), Messages.getQuestionIcon());
        if (res != Messages.YES) {
            super.doOKAction();
            return;
        }
        myNonExistingFilesMessageShown = false;
    }
    try {
        if (!checkReadOnlyFiles()) {
            return;
        }
        ProjectConversionUtil.backupFiles(myAffectedFiles, myContext.getProjectBaseDir(), myBackupDir);
        List<ConversionRunner> usedRunners = new ArrayList<>();
        for (ConversionRunner runner : myConversionRunners) {
            if (runner.isConversionNeeded()) {
                runner.preProcess();
                runner.process();
                runner.postProcess();
                usedRunners.add(runner);
            }
        }
        myContext.saveFiles(myAffectedFiles, usedRunners);
        myConverted = true;
        super.doOKAction();
    } catch (CannotConvertException | IOException e) {
        LOG.info(e);
        showErrorMessage(IdeBundle.message("error.cannot.convert.project", e.getMessage()));
    }
}
Also used : ConversionRunner(com.intellij.conversion.impl.ConversionRunner) ArrayList(java.util.ArrayList) CannotConvertException(com.intellij.conversion.CannotConvertException) IOException(java.io.IOException) File(java.io.File)

Example 2 with ConversionRunner

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

the class ConversionServiceImpl method isConversionNeeded.

public static boolean isConversionNeeded(String projectPath) {
    try {
        final ConversionContextImpl context = new ConversionContextImpl(projectPath);
        final List<ConversionRunner> runners = getSortedConverters(context);
        if (runners.isEmpty()) {
            return false;
        }
        for (ConversionRunner runner : runners) {
            if (runner.isConversionNeeded()) {
                return true;
            }
        }
        saveConversionResult(context);
    } catch (CannotConvertException e) {
        LOG.info("Cannot check whether conversion of project files is needed or not, conversion won't be performed", e);
    }
    return false;
}
Also used : ConversionRunner(com.intellij.conversion.impl.ConversionRunner) ConversionContextImpl(com.intellij.conversion.impl.ConversionContextImpl)

Example 3 with ConversionRunner

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

the class ConversionServiceImpl method getConversionRunners.

private static List<ConversionRunner> getConversionRunners(ConversionContextImpl context) throws CannotConvertException {
    final List<ConversionRunner> converters = getSortedConverters(context);
    final Iterator<ConversionRunner> iterator = converters.iterator();
    Set<String> convertersToRunIds = new HashSet<>();
    while (iterator.hasNext()) {
        ConversionRunner runner = iterator.next();
        boolean conversionNeeded = runner.isConversionNeeded();
        if (!conversionNeeded) {
            for (String id : runner.getProvider().getPrecedingConverterIds()) {
                if (convertersToRunIds.contains(id)) {
                    conversionNeeded = true;
                    break;
                }
            }
        }
        if (conversionNeeded) {
            convertersToRunIds.add(runner.getProvider().getId());
        } else {
            iterator.remove();
        }
    }
    return converters;
}
Also used : ConversionRunner(com.intellij.conversion.impl.ConversionRunner)

Example 4 with ConversionRunner

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

the class ConversionServiceImpl method convert.

@NotNull
@Override
public ConversionResult convert(@NotNull String projectPath) {
    try {
        if (!new File(projectPath).exists() || ApplicationManager.getApplication().isHeadlessEnvironment() || !isConversionNeeded(projectPath)) {
            return ConversionResultImpl.CONVERSION_NOT_NEEDED;
        }
        final ConversionContextImpl context = new ConversionContextImpl(projectPath);
        final List<ConversionRunner> converters = getConversionRunners(context);
        ConvertProjectDialog dialog = new ConvertProjectDialog(context, converters);
        dialog.show();
        if (dialog.isConverted()) {
            saveConversionResult(context);
            return new ConversionResultImpl(converters);
        }
        return ConversionResultImpl.CONVERSION_CANCELED;
    } catch (CannotConvertException e) {
        LOG.info(e);
        Messages.showErrorDialog(IdeBundle.message("error.cannot.convert.project", e.getMessage()), IdeBundle.message("title.cannot.convert.project"));
        return ConversionResultImpl.ERROR_OCCURRED;
    }
}
Also used : ConvertProjectDialog(com.intellij.conversion.impl.ui.ConvertProjectDialog) ConversionRunner(com.intellij.conversion.impl.ConversionRunner) ConversionContextImpl(com.intellij.conversion.impl.ConversionContextImpl) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ConversionRunner

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

the class ConversionServiceImpl method convertSilently.

@NotNull
@Override
public ConversionResult convertSilently(@NotNull String projectPath, @NotNull ConversionListener listener) {
    try {
        if (!isConversionNeeded(projectPath)) {
            return ConversionResultImpl.CONVERSION_NOT_NEEDED;
        }
        listener.conversionNeeded();
        ConversionContextImpl context = new ConversionContextImpl(projectPath);
        final List<ConversionRunner> runners = getConversionRunners(context);
        Set<File> affectedFiles = new HashSet<>();
        for (ConversionRunner runner : runners) {
            affectedFiles.addAll(runner.getAffectedFiles());
        }
        final List<File> readOnlyFiles = ConversionRunner.getReadOnlyFiles(affectedFiles);
        if (!readOnlyFiles.isEmpty()) {
            listener.cannotWriteToFiles(readOnlyFiles);
            return ConversionResultImpl.ERROR_OCCURRED;
        }
        final File backupDir = ProjectConversionUtil.backupFiles(affectedFiles, context.getProjectBaseDir());
        List<ConversionRunner> usedRunners = new ArrayList<>();
        for (ConversionRunner runner : runners) {
            if (runner.isConversionNeeded()) {
                runner.preProcess();
                runner.process();
                runner.postProcess();
                usedRunners.add(runner);
            }
        }
        context.saveFiles(affectedFiles, usedRunners);
        listener.successfullyConverted(backupDir);
        saveConversionResult(context);
        return new ConversionResultImpl(runners);
    } catch (CannotConvertException | IOException e) {
        listener.error(e.getMessage());
    }
    return ConversionResultImpl.ERROR_OCCURRED;
}
Also used : ConversionRunner(com.intellij.conversion.impl.ConversionRunner) ConversionContextImpl(com.intellij.conversion.impl.ConversionContextImpl) IOException(java.io.IOException) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ConversionRunner (com.intellij.conversion.impl.ConversionRunner)8 ConversionContextImpl (com.intellij.conversion.impl.ConversionContextImpl)5 File (java.io.File)4 IOException (java.io.IOException)3 NotNull (org.jetbrains.annotations.NotNull)3 CannotConvertException (com.intellij.conversion.CannotConvertException)1 ConvertProjectDialog (com.intellij.conversion.impl.ui.ConvertProjectDialog)1 ArrayList (java.util.ArrayList)1