ZmtLaunchConfigPlugin.groovy 4.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
package de.zmt.gradle;

import org.gradle.api.*
import org.gradle.api.artifacts.ProjectDependency
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files

/*
 * Launch Configuration Plugin for simulations based on zmt.core
 * <p>
 * With this plugin a launch configuration will be generated to launch the
 * simulation in eclipse via launcher.
 */
class ZmtLaunchConfigPlugin implements Plugin<Project> {
    private static final def CONFIGURE = {
        apply plugin: 'eclipse'
    
        def launcherProject = rootProject.ext.properties.get('launcherProject')
        def launchConfig = createLaunchConfigPath(project)
        
        // need to have dependencies and stuff evaluated
        afterEvaluate {
            addDependencies(project, launcherProject)
        }

        // create launch configuration
        tasks.eclipse.doLast {
            launchConfig.text = createLaunchConfigText(project, launcherProject)
        }

        // delete launch configuration
        tasks.cleanEclipse.doLast {
            Files.deleteIfExists(launchConfig)
        }
    }

    @Override
    void apply(Project project) {
        project.configure(project, CONFIGURE)
    }
    
    /**
     * Adds dependencies according to the availability of the
     * launcher project.
     * 
     * @param project the project
     * @param launcherProject the launcher project
     */
    private static void addDependencies(Project project, Project launcherProject) {
        /* add runtime dependency for this project in launcher 
         * (if available) to make it executable in eclipse */
        launcherProject?.afterEvaluate {
            launcherProject.dependencies {
                runtime(project) {
                    transitive = false
                }
            }
        }
        
        /* If launcher project is not available a runtime dependency
         * for launcher is added to this project. */
        if (launcherProject == null) {
            project.dependencies {
                runtime 'de.zmt:zmt-launcher:latest.release'
            }
        }
    }
    
    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")
    }
    
    /**
     * Creates text of an eclipse configuration to launch current
     * project with zmt-launcher. If the launcher project is
     * <code>null</code> the configuration will point to the libary.
     * 
     * @param project the project
     * @param launcherProject the launcher project
     * @return launch configuration text
     */
    private static String createLaunchConfigText(Project project, Project launcherProject) {
        // if project is available, return launch config pointing to it
        if (launcherProject != null) {
            return """<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>
<launchConfiguration type=\"org.eclipse.jdt.launching.localJavaApplication\">
<listAttribute key=\"org.eclipse.debug.core.MAPPED_RESOURCE_PATHS\">
<listEntry value=\"${launcherProject.projectDir}/src/main/java/de/zmt/Main.java\"/>
</listAttribute>
<listAttribute key=\"org.eclipse.debug.core.MAPPED_RESOURCE_TYPES\">
<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=\"$project.name gui\"/>
<stringAttribute key=\"org.eclipse.jdt.launching.PROJECT_ATTR\" value=\"$launcherProject.name\"/>
<stringAttribute key=\"org.eclipse.jdt.launching.WORKING_DIRECTORY\" value=\"\${workspace_loc:$project.name}\"/>
</launchConfiguration>"""
        }
        // ... otherwise return config pointing to launcher library
        return """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="${project.projectDir}"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</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="$project.name gui"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="$project.name"/>
</launchConfiguration>"""
    }
}