Search in sources :

Example 1 with Attribute

use of net.sourceforge.usbdm.cdt.ui.examplewizard.ExampleList.Attribute in project usbdm-eclipse-plugins by podonoghue.

the class UsbdmExampleProjectsWizard method performFinish.

@Override
public boolean performFinish() {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    ZipFile zipFile = null;
    String internalProjectDirectoryName = null;
    if (!usbdmSelectProjectWizardPage.validate()) {
        return false;
    }
    // Unzip example into workspace
    try {
        usbdmSelectProjectWizardPage.saveSetting();
        ProjectInformation projectInformation = usbdmSelectProjectWizardPage.getProjectInformation();
        zipFile = new ZipFile(projectInformation.getPath().toFile());
        ArrayList<Attribute> attributes = projectInformation.getAttributes();
        attributes.add(new Attribute("projectName", projectInformation.getDescription()));
        addCommonAttributes(attributes);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        if (!entries.hasMoreElements()) {
            throw new UsbdmException("Example Zip file is empty");
        }
        IPath workspaceRootPath = workspace.getRoot().getLocation();
        IPath projectPath = null;
        // Do pass through zip file creating directories & files
        entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            // System.err.println("performFinish() entry = " + entry.getName());
            IPath internalPath = new Path(entry.getName());
            if (internalPath.segmentCount() < 1) {
                throw new UsbdmException("Example project has invalid structure, \'" + entry.toString() + "\' is missing project directory)");
            }
            if (internalProjectDirectoryName == null) {
                // Haven't got project directory yet - determine from root of first entry
                internalProjectDirectoryName = internalPath.segment(0);
                projectPath = workspaceRootPath.append(internalProjectDirectoryName);
                // System.err.println("performFinish() Project Path: " + projectPath.toString());
                File directory = projectPath.toFile();
                if (directory.exists()) {
                    // Project directory should not already exist
                    throw new UsbdmException("Project Directory already exists (" + internalProjectDirectoryName + ")");
                }
                // System.err.println("performFinish() Creating project directory:   " + directory.toString());
                directory.mkdir();
            }
            if (!internalProjectDirectoryName.equals(internalPath.segment(0))) {
                // Entry is not a child of project directory
                throw new UsbdmException("Example project has invalid structure, \'" + entry.toString() + "\' is outside project directory)");
            }
            if ((internalPath.segmentCount() == 1) && !entry.isDirectory()) {
                // Only 1 segment => must be project directory
                throw new UsbdmException("Example project has invalid structure, \'" + entry.toString() + "\' is outside project directory)");
            }
            IPath filePath = workspaceRootPath.append(internalPath);
            // Create each directory in path (excluding last segment which may be a file or directory)
            File directory = filePath.uptoSegment(filePath.segmentCount() - 1).toFile();
            if (!directory.exists()) {
                // System.err.println("performFinish() Creating directories in path: " + directory.toString());
                directory.mkdirs();
            }
            if (entry.isDirectory()) {
                // Make the directory
                directory = filePath.toFile();
                if (!directory.exists()) {
                    // System.err.println("performFinish() Creating directory:           " + filePath.toString());
                    directory.mkdir();
                }
            } else {
                // System.err.println("performFinish() Creating file:                " + filePath.toString());
                String fileExtension = filePath.getFileExtension();
                String[] textFiles = { "project", "cproject", "h", "hpp", "c", "cpp", "s", "asm", "ld", "launch", "mk", "rc", "d" };
                boolean doConversion = false;
                if ((fileExtension != null) && (fileExtension.length() > 0)) {
                    for (String textExtension : textFiles) {
                        if (fileExtension.equalsIgnoreCase(textExtension)) {
                            doConversion = true;
                            break;
                        }
                    }
                } else {
                    String filename = filePath.lastSegment();
                    String[] specialFiles = { "makefile", "timestamp" };
                    if ((filename != null) && (filename.length() > 0)) {
                        for (String specialFile : specialFiles) {
                            if (filename.equalsIgnoreCase(specialFile)) {
                                doConversion = true;
                                break;
                            }
                        }
                    }
                }
                if (doConversion) {
                    // System.err.println("performFinish() Converting file:                " + filePath.toString());
                    convertTextStream(zipFile.getInputStream(entry), new FileOutputStream(filePath.toFile()), attributes);
                } else {
                    // System.err.println("performFinish() Copying file:                   " + filePath.toString());
                    copyBinaryStream(zipFile.getInputStream(entry), new FileOutputStream(filePath.toFile()));
                }
            }
        }
        ;
        if (projectPath == null) {
            throw new UsbdmException("No project Folder found");
        }
        IProjectDescription description = workspace.loadProjectDescription(projectPath.append(".project"));
        IProject newProject = workspace.getRoot().getProject(description.getName());
        newProject.create(description, null);
        newProject.open(null);
        String additionalProjectNature = null;
        if ((usbdmSelectProjectWizardPage.getBuildToolsId().equals(UsbdmSharedConstants.CODESOURCERY_ARM_BUILD_ID)) || (usbdmSelectProjectWizardPage.getBuildToolsId().equals(UsbdmSharedConstants.ARMLTD_ARM_BUILD_ID))) {
            additionalProjectNature = "net.sourceforge.usbdm.cdt.tools.ArmProjectNature";
        } else if (usbdmSelectProjectWizardPage.getBuildToolsId().equals(UsbdmSharedConstants.CODESOURCERY_COLDFIRE_BUILD_ID)) {
            additionalProjectNature = "net.sourceforge.usbdm.cdt.tools.ColdfireProjectNature";
        }
        if (additionalProjectNature != null) {
            IProjectDescription projectDescription = newProject.getDescription();
            String[] currentIds = projectDescription.getNatureIds();
            String[] newIds = new String[currentIds.length + 1];
            int index;
            for (index = 0; index < currentIds.length; index++) {
                newIds[index] = currentIds[index];
            // System.err.println("AddNature.process() Copying nature : " + newIds[index]);
            }
            newIds[index] = additionalProjectNature;
            // System.err.println("AddNature.process() Adding nature : " + newIds[index]);
            projectDescription.setNatureIds(newIds);
            newProject.setDescription(projectDescription, null);
            ManagedBuildManager.saveBuildInfo(newProject, true);
        }
    } catch (Exception e) {
        e.printStackTrace();
        MessageBox errBox = new MessageBox(getShell(), SWT.ERROR);
        errBox.setText("Error Creating Project");
        errBox.setMessage("Reason:\n\n    " + e.getMessage());
        errBox.open();
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
            }
        }
    }
    return true;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) Attribute(net.sourceforge.usbdm.cdt.ui.examplewizard.ExampleList.Attribute) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ProjectInformation(net.sourceforge.usbdm.cdt.ui.examplewizard.ExampleList.ProjectInformation) IProject(org.eclipse.core.resources.IProject) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException) IOException(java.io.IOException) MessageBox(org.eclipse.swt.widgets.MessageBox) ZipFile(java.util.zip.ZipFile) IWorkspace(org.eclipse.core.resources.IWorkspace) FileOutputStream(java.io.FileOutputStream) IProjectDescription(org.eclipse.core.resources.IProjectDescription) UsbdmException(net.sourceforge.usbdm.jni.UsbdmException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 Attribute (net.sourceforge.usbdm.cdt.ui.examplewizard.ExampleList.Attribute)1 ProjectInformation (net.sourceforge.usbdm.cdt.ui.examplewizard.ExampleList.ProjectInformation)1 UsbdmException (net.sourceforge.usbdm.jni.UsbdmException)1 IProject (org.eclipse.core.resources.IProject)1 IProjectDescription (org.eclipse.core.resources.IProjectDescription)1 IWorkspace (org.eclipse.core.resources.IWorkspace)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1