Search in sources :

Example 1 with ProgressMonitor

use of com.liferay.blade.api.ProgressMonitor in project liferay-ide by liferay.

the class MigrateProjectHandler method findMigrationProblems.

public void findMigrationProblems(final IPath[] locations, final String[] projectName) {
    Job job = new WorkspaceJob("Finding migration problems...") {

        @Override
        public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
            IStatus retval = Status.OK_STATUS;
            final BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
            ProgressMonitor override = new ProgressMonitor() {

                @Override
                public void beginTask(String taskName, int totalWork) {
                    monitor.beginTask(taskName, totalWork);
                }

                @Override
                public void done() {
                    monitor.done();
                }

                @Override
                public boolean isCanceled() {
                    return monitor.isCanceled();
                }

                @Override
                public void setTaskName(String taskName) {
                    monitor.setTaskName(taskName);
                }

                @Override
                public void worked(int work) {
                    monitor.worked(work);
                }
            };
            try {
                final ServiceReference<Migration> sr = context.getServiceReference(Migration.class);
                final Migration m = context.getService(sr);
                List<Problem> allProblems = null;
                boolean singleFile = false;
                if ((locations.length == 1) && locations[0].toFile().exists() && locations[0].toFile().isFile()) {
                    singleFile = true;
                }
                MigrationProblemsContainer container = null;
                if (_combineExistedProblem == true) {
                    container = UpgradeAssistantSettingsUtil.getObjectFromStore(MigrationProblemsContainer.class);
                }
                List<MigrationProblems> migrationProblemsList = new ArrayList<>();
                if (container == null) {
                    container = new MigrationProblemsContainer();
                }
                if (container.getProblemsArray() != null) {
                    List<MigrationProblems> mpList = Arrays.asList(container.getProblemsArray());
                    for (MigrationProblems mp : mpList) {
                        migrationProblemsList.add(mp);
                    }
                }
                for (int j = 0; j < locations.length; j++) {
                    allProblems = new ArrayList<>();
                    if (!override.isCanceled() && _shouldSearch(locations[j].toFile())) {
                        List<Problem> problems = null;
                        if (singleFile) {
                            _clearFileMarkers(locations[j].toFile());
                            Set<File> files = new HashSet<>();
                            files.add(locations[j].toFile());
                            problems = m.findProblems(files, override);
                        } else {
                            problems = m.findProblems(locations[j].toFile(), override);
                        }
                        for (Problem problem : problems) {
                            if (_shouldAdd(problem)) {
                                allProblems.add(problem);
                            }
                        }
                    }
                    if (ListUtil.isNotEmpty(allProblems)) {
                        _addMarkers(allProblems);
                        MigrationProblems migrationProblems = new MigrationProblems();
                        FileProblems[] fileProblems = FileProblemsUtil.newFileProblemsListFrom(allProblems.toArray(new Problem[0]));
                        migrationProblems.setProblems(fileProblems);
                        migrationProblems.setType("Code Problems");
                        migrationProblems.setSuffix(projectName[j]);
                        int index = _isAlreadyExist(migrationProblemsList, projectName, j);
                        if (index != -1) {
                            if (singleFile) {
                                UpgradeProblems up = migrationProblemsList.get(index);
                                FileProblems[] problems = up.getProblems();
                                for (int n = 0; n < problems.length; n++) {
                                    FileProblems fp = problems[n];
                                    String problemFilePath = fp.getFile().getPath();
                                    String initProblemFilePath = locations[0].toFile().getPath();
                                    if (problemFilePath.equals(initProblemFilePath)) {
                                        problems[n] = fileProblems[0];
                                        break;
                                    }
                                }
                                migrationProblems.setProblems(problems);
                            }
                            migrationProblemsList.set(index, migrationProblems);
                        } else {
                            migrationProblemsList.add(migrationProblems);
                        }
                    } else {
                        int index = _isAlreadyExist(migrationProblemsList, projectName, j);
                        if (index != -1) {
                            if (singleFile) {
                                MigrationProblems mp = migrationProblemsList.get(index);
                                FileProblems[] fps = mp.getProblems();
                                List<FileProblems> fpList = Arrays.asList(fps);
                                List<FileProblems> newFPList = new ArrayList<>();
                                for (FileProblems fp : fpList) {
                                    String problemFilePath = fp.getFile().getPath();
                                    String initProblemFilePath = locations[0].toFile().getPath();
                                    if (!problemFilePath.equals(initProblemFilePath)) {
                                        newFPList.add(fp);
                                    }
                                }
                                mp.setProblems(newFPList.toArray(new FileProblems[0]));
                                migrationProblemsList.set(index, mp);
                            } else {
                                migrationProblemsList.remove(index);
                            }
                        }
                    }
                }
                if (ListUtil.isNotEmpty(migrationProblemsList)) {
                    container.setProblemsArray(migrationProblemsList.toArray(new MigrationProblems[0]));
                    UpgradeAssistantSettingsUtil.setObjectToStore(MigrationProblemsContainer.class, container);
                } else {
                    UpgradeAssistantSettingsUtil.setObjectToStore(MigrationProblemsContainer.class, null);
                }
                if (singleFile) {
                    refreshViewer(allProblems);
                } else {
                    allProblems.add(new Problem());
                    m.reportProblems(allProblems, Migration.DETAIL_LONG, "ide");
                }
            } catch (Exception e) {
                retval = ProjectUI.createErrorStatus("Error in migrate command", e);
            }
            return retval;
        }
    };
    try {
        ProjectUI.getProgressService().showInDialog(Display.getDefault().getActiveShell(), job);
    } catch (Exception e) {
    }
    job.schedule();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Migration(com.liferay.blade.api.Migration) ArrayList(java.util.ArrayList) MigrationProblems(com.liferay.ide.project.core.upgrade.MigrationProblems) MigrationProblemsContainer(com.liferay.ide.project.core.upgrade.MigrationProblemsContainer) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Job(org.eclipse.core.runtime.jobs.Job) HashSet(java.util.HashSet) UpgradeProblems(com.liferay.ide.project.core.upgrade.UpgradeProblems) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) CoreException(org.eclipse.core.runtime.CoreException) ExecutionException(org.eclipse.core.commands.ExecutionException) ProgressMonitor(com.liferay.blade.api.ProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) FileProblems(com.liferay.ide.project.core.upgrade.FileProblems) Problem(com.liferay.blade.api.Problem) IFile(org.eclipse.core.resources.IFile) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Example 2 with ProgressMonitor

use of com.liferay.blade.api.ProgressMonitor in project liferay-ide by liferay.

the class ProjectMigrationService method analyzeFile.

protected FileVisitResult analyzeFile(File file, final List<Problem> problems, ProgressMonitor monitor) {
    Path path = file.toPath();
    String fileName = path.getFileName().toString();
    String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
    monitor.setTaskName("Analyzing file " + _count + "/" + _total + " " + fileName);
    ServiceReference<FileMigrator>[] fileMigrators = _fileMigratorTracker.getServiceReferences();
    if (ListUtil.isNotEmpty(fileMigrators)) {
        Stream<ServiceReference<FileMigrator>> serviceStream = Arrays.asList(fileMigrators).stream();
        List<ServiceReference<FileMigrator>> fileMigratorList = serviceStream.filter(predicate -> {
            String fileExtensionString = (String) predicate.getProperty("file.extensions");
            List<String> extensionList = Arrays.asList(fileExtensionString.split(","));
            return extensionList.contains(extension);
        }).collect(Collectors.toList());
        if (ListUtil.isNotEmpty(fileMigratorList)) {
            try {
                Stream<ServiceReference<FileMigrator>> migratorStream = fileMigratorList.stream();
                migratorStream.map(_context::getService).parallel().forEach(fm -> {
                    List<Problem> fileProlbems = fm.analyze(file);
                    if (ListUtil.isNotEmpty(fileProlbems)) {
                        problems.addAll(fileProlbems);
                    }
                });
            } catch (Exception e) {
            }
        }
        _count++;
    }
    return FileVisitResult.CONTINUE;
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) ProgressMonitor(com.liferay.blade.api.ProgressMonitor) Migration(com.liferay.blade.api.Migration) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) ListUtil(com.liferay.ide.core.util.ListUtil) Activate(org.osgi.service.component.annotations.Activate) MigrationListener(com.liferay.blade.api.MigrationListener) Path(java.nio.file.Path) ServiceReference(org.osgi.framework.ServiceReference) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) OutputStream(java.io.OutputStream) FileVisitor(java.nio.file.FileVisitor) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Files(java.nio.file.Files) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Collectors(java.util.stream.Collectors) FileMigrator(com.liferay.blade.api.FileMigrator) File(java.io.File) BundleContext(org.osgi.framework.BundleContext) FileVisitResult(java.nio.file.FileVisitResult) Problem(com.liferay.blade.api.Problem) List(java.util.List) Stream(java.util.stream.Stream) Reporter(com.liferay.blade.api.Reporter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Collections(java.util.Collections) ArrayList(java.util.ArrayList) List(java.util.List) Problem(com.liferay.blade.api.Problem) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

Migration (com.liferay.blade.api.Migration)2 Problem (com.liferay.blade.api.Problem)2 ProgressMonitor (com.liferay.blade.api.ProgressMonitor)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 BundleContext (org.osgi.framework.BundleContext)2 FileMigrator (com.liferay.blade.api.FileMigrator)1 MigrationListener (com.liferay.blade.api.MigrationListener)1 Reporter (com.liferay.blade.api.Reporter)1 ListUtil (com.liferay.ide.core.util.ListUtil)1 FileProblems (com.liferay.ide.project.core.upgrade.FileProblems)1 MigrationProblems (com.liferay.ide.project.core.upgrade.MigrationProblems)1 MigrationProblemsContainer (com.liferay.ide.project.core.upgrade.MigrationProblemsContainer)1 UpgradeProblems (com.liferay.ide.project.core.upgrade.UpgradeProblems)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 FileVisitResult (java.nio.file.FileVisitResult)1 FileVisitor (java.nio.file.FileVisitor)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1