Search in sources :

Example 6 with ConversionRunner

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

the class ConversionServiceImpl method convertModule.

@Override
@NotNull
public ConversionResult convertModule(@NotNull final Project project, @NotNull final File moduleFile) {
    final String url = project.getPresentableUrl();
    assert url != null : project;
    final String projectPath = FileUtil.toSystemDependentName(url);
    if (!isConversionNeeded(projectPath, moduleFile)) {
        return ConversionResultImpl.CONVERSION_NOT_NEEDED;
    }
    final int res = Messages.showYesNoDialog(project, IdeBundle.message("message.module.file.has.an.older.format.do.you.want.to.convert.it"), IdeBundle.message("dialog.title.convert.module"), Messages.getQuestionIcon());
    if (res != Messages.YES) {
        return ConversionResultImpl.CONVERSION_CANCELED;
    }
    if (!moduleFile.canWrite()) {
        Messages.showErrorDialog(project, IdeBundle.message("error.message.cannot.modify.file.0", moduleFile.getAbsolutePath()), IdeBundle.message("dialog.title.convert.module"));
        return ConversionResultImpl.ERROR_OCCURRED;
    }
    try {
        ConversionContextImpl context = new ConversionContextImpl(projectPath);
        final List<ConversionRunner> runners = createConversionRunners(context, Collections.emptySet());
        final File backupFile = ProjectConversionUtil.backupFile(moduleFile);
        List<ConversionRunner> usedRunners = new ArrayList<>();
        for (ConversionRunner runner : runners) {
            if (runner.isModuleConversionNeeded(moduleFile)) {
                runner.convertModule(moduleFile);
                usedRunners.add(runner);
            }
        }
        context.saveFiles(Collections.singletonList(moduleFile), usedRunners);
        Messages.showInfoMessage(project, IdeBundle.message("message.your.module.was.successfully.converted.br.old.version.was.saved.to.0", backupFile.getAbsolutePath()), IdeBundle.message("dialog.title.convert.module"));
        return new ConversionResultImpl(runners);
    } catch (CannotConvertException e) {
        LOG.info(e);
        Messages.showErrorDialog(IdeBundle.message("error.cannot.load.project", e.getMessage()), "Cannot Convert Module");
        return ConversionResultImpl.ERROR_OCCURRED;
    } catch (IOException e) {
        LOG.info(e);
        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)

Example 7 with ConversionRunner

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

the class ConversionServiceImpl method isConversionNeeded.

private static boolean isConversionNeeded(String projectPath, File moduleFile) {
    try {
        ConversionContextImpl context = new ConversionContextImpl(projectPath);
        final List<ConversionRunner> runners = createConversionRunners(context, Collections.emptySet());
        for (ConversionRunner runner : runners) {
            if (runner.isModuleConversionNeeded(moduleFile)) {
                return true;
            }
        }
        return false;
    } catch (CannotConvertException e) {
        LOG.info(e);
        return false;
    }
}
Also used : ConversionRunner(com.intellij.conversion.impl.ConversionRunner) ConversionContextImpl(com.intellij.conversion.impl.ConversionContextImpl)

Example 8 with ConversionRunner

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

the class ConversionServiceImpl method createConversionRunners.

private static List<ConversionRunner> createConversionRunners(ConversionContextImpl context, final Set<String> performedConversionIds) {
    List<ConversionRunner> runners = new ArrayList<>();
    final ConverterProvider[] providers = ConverterProvider.EP_NAME.getExtensions();
    for (ConverterProvider provider : providers) {
        if (!performedConversionIds.contains(provider.getId())) {
            runners.add(new ConversionRunner(provider, context));
        }
    }
    final Graph<ConverterProvider> graph = GraphGenerator.generate(CachingSemiGraph.cache(new ConverterProvidersGraph(providers)));
    final DFSTBuilder<ConverterProvider> builder = new DFSTBuilder<>(graph);
    if (!builder.isAcyclic()) {
        final Pair<ConverterProvider, ConverterProvider> pair = builder.getCircularDependency();
        LOG.error("cyclic dependencies between converters: " + pair.getFirst().getId() + " and " + pair.getSecond().getId());
    }
    final Comparator<ConverterProvider> comparator = builder.comparator();
    Collections.sort(runners, (o1, o2) -> comparator.compare(o1.getProvider(), o2.getProvider()));
    return runners;
}
Also used : ConversionRunner(com.intellij.conversion.impl.ConversionRunner)

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