Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cbd134d5239165212947770703fa9bd03fea1c20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*****************************************************************************
 * Copyright (c) 2020 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Pauline DEVILLE (CEA LIST) <pauline.deville@cea.fr> - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.toolsmiths.plugin.builder;

import java.util.Map;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osgi.service.resolver.BundleSpecification;
import org.eclipse.osgi.service.resolver.VersionRange;
import org.eclipse.papyrus.toolsmiths.plugin.builder.preferences.PluginBuilderPreferencesConstants;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.osgi.framework.Version;

/**
 * This builder check if the manifest follow papyrus's coding rules:
 * - required bundles should not be reexported
 * - bundle version should be specified using a range
 */
public class ManifestBuilder extends AbstractPapyrusBuilder {

	public static final String DEPENDENCY_MARKER_ATTRIBUTE = "dependency";

	public static final String KIND_MARKER_ATTRIBUTE = "kind";

	public static final int REEXPORT_KIND = 0;

	public static final int MISSING_VERSION_RANGE_KIND = REEXPORT_KIND + 1;

	public static final int MAXIMUM_RANGE_MISSING_KIND = MISSING_VERSION_RANGE_KIND + 1;

	public static final int EXCLUDE_MINIMUM_RANGE_KIND = MAXIMUM_RANGE_MISSING_KIND + 1;

	public static final int INCLUDE_MAXIMUM_RANGE_KIND = EXCLUDE_MINIMUM_RANGE_KIND + 1;

	/**
	 * @see org.eclipse.papyrus.toolsmiths.plugin.builder.AbstractPapyrusBuilder#build(org.eclipse.core.resources.IProject, org.eclipse.papyrus.toolsmiths.plugin.builder.PapyrusPluginBuilder, int, java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
	 *
	 * @param builtProject
	 * @param papyrusBuilder
	 * @param kind
	 * @param args
	 * @param monitor
	 * @return
	 * @throws CoreException
	 */
	@Override
	public IProject[] build(IProject builtProject, PapyrusPluginBuilder papyrusBuilder, int kind, Map<String, String> args, IProgressMonitor monitor) throws CoreException {
		final IPluginModelBase pluginModelBase = PluginRegistry.findModel(builtProject);
		BundleSpecification[] requiredBundles = pluginModelBase.getBundleDescription().getRequiredBundles();
		String message = ""; //$NON-NLS-1$

		for (BundleSpecification bundle : requiredBundles) {
			if (bundle.isExported() && isCheckReexportActivated()) {
				message = bundle.getName() + " : this bundle should not be reexported"; //$NON-NLS-1$
				createErrorMarker(pluginModelBase, bundle, message, REEXPORT_KIND);
			}

			if (isCheckDependencyRangeActivated()) {
				VersionRange versionRange = bundle.getVersionRange();
				if (versionRange != null) {
					if (versionRange.isExact()) {
						message = bundle.getName() + " : we should define range as bundle version"; //$NON-NLS-1$
						createErrorMarker(pluginModelBase, bundle, message, MISSING_VERSION_RANGE_KIND);
					} else {
						if (false == versionRange.getIncludeMinimum()) {
							message = bundle.getName() + " : we should include the minimum bundle version -> ["; //$NON-NLS-1$
							createErrorMarker(pluginModelBase, bundle, message, EXCLUDE_MINIMUM_RANGE_KIND);
						}
						if (true == versionRange.getIncludeMaximum()) {
							message = bundle.getName() + " : we should exclude the maximum bundle version -> )"; //$NON-NLS-1$
							createErrorMarker(pluginModelBase, bundle, message, INCLUDE_MAXIMUM_RANGE_KIND);
						}
						if (versionRange.getLeft() == null || versionRange.getLeft().equals(Version.emptyVersion)) {
							message = bundle.getName() + " : we should define a minimum bundle version"; //$NON-NLS-1$
							createErrorMarker(pluginModelBase, bundle, message, MISSING_VERSION_RANGE_KIND);

						} else if (versionRange.getRight() == null) {
							message = bundle.getName() + " : we should define a maximum bundle version"; //$NON-NLS-1$
							createErrorMarker(pluginModelBase, bundle, message, MAXIMUM_RANGE_MISSING_KIND);
						}
					}
				}
			}
		}

		return null;
	}

	private void createErrorMarker(IPluginModelBase pluginModelBase, BundleSpecification bundle, String message, int versionRangeMissingKind) {
		IResource manifest = pluginModelBase.getUnderlyingResource();
		IMarker marker = createErrorMarker(manifest, message);
		try {
			marker.setAttribute(DEPENDENCY_MARKER_ATTRIBUTE, bundle.getName());
			marker.setAttribute(KIND_MARKER_ATTRIBUTE, versionRangeMissingKind);
		} catch (CoreException e) {
			Activator.log.error(e);
		}
	}

	/**
	 *
	 * @return
	 *         <code>true</code> if the check of the reexport is activated
	 */
	private boolean isCheckReexportActivated() {
		return Activator.getDefault().getPreferenceStore().getBoolean(PluginBuilderPreferencesConstants.PAPYRUS_MANIFEST_BUILDER_CHECK_NO_REEXPORT);
	}

	/**
	 *
	 * @return
	 *         <code>true</code> if the check of dependency range is activated
	 */
	private boolean isCheckDependencyRangeActivated() {
		return Activator.getDefault().getPreferenceStore().getBoolean(PluginBuilderPreferencesConstants.PAPYRUS_MANIFEST_BUILDER_CHECK_DEPENDENCY_RANGE);

	}

}

Back to the top