Commit 0befa382 authored by Arpad Lovassy's avatar Arpad Lovassy
Browse files

Merge branch 'aknapp' into 'master'

Implementing Eclipse project importer app

See merge request eclipse/titan/titan.EclipsePlug-ins!627
parents e76dad7e e10c6fd9
......@@ -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
}
}
......@@ -7,6 +7,9 @@
******************************************************************************/
package org.eclipse.titan.designer.application;
import java.net.URI;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
......@@ -14,9 +17,19 @@ import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.common.path.PathUtil;
import org.eclipse.titan.common.path.TITANPathUtilities;
import org.eclipse.titan.common.utils.StringUtils;
import org.eclipse.titan.designer.core.TITANJavaBuilder;
import org.eclipse.titan.designer.core.ant.AntLaunchConfigGenerator;
import org.eclipse.titan.designer.core.ant.AntScriptGenerator;
import org.eclipse.titan.designer.core.ant.CliScriptGenerator;
import org.eclipse.titan.designer.properties.data.MakefileCreationData;
import org.eclipse.titan.designer.properties.data.ProjectBuildPropertyData;
/**
* Externally callable application, that takes exactly one argument: the name of the project it should build.
......@@ -25,44 +38,92 @@ import org.eclipse.titan.common.logging.ErrorReporter;
* */
public final class InvokeBuild implements IApplication {
/**
* Turns on the automatic JAR export feature on the specified project
* @param project
*/
public void addJarBuilder(final IProject project, final String pathToJar) throws CoreException {
String temp = pathToJar;
final URI path = URIUtil.toURI(temp);
final URI resolvedPath = TITANPathUtilities.resolvePathURI(temp, project.getLocation().toOSString());
if (path.equals(resolvedPath)) {
temp = PathUtil.getRelativePath(project.getLocation().toOSString(), temp);
}
project.setPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER,
MakefileCreationData.TARGET_EXECUTABLE_PROPERTY), temp);
project.setPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER,
MakefileCreationData.DEFAULT_JAVA_TARGET_PROPERTY), MakefileCreationData.DefaultJavaTarget.EXECUTABLE.toString());
project.setPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER,
MakefileCreationData.GENERATE_START_SH_SCRIPT_PROPERTY), String.valueOf(true));
if (AntScriptGenerator.generateAndStoreBuildXML(project, true)) {
AntLaunchConfigGenerator.createAntLaunchConfiguration(project);
AntLaunchConfigGenerator.addAntBuilder(project);
project.refreshLocal(IResource.DEPTH_ONE, null);
}
CliScriptGenerator.generateAndStoreScripts(project);
AntLaunchConfigGenerator.setAntBuilderEnabled(project, true);
}
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(final IApplicationContext context) throws Exception {
Platform.getBundle("org.eclipse.titan.designer").start();
final String[] projectNames = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
final String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
if (projectNames.length != 1) {
System.out.println("This application takes as parameter the name of the project it should build.");
return Integer.valueOf(-1);
if (args.length % 2 != 1) {
System.out.println("This application takes as parameter the name of the project it should build.\n"
+ "Optionally JAR output can be specified by the `-jar path_to_jar' option.");
return closeWorkspace();
}
String projectName = null, pathToJar = null;
for (int i = 0; i < args.length; ++i) {
if (args[i].equals("-jar") && i < args.length-1) {
pathToJar = args[++i];
continue;
}
projectName = args[i];
}
if (!PathUtil.isValidFilePath(pathToJar)) {
System.out.println("The path `" + pathToJar + "' is not valid.");
return closeWorkspace();
}
final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
for (final IProject project : projects) {
if (project != null && project.getName().equals(projectNames[0])) {
if (project != null && project.getName().equals(projectName)) {
try {
project.refreshLocal(IResource.DEPTH_INFINITE, null);
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace(e);
if (!StringUtils.isNullOrEmpty(pathToJar) && TITANJavaBuilder.isBuilderEnabled(project)) {
addJarBuilder(project, pathToJar);
}
project.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
try {
ResourcesPlugin.getWorkspace().save(true, null);
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace("Error while closing workspace",e);
ErrorReporter.logExceptionStackTrace(e);
} catch (Exception e) {
ErrorReporter.logExceptionStackTrace("Unknown error", e);
}
return EXIT_OK;
return closeWorkspace();
}
}
System.out.println("The project with name `" + projectNames[0] + "' could not be found.");
System.out.println("The project with name `" + projectName + "' could not be found.");
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;
}
}
......@@ -224,9 +224,12 @@ public final class AntScriptGenerator {
return null;
}
final File jarFile = new File(jarPathString);
final String jarFolder = jarFile.getParent();
String jarFolder = jarFile.getParent();
if (StringUtils.isNullOrEmpty(jarFolder)) {
jarFolder = ".";
}
final String jarFileName = jarFile.getName();
if (StringUtils.isNullOrEmpty(jarFolder) || StringUtils.isNullOrEmpty(jarFileName)) {
if (StringUtils.isNullOrEmpty(jarFileName)) {
ErrorReporter.INTERNAL_ERROR("Jar file is null or empty");
return null;
}
......
/******************************************************************************
* 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