Commit 6b685bc3 authored by Adam Knapp's avatar Adam Knapp
Browse files

Implementing Eclipse project importer app



&17
Signed-off-by: Adam Knapp's avatarAdam Knapp <adam.knapp@ericsson.com>
parent e76dad7e
......@@ -1431,6 +1431,14 @@
</includes>
</viewerContentBinding>
</extension>
<extension id="org.eclipse.titan.designer.application.ImportProject"
point="org.eclipse.core.runtime.applications">
<application cardinality="singleton-global" thread="main"
visible="true">
<run class="org.eclipse.titan.designer.application.ImportProject">
</run>
</application>
</extension>
<extension id="org.eclipse.titan.designer.application.InvokeBuild"
point="org.eclipse.core.runtime.applications">
<application cardinality="singleton-global" thread="main"
......
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
package org.eclipse.titan.designer.application;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.designer.wizards.TITANProjectOpener;
/**
* Externally callable application, that takes exactly one argument:
* the path of the project it should import to the workspace.
*
* @author Adam Knapp
* */
public final class ImportProject implements IApplication {
private Integer closeWorkspace() {
try {
ResourcesPlugin.getWorkspace().save(true, null);
return EXIT_OK;
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace("Error while closing workspace", e);
return Integer.valueOf(-1);
}
}
@Override
/** {@inheritDoc} */
public Object start(IApplicationContext context) throws Exception {
Platform.getBundle("org.eclipse.titan.designer").start();
final String[] projectNames = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
if (projectNames.length != 1) {
System.out.println("This application takes as parameter the path of the project it should import to the workspace.");
return Integer.valueOf(-1);
}
IPath projectPath = new Path(projectNames[0]);
final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (final IProject project : projects) {
if (project != null && project.getName().equals(projectPath.lastSegment())) {
System.out.println("The project with name `" + projectPath.lastSegment() + "' already exists in the workspace.");
return closeWorkspace();
}
}
try {
final IProject project = TITANProjectOpener.open(projectPath);
if (!TITANProjectOpener.check(project)) {
System.out.println("The project with name `" + project.getName() + "' is imported, but it is not a Titan project.");
}
} catch (Exception e) {
ErrorReporter.logExceptionStackTrace("Error while imporing project with name `"
+ projectPath.lastSegment() + "' in the workspace", e);
closeWorkspace();
return Integer.valueOf(-1);
}
return closeWorkspace();
}
@Override
/** {@inheritDoc} */
public void stop() {
// nothing to be done
}
}
......@@ -219,6 +219,11 @@ public final class TITANNature implements IProjectNature {
}
}
/**
* Returns whether the specified project has the TITAN Builder or not
* @param project the project in question
* @return whether the specified project has the TITAN Builder or not
*/
public static boolean hasTITANBuilder(final IProject project) {
if (project == null || !project.isAccessible()) {
return false;
......@@ -234,4 +239,26 @@ public final class TITANNature implements IProjectNature {
return false;
}
/**
* Returns whether the specified project has the TITAN Java Builder or not
* @param project the project in question
* @return whether the specified project has the TITAN Builder or not
*/
public static boolean hasTITANJavaBuilder(final IProject project) {
if (project == null || !project.isAccessible()) {
return false;
}
try {
final ICommand[] buildSpec = project.getDescription().getBuildSpec();
for (int i = 0; i < buildSpec.length; ++i) {
if (TITANJavaBuilder.BUILDER_ID.equals(buildSpec[i].getBuilderName())) {
return true;
}
}
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace(e);
}
return false;
}
}
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
******************************************************************************/
package org.eclipse.titan.designer.wizards;
import java.io.FileNotFoundException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.titan.designer.core.TITANNature;
/**
* Helper class to open an already existing and configured Titan project that is not part of
* the actual workspace, i.e. helps to bring in the project into the current workspace context.
* The intention is to provide simplified functionality of "Import projects from File System or Archive" wizard.
*
* @author Adam Knapp
* */
public final class TITANProjectOpener {
/**
* Checks whether the specified project is a TITAN project or not
* @param project Project to check
* @return Whether the specified project is a TITAN project or not
*/
public static boolean check(final IProject project) {
if (project == null || !project.isAccessible()) {
return false;
}
if (!TITANNature.hasTITANNature(project)) {
return false;
}
if (!TITANNature.hasTITANBuilder(project) && !TITANNature.hasTITANJavaBuilder(project)) {
return false;
}
return true;
}
/**
* Creates a new project in the actual workspace according to the project description on the specified path
* @param path Path of the folder that contains the project with the project description file (".project")
* @return If a project with the same name already exists in the workspace then the handler of this project
* is returned, otherwise the project on the specified path is created in the workspace.
* {@code null} is returned upon empty or null path
* @throws CoreException
*/
public static IProject open(final IPath path) throws CoreException, FileNotFoundException {
if (path == null || path.isEmpty()) {
return null;
}
final String projectName = path.lastSegment();
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IProject[] projects = workspace.getRoot().getProjects();
for (final IProject project : projects) {
if (project.getName().equals(projectName)) {
return project;
}
}
IPath toProjectFile = path.append(".project");
if (!toProjectFile.isAbsolute()) {
toProjectFile = workspace.getRoot().getLocation().append(toProjectFile);
}
if (!toProjectFile.toFile().exists()) {
throw new FileNotFoundException("Project description file (.project) was not found "
+ "on the specified path: `" + toProjectFile.toOSString() + "'");
}
final IProjectDescription description = workspace.loadProjectDescription(toProjectFile);
for (final IProject project : projects) {
if (project.getName().equals(description.getName())) {
return project;
}
}
IProject newProject = workspace.getRoot().getProject(description.getName());
newProject.create(description, null);
newProject.open(IResource.FORCE, null);
newProject.refreshLocal(IResource.DEPTH_INFINITE, null);
return newProject;
}
/**
* Creates a new project in the actual workspace according to the project description on the specified path
* @param pathString Path of the folder that contains the project with the project description file (".project")
* @return If a project with the same name already exists in the workspace then the handler of this project
* is returned, otherwise the project on the specified path is created in the workspace.
* {@code null} is returned upon empty or null path
* @throws CoreException
*/
public static IProject open(final String pathString) throws CoreException, FileNotFoundException {
return open(new Path(pathString));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment