diff options
Diffstat (limited to 'rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint')
42 files changed, 2620 insertions, 0 deletions
diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/Activator.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/Activator.java new file mode 100644 index 0000000000..5faac8b3de --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/Activator.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.rpm.rpmlint; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.linuxtools.rpm.rpmlint.preferences.PreferenceConstants; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + /** + * The id of this plugin. + */ + public static final String PLUGIN_ID = "org.eclipse.linuxtools.rpm.rpmlint"; //$NON-NLS-1$ + + /** + * Specfile extension constant i.e. .spec files. + */ + public static final String SPECFILE_EXTENSION = "spec"; //$NON-NLS-1$ + + /** + * RPM extension constant, i.e. .rpm files. + */ + public static final String RPMFILE_EXTENSION = "rpm"; //$NON-NLS-1$ + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + plugin = this; + } + + /** + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + @Override + public void start(BundleContext context) throws Exception { + super.start(context); + } + + /** + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + @Override + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + + /** + * Returns an image descriptor for the image file at the given + * plug-in relative path + * + * @param path the path + * @return the image descriptor + */ + public static ImageDescriptor getImageDescriptor(String path) { + return imageDescriptorFromPlugin(PLUGIN_ID, path); + } + + /** + * Returns the rpmlint path stored in the preferences. + * @return The path to the rpmlint executable. + */ + public static String getRpmlintPath() { + return plugin.getPreferenceStore().getString( + PreferenceConstants.P_RPMLINT_PATH); + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/Messages.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/Messages.java new file mode 100644 index 0000000000..ba8926daaf --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/Messages.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2009 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint; + +import org.eclipse.osgi.util.NLS; + +public class Messages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.linuxtools.rpm.rpmlint.messages"; //$NON-NLS-1$ + public static String RpmlintLog_0; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + //should not be instantiated + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/RpmlintLog.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/RpmlintLog.java new file mode 100644 index 0000000000..3878572fc3 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/RpmlintLog.java @@ -0,0 +1,103 @@ +/*******************************************************************************
+ * Copyright (c) 2007 Alphonse Van Assche.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Alphonse Van Assche - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.rpm.rpmlint;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+
+/**
+ * The logger of convenience for the Rpmlint Plug-In.
+ */
+public class RpmlintLog {
+ /**
+ * Log the specified information.
+ *
+ * @param message A human-readable message, localized to the
+ * current locale.
+ */
+ public static void logInfo(String message) {
+ log(IStatus.INFO, IStatus.OK, message, null);
+ }
+
+ /**
+ * Log the specified error.
+ *
+ * @param exception A low-level exception.
+ */
+ public static void logError(Throwable exception) {
+ logError(Messages.RpmlintLog_0, exception);
+ }
+
+ /**
+ * Log the specified error.
+ *
+ * @param message A human-readable message, localized to the
+ * current locale.
+ * @param exception A low-level exception, or <code>null</code>
+ * if not applicable.
+ */
+ public static void logError(String message, Throwable exception) {
+ log(IStatus.ERROR, IStatus.OK, message, exception);
+ }
+
+ /**
+ * Log the specified information.
+ *
+ * @param severity The severity; one of the following:
+ * <code>IStatus.OK</code>,
+ * <code>IStatus.ERROR</code>,
+ * <code>IStatus.INFO</code>, or
+ * <code>IStatus.WARNING</code>.
+ * @param code The plug-in-specific status code, or <code>OK</code>.
+ * @param message A human-readable message, localized to the
+ * current locale.
+ * @param exception A low-level exception, or <code>null</code>
+ * if not applicable.
+ */
+ public static void log(int severity, int code, String message,
+ Throwable exception) {
+
+ log(createStatus(severity, code, message, exception));
+ }
+
+ /**
+ * Create a status object representing the specified information.
+ *
+ * @param severity The severity; one of the following:
+ * <code>IStatus.OK</code>,
+ * <code>IStatus.ERROR</code>,
+ * <code>IStatus.INFO</code>, or
+ * <code>IStatus.WARNING</code>.
+ * @param code The plug-in-specific status code, or
+ * <code>OK</code>.
+ * @param message A human-readable message, localized to the
+ * current locale.
+ * @param exception A low-level exception, or <code>null</code>
+ * if not applicable.
+ * @return the status object (not <code>null</code>).
+ */
+ public static IStatus createStatus(int severity, int code,
+ String message, Throwable exception) {
+
+ return new Status(severity, Activator.PLUGIN_ID, code,
+ message, exception);
+ }
+
+ /**
+ * Log the given status.
+ *
+ * @param status The status to log.
+ */
+ public static void log(IStatus status) {
+ Activator.getDefault().getLog().log(status);
+ }
+}
\ No newline at end of file diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/Messages.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/Messages.java new file mode 100644 index 0000000000..a3f016b6aa --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/Messages.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2009 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.actions; + +import org.eclipse.osgi.util.NLS; + +public class Messages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.linuxtools.rpm.rpmlint.actions.messages"; //$NON-NLS-1$ + public static String RunRpmlintAction_0; + public static String RunRpmlintAction_1; + public static String RunRpmlintAction_2; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + // don't allow instantiating + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/RunRpmlintAction.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/RunRpmlintAction.java new file mode 100644 index 0000000000..5cb3c9ed11 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/RunRpmlintAction.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * Copyright (c) 2009 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.actions; + +import java.io.IOException; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.jface.dialogs.ErrorDialog; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.linuxtools.rpm.core.utils.Utils; +import org.eclipse.linuxtools.rpm.rpmlint.Activator; +import org.eclipse.linuxtools.rpm.rpmlint.RpmlintLog; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.console.ConsolePlugin; +import org.eclipse.ui.console.IConsole; +import org.eclipse.ui.console.IConsoleManager; +import org.eclipse.ui.console.MessageConsole; +import org.eclipse.ui.console.MessageConsoleStream; +import org.eclipse.ui.handlers.HandlerUtil; + +/** + * Manually invoke rpmlint action, which prints the output of rpmlint execution to the console. + * + */ +public class RunRpmlintAction extends AbstractHandler{ + /** + * @param event The execution event. + * @return Nothing. + */ + public Object execute(ExecutionEvent event) { + ISelection selection = HandlerUtil.getCurrentSelection(event); + if (selection instanceof IStructuredSelection) { + for (Object element : ((IStructuredSelection) selection).toList()) { + IFile rpmFile = null; + if (element instanceof IFile) { + rpmFile = (IFile) element; + } else if (element instanceof IAdaptable) { + rpmFile = (IFile) ((IAdaptable) element) + .getAdapter(IFile.class); + } + if (rpmFile != null) { + try { + if (Utils.fileExist(Activator.getRpmlintPath())) { + String output = Utils.runCommandToString(Activator + .getRpmlintPath(), + "-i", rpmFile.getLocation() //$NON-NLS-1$ + .toString()); + MessageConsole myConsole = findConsole(Messages.RunRpmlintAction_0); + MessageConsoleStream out = myConsole + .newMessageStream(); + myConsole.clearConsole(); + myConsole.activate(); + out.println(output); + } else { + IStatus warning = new Status( + IStatus.WARNING, + Activator.PLUGIN_ID, + 1, + Messages.RunRpmlintAction_1, + null); + ErrorDialog.openError(PlatformUI.getWorkbench() + .getActiveWorkbenchWindow().getShell(), + Messages.RunRpmlintAction_2, + null, warning); + } + } catch (IOException e) { + // FIXME: rpmlint is not installed in the default place + // -> ask user to open the prefs page. + RpmlintLog.logError(e); + } + } + } + } + return null; + + } + + private MessageConsole findConsole(String name) { + ConsolePlugin plugin = ConsolePlugin.getDefault(); + IConsoleManager conMan = plugin.getConsoleManager(); + IConsole[] existing = conMan.getConsoles(); + for (int i = 0; i < existing.length; i++) + if (name.equals(existing[i].getName())) + return (MessageConsole) existing[i]; + // no console found, so create a new one + MessageConsole myConsole = new MessageConsole(name, null); + conMan.addConsoles(new IConsole[] { myConsole }); + return myConsole; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/ToggleRpmlintNatureAction.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/ToggleRpmlintNatureAction.java new file mode 100644 index 0000000000..d66366d69f --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/ToggleRpmlintNatureAction.java @@ -0,0 +1,90 @@ +/******************************************************************************* + * Copyright (c) 2009 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.actions; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.linuxtools.rpm.rpmlint.builder.RpmlintNature; +import org.eclipse.ui.handlers.HandlerUtil; + +/** + * Toggle rpmlint nature for the given project. + * As a result this enables/disables rpmlint builder. + * + */ +public class ToggleRpmlintNatureAction extends AbstractHandler { + + + /** + * @param event The event + * @return Null. + */ + public Object execute(ExecutionEvent event) { + ISelection selection = HandlerUtil.getCurrentSelection(event); + if (selection instanceof IStructuredSelection) { + for (Object element: ((IStructuredSelection) selection).toList()) { + IProject project = null; + if (element instanceof IProject) { + project = (IProject) element; + } else if (element instanceof IAdaptable) { + project = (IProject) ((IAdaptable) element) + .getAdapter(IProject.class); + } + if (project != null) { + toggleNature(project); + } + } + } + return null; + } + + /** + * Toggles sample nature on a project + * + * @param project + * to have sample nature added or removed + */ + private void toggleNature(IProject project) { + try { + IProjectDescription description = project.getDescription(); + String[] natures = description.getNatureIds(); + + for (int i = 0; i < natures.length; ++i) { + if (RpmlintNature.NATURE_ID.equals(natures[i])) { + // Remove the nature + String[] newNatures = new String[natures.length - 1]; + System.arraycopy(natures, 0, newNatures, 0, i); + System.arraycopy(natures, i + 1, newNatures, i, + natures.length - i - 1); + description.setNatureIds(newNatures); + project.setDescription(description, null); + return; + } + } + + // Add the nature + String[] newNatures = new String[natures.length + 1]; + System.arraycopy(natures, 0, newNatures, 0, natures.length); + newNatures[natures.length] = RpmlintNature.NATURE_ID; + description.setNatureIds(newNatures); + project.setDescription(description, null); + } catch (CoreException e) { + //TODO log exception + } + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/messages.properties b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/messages.properties new file mode 100644 index 0000000000..cee6e0ee15 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/actions/messages.properties @@ -0,0 +1,13 @@ +############################################################################### +# Copyright (c) 2009 Red Hat, Inc. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# Red Hat - initial API and implementation +############################################################################### +RunRpmlintAction_0=Rpmlint console +RunRpmlintAction_1=No rpmlint installed or configured +RunRpmlintAction_2=Please install and configure rpmlint in the plugin preference page. diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/Messages.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/Messages.java new file mode 100644 index 0000000000..f9c87cc021 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/Messages.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2009 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.builder; + +import org.eclipse.osgi.util.NLS; + +public class Messages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.linuxtools.rpm.rpmlint.builder.messages"; //$NON-NLS-1$ + public static String RpmlintBuilder_0; + public static String RpmlintBuilder_1; + public static String RpmlintBuilder_2; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + // should not be instantiated + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintBuilder.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintBuilder.java new file mode 100644 index 0000000000..ef009c363b --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintBuilder.java @@ -0,0 +1,150 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.builder; + +import java.util.ArrayList; +import java.util.Map; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResourceDelta; +import org.eclipse.core.resources.IncrementalProjectBuilder; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.jface.text.Document; +import org.eclipse.linuxtools.rpm.rpmlint.Activator; +import org.eclipse.linuxtools.rpm.rpmlint.parser.RpmlintItem; +import org.eclipse.linuxtools.rpm.rpmlint.parser.RpmlintParser; +import org.eclipse.linuxtools.rpm.ui.editor.markers.SpecfileErrorHandler; +import org.eclipse.linuxtools.rpm.ui.editor.markers.SpecfileTaskHandler; +import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser; + +/** + * Project builder responsible for invoking rpmlint and processing it's response. + */ +public class RpmlintBuilder extends IncrementalProjectBuilder { + + /** + * Total number of chunks to divede the work in. + */ + public static final int MAX_WORKS = 100; + + /** + * ID for this builder. + */ + public static final String BUILDER_ID = Activator.PLUGIN_ID + ".rpmlintBuilder"; //$NON-NLS-1$ + + /** + * ID for rpmlint marker problems. + */ + public static final String MARKER_ID = Activator.PLUGIN_ID + ".rpmlintProblem"; //$NON-NLS-1$ + + private SpecfileParser specfileParser; + + private SpecfileErrorHandler errorHandler; + private SpecfileTaskHandler taskHandler; + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.internal.events.InternalBuilder#build(int, + * java.util.Map, org.eclipse.core.runtime.IProgressMonitor) + */ + @Override + protected IProject[] build(int kind, Map args, IProgressMonitor monitor) + throws CoreException { + // TODO: handle the monitor in a more clean way. + monitor.beginTask(Messages.RpmlintBuilder_0, MAX_WORKS); + monitor.worked(20); + if (kind == FULL_BUILD) { + fullBuild(monitor); + } else { + IResourceDelta delta = getDelta(getProject()); + if (delta == null) { + fullBuild(monitor); + } else { + incrementalBuild(delta, monitor); + } + } + return null; + } + + protected void fullBuild(IProgressMonitor monitor) throws CoreException { + RpmlintPreVisitor resourceVisitor = new RpmlintPreVisitor(); + getProject().accept(resourceVisitor); + checkCancel(monitor); + monitor.worked(50); + monitor.setTaskName(Messages.RpmlintBuilder_1); + ArrayList<RpmlintItem> rpmlintItems = RpmlintParser.getInstance().parseVisisted( + resourceVisitor.getVisitedPaths()); + visitAndMarkRpmlintItems(monitor, rpmlintItems); + } + + protected void incrementalBuild(IResourceDelta delta, + IProgressMonitor monitor) throws CoreException { + RpmlintDeltaVisitor deltaVisitor = new RpmlintDeltaVisitor(); + delta.accept(deltaVisitor); + monitor.worked(50); + monitor.setTaskName(Messages.RpmlintBuilder_1); + ArrayList<RpmlintItem> rpmlintItems = RpmlintParser.getInstance().parseVisisted( + deltaVisitor.getVisitedPaths()); + visitAndMarkRpmlintItems(monitor, rpmlintItems); + } + + private void visitAndMarkRpmlintItems(IProgressMonitor monitor, + ArrayList<RpmlintItem> rpmlintItems) throws CoreException { + if (rpmlintItems.size() > 0) { + checkCancel(monitor); + monitor.worked(70); + monitor.setTaskName(Messages.RpmlintBuilder_2); + getProject().accept(new RpmlintMarkerVisitor(this, rpmlintItems)); + monitor.worked(MAX_WORKS); + } + } + + protected SpecfileParser getSpecfileParser() { + if (specfileParser == null) { + specfileParser = new SpecfileParser(); + } + return specfileParser; + } + + protected SpecfileErrorHandler getSpecfileErrorHandler(IFile file, + String specContent) { + if (errorHandler == null) { + errorHandler = new SpecfileErrorHandler(file, new Document( + specContent)); + } else { + errorHandler.setFile(file); + errorHandler.setDocument(new Document(specContent)); + } + return errorHandler; + } + + protected SpecfileTaskHandler getSpecfileTaskHandler(IFile file, + String specContent) { + if (taskHandler == null) { + taskHandler = new SpecfileTaskHandler(file, new Document( + specContent)); + } else { + taskHandler.setFile(file); + taskHandler.setDocument(new Document(specContent)); + } + return taskHandler; + } + + protected void checkCancel(IProgressMonitor monitor) { + if (monitor.isCanceled()) { + throw new OperationCanceledException(); + } + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintDeltaVisitor.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintDeltaVisitor.java new file mode 100644 index 0000000000..71a67c2740 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintDeltaVisitor.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.builder; + +import java.util.ArrayList; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceDelta; +import org.eclipse.core.resources.IResourceDeltaVisitor; +import org.eclipse.linuxtools.rpm.rpmlint.Activator; +import org.eclipse.linuxtools.rpm.rpmlint.parser.RpmlintParser; + +public class RpmlintDeltaVisitor implements IResourceDeltaVisitor { + + private ArrayList<String> paths = new ArrayList<String>(); + + /* (non-Javadoc) + * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta) + */ + public boolean visit(IResourceDelta delta) { + IResource resource = delta.getResource(); + if (Activator.SPECFILE_EXTENSION.equals(resource.getFileExtension()) + || Activator.RPMFILE_EXTENSION.equals(resource + .getFileExtension())) { + switch (delta.getKind()) { + // we first visiting resources to be able to run the rpmlint command + // only once. That improve drastically the performance. + case IResourceDelta.ADDED: + paths.add(resource.getLocation().toOSString()); + break; + case IResourceDelta.CHANGED: + RpmlintParser.getInstance().deleteMarkers(resource); + paths.add(resource.getLocation().toOSString()); + break; + } + } + return true; + } + + public ArrayList<String> getVisitedPaths() { + return paths; + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintMarkerRemoveVisitor.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintMarkerRemoveVisitor.java new file mode 100644 index 0000000000..33378285fc --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintMarkerRemoveVisitor.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.builder; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.linuxtools.rpm.rpmlint.Activator; +import org.eclipse.linuxtools.rpm.rpmlint.parser.RpmlintParser; +import org.eclipse.linuxtools.rpm.ui.editor.markers.SpecfileErrorHandler; + +/** + * Removes all the markers created by rpmlint. + * + */ +public class RpmlintMarkerRemoveVisitor implements IResourceVisitor { + + /** + * Removes all rpmlint markers for spec and rpm files. + * + * @see org.eclipse.core.resources.IResourceVisitor#visit(org.eclipse.core.resources.IResource) + */ + public boolean visit(IResource resource) throws CoreException { + if (Activator.SPECFILE_EXTENSION.equals(resource.getFileExtension()) + || Activator.RPMFILE_EXTENSION.equals(resource + .getFileExtension())) { + RpmlintParser.getInstance().deleteMarkers(resource); + // remove internal marks + resource.deleteMarkers( + SpecfileErrorHandler.SPECFILE_ERROR_MARKER_ID, false, + IResource.DEPTH_ZERO); + } + return true; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintMarkerVisitor.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintMarkerVisitor.java new file mode 100644 index 0000000000..0c988f70a2 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintMarkerVisitor.java @@ -0,0 +1,173 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.builder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.linuxtools.rpm.rpmlint.Activator; +import org.eclipse.linuxtools.rpm.rpmlint.RpmlintLog; +import org.eclipse.linuxtools.rpm.rpmlint.parser.RpmlintItem; +import org.eclipse.linuxtools.rpm.rpmlint.parser.RpmlintParser; +import org.eclipse.linuxtools.rpm.ui.editor.markers.SpecfileErrorHandler; + +public class RpmlintMarkerVisitor implements IResourceVisitor { + + private ArrayList<RpmlintItem> rpmlintItems; + + private int lineNumber; + + private RpmlintBuilder builder; + + private String specContent; + + private boolean firstWarningInResource; + + private IFile currentFile; + + private IDocument document; + + private int charStart; + + private int charEnd; + + + public RpmlintMarkerVisitor(RpmlintBuilder builder, ArrayList<RpmlintItem> rpmlintItems) { + this.rpmlintItems = rpmlintItems; + this.builder = builder; + rpmlintItems = new ArrayList<RpmlintItem>(); + } + + /* (non-Javadoc) + * @see org.eclipse.core.resources.IResourceVisitor#visit(org.eclipse.core.resources.IResource) + */ + public boolean visit(IResource resource) throws CoreException { + if (Activator.SPECFILE_EXTENSION.equals(resource.getFileExtension())) { + firstWarningInResource = true; + for (RpmlintItem item : rpmlintItems) { + if (item.getFileName().equals(resource.getLocation().toOSString())) { + currentFile = ((IFile)resource); + if (firstWarningInResource) { + RpmlintParser.getInstance().deleteMarkers(resource); + // remove internal marks on the current resource + currentFile.deleteMarkers(SpecfileErrorHandler.SPECFILE_ERROR_MARKER_ID, false, IResource.DEPTH_ZERO); + firstWarningInResource = false; + } + + specContent = fileToString(currentFile); + // FIXME: workaround the wrong line number with configure-without-libdir-spec + if (item.getId().equals("configure-without-libdir-spec")) { //$NON-NLS-1$ + item.setLineNbr(-1); + lineNumber = RpmlintParser.getInstance().getRealLineNbr(specContent, "./configure"); //$NON-NLS-1$ + if (lineNumber == -1) + lineNumber = RpmlintParser.getInstance().getRealLineNbr(specContent, "%configure"); //$NON-NLS-1$ + item.setLineNbr(lineNumber); + } + + lineNumber = item.getLineNbr(); + if (lineNumber == -1) { + lineNumber = RpmlintParser.getInstance().getRealLineNbr(specContent, item.getRefferedContent()); + if (lineNumber == -1) { + lineNumber = 1; + } + } + lineNumber -= 1; + // end workaround + + // BTW we mark specfile with the internal marker. + builder.getSpecfileParser().setErrorHandler(builder.getSpecfileErrorHandler(currentFile, specContent)); + builder.getSpecfileParser().setTaskHandler(builder.getSpecfileTaskHandler(currentFile, specContent)); + builder.getSpecfileParser().parse(specContent); + + document = new Document(specContent); + charStart = getLineOffset(lineNumber); + charEnd = charStart + getLineLenght(lineNumber); + RpmlintParser.getInstance().addMarker((IFile) resource, item.getId() + ": " //$NON-NLS-1$ + + item.getMessage(), lineNumber, charStart, charEnd, + item.getSeverity(), item.getId(), + item.getRefferedContent()); + } + } + } else if (Activator.RPMFILE_EXTENSION.equals(resource + .getFileExtension())) { + firstWarningInResource = true; + for (RpmlintItem item : rpmlintItems) { + currentFile = ((IFile) resource); + if (firstWarningInResource) { + RpmlintParser.getInstance().deleteMarkers(resource); + // remove internal marks on the current resource + currentFile.deleteMarkers( + SpecfileErrorHandler.SPECFILE_ERROR_MARKER_ID, + false, IResource.DEPTH_ZERO); + firstWarningInResource = false; + } + + // BTW we mark specfile with the internal marker. + builder.getSpecfileParser().setErrorHandler( + builder.getSpecfileErrorHandler(currentFile, + specContent)); + builder.getSpecfileParser().setTaskHandler( + builder.getSpecfileTaskHandler(currentFile, + specContent)); + + RpmlintParser.getInstance().addMarker((IFile) resource, + item.getId() + ": " //$NON-NLS-1$ + + item.getMessage(), item.getSeverity(), + item.getId(), item.getRefferedContent()); + } + } + return true; + } + + private int getLineOffset(int lineNumber) { + try { + return document.getLineOffset(lineNumber); + } catch (BadLocationException e) { + RpmlintLog.logError(e); + return 1; + } + } + + private int getLineLenght(int lineNumber) { + try { + return document.getLineLength(lineNumber); + } catch (BadLocationException e) { + RpmlintLog.logError(e); + return 1; + } + } + + private String fileToString(IFile file) { + String ret = ""; //$NON-NLS-1$ + try { + InputStream in = file.getContents(); + int nbrOfByte = in.available(); + byte[] bytes = new byte[nbrOfByte]; + in.read(bytes); + ret = new String(bytes); + in.close(); + } catch (CoreException e) { + RpmlintLog.logError(e); + } catch (IOException e) { + RpmlintLog.logError(e); + } + return ret; + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintNature.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintNature.java new file mode 100644 index 0000000000..ec40872609 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintNature.java @@ -0,0 +1,90 @@ +/******************************************************************************* + * Copyright (c) 2009 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.builder; + +import org.eclipse.core.resources.ICommand; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.resources.IProjectNature; +import org.eclipse.core.runtime.CoreException; + +public class RpmlintNature implements IProjectNature { + + /** + * ID of this project nature + */ + public static final String NATURE_ID = "org.eclipse.linuxtools.rpm.rpmlint.rpmlintNature"; //$NON-NLS-1$ + + private IProject project; + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.resources.IProjectNature#configure() + */ + public void configure() throws CoreException { + IProjectDescription desc = project.getDescription(); + ICommand[] commands = desc.getBuildSpec(); + for (ICommand command : commands) { + if (command.getBuilderName().equals(RpmlintBuilder.BUILDER_ID)) { + return; + } + } + ICommand[] newCommands = new ICommand[commands.length + 1]; + System.arraycopy(commands, 0, newCommands, 0, commands.length); + ICommand command = desc.newCommand(); + command.setBuilderName(RpmlintBuilder.BUILDER_ID); + newCommands[newCommands.length - 1] = command; + desc.setBuildSpec(newCommands); + project.setDescription(desc, null); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.resources.IProjectNature#deconfigure() + */ + public void deconfigure() throws CoreException { + IProjectDescription description = getProject().getDescription(); + ICommand[] commands = description.getBuildSpec(); + for (int i = 0; i < commands.length; ++i) { + if (commands[i].getBuilderName().equals(RpmlintBuilder.BUILDER_ID)) { + ICommand[] newCommands = new ICommand[commands.length - 1]; + System.arraycopy(commands, 0, newCommands, 0, i); + System.arraycopy(commands, i + 1, newCommands, i, + commands.length - i - 1); + description.setBuildSpec(newCommands); + // Remove rpmlint marks on all specfiles into the project. + project.accept(new RpmlintMarkerRemoveVisitor()); + return; + } + } + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.resources.IProjectNature#getProject() + */ + public IProject getProject() { + return project; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.resources.IProjectNature#setProject(org.eclipse.core.resources.IProject) + */ + public void setProject(IProject project) { + this.project = project; + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintPreVisitor.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintPreVisitor.java new file mode 100644 index 0000000000..f15d74da4e --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/RpmlintPreVisitor.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.builder; + +import java.util.ArrayList; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceVisitor; +import org.eclipse.linuxtools.rpm.rpmlint.Activator; + +public class RpmlintPreVisitor implements IResourceVisitor { + + private ArrayList<String> paths = new ArrayList<String>(); + + /* (non-Javadoc) + * @see org.eclipse.core.resources.IResourceVisitor#visit(org.eclipse.core.resources.IResource) + */ + public boolean visit(IResource resource) { + if (Activator.SPECFILE_EXTENSION.equals(resource.getFileExtension()) + || Activator.RPMFILE_EXTENSION.equals(resource + .getFileExtension())) { + // we previsiting resource to be able to run rpmlint command + // only once. That improve drasticaly the perfs. + paths.add(resource.getLocation().toOSString()); + } + return true; + } + + public ArrayList<String> getVisitedPaths() { + return paths; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/messages.properties b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/messages.properties new file mode 100644 index 0000000000..39d2deb453 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/builder/messages.properties @@ -0,0 +1,13 @@ +############################################################################### +# Copyright (c) 2009 Red Hat, Inc. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# Red Hat - initial API and implementation +############################################################################### +RpmlintBuilder_0=Check rpmlint problems +RpmlintBuilder_1=Retrive Rpmlint problems... +RpmlintBuilder_2=Add Rpmlint problems... diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/messages.properties b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/messages.properties new file mode 100644 index 0000000000..45979e8f19 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/messages.properties @@ -0,0 +1,11 @@ +############################################################################### +# Copyright (c) 2009 Red Hat, Inc. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# Red Hat - initial API and implementation +############################################################################### +RpmlintLog_0=Unexpected Exception diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/parser/RpmlintItem.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/parser/RpmlintItem.java new file mode 100644 index 0000000000..0b34605955 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/parser/RpmlintItem.java @@ -0,0 +1,130 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.parser; + +import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser; + +public class RpmlintItem { + + private static final String[] sections = SpecfileParser.simpleSections; + + private int lineNbr; + + private int severity; + + private String id; + + private String refferedContent; + + private String refferedSection; + + private String message; + + private String fileName; + + public String getFileName() { + return fileName; + } + + public void setFileName(String file) { + this.fileName = file; + } + + public int getLineNbr() { + return lineNbr; + } + + public void setLineNbr(int lineNbr) { + this.lineNbr = lineNbr; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getRefferedContent() { + return refferedContent; + } + + public void setRefferedContent(String refferedContent) { + for (int i = 0; i < sections.length; i++) { + if (refferedContent.startsWith(sections[i])) { + this.refferedContent = refferedContent.trim(); + if (this.refferedContent.equals("")) //$NON-NLS-1$ + this.refferedContent = sections[i]; + this.refferedSection = sections[i]; + i = sections.length; + } else { + this.refferedContent = refferedContent; + this.refferedSection = ""; //$NON-NLS-1$ + } + } + } + + public String getrefferedSection() { + return refferedSection; + } + + public int getSeverity() { + return severity; + } + + public void setSeverity(String severity) { + severity = severity.replaceAll(":", "").trim(); //$NON-NLS-1$ //$NON-NLS-2$ + switch (severity.charAt(0)) { + case 'I': + this.severity = 0; + break; + case 'W': + this.severity = 1; + break; + case 'E': + this.severity = 2; + break; + default: + this.severity = 0; + break; + } + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("line number: "); //$NON-NLS-1$ + stringBuilder.append(this.lineNbr); + stringBuilder.append("\nfile name: "); //$NON-NLS-1$ + stringBuilder.append(this.fileName); + stringBuilder.append("\nseverity: "); //$NON-NLS-1$ + stringBuilder.append(this.severity); + stringBuilder.append("\nId: "); //$NON-NLS-1$ + stringBuilder.append(this.id); + stringBuilder.append("\nrefered content: "); //$NON-NLS-1$ + stringBuilder.append(this.refferedContent); + stringBuilder.append("\nmessage: "); //$NON-NLS-1$ + stringBuilder.append(this.getMessage()); + stringBuilder.append("\n"); //$NON-NLS-1$ + return stringBuilder.toString(); + } + + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/parser/RpmlintParser.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/parser/RpmlintParser.java new file mode 100644 index 0000000000..954884128a --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/parser/RpmlintParser.java @@ -0,0 +1,341 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.parser; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.linuxtools.rpm.core.utils.Utils; +import org.eclipse.linuxtools.rpm.rpmlint.Activator; +import org.eclipse.linuxtools.rpm.rpmlint.RpmlintLog; +import org.eclipse.linuxtools.rpm.rpmlint.builder.RpmlintBuilder; +import org.eclipse.linuxtools.rpm.rpmlint.preferences.PreferenceConstants; +import org.eclipse.linuxtools.rpm.rpmlint.resolutions.RpmlintMarkerResolutionGenerator; + +public class RpmlintParser { + + + private static final String COLON = ":"; //$NON-NLS-1$ + private static final String SPACE = " "; //$NON-NLS-1$ + private static final String EMPTY_STRING = ""; //$NON-NLS-1$ + private static RpmlintParser rpmlintParser; + + // default constructor + private RpmlintParser() { + // Empty constructor for making it a singleton. + } + + public static RpmlintParser getInstance() { + if (rpmlintParser == null) { + rpmlintParser = new RpmlintParser(); + } + return rpmlintParser; + } + + /** + * Parse visited resources. + * @param visitedResources The list of resources to parse. + * + * @return + * a <code>RpmlintItem</code> ArrayList. + */ + public ArrayList<RpmlintItem> parseVisisted(ArrayList<String> visitedResources) { + ArrayList<RpmlintItem> retList = new ArrayList<RpmlintItem>(); + // workaround "Argument list too long" xargs limitation. + ArrayList<List<String>> commandsList = splitArrayList(visitedResources, 500); + Iterator<List<String>> iterator = commandsList.iterator(); + while (iterator.hasNext()) { + if (commandsList.size() > 1) + retList.addAll(parseRpmlintOutput(runRpmlintCommand(iterator.next()))); + else + return parseRpmlintOutput(runRpmlintCommand(visitedResources)); + } + return retList; + + } + + public void addMarker(IFile file, String message, int lineNumber, int charStart, int charEnd, + int severity, String rpmlintID, String rpmlintrefferedContent) { + try { + IMarker marker = file + .createMarker(RpmlintBuilder.MARKER_ID); + marker.setAttribute(IMarker.LOCATION, file.getFullPath().toString()); + marker.setAttribute(IMarker.MESSAGE, message); + marker.setAttribute(IMarker.SEVERITY, severity); + marker.setAttribute(IMarker.LINE_NUMBER, lineNumber); + marker.setAttribute(IMarker.CHAR_START, charStart); + marker.setAttribute(IMarker.CHAR_END, charEnd); + marker.setAttribute(RpmlintMarkerResolutionGenerator.RPMLINT_ERROR_ID, rpmlintID); + marker.setAttribute(RpmlintMarkerResolutionGenerator.RPMLINT_REFFERED_CONTENT, rpmlintrefferedContent); + + } catch (CoreException e) { + RpmlintLog.logError(e); + } + } + + public void addMarker(IFile file, String message, int severity, + String rpmlintID, String rpmlintrefferedContent) { + try { + IMarker marker = file.createMarker(RpmlintBuilder.MARKER_ID); + marker + .setAttribute(IMarker.LOCATION, file.getFullPath() + .toString()); + marker.setAttribute(IMarker.MESSAGE, message); + marker.setAttribute(IMarker.SEVERITY, severity); + marker.setAttribute( + RpmlintMarkerResolutionGenerator.RPMLINT_ERROR_ID, + rpmlintID); + marker.setAttribute( + RpmlintMarkerResolutionGenerator.RPMLINT_REFFERED_CONTENT, + rpmlintrefferedContent); + + } catch (CoreException e) { + RpmlintLog.logError(e); + } + } + + public void deleteMarkers(IResource resource) { + try { + resource.deleteMarkers(RpmlintBuilder.MARKER_ID, false, IResource.DEPTH_ZERO); + } catch (CoreException e) { + RpmlintLog.logError(e); + } + } + + /** + * Parse a given rpmlint <code>InputStream</code> + * + * @param + * rpmlint <code>InputStream</code> to parse. + * @return + * a <code>RpmlintItem</code> ArrayList. + */ + private ArrayList<RpmlintItem> parseRpmlintOutput(BufferedInputStream in) { + RpmlintItem item = new RpmlintItem(); + ArrayList<RpmlintItem> rpmlintItems = new ArrayList<RpmlintItem>(); + LineNumberReader reader = new LineNumberReader(new InputStreamReader(in)); + String line; + boolean isFirtItemLine = true; + String[] lineItems; + String description = EMPTY_STRING; + try { + while ((line = reader.readLine()) != null) { + if (isFirtItemLine) { + isFirtItemLine = false; + lineItems = line.split(COLON, 4); + item.setFileName(lineItems[0]); + int lineNbr; + + + // FIXME: last rpmlint version (0.83) contain a summary + // line at the bottom of it output, so if we + // detected this line we can safely return rpmlintItems, + // maybe we can find a better way to detect this line. + try { + Integer.parseInt(line.split(SPACE)[0]); + return rpmlintItems; + } catch (NumberFormatException e) { + // this line is not the summary + } + + // TODO: ask rpmlint upstream to display always the same output. + // at the moment the line number is not always displayed. + // If the same output is always used, all the workarounds for the line number can be + // removed. + try { + lineNbr = Integer.parseInt(lineItems[1]); + item.setSeverity(lineItems[2]); + lineItems = lineItems[3].trim().split(SPACE, 2); + } catch (NumberFormatException e) { + // No line number showed for this rpmlint warning. + lineItems = line.split(COLON, 3); + lineNbr = -1; + item.setSeverity(lineItems[1]); + lineItems = lineItems[2].trim().split(SPACE, 2); + } + item.setLineNbr(lineNbr); + item.setId(lineItems[0]); + if (lineItems.length > 1) { + // Maybe this error occur when rpmlint execute 'rpm -q --qf= + // --specfile file.spec' command + RpmlintItem tmpItem = parseRpmOutput(item, lineItems[1]) ; + if (tmpItem == null) { + item.setRefferedContent(lineItems[1]); + } else { + item = tmpItem; + } + } else { + item.setRefferedContent(EMPTY_STRING); + } + } else { + description += line + '\n'; + } + + if (line.equals(EMPTY_STRING)) { + if (item.getMessage() == null) + item.setMessage(description.substring(0, description.length() - 2)); + int useOfTabsAndSpaces = getMixedUseOfTabsAndSpaces(item.getRefferedContent()); + if (useOfTabsAndSpaces != -1) + item.setLineNbr(useOfTabsAndSpaces); + rpmlintItems.add(item); + item = new RpmlintItem(); + + // Reinitialize parser for the next item + isFirtItemLine=true; + description = EMPTY_STRING; + } + + } + // Close the input stream + in.close(); + } catch (IOException e) { + RpmlintLog.logError(e); + } + return rpmlintItems; + } + + private RpmlintItem parseRpmOutput(RpmlintItem item, String line) { + String[] rpmErrorItems = line.split(COLON, 4); + if (item.getId().equalsIgnoreCase("specfile-error")) { //$NON-NLS-1$ + // set severity + item.setSeverity("E"); //$NON-NLS-1$ + } else { + return null; + } + // set line number + try { + if (rpmErrorItems[1].matches(" line [0-9]+$")) { //$NON-NLS-1$ + item.setLineNbr(Integer.parseInt( + rpmErrorItems[1].replace(" line ", ""))); //$NON-NLS-1$ //$NON-NLS-2$ + item.setMessage(rpmErrorItems[2]); + item.setRefferedContent(rpmErrorItems[3]); + } else { + item.setLineNbr(-1); + item.setMessage(rpmErrorItems[1]); + item.setRefferedContent(""); //$NON-NLS-1$ + } + } catch (NumberFormatException e) { + return null; + } + + return item; + } + + /** + * Run rpmlint command on given visitedResources. + * + * @param specContent + * the specfile content + * @return the rpmlint command <code>InputStream</code> + * @throws IOException + */ + private BufferedInputStream runRpmlintCommand(List<String> visitedResources) { + BufferedInputStream in = null; + int i = 2; + String[] cmd = new String[visitedResources.size() + i]; + cmd[0] = Activator.getRpmlintPath(); + cmd[1] = "-i"; //$NON-NLS-1$ + Iterator<String> iterator = visitedResources.iterator(); + while(iterator.hasNext()) { + cmd[i] = iterator.next(); + i++; + } + try { + in = Utils.runCommandToInputStream(cmd); + } catch (IOException e) { + // FIXME: rpmlint is not installed in the default place -> ask user to open the prefs page. + RpmlintLog.logError(e); + } + return in; + } + + /** + * + * Return the line number for given specContent and strToFind, it returns -1 + * if the string to find is not found. + * + * @param specContent The content of the spec file. + * + * @param strToFind The string we are looking for. + * @return the line number + */ + public int getRealLineNbr(String specContent, String strToFind) { + int ret = -1; + if (strToFind.equals(EMPTY_STRING)) { + return ret; + } + String line; + LineNumberReader reader = new LineNumberReader(new StringReader( + specContent)); + try { + while ((line = reader.readLine()) != null) { + if (line.replaceAll("\t| ", EMPTY_STRING).indexOf( //$NON-NLS-1$ + strToFind.replaceAll("\t| ", EMPTY_STRING)) > -1) { //$NON-NLS-1$ + ret = reader.getLineNumber(); + } + } + } catch (IOException e) { + // return -1 if an I/O Exception occure. + } + return ret; + } + + private int getMixedUseOfTabsAndSpaces(String refferedContent){ + int lineNbr = -1; + if (refferedContent.indexOf("(spaces: line") > -1) { //$NON-NLS-1$ + String tabsAndSpacesPref = Activator + .getDefault() + .getPreferenceStore() + .getString(PreferenceConstants.P_RPMLINT_TABS_AND_SPACES); + String[] spacesAndTabs = refferedContent.split("line"); //$NON-NLS-1$ + if (tabsAndSpacesPref == PreferenceConstants.P_RPMLINT_SPACES) + lineNbr = Integer.parseInt(spacesAndTabs[1] + .split(",")[0].trim()); //$NON-NLS-1$ + else + lineNbr = Integer.parseInt(spacesAndTabs[2] + .replaceFirst("\\)", EMPTY_STRING).trim()); //$NON-NLS-1$ + } + return lineNbr; + } + + private ArrayList<List<String>> splitArrayList(ArrayList<String> list, int listSize) { + ArrayList<List<String>> resultList = new ArrayList<List<String>>(); + if (list.size() <= listSize) { + resultList.add(list); + return resultList; + } + for (int i = 0; i < getNumberOfIterations(list, listSize); i++) { + int maxLength = ((i + 1) * listSize > list.size()) ? list + .size() : (i + 1) * listSize; + List<List<String>> sublist = new ArrayList<List<String>>(); + sublist.add(list.subList(i * listSize, maxLength)); + resultList.addAll(sublist); + } + return resultList; + } + + private int getNumberOfIterations(List<String> list, int subCollectionSize) { + return list.size() % subCollectionSize == 0 ? list.size() + / subCollectionSize : (list.size() / subCollectionSize) + 1; + } + + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/Messages.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/Messages.java new file mode 100644 index 0000000000..d6f9629334 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/Messages.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2009 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.preferences; + +import org.eclipse.osgi.util.NLS; + +public class Messages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.linuxtools.rpm.rpmlint.preferences.messages"; //$NON-NLS-1$ + public static String RpmlintPreferencePage_0; + public static String RpmlintPreferencePage_1; + public static String RpmlintPreferencePage_2; + public static String RpmlintPreferencePage_3; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + // should not be instantiated + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/PreferenceConstants.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/PreferenceConstants.java new file mode 100644 index 0000000000..c011951f3c --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/PreferenceConstants.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.rpm.rpmlint.preferences; + +/** + * Constant definitions for rpmlint plug-in preferences + */ +public class PreferenceConstants { + /* + * keys + */ + public static final String P_RPMLINT_PATH = "RpmlintPath"; //$NON-NLS-1$ + public static final String P_RPMLINT_TABS = "RpmlintTab"; //$NON-NLS-1$ + public static final String P_RPMLINT_SPACES = "RpmlintSpaces"; //$NON-NLS-1$ + public static final String P_RPMLINT_TABS_AND_SPACES = P_RPMLINT_SPACES; + + /* + * default values + */ + public static final String DP_RPMLINT_PATH = "/usr/bin/rpmlint"; //$NON-NLS-1$ +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/PreferenceInitializer.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/PreferenceInitializer.java new file mode 100644 index 0000000000..156632caf9 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/PreferenceInitializer.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.rpm.rpmlint.preferences; + +import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.linuxtools.rpm.rpmlint.Activator; + + +/** + * Class used to initialize default preference values. + */ +public class PreferenceInitializer extends AbstractPreferenceInitializer { + + /* + * (non-Javadoc) + * + * @see org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences() + */ + @Override + public void initializeDefaultPreferences() { + IPreferenceStore store = Activator.getDefault().getPreferenceStore(); + store.setDefault(PreferenceConstants.P_RPMLINT_PATH, PreferenceConstants.DP_RPMLINT_PATH); + store.setDefault(PreferenceConstants.P_RPMLINT_TABS_AND_SPACES, PreferenceConstants.P_RPMLINT_SPACES); + + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/RpmlintPreferencePage.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/RpmlintPreferencePage.java new file mode 100644 index 0000000000..40e162dcd4 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/RpmlintPreferencePage.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.preferences; + +import org.eclipse.jface.preference.FieldEditorPreferencePage; +import org.eclipse.jface.preference.FileFieldEditor; +import org.eclipse.jface.preference.RadioGroupFieldEditor; +import org.eclipse.linuxtools.rpm.rpmlint.Activator; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPreferencePage; + +public class RpmlintPreferencePage extends FieldEditorPreferencePage implements + IWorkbenchPreferencePage { + + /** + * default constructor + */ + public RpmlintPreferencePage() { + super(GRID); + setPreferenceStore(Activator.getDefault().getPreferenceStore()); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors() + */ + @Override + public void createFieldEditors() { + FileFieldEditor rpmlintFileFieldEditor = new FileFieldEditor( + PreferenceConstants.P_RPMLINT_PATH, Messages.RpmlintPreferencePage_0, + getFieldEditorParent()); + addField(rpmlintFileFieldEditor); + RadioGroupFieldEditor spacesAndTabsRadioGroup = new RadioGroupFieldEditor( + PreferenceConstants.P_RPMLINT_TABS_AND_SPACES, + Messages.RpmlintPreferencePage_1, 1, new String[][] { + { Messages.RpmlintPreferencePage_2, + PreferenceConstants.P_RPMLINT_SPACES }, + { Messages.RpmlintPreferencePage_3, + PreferenceConstants.P_RPMLINT_TABS } }, + getFieldEditorParent(), true); + addField(spacesAndTabsRadioGroup); + + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench) + */ + public void init(IWorkbench workbench) { + // Do nothing + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/messages.properties b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/messages.properties new file mode 100644 index 0000000000..83fae4a31c --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/preferences/messages.properties @@ -0,0 +1,14 @@ +############################################################################### +# Copyright (c) 2009 Red Hat, Inc. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# Red Hat - initial API and implementation +############################################################################### +RpmlintPreferencePage_0=rpmlint path +RpmlintPreferencePage_1=Mixed use of tabs and spaces +RpmlintPreferencePage_2=Show warnings about spaces +RpmlintPreferencePage_3=Show warnings about tabulations diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/AInsertLineResolution.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/AInsertLineResolution.java new file mode 100644 index 0000000000..225779e8e9 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/AInsertLineResolution.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.linuxtools.rpm.rpmlint.RpmlintLog; +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; + +/** + * Defines the common functionality for a resolution which fix is only inserting line. + * + */ +abstract public class AInsertLineResolution extends ARpmlintResolution { + + protected int markerLine; + + /** + * Returns the line to be inserted for the fix. + * Note: If there are some whitespace requirements for this line(e.g. an empty line after it) + * just append it to the real fix. + * + * @return The entire line for the fix. + */ + public abstract String getLineToInsert(); + + /** + * Returns the number of the line, which will succeed the inserted line. + * + * @param editor The Specfile editor if it's needed for determining the correct place. + * @return The number of the line following the inserted line. + */ + public abstract int getLineNumberForInsert(SpecfileEditor editor); + + /** + * Inserts an entire line at a given position as a resolution for a problem. + * + * @see org.eclipse.ui.IMarkerResolution#run(org.eclipse.core.resources.IMarker) + */ + public void run(IMarker marker) { + markerLine = marker.getAttribute(IMarker.LINE_NUMBER, 0); + SpecfileEditor editor = getEditor(marker); + if (editor == null) { + return; + } + // Get the document + IDocument doc = editor.getSpecfileSourceViewer().getDocument(); + + try { + int index = doc.getLineOffset(getLineNumberForInsert(editor)); + doc.replace(index, 0, getLineToInsert()); + } catch (BadLocationException e) { + RpmlintLog.logError(e); + } + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/ARemoveLineResolution.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/ARemoveLineResolution.java new file mode 100644 index 0000000000..0a5f3512c3 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/ARemoveLineResolution.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.linuxtools.rpm.rpmlint.RpmlintLog; +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; + +abstract public class ARemoveLineResolution extends ARpmlintResolution { + + public void run(IMarker marker) { + + SpecfileEditor editor = getEditor(marker); + if (editor == null) { + return; + } + // Get the document + IDocument doc = editor.getSpecfileSourceViewer().getDocument(); + + try { + int index = doc.getLineOffset(marker.getAttribute(IMarker.LINE_NUMBER, 0)); + int lineLength = doc.getLineLength(marker.getAttribute(IMarker.LINE_NUMBER, 0)); + doc.replace(index, lineLength, ""); //$NON-NLS-1$ + } catch (BadLocationException e) { + RpmlintLog.logError(e); + } + } + + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/AReplaceTextResolution.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/AReplaceTextResolution.java new file mode 100644 index 0000000000..ff028b6f85 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/AReplaceTextResolution.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.linuxtools.rpm.rpmlint.RpmlintLog; +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; + +abstract public class AReplaceTextResolution extends ARpmlintResolution { + + /** + * Returns the original string + * + * @return the original string + */ + abstract public String getOriginalString(); + + /** + * Returns the string to replace in the <code>Document</code> + * + * @return the string to replace + * + */ + abstract public String getReplaceString(); + + public void run(IMarker marker) { + + SpecfileEditor editor = getEditor(marker); + if (editor == null) { + return; + } + IDocument doc = editor.getSpecfileSourceViewer().getDocument(); + + try { + int lineNumber = marker.getAttribute(IMarker.LINE_NUMBER, 0); + int index = doc.getLineOffset(lineNumber); + String line = editor.getSpecfile().getLine(lineNumber); + int rowIndex = line.indexOf(getOriginalString()); + if (rowIndex > -1){ + doc.replace(index+rowIndex, getOriginalString().length(), getReplaceString()); + } + } catch (BadLocationException e) { + RpmlintLog.logError(e); + } + } + + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/ARpmlintResolution.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/ARpmlintResolution.java new file mode 100644 index 0000000000..64d27483f1 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/ARpmlintResolution.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2007, 2009 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.linuxtools.rpm.rpmlint.RpmlintLog; +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; +import org.eclipse.swt.graphics.Image; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IMarkerResolution2; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.ide.IDE; + +/** + * Base class providing common functionality for rpmlint resolution. + * + */ +public abstract class ARpmlintResolution implements IMarkerResolution2 { + + + /** + * No image for rpmlint resolutions for now. + * + * @see org.eclipse.ui.IMarkerResolution2#getImage() + */ + public Image getImage() { + return null; + } + + /** + * Returns the SpecfileEditor for the given IMarker if any. + * + * @param marker The marker to use for retrieving the editor. + * @return The SpecfileEditor this marker is from or null if it's not a SpecfileEditor. + */ + protected SpecfileEditor getEditor(IMarker marker) { + // Open or activate the editor. + IWorkbenchPage page = PlatformUI.getWorkbench() + .getActiveWorkbenchWindow().getActivePage(); + IEditorPart part; + try { + part = IDE.openEditor(page, marker); + } catch (PartInitException e) { + RpmlintLog.logError(e); + return null; + } + // Get the editor's document. + if (!(part instanceof SpecfileEditor)) { + return null; + } + SpecfileEditor editor = (SpecfileEditor) part; + return editor; + } +}
\ No newline at end of file diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/HardcodedPackagerTag.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/HardcodedPackagerTag.java new file mode 100644 index 0000000000..2091dad593 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/HardcodedPackagerTag.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +/** + * Quick fix for the hardcoded-packager-tag warning. + * This is as simple as removing the line. + * + */ +public class HardcodedPackagerTag extends ARemoveLineResolution { + /** + * Rpmlint warning id. + */ + public static final String ID = "hardcoded-packager-tag"; //$NON-NLS-1$ + + /** + * @see org.eclipse.ui.IMarkerResolution2#getDescription() + */ + public String getDescription() { + return Messages.HardcodedPackagerTag_0; + } + + /** + * @see org.eclipse.ui.IMarkerResolution#getLabel() + */ + public String getLabel() { + return ID; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/HardcodedPrefixTag.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/HardcodedPrefixTag.java new file mode 100644 index 0000000000..efb370a5a4 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/HardcodedPrefixTag.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + + +/** + * Quick fix for the hardcoded-prefix-tag warning. + * + */ +public class HardcodedPrefixTag extends ARemoveLineResolution{ + /** + * Rpmlint warning id. + */ + public static final String ID = "hardcoded-prefix-tag"; //$NON-NLS-1$ + + /** + * @see org.eclipse.ui.IMarkerResolution2#getDescription() + */ + public String getDescription() { + return Messages.HardcodedPrefixTag_0; + } + + /** + * @see org.eclipse.ui.IMarkerResolution#getLabel() + */ + public String getLabel() { + return ID; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/MacroInChangelog.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/MacroInChangelog.java new file mode 100644 index 0000000000..b0502b6a33 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/MacroInChangelog.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +public class MacroInChangelog extends AReplaceTextResolution { + + public static final String ID = "macro-in-%changelog"; //$NON-NLS-1$ + + /* + * (non-Javadoc) + * + * @see org.eclipse.linuxtools.rpm.rpmlint.quickfixes.AReplaceTextResolution#getOriginalString() + */ + @Override + public String getOriginalString() { + return "%"; //$NON-NLS-1$ + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.linuxtools.rpm.rpmlint.quickfixes.AReplaceTextResolution#getReplaceString() + */ + @Override + public String getReplaceString() { + return "%%"; //$NON-NLS-1$ + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ui.IMarkerResolution2#getDescription() + */ + public String getDescription() { + return Messages.MacroInChangelog_0 + + Messages.MacroInChangelog_1; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ui.IMarkerResolution#getLabel() + */ + public String getLabel() { + return ID; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/Messages.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/Messages.java new file mode 100644 index 0000000000..bc42a95f26 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/Messages.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2009 Red Hat, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import org.eclipse.osgi.util.NLS; + +public class Messages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.linuxtools.rpm.rpmlint.resolutions.messages"; //$NON-NLS-1$ + public static String HardcodedPackagerTag_0; + public static String HardcodedPrefixTag_0; + public static String MacroInChangelog_0; + public static String MacroInChangelog_1; + public static String NoBuildrootTag_0; + public static String NoBuildSection_0; + public static String NoCleaningOfBuildroot_0; + public static String NoCleanSection_0; + public static String NoInstallSection_0; + public static String NoPrepSection_0; + public static String PatchNotApplied_0; + public static String RpmBuildrootUsage_0; + public static String SetupNotQuiet_0; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + //should not be instantiated + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoBuildSection.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoBuildSection.java new file mode 100644 index 0000000000..3c993ab23d --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoBuildSection.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import java.util.List; + +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; +import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileSection; + +/** + * Resolution for the no-%build-section rpmlint warning. + * Fix is to put an empty %build section. + * + */ +public class NoBuildSection extends AInsertLineResolution { + public static final String ID = "no-%build-section"; //$NON-NLS-1$ + + public String getDescription() { + return Messages.NoBuildSection_0; + } + + public String getLabel() { + return ID; + } + + @Override + public String getLineToInsert() { + return "%build\n\n"; //$NON-NLS-1$ + } + + @Override + public int getLineNumberForInsert(SpecfileEditor editor) { + List<SpecfileSection> sections = editor.getSpecfile().getSections(); + for (SpecfileSection section : sections) { + if (section.getName().equals("install")) { //$NON-NLS-1$ + return section.getLineNumber(); + } + } + return 0; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoBuildrootTag.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoBuildrootTag.java new file mode 100644 index 0000000000..692267df2d --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoBuildrootTag.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import java.util.List; + +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; +import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileSection; + +/** + * Quick fix for the "no-buildroot-tag" error. + * TODO: Provide UI for defining the BuildRoot format. + * + */ +public class NoBuildrootTag extends AInsertLineResolution { + public static final String ID = "no-buildroot-tag"; //$NON-NLS-1$ + + public String getDescription() { + return Messages.NoBuildrootTag_0; + } + + public String getLabel() { + return ID; + } + + @Override + public String getLineToInsert() { + return "BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root\n\n"; //$NON-NLS-1$ + } + + @Override + public int getLineNumberForInsert(SpecfileEditor editor) { + List<SpecfileSection> sections = editor.getSpecfile() + .getComplexSections(); + for (SpecfileSection section : sections) { + if (section.getName().equals("description") //$NON-NLS-1$ + && section.getPackage() == null) { + return section.getLineNumber(); + } + } + return 0; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoCleanSection.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoCleanSection.java new file mode 100644 index 0000000000..e33f791e6f --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoCleanSection.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import java.util.List; + +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; +import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileSection; + +public class NoCleanSection extends AInsertLineResolution { + public static final String ID = "no-%clean-section"; //$NON-NLS-1$ + + public String getDescription() { + return Messages.NoCleanSection_0; + } + + public String getLabel() { + return ID; + } + + @Override + public String getLineToInsert() { + return "%clean\n\n"; //$NON-NLS-1$ + } + + @Override + public int getLineNumberForInsert(SpecfileEditor editor) { + List<SpecfileSection> sections = editor.getSpecfile().getSections(); + for (SpecfileSection section : sections) { + if (section.getName().equals("changelog")) { //$NON-NLS-1$ + return section.getLineNumber(); + } + } + return 0; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoCleaningOfBuildroot.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoCleaningOfBuildroot.java new file mode 100644 index 0000000000..c0118c42b1 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoCleaningOfBuildroot.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import java.util.List; + +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; +import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileSection; + +public class NoCleaningOfBuildroot extends AInsertLineResolution { + + public static final String ID = "no-cleaning-of-buildroot"; //$NON-NLS-1$ + + public String getDescription() { + return Messages.NoCleaningOfBuildroot_0; + } + + public String getLabel() { + return ID; + } + + @Override + public String getLineToInsert() { + return "rm -Rf $RPM_BUILD_ROOT\n"; //$NON-NLS-1$ + } + + @Override + public int getLineNumberForInsert(SpecfileEditor editor) { + List<SpecfileSection> sections = editor.getSpecfile().getSections(); + for (SpecfileSection section : sections) { + if (section.getName().equals("install") //$NON-NLS-1$ + || section.getName().equals("clean")) { //$NON-NLS-1$ + if (markerLine == section.getLineNumber()) { + return section.getLineNumber() + 1; + } + } + } + return 0; + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoInstallSection.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoInstallSection.java new file mode 100644 index 0000000000..b695307a98 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoInstallSection.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import java.util.List; + +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; +import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileSection; + +public class NoInstallSection extends AInsertLineResolution { + public static final String ID = "no-%install-section"; //$NON-NLS-1$ + + public String getDescription() { + return Messages.NoInstallSection_0; + } + + public String getLabel() { + return ID; + } + + @Override + public String getLineToInsert() { + return "%install\n\n"; //$NON-NLS-1$ + } + + @Override + public int getLineNumberForInsert(SpecfileEditor editor) { + List<SpecfileSection> sections = editor.getSpecfile().getSections(); + for (SpecfileSection section : sections) { + if (section.getName().equals("clean")) { //$NON-NLS-1$ + return section.getLineNumber(); + } + } + return 0; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoPrepSection.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoPrepSection.java new file mode 100644 index 0000000000..eb92cd2c0f --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/NoPrepSection.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import java.util.List; + +import org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor; +import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileSection; + +public class NoPrepSection extends AInsertLineResolution{ + public static final String ID = "no-%prep-section"; //$NON-NLS-1$ + + public String getDescription() { + return Messages.NoPrepSection_0; + } + + public String getLabel() { + return ID; + } + + @Override + public String getLineToInsert() { + return "%prep\n\n"; //$NON-NLS-1$ + } + + @Override + public int getLineNumberForInsert(SpecfileEditor editor) { + List<SpecfileSection> sections = editor.getSpecfile().getSections(); + for (SpecfileSection section : sections) { + if (section.getName().equals("build")) { //$NON-NLS-1$ + return section.getLineNumber(); + } + } + return 0; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/PatchNotApplied.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/PatchNotApplied.java new file mode 100644 index 0000000000..7027b653b7 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/PatchNotApplied.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +public class PatchNotApplied extends ARemoveLineResolution { + + public static final String ID = "patch-not-applied"; //$NON-NLS-1$ + + /* (non-Javadoc) + * @see org.eclipse.ui.IMarkerResolution2#getDescription() + */ + public String getDescription() { + return Messages.PatchNotApplied_0; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IMarkerResolution#getLabel() + */ + public String getLabel() { + return ID; + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/RpmBuildrootUsage.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/RpmBuildrootUsage.java new file mode 100644 index 0000000000..649976f4fa --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/RpmBuildrootUsage.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2008 Alexander Kurtakov. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alexander Kurtakov - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +public class RpmBuildrootUsage extends ARemoveLineResolution{ + public static final String ID = "rpm-buildroot-usage"; //$NON-NLS-1$ + + /* + * (non-Javadoc) + * + * @see org.eclipse.ui.IMarkerResolution2#getDescription() + */ + public String getDescription() { + return Messages.RpmBuildrootUsage_0; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ui.IMarkerResolution#getLabel() + */ + public String getLabel() { + return ID; + } +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/RpmlintMarkerResolutionGenerator.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/RpmlintMarkerResolutionGenerator.java new file mode 100644 index 0000000000..1f26d3a38c --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/RpmlintMarkerResolutionGenerator.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.ui.IMarkerResolution; +import org.eclipse.ui.IMarkerResolutionGenerator2; + +public class RpmlintMarkerResolutionGenerator implements IMarkerResolutionGenerator2 { + + // rpmlint error id atribut name + public static final String RPMLINT_ERROR_ID = "rpmlintErrorId"; //$NON-NLS-1$ + + // rpmlint refered text + public static final String RPMLINT_REFFERED_CONTENT = "rpmlintrefferedContent"; //$NON-NLS-1$ + + + /* (non-Javadoc) + * @see org.eclipse.ui.IMarkerResolutionGenerator2#hasResolutions(org.eclipse.core.resources.IMarker) + */ + public boolean hasResolutions(IMarker marker) { + String rpmlintErrorId = getRpmlintErrorId(marker); + if (rpmlintErrorId.equals(SetupNotQuiet.ID)) { + return true; + } else if (rpmlintErrorId.equals(PatchNotApplied.ID)) { + return true; + } else if (rpmlintErrorId.equals(NoBuildrootTag.ID)) { + return true; + } else if (rpmlintErrorId.equals(NoCleaningOfBuildroot.ID)){ + return true; + } else if (rpmlintErrorId.equals(NoBuildSection.ID)){ + return true; + } else if (rpmlintErrorId.equals(MacroInChangelog.ID)){ + return true; + } else if (rpmlintErrorId.equals(RpmBuildrootUsage.ID)){ + return true; + } else if (rpmlintErrorId.equals(HardcodedPrefixTag.ID)){ + return true; + } else if (rpmlintErrorId.equals(HardcodedPackagerTag.ID)){ + return true; + } else if (rpmlintErrorId.equals(NoPrepSection.ID)){ + return true; + } else if (rpmlintErrorId.equals(NoInstallSection.ID)){ + return true; + } else if (rpmlintErrorId.equals(NoCleanSection.ID)){ + return true; + } + + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IMarkerResolutionGenerator#getResolutions(org.eclipse.core.resources.IMarker) + */ + public IMarkerResolution[] getResolutions(IMarker marker) { + List<IMarkerResolution> resolutions = new ArrayList<IMarkerResolution>(); + String rpmlintErrorId = getRpmlintErrorId(marker); + if (rpmlintErrorId.equals(SetupNotQuiet.ID)) { + resolutions.add(new SetupNotQuiet()); + } else if (rpmlintErrorId.equals(PatchNotApplied.ID)) { + resolutions.add(new PatchNotApplied()); + } else if (rpmlintErrorId.equals(NoBuildrootTag.ID)) { + resolutions.add(new NoBuildrootTag()); + } else if (rpmlintErrorId.equals(NoCleaningOfBuildroot.ID)){ + resolutions.add(new NoCleaningOfBuildroot()); + } else if (rpmlintErrorId.equals(NoBuildSection.ID)){ + resolutions.add(new NoBuildSection()); + } else if (rpmlintErrorId.equals(MacroInChangelog.ID)){ + resolutions.add(new MacroInChangelog()); + } else if (rpmlintErrorId.equals(RpmBuildrootUsage.ID)){ + resolutions.add(new RpmBuildrootUsage()); + } else if (rpmlintErrorId.equals(HardcodedPrefixTag.ID)){ + resolutions.add(new HardcodedPrefixTag()); + } else if (rpmlintErrorId.equals(HardcodedPackagerTag.ID)){ + resolutions.add(new HardcodedPackagerTag()); + } else if (rpmlintErrorId.equals(NoPrepSection.ID)){ + resolutions.add(new NoPrepSection()); + } else if (rpmlintErrorId.equals(NoInstallSection.ID)){ + resolutions.add(new NoInstallSection()); + } else if (rpmlintErrorId.equals(NoCleanSection.ID)){ + resolutions.add(new NoCleanSection()); + } + + return resolutions.toArray(new IMarkerResolution[resolutions.size()]); + } + + /** + * Return the rpmlint error id attribute for the specified marker + * + * @param marker the marker to check + * @return the rpmlint error id or <code>""</code> if none + */ + private String getRpmlintErrorId(IMarker marker) { + return marker.getAttribute(RPMLINT_ERROR_ID, ""); //$NON-NLS-1$ + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/SetupNotQuiet.java b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/SetupNotQuiet.java new file mode 100644 index 0000000000..c0b2f4a609 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/SetupNotQuiet.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (c) 2007 Alphonse Van Assche. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Alphonse Van Assche - initial API and implementation + *******************************************************************************/ +package org.eclipse.linuxtools.rpm.rpmlint.resolutions; + +public class SetupNotQuiet extends AReplaceTextResolution { + + public static final String ID = "setup-not-quiet"; //$NON-NLS-1$ + + /* (non-Javadoc) + * @see org.eclipse.linuxtools.rpm.rpmlint.quickfixes.AReplaceTextResolution#getOriginalString() + */ + @Override + public String getOriginalString() { + return "%setup"; //$NON-NLS-1$ + } + + /* (non-Javadoc) + * @see org.eclipse.linuxtools.rpm.rpmlint.quickfixes.AReplaceTextResolution#getReplaceString() + */ + @Override + public String getReplaceString() { + return "%setup -q"; //$NON-NLS-1$ + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IMarkerResolution2#getDescription() + */ + public String getDescription() { + return Messages.SetupNotQuiet_0; + } + + /* (non-Javadoc) + * @see org.eclipse.ui.IMarkerResolution#getLabel() + */ + public String getLabel() { + return ID; + } + +} diff --git a/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/messages.properties b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/messages.properties new file mode 100644 index 0000000000..b2656de764 --- /dev/null +++ b/rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/rpm/rpmlint/resolutions/messages.properties @@ -0,0 +1,23 @@ +############################################################################### +# Copyright (c) 2009 Red Hat, Inc. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# Red Hat - initial API and implementation +############################################################################### +HardcodedPackagerTag_0=The Packager tag is hardcoded in your spec file. It should be removed, so as to use rebuilder's own defaults. +HardcodedPrefixTag_0=The Prefix tag is hardcoded in your spec file. It should be removed, so as to allow package relocation. +MacroInChangelog_0=Macros are expanded in %changelog too, which can in unfortunate cases lead +MacroInChangelog_1=to the package not building at all, or other subtle unexpected conditions that\taffect the build. +NoBuildrootTag_0=The BuildRoot tag isn't used in your spec. It must be used in order to allow building the package as non root on some systems. +NoBuildSection_0=Insert empty %build section +NoCleaningOfBuildroot_0=You should clean $RPM_BUILD_ROOT in the %clean section and just after the beginning of %install section. Use "rm -Rf $RPM_BUILD_ROOT" +NoCleanSection_0=Insert empty %clean section +NoInstallSection_0=Insert empty %install section +NoPrepSection_0=Insert empty %prep section +PatchNotApplied_0=A patch is included in your package but was not applied. Refer to the patches documentation to see what's wrong. +RpmBuildrootUsage_0=$RPM_BUILD_ROOT should not be touched during %build or %prep stage, as it will break short circuiting. +SetupNotQuiet_0=Use the -q option to the %setup macro to avoid useless build output from unpacking the sources |