mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-22 14:12:10 +02:00
start of .cdtproject management
This commit is contained in:
parent
d4e3f32c1a
commit
ce3ece8454
8 changed files with 410 additions and 3 deletions
|
@ -4,6 +4,9 @@ providerName=Eclipse.org
|
|||
cnature.name=C Nature
|
||||
ccnature.name=C++ Nature
|
||||
CProblemMarker.name=C Problem
|
||||
|
||||
CBuildCommand.name=C Builder Command
|
||||
CBuildConsole.name=C Builder Console
|
||||
|
||||
CProjectInfo.name=C Project Info
|
||||
projectinfo.name=Core Make Project
|
||||
genericmake.name=Generic Make
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
<extension-point id="CBuildCommand" name="%CBuildCommand.name"/>
|
||||
<extension-point id="CBuildConsole" name="%CBuildConsole.name"/>
|
||||
<extension-point id="CProjectInfo" name="%CProjectInfo.name"/>
|
||||
|
||||
<extension
|
||||
id="cbuilder"
|
||||
|
@ -84,5 +85,14 @@
|
|||
command="make">
|
||||
</buildcommand>
|
||||
</extension>
|
||||
|
||||
<extension
|
||||
point="org.eclipse.cdt.core.CProjectInfo"
|
||||
name="%projectinfo.name"
|
||||
id="make">
|
||||
<platform id="org.eclipse.cdt.core.genericmake"
|
||||
name="%genericmake.name" architecture="*"/>
|
||||
<run
|
||||
class = "org.eclipse.cdt.internal.core.make.MakeProject">
|
||||
</run>
|
||||
</extension>
|
||||
</plugin>
|
||||
|
|
|
@ -12,6 +12,9 @@ import java.util.ResourceBundle;
|
|||
import org.eclipse.cdt.core.index.IndexModel;
|
||||
import org.eclipse.cdt.core.model.CoreModel;
|
||||
import org.eclipse.cdt.core.resources.IConsole;
|
||||
import org.eclipse.cdt.internal.core.CProjectDescriptor;
|
||||
import org.eclipse.cdt.internal.core.model.CProject;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IWorkspace;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
@ -26,6 +29,8 @@ import org.eclipse.core.runtime.Status;
|
|||
|
||||
public class CCorePlugin extends Plugin {
|
||||
|
||||
public static final int STATUS_CDTPROJECT_EXISTS = 1;
|
||||
|
||||
public static final String PLUGIN_ID= "org.eclipse.cdt.core";
|
||||
public static final String BUILDER_ID= PLUGIN_ID + ".cbuilder";
|
||||
|
||||
|
@ -121,7 +126,8 @@ public class CCorePlugin extends Plugin {
|
|||
return new IConsole() {
|
||||
public void clear() {
|
||||
}
|
||||
|
||||
public void start(IProject project) {
|
||||
}
|
||||
public ConsoleOutputStream getOutputStream() {
|
||||
return new ConsoleOutputStream();
|
||||
}
|
||||
|
@ -137,4 +143,12 @@ public class CCorePlugin extends Plugin {
|
|||
public IndexModel getIndexModel() {
|
||||
return IndexModel.getDefault();
|
||||
}
|
||||
|
||||
public ICProjectDescriptor getCProjectDescription(IProject project) throws CoreException {
|
||||
return CProjectDescriptor.getDescription(project);
|
||||
}
|
||||
|
||||
public void mapCProjectOwner(IProject project, String id) throws CoreException {
|
||||
CProjectDescriptor.configure(project, id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* (c) Copyright QNX Software System Ltd. 2002.
|
||||
* All Rights Reserved.
|
||||
*/
|
||||
package org.eclipse.cdt.core;
|
||||
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
||||
public interface ICProjectDescriptor {
|
||||
public static final String DESCRIPTION_FILE_NAME = ".cdtproject";
|
||||
|
||||
public ICProjectOwnerInfo getProjectOwner();
|
||||
public String[] getPlatforms();
|
||||
public IProject getProject();
|
||||
// public IBuilderInfo getBuilderInfo();
|
||||
// public setBuilder(String id) or should this be add... ?
|
||||
public void saveInfo() throws CoreException;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
* (c) Copyright QNX Software System Ltd. 2002.
|
||||
* All Rights Reserved.
|
||||
*/
|
||||
package org.eclipse.cdt.core;
|
||||
|
||||
/**
|
||||
* @author DInglis
|
||||
*
|
||||
* To change this generated comment edit the template variable "typecomment":
|
||||
* Window>Preferences>Java>Templates.
|
||||
* To enable and disable the creation of type comments go to
|
||||
* Window>Preferences>Java>Code Generation.
|
||||
*/
|
||||
public interface ICProjectOwner {
|
||||
public void configure(ICProjectDescriptor cproject);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
/*
|
||||
* (c) Copyright QNX Software System Ltd. 2002.
|
||||
* All Rights Reserved.
|
||||
*/
|
||||
package org.eclipse.cdt.core;
|
||||
|
||||
public interface ICProjectOwnerInfo {
|
||||
public String getID();
|
||||
public String getName();
|
||||
public String[] getPlatforms();
|
||||
public String[] getArchitectures(String platform);
|
||||
}
|
|
@ -0,0 +1,253 @@
|
|||
/*
|
||||
* (c) Copyright QNX Software System Ltd. 2002.
|
||||
* All Rights Reserved.
|
||||
*/
|
||||
package org.eclipse.cdt.internal.core;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
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.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.ICProjectDescriptor;
|
||||
import org.eclipse.cdt.core.ICProjectOwnerInfo;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.IProjectDescription;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Platform;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public class CProjectDescriptor implements ICProjectDescriptor {
|
||||
/** constants */
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
private String ownerId;
|
||||
private IProject project;
|
||||
|
||||
private String builderID;
|
||||
|
||||
private static final String PROJECT_DESCRIPTION = "cdtProject";
|
||||
|
||||
private CProjectDescriptor(IProject project, String id) throws CoreException {
|
||||
this.project = project;
|
||||
ownerId = id;
|
||||
IPath projectLocation = project.getDescription().getLocation();
|
||||
|
||||
final boolean isDefaultLocation = projectLocation == null;
|
||||
if (isDefaultLocation) {
|
||||
projectLocation = getProjectDefaultLocation(project);
|
||||
}
|
||||
IPath descriptionPath = projectLocation.append(ICProjectDescriptor.DESCRIPTION_FILE_NAME);
|
||||
|
||||
if (descriptionPath.toFile().exists()) {
|
||||
IStatus status = new Status(IStatus.WARNING, CCorePlugin.getDefault().PLUGIN_ID, CCorePlugin.STATUS_CDTPROJECT_EXISTS, "CDTProject already exisits", (Throwable)null);
|
||||
throw new CoreException(status);
|
||||
}
|
||||
}
|
||||
|
||||
private CProjectDescriptor(IProject project) throws CoreException {
|
||||
this.project = project;
|
||||
FileInputStream file = null;
|
||||
IPath projectLocation = project.getDescription().getLocation();
|
||||
|
||||
final boolean isDefaultLocation = projectLocation == null;
|
||||
if (isDefaultLocation) {
|
||||
projectLocation = getProjectDefaultLocation(project);
|
||||
}
|
||||
IPath descriptionPath = projectLocation.append(ICProjectDescriptor.DESCRIPTION_FILE_NAME);
|
||||
|
||||
if (!descriptionPath.toFile().exists()) {
|
||||
IStatus status = new Status(IStatus.ERROR, CCorePlugin.getDefault().PLUGIN_ID, -1, "CDTProject file not found", (Throwable)null);
|
||||
throw new CoreException(status);
|
||||
}
|
||||
try {
|
||||
file = new FileInputStream(projectLocation.toFile());
|
||||
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = parser.parse(file);
|
||||
Node attrib = (Node) read(document.getFirstChild());
|
||||
ownerId = searchNode(attrib, "id").getNodeValue();
|
||||
}
|
||||
catch (IOException e) {
|
||||
}
|
||||
catch (SAXException e) {
|
||||
}
|
||||
catch (ParserConfigurationException e) {
|
||||
}
|
||||
finally {
|
||||
if (file != null) {
|
||||
try {
|
||||
file.close();
|
||||
}
|
||||
catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected IPath getProjectDefaultLocation(IProject project) {
|
||||
return Platform.getLocation().append(project.getFullPath());
|
||||
}
|
||||
|
||||
public ICProjectOwnerInfo getProjectOwner() {
|
||||
return new CProjectOwner(ownerId);
|
||||
}
|
||||
|
||||
public String[] getPlatforms() {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
protected String getString(Node target, String tagName) {
|
||||
Node node = searchNode(target, tagName);
|
||||
return node != null ? (node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue()) : null;
|
||||
}
|
||||
|
||||
protected String[] getStrings(Node target) {
|
||||
if (target == null)
|
||||
return null;
|
||||
NodeList list = target.getChildNodes();
|
||||
if (list.getLength() == 0)
|
||||
return EMPTY_STRING_ARRAY;
|
||||
List result = new ArrayList(list.getLength());
|
||||
for (int i = 0; i < list.getLength(); i++) {
|
||||
Node node = list.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE)
|
||||
result.add((String) read(node.getChildNodes().item(0)));
|
||||
}
|
||||
return (String[]) result.toArray(new String[result.size()]);
|
||||
}
|
||||
|
||||
protected Object read(Node node) {
|
||||
if (node == null)
|
||||
return null;
|
||||
switch (node.getNodeType()) {
|
||||
case Node.ELEMENT_NODE :
|
||||
if (node.getNodeName().equals(PROJECT_DESCRIPTION))
|
||||
return node.getAttributes();
|
||||
/*
|
||||
if (node.getNodeName().equals(BUILDER))
|
||||
return readBuildSpec(node);
|
||||
if (node.getNodeName().equals(DEBUGGER)
|
||||
return readProjectDescription(node);
|
||||
*/
|
||||
case Node.TEXT_NODE :
|
||||
String value = node.getNodeValue();
|
||||
return value == null ? null : value.trim();
|
||||
default :
|
||||
return node.toString();
|
||||
}
|
||||
}
|
||||
|
||||
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 IProject getProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
public void saveInfo() throws CoreException {
|
||||
String xml;
|
||||
try {
|
||||
xml = getAsXML();
|
||||
}
|
||||
catch (IOException e) {
|
||||
IStatus s= new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, e.getMessage(), e);
|
||||
throw new CoreException(s);
|
||||
}
|
||||
|
||||
IFile rscFile = getProject().getFile(ICProjectDescriptor.DESCRIPTION_FILE_NAME);
|
||||
InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
|
||||
// update the resource content
|
||||
if (rscFile.exists()) {
|
||||
rscFile.setContents(inputStream, IResource.FORCE, null);
|
||||
} else {
|
||||
rscFile.create(inputStream, IResource.FORCE, null);
|
||||
}
|
||||
}
|
||||
|
||||
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"), //$NON-NLS-1$
|
||||
format);
|
||||
serializer.asDOMSerializer().serialize(doc);
|
||||
return s.toString("UTF8"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
protected String getAsXML() throws IOException {
|
||||
Element element;
|
||||
Document doc = new DocumentImpl();
|
||||
Element configRootElement = doc.createElement(PROJECT_DESCRIPTION);
|
||||
doc.appendChild(configRootElement);
|
||||
configRootElement.setAttribute("id", ownerId); //$NON-NLS-1$
|
||||
element= createBuilderElement(doc);
|
||||
if ( element != null )
|
||||
configRootElement.appendChild(element);
|
||||
return serializeDocument(doc);
|
||||
}
|
||||
|
||||
|
||||
protected Element createBuilderElement(Document doc) {
|
||||
if ( builderID != null ) {
|
||||
Element element = doc.createElement("cdtBuilder");
|
||||
element.setAttribute("id", builderID);
|
||||
return element;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static synchronized ICProjectDescriptor getDescription(IProject project) throws CoreException {
|
||||
return new CProjectDescriptor(project);
|
||||
}
|
||||
|
||||
public static synchronized void configure(IProject project, String id) throws CoreException {
|
||||
CProjectDescriptor cproject;
|
||||
try {
|
||||
cproject = new CProjectDescriptor(project, id);
|
||||
}
|
||||
catch (CoreException e) { // if .cdtproject already exists will use that
|
||||
IStatus status = e.getStatus();
|
||||
if ( status.getCode() == CCorePlugin.STATUS_CDTPROJECT_EXISTS )
|
||||
return;
|
||||
else
|
||||
throw e;
|
||||
}
|
||||
CProjectOwner cowner = new CProjectOwner(id);
|
||||
cowner.configure(project, cproject);
|
||||
cproject.saveInfo();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* (c) Copyright QNX Software System Ltd. 2002.
|
||||
* All Rights Reserved.
|
||||
*/
|
||||
package org.eclipse.cdt.internal.core;
|
||||
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.ICProjectDescriptor;
|
||||
import org.eclipse.cdt.core.ICProjectOwner;
|
||||
import org.eclipse.cdt.core.ICProjectOwnerInfo;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IConfigurationElement;
|
||||
import org.eclipse.core.runtime.IExtension;
|
||||
import org.eclipse.core.runtime.IExtensionPoint;
|
||||
|
||||
public class CProjectOwner implements ICProjectOwnerInfo {
|
||||
String pluginId;
|
||||
IExtension extension;
|
||||
|
||||
public CProjectOwner(String id) {
|
||||
pluginId = id;
|
||||
IExtensionPoint extpoint = CCorePlugin.getDefault().getDescriptor().getExtensionPoint("CProjectOwner");
|
||||
if (extpoint != null) {
|
||||
extension = extpoint.getExtension(pluginId);
|
||||
}
|
||||
}
|
||||
|
||||
public String getID() {
|
||||
return pluginId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return extension == null ? null : extension.getLabel();
|
||||
}
|
||||
|
||||
public String[] getPlatforms() {
|
||||
IConfigurationElement element[] = extension.getConfigurationElements();
|
||||
String platforms[] = new String[element.length];
|
||||
for( int i = 0; i < element.length; i++ ) {
|
||||
platforms[i] = element[i].getAttribute("id");
|
||||
}
|
||||
return platforms;
|
||||
}
|
||||
|
||||
public String getPlatformName(String platform) {
|
||||
IConfigurationElement element[] = extension.getConfigurationElements();
|
||||
String platforms[] = new String[element.length];
|
||||
for( int i = 0; i < element.length; i++ ) {
|
||||
if ( platform.equals(element[i].getAttribute("id")) ) {
|
||||
return element[i].getAttribute("name");
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public String[] getArchitectures(String platform) {
|
||||
IConfigurationElement element[] = extension.getConfigurationElements();
|
||||
String platforms[] = new String[element.length];
|
||||
for( int i = 0; i < element.length; i++ ) {
|
||||
if ( platform.equals(element[i].getAttribute("id")) ) {
|
||||
StringTokenizer stoken = new StringTokenizer(element[i].getAttribute("architecture"), ",");
|
||||
String[] archs = new String[stoken.countTokens()];
|
||||
for( int j = 0; j < archs.length; j++ ) {
|
||||
archs[i] = stoken.nextToken();
|
||||
}
|
||||
}
|
||||
}
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
void configure(IProject project, ICProjectDescriptor cproject) throws CoreException {
|
||||
IConfigurationElement element[] = extension.getConfigurationElements();
|
||||
ICProjectOwner owner = (ICProjectOwner) element[0].createExecutableExtension("class");
|
||||
owner.configure(cproject);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue