view src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 32:927f02ccc596 version-0.4.6

Update to Gradle 7.4.2
author Kyle Miller <kyle.miller@mcoecn.org>
date Fri, 05 Aug 2022 19:22:06 +0100
parents bf31e9ad44cf
children
line wrap: on
line source
/*
 * Copyright (c) 2020.  Ohio Department of Education. - All Rights Reserved.
 * Unauthorized copying of this file, in any medium, is strictly prohibited.
 * Written by the State Software Development Team (http://ssdt.oecn.k12.oh.us/)
 *
 */

package org.ssdt_ohio.gradle.plugins

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.ExtraPropertiesExtension.UnknownPropertyException
import org.gradle.api.plugins.GroovyPlugin

class VersionClassPlugin implements Plugin<Project> {

    def void apply(Project project) {
        project.getPlugins().apply(GroovyPlugin.class)
        def genSrc = 'generated-src/version'
        def generatedSrcDir = new File(project.buildDir, genSrc)

        def makeVersionClassTask = project.task('makeVersionClass') {
			doLast {
				def now = new Date().format('yyyy-MM-dd hh:mm:ss')

				def buildNumber = -1
				if (project.rootProject?.file('build-number.txt').exists()) {
					def props = new Properties()
					project.rootProject?.file('build-number.txt').withInputStream {stream ->
						props.load(stream)
					}
					buildNumber = props['build.number']
				}

				def targetPackage = getTargetClass(project)

				if (!targetPackage) {
					return
				}

				def outFile = new File(generatedSrcDir, "groovy/" + targetPackage.replaceAll('\\.', "/") + "/ProjectVersion.groovy")

				outFile.getParentFile().mkdirs()
				logger.info("creating $targetPackage in $outFile")
				def f = new FileWriter(outFile)
				f.write("""
					package $targetPackage
					/**
					 * Generated by gradle build. $now ($buildNumber)
					 * @version ${project.version}
					 */
					public class ProjectVersion {

						final String name = "${project.name}"
						final String group = "${project.group}"
						final String version = "${project.version}"
						final String buildDate = "$now"
						final int buildNumber = $buildNumber

						String getFullVersion() {
							"\$version \$buildDate (#\$buildNumber)"
						}

						String getId() {
							"\$group:\$name:\$version"
						}

						String toString() {
							"\$id \$buildDate (#\$buildNumber)"
						}
					}
					""")
				f.close()
			}
        }

        project.sourceSets {
            main {
                groovy {
                    srcDir project.buildDir.name + '/' + genSrc + '/groovy'
                }
            }
        }

        makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource().findAll{ !it.name.startsWith('ProjectVersion')}
        )
        
        makeVersionClassTask.getOutputs().dir(project.buildDir.name + '/' + genSrc)

        if (project.getBuildFile() != null && project.getBuildFile().exists()) {
            makeVersionClassTask.getInputs().files(project.getBuildFile())
        }

        project.getTasks().getByName('compileGroovy').dependsOn('makeVersionClass')


    }



    private getTargetClass(Project project) {
        try {
            return project.extensions.extraProperties.get('versionPackage')
        } catch (UnknownPropertyException ex) {}

        def source = project.sourceSets.main.groovy.getSrcDirTrees()

        def files = project.sourceSets.main.groovy
        
        if (!files || files.size() == 0) {
            return null
        }

        def targetDir = files.sort {
            it.parentFile.toString().replaceAll('(\\\\|/)','.').split(/\./).size()
        }.first().parentFile.toString()

        def targetPackage = targetDir
        source.each {
            targetPackage -= it.getDir().getPath().toString()
        }
        targetPackage = targetPackage.replaceAll("\\\\", '.').replaceAll('/', '.')
        targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage

        return targetPackage

    }

}