Commit 4c184043 authored by Adam Knapp's avatar Adam Knapp
Browse files

Automated script generation feature for starting JAR



Change-Id: I73de30fcdc3e4e2f38df85ad74b0cfe850d53be3
Signed-off-by: default avatarAdam Knapp <adam.knapp@sigmatechnology.se>
parent 5052e4c0
......@@ -150,13 +150,14 @@ public final class AntScriptGenerator {
/**
* Creates and then stores the {@code jarbuild.xml} ANT script for the specified project's root
* @param project Project where the ANT script is required
* @param isUpdated Whether the file path was modified on the UI
* @return {@code true} if the generation was completed or {@code false} if error occurred during the process
*/
public static boolean generateAndStoreBuildXML(final IProject project) {
public static boolean generateAndStoreBuildXML(final IProject project, final boolean isUpdated) {
if (project == null) {
return false;
}
if (existsBuildXML(project)) {
if (existsBuildXML(project) && !isUpdated) {
return true;
}
try {
......
/******************************************************************************
* 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.core.ant;
import java.io.File;
import java.io.IOException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.titan.common.logging.ErrorReporter;
import org.eclipse.titan.common.utils.CommentUtils;
import org.eclipse.titan.common.utils.IOUtils;
import org.eclipse.titan.common.utils.StringUtils;
import org.eclipse.titan.designer.GeneralConstants;
import org.eclipse.titan.designer.properties.data.MakefileCreationData;
import org.eclipse.titan.designer.properties.data.ProjectBuildPropertyData;
/**
* Utility class for generating Linux shell and Windows command scripts.
* @author Adam Knapp
*/
public final class CliScriptGenerator {
public static final String SCRIPT_NAME = "titan_java_start";
public static final String CMD_SCRIPT_NAME = SCRIPT_NAME + ".cmd";
public static final String SHELL_SCRIPT_NAME = SCRIPT_NAME + ".sh";
private static final String SHELL_SCRIPT_HEADER = "#!/bin/sh\n";
public static String createCmdScriptContent(final IProject project) {
if (project == null) {
return "";
}
final StringBuilder builder = new StringBuilder();
builder.append(CommentUtils.getHeaderCommentsWithCopyright(":: ", GeneralConstants.VERSION_STRING));
return builder.toString();
}
public static String createShellScriptContent(final IProject project) {
if (project == null) {
return "";
}
final StringBuilder builder = new StringBuilder();
builder.append(SHELL_SCRIPT_HEADER)
.append(CommentUtils.getHeaderCommentsWithCopyright("# ", GeneralConstants.VERSION_STRING));
return builder.toString();
}
public static boolean existCmdScript(final IProject project) throws CoreException {
if (project == null) {
return false;
}
final String jarFolder = getJarFolder(project);
if (StringUtils.isNullOrEmpty(jarFolder)) {
return false;
}
final File scriptFile = new File(jarFolder + File.separator + CMD_SCRIPT_NAME);
return scriptFile.exists();
}
public static boolean existShellScript(final IProject project) throws CoreException {
if (project == null) {
return false;
}
final String jarFolder = getJarFolder(project);
if (StringUtils.isNullOrEmpty(jarFolder)) {
return false;
}
final File scriptFile = new File(jarFolder + File.separator + SHELL_SCRIPT_NAME);
return scriptFile.exists();
}
public static void generateAndStoreScripts(final IProject project) {
if (project == null) {
return;
}
try {
String temp = project.getPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER,
MakefileCreationData.GENERATE_START_SH_SCRIPT_PROPERTY));
final boolean shellScriptNeeded = ProjectBuildPropertyData.TRUE_STRING.equals(temp) ? true : false;
temp = project.getPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER,
MakefileCreationData.GENERATE_START_BAT_SCRIPT_PROPERTY));
final boolean cmdScriptNeeded = ProjectBuildPropertyData.TRUE_STRING.equals(temp) ? true : false;
String shellScriptContent = null;
String cmdScriptContent = null;
if (shellScriptNeeded) {
if (!existShellScript(project)) {
shellScriptContent = createShellScriptContent(project);
}
}
if (cmdScriptNeeded) {
if (!existCmdScript(project)) {
cmdScriptContent = createCmdScriptContent(project);
}
}
storeScripts(project, shellScriptContent, cmdScriptContent);
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace(e);
} catch (IOException e) {
ErrorReporter.logExceptionStackTrace(e);
}
}
private static String getJarFolder(final IProject project) throws CoreException {
final String jarPathString = project.getPersistentProperty(new QualifiedName(ProjectBuildPropertyData.QUALIFIER,
MakefileCreationData.TARGET_EXECUTABLE_PROPERTY));
if (StringUtils.isNullOrEmpty(jarPathString)) {
ErrorReporter.INTERNAL_ERROR("Jar file is null or empty");
return null;
}
File jarFile = new File(jarPathString);
if (!jarFile.isAbsolute()) {
jarFile = new File(project.getLocation().toOSString() + File.separator + jarPathString);
}
return jarFile.getParent();
}
public static void storeScripts(final IProject project, final String shellScriptContent,
final String cmdScriptContent) throws CoreException, IOException {
if (project == null || (shellScriptContent == null && cmdScriptContent == null)) {
return;
}
final String jarFolderString = getJarFolder(project);
if (shellScriptContent != null) {
File shellScriptFile = new File(jarFolderString + File.separator + SHELL_SCRIPT_NAME);
IOUtils.writeStringToFile(shellScriptFile, shellScriptContent);
}
if (cmdScriptContent != null) {
File cmdScriptFile = new File(jarFolderString + File.separator + CMD_SCRIPT_NAME);
IOUtils.writeStringToFile(cmdScriptFile, cmdScriptContent);
}
}
}
......@@ -31,6 +31,7 @@ import org.eclipse.titan.common.path.TITANPathUtilities;
import org.eclipse.titan.common.utils.StringUtils;
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.preferences.pages.ComboFieldEditor;
import org.eclipse.titan.designer.productUtilities.ProductConstants;
import org.eclipse.titan.designer.properties.data.MakefileCreationData;
......@@ -124,12 +125,12 @@ public class JavaCreationTab {
temporalJavaTargetFileFieldEditor.setPage(page);
}
generateStartShScript = new Button(automaticBuildPropertiesComposite, SWT.CHECK);
generateStartShScript.setText("Generate ttcn3_start shell script for starting the JAR (Linux)");
generateStartShScript.setText("Generate '" + CliScriptGenerator.SHELL_SCRIPT_NAME + "' shell script for starting the JAR (Linux)");
generateStartShScript.setEnabled(false);
generateStartShScript
.setToolTipText("If this option is set, also a shell script will be generated to start the JAR");
generateStartBatScript = new Button(automaticBuildPropertiesComposite, SWT.CHECK);
generateStartBatScript.setText("Generate ttcn3_start.bat script for starting the JAR (WIN)");
generateStartBatScript.setText("Generate '" + CliScriptGenerator.CMD_SCRIPT_NAME + "' script for starting the JAR (WIN)");
generateStartBatScript.setEnabled(false);
generateStartBatScript
.setToolTipText("If this option is set, also a .bat script will be generated to start the JAR");
......@@ -295,13 +296,14 @@ public class JavaCreationTab {
if (path.equals(resolvedPath)) {
temp = PathUtil.getRelativePath(project.getLocation().toOSString(), temp);
}
setProperty(project, MakefileCreationData.TARGET_EXECUTABLE_PROPERTY, temp);
final boolean updated = setProperty(project, MakefileCreationData.TARGET_EXECUTABLE_PROPERTY, temp);
if (isExecutableSelected()) {
if (AntScriptGenerator.generateAndStoreBuildXML(project)) {
if (AntScriptGenerator.generateAndStoreBuildXML(project, updated)) {
AntLaunchConfigGenerator.createAntLaunchConfiguration(project);
AntLaunchConfigGenerator.addAntBuilder(project);
project.refreshLocal(IResource.DEPTH_ONE, null);
}
CliScriptGenerator.generateAndStoreScripts(project);
}
AntLaunchConfigGenerator.setAntBuilderEnabled(project, isExecutableSelected());
} catch (CoreException e) {
......@@ -329,6 +331,8 @@ public class JavaCreationTab {
* the name of the property to change.
* @param value
* the value to set.
* @return
* whether the value of the specified property is different from the old one or not.
*
* @exception CoreException
* if this method fails. Reasons include:
......@@ -342,12 +346,14 @@ public class JavaCreationTab {
* details.</li>
* </ul>
* */
private void setProperty(final IProject project, final String name, final String value) throws CoreException {
private boolean setProperty(final IProject project, final String name, final String value) throws CoreException {
final QualifiedName qualifiedName = new QualifiedName(ProjectBuildPropertyData.QUALIFIER, name);
final String oldValue = project.getPersistentProperty(qualifiedName);
if (value != null && !value.equals(oldValue)) {
project.setPersistentProperty(qualifiedName, value);
return true;
}
return false;
}
/**
......
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