view init.gradle @ 250:88d5807ea351

USASR-2891: Add back removed creation of directory
author Christopher Springer <springer@nwoca.org>
date Fri, 27 Oct 2017 16:46:37 +0100
parents c3b7af90d1e4
children 476b0c13b22e
line wrap: on
line source
import groovy.sql.*

final GradleVersion gradleCurrent = GradleVersion.current()
final GradleVersion gradleV20 = GradleVersion.version('2.0')
println "Gradle Version: $gradleCurrent"
if (gradleCurrent >= gradleV20) {
    throw new RuntimeException("this init script does not function with Gradle 2.+, use init20.gradle")
}
gradle.ext.ssdtDevelkitLocation = gradle.ext.has('ssdtDevelkitLocation') ? gradle.ssdtDevelkitLocation : 'http://hg.ssdt-ohio.org/browse/public/develkit'

ant.property(file: System.getProperty('user.home') + "/.ssdt/private.properties")
gradle.ext.ivyUserDir = ant.properties['ivy.default.ivy.user.dir'] ?: System.getProperty('user.home') + "/.ivy2"

gradle.ext.ssdtProjectId = System.getenv('bamboo_project_id') ?: rootProject.name

gradle.addListener(new ArtifactoryGradleSettings())

def hostname 
try { hostname = "hostname".execute().text.toLowerCase().readLines().first() } catch (e) { hostname = 'unknown'}

gradle.ext.bambooBuild = System.getenv().any { it.key.toLowerCase().contains('bamboo')} || hostname?.startsWith('ssdt-')

gradle.ext.bambooPlan = (System.getenv('BAMBOO_PLAN') ?: 'UNKNOWN-UNKNOWN-JOB1').split('-')[0..1].join('-')
logger.info "Bamboo plan: ${gradle.bambooPlan}"

gradle.ext.buildTimestamp = System.currentTimeMillis().toString().padLeft(14,'0')

gradle.ext.hgRepositoryUrl = ""
try {	
	gradle.ext.hgRepositoryUrl = ("hg path".execute().text.split('=') ?: ['',''] )[1].trim()
} catch (e) {  }

def springModuleTranslator = [
	'spring-transaction' : 'spring-tx',
	'spring-web-servlet' : 'spring-webmvc',
	].withDefault { it }

gradle.ext.normalizeSpring = { DependencyResolveDetails details ->
		if (details.requested.group == 'org.springframework' && details.requested.name.startsWith ('org.springframework.' )) {
			def shortName = springModuleTranslator[details.requested.name.replace('org.springframework.', 'spring-').replace('.','-')]
			details.useTarget(group: 'org.springframework', name: shortName, version: details.requested.version)
		}
		if (details.requested.group == 'org.springframework.security' && details.requested.name.startsWith ('org.springframework.' )) {
			def shortName = springModuleTranslator[details.requested.name.replace('org.springframework.', 'spring-').replace('.','-')]
			details.useTarget( "${details.requested.group}:$shortName:${details.requested.version}")
		}
	}
       

gradle.ext.branchName = System.getenv('bamboo_planRepository_branch')
gradle.ext.branchStream = ""

if (!gradle.branchName ) {
	gradle.ext.branchName = "hg branch".execute().text.trim()
	def rawParents = 'hg parents'.execute().text
	def parent = rawParents.split('\n').find { it.startsWith("branch") }?.split(":")?.getAt(1)?.trim()	
	gradle.ext.branchName = parent ?: gradle.branchName		
}

if (file('.hgflow').exists()) {
	def flows = new Properties()
	flows.load(file('.hgflow').newReader())
	flows.stringPropertyNames().each {
		if (!it.startsWith("[") && gradle.branchName.startsWith(flows.getProperty(it))) {
			gradle.ext.branchStream = it
		}	
	}	
}

gradle.branchName = gradle.branchName.replace('@','-')
gradle.ext.branchHash = generateMD5(gradle.branchName)
loadEnvironments()

gradle.environment.put('hgRepositoryUrl',gradle.hgRepositoryUrl)
gradle.environment.put('branchName',gradle.branchName)
gradle.environment.put('branchStream',gradle.branchStream)
gradle.environment.put('branchHash',gradle.branchHash)

	
rootProject.ext.indyCapable = {
    boolean capable = true
    try {
        Class.forName('java.lang.invoke.MethodHandle')
    } catch (e) {
        capable = false
    }
    capable && !rootProject.hasProperty('skipIndy')
}

rootProject.ext.useIndy = {
    boolean indy = false
    // first, check if a system property activates indy support
    indy |= System.hasProperty('indy') && Boolean.valueOf(System.getProperty('indy'))

    // check ssdt environment for indy property.
    indy |= (gradle.environment.indy) ? gradle.environment.indy.toBoolean() : false

    // check if the main project has an extension property setting indy (-Pindy).
    if (rootProject.hasProperty('indy')) { indy =  (Boolean.valueOf(rootProject.indy)) }

    // set the groovy runtime system property to ensure forked junit test will get the indy flag properly
    if (indy && rootProject.indyCapable()) System.setProperty("groovy.target.indy","true")

    indy && rootProject.indyCapable()
}

println "Indy available: ${rootProject.indyCapable()} enabled: ${rootProject.useIndy()}"
        
if (gradle.bambooBuild) {
   gradle.ext.ssdtGradlekitLocation = gradle.ext.has('ssdtGradlekitLocation') ? gradle.ssdtGradlekitLocation : 'http://hg.ssdt-ohio.org/ssdt/gradlekit/raw-file/tip'
   logger.info "applying SSDT artifactory Gradle Settings (bamboo: $gradle.bambooBuild host: $hostname)"
   apply from: "${gradle.ssdtGradlekitLocation}/artifactory.gradle" 
}


buildscript {
    repositories {
        maven { url 'http://repos.ssdt.nwoca.org/artifactory/ssdt-repo' }
    }
}

apply from: "${gradle.ssdtDevelkitLocation}/metrics.gradle"   

task wrapper(type: Wrapper) {}

rootProject.afterEvaluate { r ->

   if (r.plugins.hasPlugin(org.gradle.plugins.ide.idea.IdeaPlugin)) {
   
		r.idea.project.ipr.withXml { xml ->
			if (file('.hg').exists()) {
				println 'enabling HG on project'
				def vcs = findComponent(xml.asNode(),'VcsDirectoryMappings')
				vcs.mapping.@vcs = 'hg4idea'
			}
			
			def codeStyle = findComponent(xml.asNode(),'ProjectCodeStyleSettingsManager')
			if (codeStyle) { xml.asNode().remove(codeStyle) }
			
			def styleManager = xml.asNode().appendNode('component', [name: 'ProjectCodeStyleSettingsManager'])
			styleManager.appendNode 'option', [name: 'USE_PER_PROJECT_SETTINGS',value: true]
			
			def styleOptions = new NodeBuilder().option(name:'PER_PROJECT_SETTINGS') {
				value {
					GroovyCodeStyleSettings {
						option(name: 'CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND', value:'10') 
						option(name: 'PACKAGES_TO_USE_IMPORT_ON_DEMAND') {
							value {
								'package'( name:"java.awt", withSubpackages: false, static: false )
								'package'( name:"javax.swing", withSubpackages:false, static:false )
								'package'( name:"javax.persistence", withSubpackages:false, static:false )
								'package'( name:"javax.validation", withSubpackages: true, static:false )
							}
						}
					}
					XML {
						option(name: 'XML_LEGACY_SETTINGS_IMPORTED',value:true)
					}
					
				}
			}
			
			styleManager.append styleOptions
			
			if (r.group.contains('org.ssdt') ) {
			
				def copyRight = findComponent(xml.asNode(),'CopyrightManager')
				if (copyRight) { xml.asNode().remove(copyRight) }

				copyRight = new NodeBuilder().component(name: 'CopyrightManager', default: 'ODE') {
				
					copyright {
						option (name: 'notice', value:'Copyright (c) $today.year.  Ohio Department of Education. - All Rights Reserved.\nUnauthorized copying of this file, in any medium, is strictly prohibited.\nWritten by State Software Development Team (http://ssdt.oecn.k12.oh.us/)' )
						option (name: 'keyword', value: 'Copyright')
						option (name: 'allowReplaceKeyword', value: 'South Front' )
						option (name: 'myName', value: 'ODE' )
						option (name: 'myLocal', value: true )				
					}
					LanguageOptions(name: 'CSS') {
						option(name:'fileTypeOverride', value: 1)
					}
					LanguageOptions(name: 'JSP') {
						option(name:'fileTypeOverride', value: 1)
						option(name:'prefixLines', value: false)
					}
					LanguageOptions(name: 'JSPX') {
						option(name:'fileTypeOverride', value: 1)
						option(name:'prefixLines', value: false)
					}
					LanguageOptions(name: 'JavaScript') {
						option(name:'fileTypeOverride', value: 1)
					}
					LanguageOptions(name: 'Properties') {
						option(name:'fileTypeOverride', value: 1)
					}
				}
				
				xml.asNode().append copyRight
			
			}
		}
   }
   
}

def findComponent(project, name) {
	project.component.find { it.@name == name }
}

allprojects {

    task cleanLocal(description: "removes all artifacts from developer's local repository") << {

        def local = project.repositories.find { it.name == 'local' } 
        if ( local ) {                  
            logger.info "removing local repo: $it"
            new File(System.properties['user.home'] + "/.ssdt/local-repo").deleteDir() 
            def localDir = new File(gradle.ivyUserDir + "/local")
            localDir.deleteDir()
            logger.info "verifying removal of local repo"
            if (localDir.exists()) {
                throw new org.gradle.api.GradleException("Unable to clean ${localDir}. Files may be locked by another process.")
            }
        }
    }    
    
    cleanLocal.onlyIf { 
        project.repositories.any { it.name == 'local' }
    }    
	
	tasks.addRule("Pattern: <environment>As[Test]Properties: Generates <environment>.properties as resource or Test resource") { String taskName ->
        if ((taskName - 'Test').endsWith("AsProperties") && !taskName.startsWith('clean')) {
            def t = taskName.contains('Test') ? processTestResources.destinationDir : processResources.destinationDir
            def e = (taskName - 'Test' - 'AsProperties').capitalize()
            task(taskName) {
                ext.propertyFile = new File(t, "${e.toLowerCase()}.properties")
                inputs.files "../environment${e}.groovy", "../private${e}.groovy", '../private.properties'               
                doLast {
                    t.mkdirs()
                    def ps = gradle."environment${e}".toProperties()
					ps['ssdt.project'] = project.name
					ps.store(propertyFile.newOutputStream(), "by $taskName of $this")
					def l = propertyFile.readLines().sort()
					propertyFile.text = l.join('\n')
                }
            }
        }
    }

	if (gradle.hasProperty('environmentDatabase')) {
		tasks.addRule("Pattern: database{Drop|Create}...: drops or creates the environment's database") { String taskName ->
			if(taskName.startsWith('database') && (taskName.contains('Drop') || taskName.contains('Create')) ) {
			   task(taskName) {
					doLast {
						def dropIt = taskName.contains("Drop")
						def createIt = taskName.contains("Create")
						def driverName = gradle.environmentDatabase.jdbc.driverClassName
						def databaseUrl = gradle.environmentDatabase.jdbc.url
						if (driverName != "org.postgresql.Driver") {
							logger.info "skipping database operation for non-postgresql driver"
							throw new StopActionException("driver $driverName not supported by $taskName")
						}
						if (!configurations.hasProperty('jdbcdriver')) {
							throw new GradleException("databaseDrop requires a 'jdbcdriver' configuration with a postgresql (or other) driver" )
						} 
						configurations.jdbcdriver.each {File file ->
							GroovyObject.class.classLoader.addURL(file.toURL())
						}						
						Class.forName(driverName)
						def url = new URI(databaseUrl.substring(5))
						def jdbcBaseUrl = databaseUrl - url.path
						def databaseName = url.path - "/"
						def adminUrl = jdbcBaseUrl + "/postgres"
						def sql =  Sql.newInstance(adminUrl,
											gradle.environmentDatabase.jdbc.username,
											gradle.environmentDatabase.jdbc.password,
											driverName)
						if (dropIt) {
							logger.info "dropping $databaseName (if exists) from $adminUrl"
							sql.execute 'DROP DATABASE IF EXISTS "' + databaseName + '";' 
						}

						if (createIt) {
							logger.info  "Creating database $databaseName at $adminUrl"
							sql.execute 'CREATE DATABASE "' + databaseName + '";' 
						}						
					}
			   }
			
			}
		}
	}
    
}


class ArtifactoryGradleSettings extends BuildAdapter implements BuildListener {
    
    def void projectsEvaluated(Gradle gradle) {
        def ssdtArtifactory = 'http://repos.ssdt.nwoca.org/artifactory'
        Project root = gradle.getRootProject()       
        root.allprojects {
		
		
               
            def thisProject = delegate
			thisProject.status = 'integration'
			if (gradle.branchStream) {
				thisProject.status = 'integration'
				def fixupVersion = thisProject.version - ".SNAPSHOT"
				if ( gradle.branchStream == 'feature' ) {
					fixupVersion = fixupVersion + ".SNAPSHOT"
				}
				if ( gradle.branchStream == 'develop' ) {
					fixupVersion = fixupVersion + ".SNAPSHOT"
				}				
				if ( gradle.branchStream in ['production','release'] ) {
					thisProject.status = 'release'
				}
				thisProject.version = fixupVersion
			}
			
            configurations.all {
                resolutionStrategy.cacheChangingModulesFor 60 * 60 * 8, 'seconds'
                resolutionStrategy.cacheDynamicVersionsFor 60 * 60 * 8, 'seconds'
            }        
            repositories {
			
                if (!gradle.bambooBuild) {                         
                    add(new org.apache.ivy.plugins.resolver.FileSystemResolver()) {
                        name = 'local'
                        addIvyPattern gradle.ivyUserDir + "/local/[module]-ivy-[revision].xml"
                        addArtifactPattern gradle.ivyUserDir + '/local/[artifact]-[revision](-[classifier]).[ext]'
                        descriptor = 'optional'
                        checkmodified = true
                        changingMatcher = 'regexp'
                        changingPattern = '.*SNAPSHOT'
                        force = true
                    }                       
                }                   
				
				if(gradle.branchStream == 'feature') {
					add(new org.apache.ivy.plugins.resolver.URLResolver()) {
						name = 'ssdt-branches'
						m2compatible =  false
						def url = "${ssdtArtifactory}/ssdt-branches"
						def filter1 = ";ssdt-branch.branchName+=${gradle.branchName.replaceAll('/','-')}"
						def filter2 = ";ssdt-branch.branchName=${gradle.branchName.replaceAll('/','-')}"
						addArtifactPattern("${url}/${gradle.branchHash}/[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]")
						addIvyPattern("${url}/${gradle.branchHash}/[organization]/[module]/ivy-[revision].xml")
                        checkmodified = true
                        changingMatcher = 'regexp'
                        changingPattern = '.*SNAPSHOT'
					}
				}

                add(new org.apache.ivy.plugins.resolver.URLResolver()) {
                    name = 'ssdt-snapshots'
                    m2compatible =  true
                    def url = "${ssdtArtifactory}/ssdt-snapshots"
                    addArtifactPattern("${url}/[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]")
                    addArtifactPattern("${url}/[organization]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]")
                    addIvyPattern("${url}/[organization]/[module]/ivy-[revision].xml")
                    checkmodified = true
                    changingMatcher = 'regexp'
                    changingPattern = '.*SNAPSHOT'
                }

                add(new org.apache.ivy.plugins.resolver.URLResolver()) {
                    name = 'ssdt-releases'
                    m2compatible = false
                    def url = "${ssdtArtifactory}/ssdt-releases"
                    addArtifactPattern("${url}/[organization]/[module]/[revision]/[module]-[revision](-[classifier]).[ext]")
                    addArtifactPattern("${url}/[organization]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]")
                    addIvyPattern("${url}/[organization]/[module]/ivy-[revision].xml")
                }


                maven {
                    name = 'ssdt-repository'
                    url = "${ssdtArtifactory}/repository"
                }

                              
            }           
            
            if ( thisProject.repositories.find { it.name == 'local' } && thisProject.getTasksByName('uploadArchives',false) ) {
                uploadArchives {
                   repositories {                     
                         add thisProject.repositories.local
                    }
                }
                
                thisProject.tasks.create("publishLocal") {
                    description = "Publishes this projects artifacts to developer's local repository"
                    dependsOn = ["uploadArchives"]
                }
            }

        }

        root.subprojects { p ->     
            if (root.useIndy()) {
                def groovyIndy = p.configurations.compile.files.find { f -> f.name.startsWith('groovy-all') && f.name.contains('-indy') }
                if (groovyIndy) {
                    logger.info "enabling indy on $p"
                    [compileGroovy.groovyOptions,compileTestGroovy.groovyOptions]*.with {
                        optimizationOptions = [ indy: true ]
                    }
                }
            }       
        }
    }
}


task showEnvironments << {
    println "Defined environments: $gradle.environments"   
    gradle.environments.each { e -> 
        println "\n $e:"        
        gradle.getProperty(e).flatten().sort { it.key }.each { k, v -> 
            println String.format('  %25s = %s',k, k.contains('password') ? "********" : v )
        }
    }
    if (logger.isInfoEnabled() ) {
        println "System properties:"
        System.properties.each { println "    $it" }
        println "env variables:"
        System.getenv().each { println "    $it" }
    }
}

def loadEnvironments() {
    def developerPrivate = new Properties()
    if (file('private.properties').exists()) {
        developerPrivate.load(file('private.properties').newReader())
    }
    def envOverrides = [:]
    
    if ( !hasProperty('env') ) {
        gradle.ext.env = developerPrivate.env ?: 'dev'
    } else {
        def values = getProperty('env').split(',')
        gradle.ext.env = values.first()
        values.tail().each {
            def (k,v) = it.split('=')
            envOverrides.put(k,v)
        }
    }
    
    println "Environment is: $gradle.env ($envOverrides)"
    def slurper = new ConfigSlurper(gradle.env)
	slurper.setBinding(['gradle': gradle ])
	
    def environment = slurper.parse (
	'''deploy.mode="production"
	   environments {
		test { deploy.mode="test" }
		dev { deploy.mode="development"}
	   }''')
	if (developerPrivate['deploy.mode']) { environment.put('deploy.mode',developerPrivate['deploy.mode']) }
	
    def environments = []
    gradle.ext.environment = environment
    file('.').listFiles().findAll { it.name ==~ /^environment.*\.groovy$/ }.sort { it.name }.each { envFile ->
        def envName = envFile.name - '.groovy'
        def privateFile = file("private" + envName - "environment" + ".groovy")
        logger.info("loading environment $envFile.name")

        def envCfg = slurper.parse(envFile.toURL())
        envCfg.merge(slurper.parse(developerPrivate))
		envCfg.put('ssdt.projectid',gradle.ssdtProjectId)
		envCfg.put('ssdt.environment',gradle.env)
        if (privateFile.exists()) {
            logger.info("loading private environment $privateFile")
            envCfg.merge(slurper.parse(privateFile.toURL()))
        }
        
        gradle.rootProject.getProperties().find { it.key.startsWith('environment')  }.each {
            it.value.split(',').each { p ->
                def (k,v) = p.split('=')
                logger.info("$envName: overriding " + k + "=" + v  + " in $it")
                envCfg.put(k,v)
            }
        }
        
        envOverrides.each { k,v ->
            logger.info("$envName: overriding " + k + "=" + v)
            envCfg.put(k,v)
        }
        environment.merge(envCfg)
        if (envName != 'environment' ) {
           gradle.ext[envName] =envCfg 
           environments << envName
        }
    }    
    environment.merge(slurper.parse(developerPrivate))
	def deployMode = environment.deploy.mode ?: 'development'
	environments.each { gradle.ext[it].put('ssdt.deployment.mode',deployMode) } 
    environments << 'environment'
    gradle.ext.environments = environments

}

def generateMD5(String s) {
    def digest = java.security.MessageDigest.getInstance("MD5")
    digest.update(s.bytes);
    new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
 }