Search in sources :

Example 1 with RuleSetModel

use of ch.acanda.eclipse.pmd.domain.RuleSetModel in project eclipse-pmd by acanda.

the class PMDPropertyPageModelTransformer method toDomainModel.

/**
 * Transforms a rule set view model to a rule set domain model.
 */
public static RuleSetModel toDomainModel(final RuleSetViewModel viewModel) {
    final String name = viewModel.getName();
    final String path = viewModel.getLocation();
    final LocationContext context = CONTEXT_TYPE_MAP.inverse().get(viewModel.getType());
    return new RuleSetModel(name, new Location(path, context));
}
Also used : RuleSetModel(ch.acanda.eclipse.pmd.domain.RuleSetModel) LocationContext(ch.acanda.eclipse.pmd.domain.LocationContext) Location(ch.acanda.eclipse.pmd.domain.Location)

Example 2 with RuleSetModel

use of ch.acanda.eclipse.pmd.domain.RuleSetModel in project eclipse-pmd by acanda.

the class RuleSetsCache method startWatchingRuleSetFiles.

private void startWatchingRuleSetFiles(final ProjectModel projectModel) {
    if (fileWatcher.isPresent() && projectModel.isPMDEnabled()) {
        final FileChangedListener listener = new RuleSetFileListener(projectModel);
        final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectModel.getProjectName());
        for (final RuleSetModel ruleSetModel : projectModel.getRuleSets()) {
            if (ruleSetModel.getLocation().getContext() != LocationContext.REMOTE) {
                final Optional<String> resolvedLocation = LocationResolver.resolveIfExists(ruleSetModel.getLocation(), project);
                if (resolvedLocation.isPresent()) {
                    final Path file = Paths.get(resolvedLocation.get());
                    try {
                        final Subscription subscription = fileWatcher.get().subscribe(file, listener);
                        subscriptions.put(projectModel.getProjectName(), subscription);
                    } catch (final IOException e) {
                        final String msg = "Cannot watch rule set file %s. " + "Changes to this file will not be picked up for up to an hour.";
                        PMDPlugin.getDefault().warn(String.format(msg, file.toAbsolutePath()), e);
                    }
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) RuleSetModel(ch.acanda.eclipse.pmd.domain.RuleSetModel) IOException(java.io.IOException) Subscription(ch.acanda.eclipse.pmd.file.Subscription) IProject(org.eclipse.core.resources.IProject) FileChangedListener(ch.acanda.eclipse.pmd.file.FileChangedListener)

Example 3 with RuleSetModel

use of ch.acanda.eclipse.pmd.domain.RuleSetModel in project eclipse-pmd by acanda.

the class RuleSetsCacheTest method secondGetLoadsWhenLaterAddedProjectRuleSetsWereChanged.

/**
 * Verifies that the second cache access loads the rule sets if the project model's rule sets have been changed
 * after the first access. In this case, the project model was added after the rule sets cache was created.
 */
@Test
public void secondGetLoadsWhenLaterAddedProjectRuleSetsWereChanged() throws Exception {
    final WorkspaceModel workspaceModel = getWorkspaceModel();
    final RuleSetsCache cache = new RuleSetsCache(getCacheLoaderMock(), workspaceModel);
    workspaceModel.add(new ProjectModel(PROJECT_NAME_2));
    cache.getRuleSets(PROJECT_NAME_2);
    final RuleSetModel ruleSetModel = new RuleSetModel("abc", new Location("path", LocationContext.WORKSPACE));
    workspaceModel.getOrCreateProject(PROJECT_NAME_2).setRuleSets(Arrays.asList(ruleSetModel));
    final RuleSets actualRuleSets = cache.getRuleSets(PROJECT_NAME_2);
    assertSame("Second cache access should reload rule sets", RULE_SETS_BAR_2, actualRuleSets);
}
Also used : WorkspaceModel(ch.acanda.eclipse.pmd.domain.WorkspaceModel) RuleSetModel(ch.acanda.eclipse.pmd.domain.RuleSetModel) RuleSets(net.sourceforge.pmd.RuleSets) ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 4 with RuleSetModel

use of ch.acanda.eclipse.pmd.domain.RuleSetModel in project eclipse-pmd by acanda.

the class RuleSetsCacheLoader method load.

@Override
public RuleSets load(final String projectName) {
    PMDPlugin.getDefault().info("RuleSetsCache: loading rule sets for project " + projectName);
    try {
        final ProjectModel projectModel = repository.load(projectName).or(new ProjectModel(projectName));
        final ImmutableSortedSet<RuleSetModel> ruleSetModels = projectModel.getRuleSets();
        final Iterable<RuleSetReferenceId> ids = presentInstances(transform(ruleSetModels, new ToReferenceId(projectName)));
        return new RuleSetFactory().createRuleSets(ImmutableList.copyOf(ids));
    } catch (final RuleSetNotFoundException e) {
        PMDPlugin.getDefault().error("Cannot load rule sets for project " + projectName, e);
        return new RuleSets();
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSetModel(ch.acanda.eclipse.pmd.domain.RuleSetModel) RuleSetReferenceId(net.sourceforge.pmd.RuleSetReferenceId) RuleSets(net.sourceforge.pmd.RuleSets) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel)

Example 5 with RuleSetModel

use of ch.acanda.eclipse.pmd.domain.RuleSetModel in project eclipse-pmd by acanda.

the class V07ToV08Converter method moveProjectSettings.

private static void moveProjectSettings(final IProject project, final ImmutableList<RuleSetConfiguration> configs, final ProjectModelRepository repository) {
    final PMDProjectSettings pmdProjectSettings = new PMDProjectSettings(project);
    final Set<RuleSetConfiguration> activeConfigs = pmdProjectSettings.getActiveRuleSetConfigurations(configs);
    if (!activeConfigs.isEmpty()) {
        final ProjectModel projectModel = new ProjectModel(project.getName());
        projectModel.setPMDEnabled(pmdProjectSettings.isPMDEnabled());
        final List<RuleSetModel> ruleSets = new ArrayList<>(activeConfigs.size());
        for (final RuleSetConfiguration config : activeConfigs) {
            final Location location = getLocation(config, project.getWorkspace().getRoot());
            final RuleSetModel ruleSetModel = new RuleSetModel(config.getName(), location);
            ruleSets.add(ruleSetModel);
        }
        projectModel.setRuleSets(ruleSets);
        repository.save(projectModel);
    }
    if (repository.load(project.getName()).isPresent()) {
        pmdProjectSettings.deleteSettings();
    }
}
Also used : RuleSetModel(ch.acanda.eclipse.pmd.domain.RuleSetModel) ArrayList(java.util.ArrayList) ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel) Location(ch.acanda.eclipse.pmd.domain.Location)

Aggregations

RuleSetModel (ch.acanda.eclipse.pmd.domain.RuleSetModel)9 Location (ch.acanda.eclipse.pmd.domain.Location)5 ProjectModel (ch.acanda.eclipse.pmd.domain.ProjectModel)4 WorkspaceModel (ch.acanda.eclipse.pmd.domain.WorkspaceModel)3 RuleSets (net.sourceforge.pmd.RuleSets)3 LocationContext (ch.acanda.eclipse.pmd.domain.LocationContext)2 Test (org.junit.Test)2 FileChangedListener (ch.acanda.eclipse.pmd.file.FileChangedListener)1 Subscription (ch.acanda.eclipse.pmd.file.Subscription)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)1 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)1 RuleSetReferenceId (net.sourceforge.pmd.RuleSetReferenceId)1 IProject (org.eclipse.core.resources.IProject)1