1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-30 03:33:37 +02:00

move target presistance to .cdtproject

modified targets are now saved
This commit is contained in:
David Inglis 2004-03-16 19:57:02 +00:00
parent e74475bdbe
commit a46dca30a1
4 changed files with 223 additions and 116 deletions

View file

@ -20,18 +20,18 @@ public interface IMakeTarget {
String getTargetBuilderID(); String getTargetBuilderID();
boolean isStopOnError(); boolean isStopOnError();
void setStopOnError(boolean stopOnError); void setStopOnError(boolean stopOnError) throws CoreException;
boolean isDefaultBuildCmd(); boolean isDefaultBuildCmd();
void setUseDefaultBuildCmd(boolean useDefault); void setUseDefaultBuildCmd(boolean useDefault) throws CoreException;
void setBuildTarget(String target); void setBuildTarget(String target) throws CoreException;
String getBuildTarget() ; String getBuildTarget() ;
IPath getBuildCommand(); IPath getBuildCommand();
void setBuildCommand(IPath command); void setBuildCommand(IPath command) throws CoreException;
String getBuildArguments(); String getBuildArguments();
void setBuildArguments(String arguments); void setBuildArguments(String arguments) throws CoreException;
/** /**

View file

@ -23,15 +23,15 @@ import org.eclipse.core.runtime.Path;
public class MakeTarget implements IMakeTarget { public class MakeTarget implements IMakeTarget {
private final MakeTargetManager manager;
private String name;
private String target; private String target;
private String buildArguments; private String buildArguments;
private IPath buildCommand; private IPath buildCommand;
private boolean isDefaultBuildCmd; private boolean isDefaultBuildCmd;
private boolean isStopOnError; private boolean isStopOnError;
private String name;
private String targetBuilderID; private String targetBuilderID;
private IContainer container; private IContainer container;
private MakeTargetManager manager;
MakeTarget(MakeTargetManager manager, IProject project, String targetBuilderID, String name) throws CoreException { MakeTarget(MakeTargetManager manager, IProject project, String targetBuilderID, String name) throws CoreException {
this.manager = manager; this.manager = manager;
@ -64,32 +64,36 @@ public class MakeTarget implements IMakeTarget {
return isStopOnError; return isStopOnError;
} }
public void setStopOnError(boolean stopOnError) { public void setStopOnError(boolean stopOnError) throws CoreException {
isStopOnError = stopOnError; isStopOnError = stopOnError;
manager.updateTarget(this);
} }
public boolean isDefaultBuildCmd() { public boolean isDefaultBuildCmd() {
return isDefaultBuildCmd; return isDefaultBuildCmd;
} }
public void setUseDefaultBuildCmd(boolean useDefault) { public void setUseDefaultBuildCmd(boolean useDefault) throws CoreException {
isDefaultBuildCmd = useDefault; isDefaultBuildCmd = useDefault;
manager.updateTarget(this);
} }
public IPath getBuildCommand() { public IPath getBuildCommand() {
return buildCommand != null ? buildCommand: new Path(""); //$NON-NLS-1$ return buildCommand != null ? buildCommand: new Path(""); //$NON-NLS-1$
} }
public void setBuildCommand(IPath command) { public void setBuildCommand(IPath command) throws CoreException {
buildCommand = command; buildCommand = command;
manager.updateTarget(this);
} }
public String getBuildArguments() { public String getBuildArguments() {
return buildArguments != null ? buildArguments : ""; //$NON-NLS-1$ return buildArguments != null ? buildArguments : ""; //$NON-NLS-1$
} }
public void setBuildArguments(String arguments) { public void setBuildArguments(String arguments) throws CoreException {
buildArguments = arguments; buildArguments = arguments;
manager.updateTarget(this);
} }
public IContainer getContainer() { public IContainer getContainer() {
@ -133,9 +137,9 @@ public class MakeTarget implements IMakeTarget {
project.build(IncrementalProjectBuilder.FULL_BUILD, builderID, infoMap, monitor); project.build(IncrementalProjectBuilder.FULL_BUILD, builderID, infoMap, monitor);
} }
public void setBuildTarget(String target) { public void setBuildTarget(String target) throws CoreException {
this.target = target; this.target = target;
manager.updateTarget(this);
} }
public String getBuildTarget() { public String getBuildTarget() {

View file

@ -11,9 +11,6 @@
package org.eclipse.cdt.make.internal.core; package org.eclipse.cdt.make.internal.core;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -102,8 +99,7 @@ public class MakeTargetManager implements IMakeTargetManager, IResourceChangeLis
throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetManager.target_exists"), null)); //$NON-NLS-1$ throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetManager.target_exists"), null)); //$NON-NLS-1$
} }
((MakeTarget)target).setName(name); ((MakeTarget)target).setName(name);
writeTargets(projectTargets); updateTarget((MakeTarget) target);
notifyListeners(new MakeTargetEvent(this, MakeTargetEvent.TARGET_CHANGED, target));
} }
public IMakeTarget[] getTargets(IContainer container) throws CoreException { public IMakeTarget[] getTargets(IContainer container) throws CoreException {
@ -242,14 +238,21 @@ public class MakeTargetManager implements IMakeTargetManager, IResourceChangeLis
} }
} }
protected void updateTarget(MakeTarget target) throws CoreException {
if (target.getContainer() != null ) { // target has not been added to manager.
IProject project = target.getContainer().getProject();
ProjectTargets projectTargets = (ProjectTargets)projectMap.get(project);
if (projectTargets == null || !projectTargets.contains(target)) {
return; // target has not been added to manager.
}
writeTargets(projectTargets);
notifyListeners(new MakeTargetEvent(this, MakeTargetEvent.TARGET_CHANGED, target));
}
}
protected void writeTargets(ProjectTargets projectTargets) throws CoreException { protected void writeTargets(ProjectTargets projectTargets) throws CoreException {
IPath targetFilePath =
MakeCorePlugin.getDefault().getStateLocation().append(projectTargets.getProject().getName()).addFileExtension(
TARGETS_EXT);
File targetFile = targetFilePath.toFile();
try { try {
FileOutputStream file = new FileOutputStream(targetFile); projectTargets.saveTargets();
projectTargets.saveTargets(file);
} catch (IOException e) { } catch (IOException e) {
throw new CoreException( throw new CoreException(
new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetManager.error_writing_file"), e)); //$NON-NLS-1$ new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetManager.error_writing_file"), e)); //$NON-NLS-1$
@ -257,24 +260,13 @@ public class MakeTargetManager implements IMakeTargetManager, IResourceChangeLis
} }
protected ProjectTargets readTargets(IProject project) throws CoreException { protected ProjectTargets readTargets(IProject project) throws CoreException {
IPath targetFilePath = ProjectTargets projectTargets = new ProjectTargets(this, project);
MakeCorePlugin.getDefault().getStateLocation().append(project.getName()).addFileExtension(TARGETS_EXT);
File targetFile = targetFilePath.toFile();
ProjectTargets projectTargets = null;
if (targetFile.exists()) {
try {
projectTargets = new ProjectTargets(this, project, new FileInputStream(targetFile));
} catch (FileNotFoundException e) {
}
}
if (projectTargets == null) {
projectTargets = new ProjectTargets(project);
}
projectMap.put(project, projectTargets); projectMap.put(project, projectTargets);
return projectTargets; return projectTargets;
} }
protected void deleteTargets(IProject project) { protected void deleteTargets(IProject project) {
//Historical: We clean up after all other parts.
IPath targetFilePath = IPath targetFilePath =
MakeCorePlugin.getDefault().getStateLocation().append(project.getName()).addFileExtension(TARGETS_EXT); MakeCorePlugin.getDefault().getStateLocation().append(project.getName()).addFileExtension(TARGETS_EXT);
File targetFile = targetFilePath.toFile(); File targetFile = targetFilePath.toFile();

View file

@ -1,28 +1,27 @@
package org.eclipse.cdt.make.internal.core; package org.eclipse.cdt.make.internal.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.xerces.dom.DocumentImpl; import org.eclipse.cdt.core.CCorePlugin;
import org.apache.xml.serialize.Method; import org.eclipse.cdt.core.ICDescriptor;
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.IMakeTarget;
import org.eclipse.cdt.make.core.MakeCorePlugin; import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
@ -34,6 +33,9 @@ import org.w3c.dom.NodeList;
public class ProjectTargets { public class ProjectTargets {
private static final String MAKE_TARGET_KEY = MakeCorePlugin.getUniqueIdentifier() + ".buildtargets";
private static final String TARGETS_EXT = "targets"; //$NON-NLS-1$
private static final String BUILD_TARGET_ELEMENT = "buildTargets"; //$NON-NLS-1$ private static final String BUILD_TARGET_ELEMENT = "buildTargets"; //$NON-NLS-1$
private static final String TARGET_ELEMENT = "target"; //$NON-NLS-1$ private static final String TARGET_ELEMENT = "target"; //$NON-NLS-1$
private static final String TARGET_ATTR_ID = "targetID"; //$NON-NLS-1$ private static final String TARGET_ATTR_ID = "targetID"; //$NON-NLS-1$
@ -49,67 +51,39 @@ public class ProjectTargets {
private IProject project; private IProject project;
public ProjectTargets(IProject project) { public ProjectTargets(MakeTargetManager manager, IProject project) throws CoreException {
boolean writeTargets = false;
File targetFile = null;
this.project = project; this.project = project;
Document document = translateCDTProjectToDocument();
//Historical ... fall back to the workspace and look in previous
// location
if (document == null || !document.hasChildNodes()) {
IPath targetFilePath = MakeCorePlugin.getDefault().getStateLocation().append(project.getName()).addFileExtension(
TARGETS_EXT);
targetFile = targetFilePath.toFile();
try {
InputStream input = new FileInputStream(targetFile);
document = translateInputStreamToDocument(input);
writeTargets = true; // update cdtproject
} catch (FileNotFoundException ex) {
/* Ignore */
}
} }
public ProjectTargets(MakeTargetManager manager, IProject project, InputStream input) throws CoreException { if (document != null) {
this(project); extractMakeTargetsFromDocument(document, manager);
if (writeTargets) {
Document document = null;
try { try {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); saveTargets();
document = parser.parse(input); } catch (IOException e) {
} catch (Exception e) { targetFile = null;
throw new CoreException(
new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("ProjectTargets.error_reading_project_targets"), e)); //$NON-NLS-1$
}
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(TARGET_ATTR_PATH).getNodeValue();
if (path != null && !path.equals("")) { //$NON-NLS-1$
container = project.getFolder(path);
} else {
container = project;
}
try {
MakeTarget target =
new MakeTarget(
manager,
project,
attr.getNamedItem(TARGET_ATTR_ID).getNodeValue(),
attr.getNamedItem(TARGET_ATTR_NAME).getNodeValue());
target.setContainer(container);
String option = getString(node, TARGET_STOP_ON_ERROR);
if (option != null) {
target.setStopOnError(Boolean.valueOf(option).booleanValue());
}
option = getString(node, TARGET_USE_DEFAULT_CMD);
if (option != null) {
target.setUseDefaultBuildCmd(Boolean.valueOf(option).booleanValue());
}
option = getString(node, TARGET_COMMAND);
if (option != null) {
target.setBuildCommand(new Path(option));
}
option = getString(node, TARGET_ARGUMENTS);
if (option != null) {
target.setBuildArguments(option);
}
option = getString(node, TARGET);
if (option != null) {
target.setBuildTarget(option);
}
add(target);
} catch (CoreException e) {
MakeCorePlugin.log(e);
} }
if (targetFile != null) {
targetFile.delete(); // removed old
} }
} }
} }
@ -154,7 +128,8 @@ public class ProjectTargets {
public void add(MakeTarget target) throws CoreException { public void add(MakeTarget target) throws CoreException {
ArrayList list = (ArrayList) targetMap.get(target.getContainer()); ArrayList list = (ArrayList) targetMap.get(target.getContainer());
if (list != null && list.contains(target)) { if (list != null && list.contains(target)) {
throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeCorePlugin.getResourceString("MakeTargetManager.target_exists"), null)); //$NON-NLS-1$ throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1,
MakeCorePlugin.getResourceString("MakeTargetManager.target_exists"), null)); //$NON-NLS-1$
} }
if (list == null) { if (list == null) {
list = new ArrayList(); list = new ArrayList();
@ -187,8 +162,14 @@ public class ProjectTargets {
return project; return project;
} }
protected Document getAsXML() { protected Document getAsXML() throws IOException {
Document doc = new DocumentImpl(); Document doc;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException ex) {
//This should never happen.
throw new IOException("Error creating new XML storage document"); //$NON-NLS-1$
}
Element targetsRootElement = doc.createElement(BUILD_TARGET_ELEMENT); Element targetsRootElement = doc.createElement(BUILD_TARGET_ELEMENT);
doc.appendChild(targetsRootElement); doc.appendChild(targetsRootElement);
Iterator container = targetMap.entrySet().iterator(); Iterator container = targetMap.entrySet().iterator();
@ -229,14 +210,144 @@ public class ProjectTargets {
return targetElem; return targetElem;
} }
public void saveTargets(OutputStream output) throws IOException { public void saveTargets() throws IOException {
try {
Document doc = getAsXML(); Document doc = getAsXML();
OutputFormat format = new OutputFormat(); //Historical method would save the output to the stream specified
format.setIndenting(true); //translateDocumentToOutputStream(doc, output);
format.setPreserveSpace(true); translateDocumentToCDTProject(doc);
format.setLineSeparator(System.getProperty("line.separator")); //$NON-NLS-1$ } catch (CoreException ex) {
Serializer serializer = throw new IOException(ex.getMessage());
SerializerFactory.getSerializerFactory(Method.XML).makeSerializer(new OutputStreamWriter(output, "UTF8"), format); //$NON-NLS-1$ }
serializer.asDOMSerializer().serialize(doc); }
/**
* This output method saves the information into the .cdtproject metadata
* file.
*
* @param doc
* @throws IOException
*/
protected void translateDocumentToCDTProject(Document doc) throws CoreException, IOException {
ICDescriptor descriptor;
descriptor = CCorePlugin.getDefault().getCProjectDescription(getProject());
Element rootElement = descriptor.getProjectData(MAKE_TARGET_KEY);
//Nuke the children since we are going to write out new ones
NodeList kids = rootElement.getChildNodes();
for (int i = 0; i < kids.getLength(); i++) {
rootElement.removeChild(kids.item(i));
}
//Extract the root of our temporary document
Node node = doc.getFirstChild();
//Create a copy which is a part of the new document
Node appendNode = rootElement.getOwnerDocument().importNode(node, true);
//Put the copy into the document in the appropriate location
rootElement.appendChild(appendNode);
//Save the results
descriptor.saveProjectData();
}
/**
* This method parses the .cdtproject file for the XML document describing
* the build targets.
*
* @param input
* @return
*/
protected Document translateCDTProjectToDocument() {
try {
ICDescriptor descriptor;
descriptor = CCorePlugin.getDefault().getCProjectDescription(getProject());
Element rootElement = descriptor.getProjectData(MAKE_TARGET_KEY);
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element element = rootElement.getOwnerDocument().getDocumentElement();
NodeList list = rootElement.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
if ( list.item(i).getNodeType() == Node.ELEMENT_NODE) {
Node appendNode = document.importNode(list.item(i), true);
document.appendChild(appendNode);
}
}
return document;
} catch (Exception ex) {
return null;
}
}
/**
* This method parses the input stream for the XML document describing the
* build targets.
*
* @param input
* @return
*/
protected Document translateInputStreamToDocument(InputStream input) {
Document document = null;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
} catch (Exception e) {
MakeCorePlugin.log(e);
}
return document;
}
/**
* Extract the make target information which is contained in the XML
* Document
*
* @param document
*/
protected void extractMakeTargetsFromDocument(Document document, MakeTargetManager manager) {
Node node = document.getFirstChild();
if (node != null && 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(TARGET_ATTR_PATH).getNodeValue();
if (path != null && !path.equals("")) { //$NON-NLS-1$
container = project.getFolder(path);
} else {
container = project;
}
try {
MakeTarget target = new MakeTarget(manager, project, attr.getNamedItem(TARGET_ATTR_ID).getNodeValue(),
attr.getNamedItem(TARGET_ATTR_NAME).getNodeValue());
target.setContainer(container);
String option = getString(node, TARGET_STOP_ON_ERROR);
if (option != null) {
target.setStopOnError(Boolean.valueOf(option).booleanValue());
}
option = getString(node, TARGET_USE_DEFAULT_CMD);
if (option != null) {
target.setUseDefaultBuildCmd(Boolean.valueOf(option).booleanValue());
}
option = getString(node, TARGET_COMMAND);
if (option != null) {
target.setBuildCommand(new Path(option));
}
option = getString(node, TARGET_ARGUMENTS);
if (option != null) {
target.setBuildArguments(option);
}
option = getString(node, TARGET);
if (option != null) {
target.setBuildTarget(option);
}
add(target);
} catch (CoreException e) {
MakeCorePlugin.log(e);
}
}
}
}
} }
} }