|
| 1 | +import groovy.text.SimpleTemplateEngine |
| 2 | +import org.codehaus.groovy.runtime.MethodClosure |
| 3 | + |
| 4 | +ext.propertyString = this.&propertyString as MethodClosure |
| 5 | +ext.propertyBool = this.&propertyBool as MethodClosure |
| 6 | +ext.propertyStringList = this.&propertyStringList as MethodClosure |
| 7 | +ext.interpolate = this.&interpolate as MethodClosure |
| 8 | +ext.assertProperty = this.&assertProperty as MethodClosure |
| 9 | +ext.assertSubProperties = this.&assertSubProperties as MethodClosure |
| 10 | +ext.setDefaultProperty = this.&setDefaultProperty as MethodClosure |
| 11 | +ext.assertEnvironmentVariable = this.&assertEnvironmentVariable as MethodClosure |
| 12 | + |
| 13 | +String propertyString(String key) { |
| 14 | + return $property(key).toString() |
| 15 | +} |
| 16 | + |
| 17 | +boolean propertyBool(String key) { |
| 18 | + return propertyString(key).toBoolean() |
| 19 | +} |
| 20 | + |
| 21 | +Collection<String> propertyStringList(String key) { |
| 22 | + return propertyStringList(key, ' ') |
| 23 | +} |
| 24 | + |
| 25 | +Collection<String> propertyStringList(String key, String delimit) { |
| 26 | + return propertyString(key).split(delimit).findAll { !it.isEmpty() } |
| 27 | +} |
| 28 | + |
| 29 | +private Object $property(String key) { |
| 30 | + def value = project.findProperty(key) |
| 31 | + if (value instanceof String) { |
| 32 | + return interpolate(value) |
| 33 | + } |
| 34 | + return value |
| 35 | +} |
| 36 | + |
| 37 | +String interpolate(String value) { |
| 38 | + if (value.startsWith('${{') && value.endsWith('}}')) { |
| 39 | + value = value.substring(3, value.length() - 2) |
| 40 | + Binding newBinding = new Binding(this.binding.getVariables()) |
| 41 | + newBinding.setProperty('it', this) |
| 42 | + return new GroovyShell(this.getClass().getClassLoader(), newBinding).evaluate(value) |
| 43 | + } |
| 44 | + if (value.contains('${')) { |
| 45 | + return new SimpleTemplateEngine().createTemplate(value).make(project.properties).toString() |
| 46 | + } |
| 47 | + return value |
| 48 | +} |
| 49 | + |
| 50 | +void assertProperty(String propertyName) { |
| 51 | + def property = property(propertyName) |
| 52 | + if (property == null) { |
| 53 | + throw new GradleException("Property ${propertyName} is not defined!") |
| 54 | + } |
| 55 | + if (property.isEmpty()) { |
| 56 | + throw new GradleException("Property ${propertyName} is empty!") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void assertSubProperties(String propertyName, String... subPropertyNames) { |
| 61 | + assertProperty(propertyName) |
| 62 | + if (propertyBool(propertyName)) { |
| 63 | + for (String subPropertyName : subPropertyNames) { |
| 64 | + assertProperty(subPropertyName) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void setDefaultProperty(String propertyName, boolean warn, defaultValue) { |
| 70 | + def property = property(propertyName) |
| 71 | + def exists = true |
| 72 | + if (property == null) { |
| 73 | + exists = false |
| 74 | + if (warn) { |
| 75 | + project.logger.log(LogLevel.WARN, "Property ${propertyName} is not defined!") |
| 76 | + } |
| 77 | + } else if (property.isEmpty()) { |
| 78 | + exists = false |
| 79 | + if (warn) { |
| 80 | + project.logger.log(LogLevel.WARN, "Property ${propertyName} is empty!") |
| 81 | + } |
| 82 | + } |
| 83 | + if (!exists) { |
| 84 | + project.setProperty(propertyName, defaultValue.toString()) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void assertEnvironmentVariable(String propertyName) { |
| 89 | + def property = System.getenv(propertyName) |
| 90 | + if (property == null) { |
| 91 | + throw new GradleException("System Environment Variable $propertyName is not defined!") |
| 92 | + } |
| 93 | + if (property.isEmpty()) { |
| 94 | + throw new GradleException("Property $propertyName is empty!") |
| 95 | + } |
| 96 | +} |
0 commit comments