Commit f7d3a27d authored by Christian Meyer's avatar Christian Meyer
Browse files

added ZmtShortcutPlugin.groovy

    contains shortcut working previously found in ZmtLaunchPlugin
    simulation class name can be set as project property
ZmtLaunchPlugin.groovy
    removed shortcut code, now in ZmtShortcutPlugin
ZmtLaunchConfigPlugin.groovy
    uses sim class
ZmtSimulationPlugin.groovy
    applies ZmtShortcutPlugin
    updated test
runWithGui, runWithGui.bat, runWithUI-external.launch, runWithUI-project.launch
    adapted to change in variable to simClassName
parent f9b8d7ff
......@@ -13,6 +13,9 @@ import java.nio.file.Files
* simulation in eclipse via launcher.
*/
class ZmtLaunchConfigPlugin implements Plugin<Project> {
/** The name of the property pointing to the simulation class. */
public static final def SIM_CLASS_PROPERTY_NAME = 'simClassName'
private static final def TEMPLATE_ENGINE = new groovy.text.SimpleTemplateEngine()
private static final def LAUNCH_CONFIG_PROJECT_FILENAME = 'runWithUI-project.launch'
private static final def LAUNCH_CONFIG_EXTERNAL_FILENAME = 'runWithUI-external.launch'
......@@ -21,10 +24,10 @@ class ZmtLaunchConfigPlugin implements Plugin<Project> {
apply plugin: 'eclipse'
def launcherProject = rootProject.ext.properties.get('launcherProject')
def launchConfig = createLaunchConfigPath(project)
def launchConfig
// need to have dependencies and stuff evaluated
afterEvaluate {
launchConfig = createLaunchConfigPath(project)
addDependencies(project, launcherProject)
}
......@@ -44,6 +47,30 @@ class ZmtLaunchConfigPlugin implements Plugin<Project> {
project.configure(project, CONFIGURE)
}
/**
* Finds simulation class name either by project property
* {@value #SIM_CLASS_PROPERTY_NAME} or via project name.
* <p>
* <strong>NOTE: </strong> This method should be called after
* evaluating the project. Otherwise the property will not be present
* if set in project's build.gradle script.
* @param project the project where to look for the name
*/
public static String findSimClassName(Project project) {
def simClassName = project.ext.properties.get(SIM_CLASS_PROPERTY_NAME)
// property not set, deriving from project name
if (simClassName == null) {
def firstCharUpper = project.name.charAt(0).toUpperCase()
simClassName = firstCharUpper.toString() + project.name.substring(1)
project.ext.setProperty(SIM_CLASS_PROPERTY_NAME, simClassName)
println ('Property ' + SIM_CLASS_PROPERTY_NAME + ' for project ' + project + ' not set. Derived from project name: ' + simClassName)
}
return simClassName
}
/**
* Adds dependencies according to the availability of the
* launcher project.
......@@ -61,10 +88,13 @@ class ZmtLaunchConfigPlugin implements Plugin<Project> {
}
}
/**
* Creates launch config path within project directory.
* @param project the project
*/
private static Path createLaunchConfigPath(Project project) {
def upperCaseName = project.name.substring(0, 1).toUpperCase()
upperCaseName += project.name.substring(1)
return project.projectDir.toPath().resolve("Run${upperCaseName}WithUI.launch")
def simClassName = findSimClassName(project)
return project.projectDir.toPath().resolve("Run${simClassName}WithUI.launch")
}
/**
......@@ -77,7 +107,7 @@ class ZmtLaunchConfigPlugin implements Plugin<Project> {
* @return launch configuration text
*/
private static String createLaunchConfigText(Project project, Project launcherProject) {
def binding = [projectName : project.name, projectDir : project.projectDir]
def binding = [projectName : project.name, projectDir : project.projectDir, simClassName : findSimClassName(project)]
def templateFilename = LAUNCH_CONFIG_EXTERNAL_FILENAME
// if project is available, return launch config pointing to it
if (launcherProject != null) {
......@@ -85,7 +115,7 @@ class ZmtLaunchConfigPlugin implements Plugin<Project> {
binding.put('launcherProjectDir', launcherProject.projectDir)
templateFilename = LAUNCH_CONFIG_PROJECT_FILENAME
}
def templateUrl = ZmtLaunchConfigPlugin.class.getResource(LAUNCH_CONFIG_PROJECT_FILENAME)
def templateUrl = ZmtLaunchConfigPlugin.class.getResource(templateFilename)
return TEMPLATE_ENGINE.createTemplate(templateUrl).make(binding)
}
}
......
......@@ -2,58 +2,37 @@ package de.zmt.gradle;
import org.gradle.api.*
import org.gradle.api.file.DuplicatesStrategy
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.PosixFilePermissions
import java.net.URL
import groovy.lang.Writable
/*
* Distribution plugin for simulations based on zmt.core
* Launch plugin for simulations based on zmt-core.
* <p>
* With this plugin the simulation's jar file, source, dependencies
* and the runtime dependencies are added to the distribution's lib
* folder.
* <p>
* Start scripts for the launcher are added and a shortcut for running
* the simulation in GUI mode.
* folder. It also adds start scripts for the launcher.
*/
public class ZmtLaunchPlugin implements Plugin<Project> {
private static final def MAIN_CLASS_NAME = 'de.zmt.Main'
private static final def SHORTCUT_UNIX = 'run-with-gui'
private static final def SHORTCUT_WIN = 'run-with-gui.bat'
private static final def CONFIGURE = {
apply plugin: 'distribution'
// create shortcuts
def shortcutDir = Files.createTempDirectory('shortcuts')
Path unixShortcut = createLauncherShortcut(project, SHORTCUT_UNIX, shortcutDir)
// set +x permissions for unix shortcut
Files.setPosixFilePermissions(unixShortcut, PosixFilePermissions.fromString('rwxr-xr-x'))
createLauncherShortcut(project, SHORTCUT_WIN, shortcutDir)
distributions {
main {
contents {
into ('lib') {
// project's own library and dependencies
from (project.jar)
from (project.sourceJar)
from (project.configurations.runtime)
}
from (shortcutDir.toFile())
// prevent adding duplicates
// e.g. when several projects depend on the same library
// otherwise they are added several times to archives
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
distributions.main.contents {
into ('lib') {
// project's own library and dependencies
from (project.jar)
from (project.sourceJar)
from (project.configurations.runtime)
}
// prevent adding duplicates
// e.g. when several projects depend on the same library
// otherwise they are added several times to archives
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
// properties map will return null if launcher project is not set
def launcherProject = rootProject.ext.properties.get('launcherProject')
// if this project is the launcher
if (project.name == 'zmt-launcher') {
......@@ -123,27 +102,6 @@ public class ZmtLaunchPlugin implements Plugin<Project> {
}
}
private static final def TEMPLATE_ENGINE = new groovy.text.SimpleTemplateEngine()
/**
* Creates a launcher shortcut script. The template script is loaded
* from given file name, processed to match the current project
* and saved to the given directory.
*
* @param project the project
* @param filename the file name of the template and the resulting file
* @param directory the directory where to save the resulting file
* @return the path to the resulting file
*/
private static Path createLauncherShortcut(Project project, String filename, Path directory) {
Path shortcut = Files.createFile(directory.resolve(filename))
def binding = [projectName : project.name]
def templateUrl = ZmtLaunchPlugin.class.getResource(filename)
shortcut.append(TEMPLATE_ENGINE.createTemplate(templateUrl).make(binding))
return shortcut
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
......
package de.zmt.gradle;
import org.gradle.api.*
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.PosixFilePermissions
/**
* Plugin for simulations based on zmt-core.
* <p>
* This plugin adds shortcuts to conveniently launch the simulation in
* GUI mode.
*/
public class ZmtShortcutPlugin implements Plugin<Project> {
private static final def SHORTCUT_UNIX = 'run-with-gui'
private static final def SHORTCUT_WIN = 'run-with-gui.bat'
private static final def TEMPLATE_ENGINE = new groovy.text.SimpleTemplateEngine()
private static final def CONFIGURE = {
apply plugin: 'distribution'
// create shortcuts
def shortcutDir = Files.createTempDirectory('shortcuts')
afterEvaluate {
def unixShortcut = createLauncherShortcut(project, SHORTCUT_UNIX, shortcutDir)
// set +x permissions for unix shortcut
Files.setPosixFilePermissions(unixShortcut, PosixFilePermissions.fromString('rwxr-xr-x'))
createLauncherShortcut(project, SHORTCUT_WIN, shortcutDir)
}
distributions.main.contents {
from (shortcutDir.toFile())
}
}
/**
* Creates a launcher shortcut script. The template script is loaded
* from given file name, processed to match the current project
* and saved to the given directory.
*
* @param project the project
* @param filename the file name of the template and the resulting file
* @param directory the directory where to save the resulting file
* @return the path to the resulting file
*/
private static Path createLauncherShortcut(Project project, String filename, Path directory) {
Path shortcut = Files.createFile(directory.resolve(filename))
def binding = [simClassName : ZmtLaunchConfigPlugin.findSimClassName(project)]
def templateUrl = ZmtLaunchPlugin.class.getResource(filename)
shortcut.append(TEMPLATE_ENGINE.createTemplate(templateUrl).make(binding))
return shortcut
}
@Override
void apply(Project project) {
project.configure(project, CONFIGURE)
}
}
......@@ -15,6 +15,7 @@ public class ZmtSimulationPlugin implements Plugin<Project> {
apply(plugin: ZmtDeployPlugin)
apply(plugin: ZmtEclipsePlugin)
apply(plugin: ZmtLaunchConfigPlugin)
apply(plugin: ZmtShortcutPlugin)
}
@Override
......
......@@ -6,4 +6,4 @@
##
##############################################################################
./bin/launcher $projectName gui
./bin/launcher $simClassName gui
......@@ -4,4 +4,4 @@
@rem
@rem ##########################################################################
.\\bin\\launcher.bat $projectName gui
.\\bin\\launcher.bat $simClassName gui
......@@ -8,7 +8,7 @@
</listAttribute>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="de.zmt.Main"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="${projectName} gui"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="${simClassName} gui"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="${projectName}"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea"/>
</launchConfiguration>
......@@ -7,7 +7,7 @@
<listEntry value="1"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="de.zmt.Main"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="${projectName} gui"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="${simClassName} gui"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="${launcherProjectName}"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea"/>
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="\${workspace_loc:${projectName}}"/>
......
......@@ -21,6 +21,7 @@ class ZmtSimulationPluginTest {
assertTrue(plugins.hasPlugin(ZmtDeployPlugin.class))
assertTrue(plugins.hasPlugin(ZmtEclipsePlugin.class))
assertTrue(plugins.hasPlugin(ZmtLaunchConfigPlugin.class))
assertTrue(plugins.hasPlugin(ZmtShortcutPlugin.class))
}
project.evaluate()
......
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