comparison init.gradle @ 105:f7b0f9d286b8

allow environment variables to be overridden on command line
author smith@nwoca.org
date Tue, 15 May 2012 19:02:25 +0100
parents 13a1e737ca31
children 34bb1f5b9027
comparison
equal deleted inserted replaced
104:13a1e737ca31 105:f7b0f9d286b8
109 109
110 task showEnvironments << { 110 task showEnvironments << {
111 println "Defined environments: $gradle.environments" 111 println "Defined environments: $gradle.environments"
112 gradle.environments.each { e -> 112 gradle.environments.each { e ->
113 println "\n $e:" 113 println "\n $e:"
114 gradle.getProperty(e).flatten().each { k, v -> println String.format(' %25s = %s',k,v) } 114 gradle.getProperty(e).flatten().each { k, v ->
115 println String.format(' %25s = %s',k, k.contains('password') ? "********" : v )
116 }
115 } 117 }
116 } 118 }
117 119
118 def loadEnvironments() { 120 def loadEnvironments() {
119 def developerPrivate = new Properties() 121 def developerPrivate = new Properties()
123 gradle.ext.env = (hasProperty('env') ? getProperty('env') : (developerPrivate.env ?: 'dev')) 125 gradle.ext.env = (hasProperty('env') ? getProperty('env') : (developerPrivate.env ?: 'dev'))
124 println "Environment is: $gradle.env" 126 println "Environment is: $gradle.env"
125 def slurper = new ConfigSlurper(gradle.env) 127 def slurper = new ConfigSlurper(gradle.env)
126 def environment = new ConfigObject() 128 def environment = new ConfigObject()
127 def environments = [] 129 def environments = []
130
128 file('.').eachFileMatch(~/^environment.*\.groovy$/) { envFile -> 131 file('.').eachFileMatch(~/^environment.*\.groovy$/) { envFile ->
129 def envName = envFile.name - '.groovy' 132 def envName = envFile.name - '.groovy'
130 logger.info("loading environment $envFile.name") 133 logger.info("loading environment $envFile.name")
131 def envCfg = slurper.parse(envFile.toURL()) 134 def envCfg = slurper.parse(envFile.toURL())
132 envCfg.merge(slurper.parse(developerPrivate)) 135 envCfg.merge(slurper.parse(developerPrivate))
136 gradle.rootProject.getProperties().find { it.key.startsWith('environment') }.each {
137 it.value.split(',').each { p ->
138 def (k,v) = p.split('=')
139 logger.info("$envName: overriding " + k + "=" + v)
140 envCfg.put(k,v)
141 }
142 }
133 environment.merge(envCfg) 143 environment.merge(envCfg)
134 if (envName != 'environment' ) { 144 if (envName != 'environment' ) {
135 gradle.ext[envName] =envCfg 145 gradle.ext[envName] =envCfg
136 environments << envName 146 environments << envName
137 } 147 }
138 } 148 }
139 environment.merge(slurper.parse(developerPrivate)) 149 environment.merge(slurper.parse(developerPrivate))
140 environments << 'environment' 150 environments << 'environment'
141 gradle.ext.environment = environment 151 gradle.ext.environment = environment
142 gradle.ext.environments = environments 152 gradle.ext.environments = environments
153
143 } 154 }