mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
work in progress - MakeTargetProvider/MakeTarget
This commit is contained in:
parent
7f3646cfb0
commit
b9f356214f
6 changed files with 240 additions and 103 deletions
|
@ -68,10 +68,10 @@
|
|||
</extension>
|
||||
<extension
|
||||
point="org.eclipse.cdt.make.core.MakeTargetBuilder">
|
||||
<builder
|
||||
builderID="org.eclipse.cdt.core.make.makeBuilder"
|
||||
id="org.eclipse.cdt.make.MakeTargetBuilder">
|
||||
</builder>
|
||||
<builder
|
||||
builderID="org.eclipse.cdt.core.make.makeBuilder"
|
||||
id="org.eclipse.cdt.make.MakeTargetBuilder">
|
||||
</builder>
|
||||
</extension>
|
||||
|
||||
</plugin>
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
<element name="extension">
|
||||
<complexType>
|
||||
<sequence>
|
||||
<element ref="builder"/>
|
||||
</sequence>
|
||||
<attribute name="point" type="string" use="required">
|
||||
<annotation>
|
||||
|
|
|
@ -31,7 +31,7 @@ public interface IMakeTarget {
|
|||
void setBuildCommand(IPath command);
|
||||
|
||||
String getBuildArguments();
|
||||
void setBuildArguments();
|
||||
void setBuildArguments(String arguments);
|
||||
|
||||
IContainer getContainer();
|
||||
|
||||
|
|
|
@ -16,7 +16,9 @@ import org.eclipse.core.runtime.IProgressMonitor;
|
|||
|
||||
public class MakeTarget implements IMakeTarget {
|
||||
|
||||
MakeTarget(String targetBuilderID, String targetName) {
|
||||
private boolean bDirty;
|
||||
|
||||
MakeTarget(IContainer container, String targetBuilderID, String targetName) {
|
||||
// dinglis-TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
|
@ -78,7 +80,7 @@ public class MakeTarget implements IMakeTarget {
|
|||
return null;
|
||||
}
|
||||
|
||||
public void setBuildArguments() {
|
||||
public void setBuildArguments(String arguments) {
|
||||
// dinglis-TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
@ -92,4 +94,8 @@ public class MakeTarget implements IMakeTarget {
|
|||
// dinglis-TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public boolean isDirty() {
|
||||
return bDirty;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,14 +11,10 @@
|
|||
package org.eclipse.cdt.make.internal.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.eclipse.cdt.make.core.IMakeTarget;
|
||||
import org.eclipse.cdt.make.core.IMakeTargetListener;
|
||||
import org.eclipse.cdt.make.core.IMakeTargetProvider;
|
||||
|
@ -39,17 +35,10 @@ import org.eclipse.core.runtime.IExtensionPoint;
|
|||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
public class MakeTargetProvider implements IMakeTargetProvider, IResourceChangeListener {
|
||||
private static String TARGET_BUILD_EXT = MakeCorePlugin.getUniqueIdentifier() + ".MakeTargetBuilder"; //$NON-NLS-1$
|
||||
|
||||
private static String BUILD_TARGET_ELEMENT = "buildTargets"; //$NON-NLS-1$
|
||||
private static String TARGET_ELEMENT = "target"; //$NON-NLS-1$
|
||||
|
||||
private ListenerList listeners = new ListenerList();
|
||||
private HashMap projectMap = new HashMap();
|
||||
private HashMap builderMap;
|
||||
|
@ -61,101 +50,46 @@ public class MakeTargetProvider implements IMakeTargetProvider, IResourceChangeL
|
|||
if (container instanceof IWorkspaceRoot) {
|
||||
throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetProvider.add_to_workspace_root"), null)); //$NON-NLS-1$
|
||||
}
|
||||
IProject project = container.getProject();
|
||||
HashMap targetMap = (HashMap) projectMap.get(project);
|
||||
if (targetMap == null) {
|
||||
targetMap = initializeTargets(project);
|
||||
ProjectTargets projectTargets = (ProjectTargets)projectMap.get(container.getProject());
|
||||
if (projectTargets == null) {
|
||||
projectTargets = readTargets(container.getProject());
|
||||
}
|
||||
ArrayList list = (ArrayList) targetMap.get(container);
|
||||
MakeTarget target = new MakeTarget(targetBuilderID, targetName);
|
||||
if (list != null && list.contains(target)) {
|
||||
throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetProvider.target_exists"), null)); //$NON-NLS-1$
|
||||
}
|
||||
target.setContainer(container);
|
||||
if (list == null) {
|
||||
list = new ArrayList();
|
||||
targetMap.put(container, list);
|
||||
}
|
||||
list.add(target);
|
||||
MakeTarget target = new MakeTarget(container, targetBuilderID, targetName);
|
||||
projectTargets.add(target);
|
||||
notifyListeners(new MakeTargetEvent(this, MakeTargetEvent.TARGET_ADD, target));
|
||||
return target;
|
||||
}
|
||||
|
||||
public void removeTarget(IMakeTarget target) throws CoreException {
|
||||
IProject project = target.getContainer().getProject();
|
||||
HashMap targetMap = (HashMap) projectMap.get(project);
|
||||
if (targetMap == null) {
|
||||
targetMap = initializeTargets(project);
|
||||
}
|
||||
ArrayList list = (ArrayList) targetMap.get(target.getContainer());
|
||||
if (list != null && !list.contains(target)) {
|
||||
return;
|
||||
}
|
||||
list.remove(target);
|
||||
if (list.size() == 0) {
|
||||
targetMap.remove(list);
|
||||
}
|
||||
if (targetMap.size() == 0) {
|
||||
projectMap.remove(project);
|
||||
ProjectTargets projectTargets = (ProjectTargets)projectMap.get(project);
|
||||
if (projectTargets == null) {
|
||||
projectTargets = readTargets(project);
|
||||
}
|
||||
projectTargets.remove(target);
|
||||
notifyListeners(new MakeTargetEvent(this, MakeTargetEvent.TARGET_REMOVED, target));
|
||||
}
|
||||
|
||||
public void renameTarget(IMakeTarget target, String name) throws CoreException {
|
||||
IProject project = target.getContainer().getProject();
|
||||
HashMap targetMap = (HashMap) projectMap.get(project);
|
||||
if (targetMap == null) {
|
||||
targetMap = initializeTargets(project);
|
||||
ProjectTargets projectTargets = (ProjectTargets)projectMap.get(project);
|
||||
if (projectTargets == null) {
|
||||
projectTargets = readTargets(project);
|
||||
}
|
||||
ArrayList list = (ArrayList) targetMap.get(target.getContainer());
|
||||
if (list != null && !list.contains(target)) {
|
||||
throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetProvider.target_does_not_exists"), null)); //$NON-NLS-1$
|
||||
if (!projectTargets.contains((MakeTarget)target)) {
|
||||
throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetProvider.target_exists"), null)); //$NON-NLS-1$
|
||||
}
|
||||
((MakeTarget) target).setName(name);
|
||||
((MakeTarget)target).setName(name);
|
||||
projectTargets.setDirty();
|
||||
notifyListeners(new MakeTargetEvent(this, MakeTargetEvent.TARGET_CHANGED, target));
|
||||
}
|
||||
|
||||
public IMakeTarget[] getTargets(IContainer container) throws CoreException {
|
||||
IProject project = container.getProject();
|
||||
HashMap targetMap = (HashMap) projectMap.get(project);
|
||||
if (targetMap == null) {
|
||||
targetMap = initializeTargets(project);
|
||||
ProjectTargets projectTargets = (ProjectTargets)projectMap.get(container.getProject());
|
||||
if (projectTargets == null) {
|
||||
projectTargets = readTargets(container.getProject());
|
||||
}
|
||||
ArrayList list = (ArrayList) targetMap.get(container);
|
||||
if (list != null) {
|
||||
return (IMakeTarget[]) list.toArray(new IMakeTarget[list.size()]);
|
||||
}
|
||||
return new IMakeTarget[0];
|
||||
}
|
||||
|
||||
private HashMap initializeTargets(IProject project) throws CoreException {
|
||||
HashMap targetMap = new HashMap();
|
||||
IPath targetFilePath = MakeCorePlugin.getDefault().getStateLocation().append(project.getName());
|
||||
File targetFile = targetFilePath.toFile();
|
||||
if (targetFile.exists()) {
|
||||
try {
|
||||
FileInputStream file = new FileInputStream(targetFile);
|
||||
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = parser.parse(file);
|
||||
Node node = document.getFirstChild();
|
||||
if (node.getNodeName().equals(BUILD_TARGET_ELEMENT)) {
|
||||
NodeList list = node.getChildNodes();
|
||||
for( int i = 0; i < list.getLength(); i++) {
|
||||
Node item = list.item(i);
|
||||
if ( item.getNodeName().equals(TARGET_ELEMENT)) {
|
||||
NamedNodeMap attr = item.getAttributes();
|
||||
MakeTarget target = new MakeTarget(attr.getNamedItem("targetID").getNodeValue(), attr.getNamedItem("name").getNodeValue()); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
|
||||
// targetMap.put(container, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new CoreException(
|
||||
new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetProvider.failed_initializing_targets"), e)); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
return targetMap;
|
||||
return projectTargets.get(container);
|
||||
}
|
||||
|
||||
public IProject[] getTargetBuilderProjects() throws CoreException {
|
||||
|
@ -171,7 +105,7 @@ public class MakeTargetProvider implements IMakeTargetProvider, IResourceChangeL
|
|||
}
|
||||
}
|
||||
}
|
||||
return (IProject[]) tProj.toArray(new IProject[tProj.size()]);
|
||||
return (IProject[])tProj.toArray(new IProject[tProj.size()]);
|
||||
}
|
||||
|
||||
public void startup() {
|
||||
|
@ -183,7 +117,33 @@ public class MakeTargetProvider implements IMakeTargetProvider, IResourceChangeL
|
|||
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
|
||||
}
|
||||
|
||||
private void initializeBuilders() {
|
||||
public void resourceChanged(IResourceChangeEvent event) {
|
||||
// dinglis-TODO listen for project that add/remove a target type builder
|
||||
}
|
||||
|
||||
protected void writeTargets(ProjectTargets projectTargets) {
|
||||
IPath targetFilePath = MakeCorePlugin.getDefault().getStateLocation().append(projectTargets.getProject().getName());
|
||||
File targetFile = targetFilePath.toFile();
|
||||
try {
|
||||
|
||||
FileOutputStream file = new FileOutputStream(targetFile);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
protected ProjectTargets readTargets(IProject project) throws CoreException {
|
||||
IPath targetFilePath = MakeCorePlugin.getDefault().getStateLocation().append(project.getName());
|
||||
File targetFile = targetFilePath.toFile();
|
||||
if (targetFile.exists()) {
|
||||
try {
|
||||
return new ProjectTargets(project, targetFile);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
return new ProjectTargets(project);
|
||||
}
|
||||
|
||||
protected void initializeBuilders() {
|
||||
builderMap = new HashMap();
|
||||
|
||||
IExtensionPoint point = MakeCorePlugin.getDefault().getDescriptor().getExtensionPoint(MakeTargetProvider.TARGET_BUILD_EXT);
|
||||
|
@ -200,10 +160,10 @@ public class MakeTargetProvider implements IMakeTargetProvider, IResourceChangeL
|
|||
}
|
||||
}
|
||||
|
||||
private void notifyListeners(MakeTargetEvent event) {
|
||||
protected void notifyListeners(MakeTargetEvent event) {
|
||||
Object[] list = listeners.getListeners();
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
((IMakeTargetListener) list[i]).targetChanged(event);
|
||||
((IMakeTargetListener)list[i]).targetChanged(event);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -214,9 +174,4 @@ public class MakeTargetProvider implements IMakeTargetProvider, IResourceChangeL
|
|||
public void removeListener(IMakeTargetListener listener) {
|
||||
listeners.remove(listeners);
|
||||
}
|
||||
|
||||
public void resourceChanged(IResourceChangeEvent event) {
|
||||
// dinglis-TODO listen for project that add/remove a target type builder
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,175 @@
|
|||
package org.eclipse.cdt.make.internal.core;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.apache.xerces.dom.DocumentImpl;
|
||||
import org.apache.xml.serialize.Method;
|
||||
import org.apache.xml.serialize.OutputFormat;
|
||||
import org.apache.xml.serialize.Serializer;
|
||||
import org.apache.xml.serialize.SerializerFactory;
|
||||
import org.eclipse.cdt.make.core.IMakeTarget;
|
||||
import org.eclipse.cdt.make.core.MakeCorePlugin;
|
||||
import org.eclipse.core.resources.IContainer;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
public class ProjectTargets {
|
||||
private static String BUILD_TARGET_ELEMENT = "buildTargets"; //$NON-NLS-1$
|
||||
private static String TARGET_ELEMENT = "target"; //$NON-NLS-1$
|
||||
|
||||
private boolean isDirty;
|
||||
private HashMap targetMap = new HashMap();
|
||||
private IProject project;
|
||||
|
||||
public ProjectTargets(IProject project) {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
public ProjectTargets(IProject project, File targetFile) throws CoreException {
|
||||
Document document = null;
|
||||
try {
|
||||
FileInputStream file = new FileInputStream(targetFile);
|
||||
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
document = parser.parse(file);
|
||||
} catch (Exception e) {
|
||||
throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, "Error reading target file", e));
|
||||
}
|
||||
Node node = document.getFirstChild();
|
||||
if (node.getNodeName().equals(BUILD_TARGET_ELEMENT)) {
|
||||
NodeList list = node.getChildNodes();
|
||||
for (int i = 0; i < list.getLength(); i++) {
|
||||
node = list.item(i);
|
||||
if (node.getNodeName().equals(TARGET_ELEMENT)) {
|
||||
IContainer container = null;
|
||||
NamedNodeMap attr = node.getAttributes();
|
||||
String path = attr.getNamedItem("targetID").getNodeValue();
|
||||
if (path != null) {
|
||||
container = project.getFolder(path);
|
||||
} else {
|
||||
container = project;
|
||||
}
|
||||
MakeTarget target = new MakeTarget(container, attr.getNamedItem("targetID").getNodeValue(), attr.getNamedItem("name").getNodeValue()); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
String option = getString(node, "stopOnError");
|
||||
if (option != null) {
|
||||
target.setStopOnError(Boolean.valueOf(option).booleanValue());
|
||||
}
|
||||
option = getString(node, "useDefaultCommand");
|
||||
if (option != null) {
|
||||
target.setUseDefaultBuildCmd(Boolean.valueOf(option).booleanValue());
|
||||
}
|
||||
option = getString(node, "buildCommand");
|
||||
if (option != null) {
|
||||
target.setBuildCommand(new Path(option));
|
||||
}
|
||||
option = getString(node, "buildArguments");
|
||||
if (option != null) {
|
||||
target.setBuildArguments(option);
|
||||
}
|
||||
add(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected String getString(Node target, String tagName) {
|
||||
Node node = searchNode(target, tagName);
|
||||
return node != null ? (node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue()) : null;
|
||||
}
|
||||
|
||||
protected Node searchNode(Node target, String tagName) {
|
||||
NodeList list = target.getChildNodes();
|
||||
for (int i = 0; i < list.getLength(); i++) {
|
||||
if (list.item(i).getNodeName().equals(tagName))
|
||||
return list.item(i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public IMakeTarget[] get(IContainer container) {
|
||||
ArrayList list = (ArrayList)targetMap.get(container);
|
||||
if (list != null) {
|
||||
return (IMakeTarget[])list.toArray(new IMakeTarget[list.size()]);
|
||||
}
|
||||
return new IMakeTarget[0];
|
||||
}
|
||||
|
||||
public void add(MakeTarget target) throws CoreException {
|
||||
ArrayList list = (ArrayList)targetMap.get(target.getContainer());
|
||||
if (list != null && list.contains(target)) {
|
||||
throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetProvider.target_exists"), null)); //$NON-NLS-1$
|
||||
}
|
||||
if (list == null) {
|
||||
list = new ArrayList();
|
||||
targetMap.put(target.getContainer(), list);
|
||||
}
|
||||
list.add(target);
|
||||
}
|
||||
|
||||
public boolean contains(MakeTarget target) {
|
||||
ArrayList list = (ArrayList)targetMap.get(target.getContainer());
|
||||
if (list != null && list.contains(target)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void remove(IMakeTarget target) {
|
||||
ArrayList list = (ArrayList)targetMap.get(target.getContainer());
|
||||
if (list != null && !list.contains(target)) {
|
||||
return;
|
||||
}
|
||||
list.remove(target);
|
||||
if (list.size() == 0) {
|
||||
targetMap.remove(list);
|
||||
}
|
||||
}
|
||||
|
||||
public void setDirty() {
|
||||
isDirty = true;
|
||||
}
|
||||
|
||||
public boolean isDirty() {
|
||||
return isDirty;
|
||||
}
|
||||
|
||||
public IProject getProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
protected String getAsXML() throws IOException {
|
||||
Document doc = new DocumentImpl();
|
||||
Element configRootElement = doc.createElement(BUILD_TARGET_ELEMENT);
|
||||
doc.appendChild(configRootElement);
|
||||
return serializeDocument(doc);
|
||||
}
|
||||
|
||||
protected String serializeDocument(Document doc) throws IOException {
|
||||
ByteArrayOutputStream s = new ByteArrayOutputStream();
|
||||
OutputFormat format = new OutputFormat();
|
||||
format.setIndenting(true);
|
||||
format.setLineSeparator(System.getProperty("line.separator")); //$NON-NLS-1$
|
||||
Serializer serializer =
|
||||
SerializerFactory.getSerializerFactory(Method.XML).makeSerializer(new OutputStreamWriter(s, "UTF8"), format);
|
||||
serializer.asDOMSerializer().serialize(doc);
|
||||
return s.toString("UTF8"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue