Added failOnWarning parameter.

This commit is contained in:
Erik C. Thauvin 2017-04-18 16:01:54 -07:00
parent 733aa96934
commit 90605dcafc
2 changed files with 34 additions and 28 deletions

View file

@ -34,10 +34,11 @@ To invoke the `propertyFile` task:
## Parameters ## Parameters
Attribute | Description | Required Attribute | Description | Required
:---------|:--------------------------------------------------------|:-------- :---------------|:--------------------------------------------------------|:--------
`file` | The location of the property files to edit. | Yes `file` | The location of the property files to edit. | Yes
`comment` | Comment to be inserted at the top of the property file. | No `comment` | Comment to be inserted at the top of the property file. | No
`failOnWarning` | If set to `true`, the task will fail on any warnings. | No
## Entry ## Entry
@ -94,4 +95,5 @@ Operations occur after the rules are evaluated.
## Differences with the [ant PropertyFile task](https://ant.apache.org/manual/Tasks/propertyfile.html) ## Differences with the [ant PropertyFile task](https://ant.apache.org/manual/Tasks/propertyfile.html)
* The comments and layout of the original property file will not be preserved. * The comments and layout of the original property file will not be preserved.
* The `jdkproperties` parameter is not implemented.
* The default `Types.DATE` pattern is `yyyy-MM-dd HH:mm` and not `yyyy/MM/dd HH:mm`. * The default `Types.DATE` pattern is `yyyy-MM-dd HH:mm` and not `yyyy/MM/dd HH:mm`.

View file

@ -83,7 +83,7 @@ class PropertyFilePlugin @Inject constructor(val configActor: ConfigActor<Proper
configurationFor(project)?.let { config -> configurationFor(project)?.let { config ->
if (config.file.isBlank()) { if (config.file.isBlank()) {
error("Please specify a property file name.") error("Please specify a property file name.")
return TaskResult() return TaskResult(!config.failOnWarning)
} else { } else {
// Load properties // Load properties
val p = Properties() val p = Properties()
@ -95,34 +95,35 @@ class PropertyFilePlugin @Inject constructor(val configActor: ConfigActor<Proper
} }
} }
var result = TaskResult() var success = true
// Process entries // Process entries
config.entries.forEach { entry -> config.entries.forEach { entry ->
if (entry.key.isBlank()) { if (entry.key.isBlank()) {
error("An entry key must be specified.") error("An entry key must be specified.")
return TaskResult() success = false
} else { } else {
with(entry) { with(entry) {
if (value == null && default == null && operation != Operations.DELETE) { if (value == null && default == null && operation != Operations.DELETE) {
warn("An entry value or default must be specified: $key") warn("An entry value or default must be specified: $key")
success = false
} else if (type == Types.STRING && (operation == Operations.SUBTRACT)) { } else if (type == Types.STRING && (operation == Operations.SUBTRACT)) {
warn("Subtraction is not supported for String properties: $key") warn("Subtraction is not supported for String properties: $key")
success = false
} else if (operation == Operations.DELETE) { } else if (operation == Operations.DELETE) {
p.remove(entry.key) p.remove(entry.key)
} else { } else {
when (type) { when (type) {
Types.DATE -> result = processDate(p, entry) Types.DATE -> success = processDate(p, entry)
Types.INT -> result = processInt(p, entry) Types.INT -> success = processInt(p, entry)
else -> result = processString(p, entry) else -> success = processString(p, entry)
} }
} }
} }
} }
// @TODO maybe just warn and keep on going? if (config.failOnWarning && !success) {
if (!result.success) { return TaskResult(success)
return result
} }
} }
@ -136,15 +137,12 @@ class PropertyFilePlugin @Inject constructor(val configActor: ConfigActor<Proper
return TaskResult() return TaskResult()
} }
private fun processDate(p: Properties, entry: Entry): TaskResult { private fun processDate(p: Properties, entry: Entry): Boolean {
var success = true
val cal = Calendar.getInstance() val cal = Calendar.getInstance()
val value = currentValue(p.getProperty(entry.key), entry.value, entry.default, entry.operation) val value = currentValue(p.getProperty(entry.key), entry.value, entry.default, entry.operation)
val fmt = SimpleDateFormat(if (entry.pattern.isBlank()) { val fmt = SimpleDateFormat(if (entry.pattern.isBlank()) "yyyy-MM-dd HH:mm" else entry.pattern)
"yyyy-MM-dd HH:mm"
} else {
entry.pattern
})
if (value.equals("now", true) || value.isBlank()) { if (value.equals("now", true) || value.isBlank()) {
cal.time = Date() cal.time = Date()
@ -153,6 +151,7 @@ class PropertyFilePlugin @Inject constructor(val configActor: ConfigActor<Proper
cal.time = fmt.parse(value) cal.time = fmt.parse(value)
} catch (pe: ParseException) { } catch (pe: ParseException) {
warn("Date parse exception for: ${entry.key}", pe) warn("Date parse exception for: ${entry.key}", pe)
success = false
} }
} }
@ -166,6 +165,7 @@ class PropertyFilePlugin @Inject constructor(val configActor: ConfigActor<Proper
} }
} catch (nfe: NumberFormatException) { } catch (nfe: NumberFormatException) {
warn("Non-integer value for: ${entry.key}") warn("Non-integer value for: ${entry.key}")
success = false
} }
cal.add(calendarFields.getOrDefault(entry.unit, Calendar.DATE), offset) cal.add(calendarFields.getOrDefault(entry.unit, Calendar.DATE), offset)
@ -173,10 +173,11 @@ class PropertyFilePlugin @Inject constructor(val configActor: ConfigActor<Proper
p.setProperty(entry.key, fmt.format(cal.time)) p.setProperty(entry.key, fmt.format(cal.time))
return TaskResult() return success
} }
private fun processInt(p: Properties, entry: Entry): TaskResult { private fun processInt(p: Properties, entry: Entry): Boolean {
var success = true
var intValue: Int var intValue: Int
try { try {
val fmt = DecimalFormat(entry.pattern) val fmt = DecimalFormat(entry.pattern)
@ -199,28 +200,30 @@ class PropertyFilePlugin @Inject constructor(val configActor: ConfigActor<Proper
p.setProperty(entry.key, fmt.format(intValue)) p.setProperty(entry.key, fmt.format(intValue))
} catch (nfe: NumberFormatException) { } catch (nfe: NumberFormatException) {
warn("Number format exception for: ${entry.key}", nfe) warn("Number format exception for: ${entry.key}", nfe)
success = false
} catch (pe: ParseException) { } catch (pe: ParseException) {
warn("Number parsing exception for: ${entry.key}", pe) warn("Number parsing exception for: ${entry.key}", pe)
success = false
} }
return TaskResult() return success
} }
private fun processString(p: Properties, entry: Entry): TaskResult { private fun processString(p: Properties, entry: Entry): Boolean {
val value = currentValue(p.getProperty(entry.key), entry.value, entry.default, entry.operation) val value = currentValue(p.getProperty(entry.key), entry.value, entry.default, entry.operation)
if (entry.operation == Operations.SET) { if (entry.operation == Operations.SET) {
p.setProperty(entry.key, value) p.setProperty(entry.key, value)
} else if (entry.operation == Operations.ADD) { } else if (entry.operation == Operations.ADD) {
if (entry.value != null) { if (entry.value != null) {
p.setProperty(entry.key, value + entry.value) p.setProperty(entry.key, "$value${entry.value}")
} }
} }
return TaskResult() return true
} }
private fun currentValue(value: String?, newValue: String?, default: String?, operation: Enum<Operations>): String { private fun currentValue(value: String?, newValue: String?, default: String?, operation: Operations): String {
var result: String? = null var result: String? = null
if (operation == Operations.SET) { if (operation == Operations.SET) {
@ -273,7 +276,7 @@ data class Entry(
var value: String? = null, var value: String? = null,
var default: String? = null, var default: String? = null,
var type: Types = Types.STRING, var type: Types = Types.STRING,
var operation: Enum<Operations> = Operations.SET, var operation: Operations = Operations.SET,
var pattern: String = "", var pattern: String = "",
var unit: Units = Units.DAY) var unit: Units = Units.DAY)
@ -281,6 +284,7 @@ data class Entry(
class PropertyFileConfig { class PropertyFileConfig {
var file: String = "" var file: String = ""
var comment: String? = null var comment: String? = null
var failOnWarning: Boolean = false
val entries = arrayListOf<Entry>() val entries = arrayListOf<Entry>()
@Suppress("unused") @Suppress("unused")
@ -289,7 +293,7 @@ class PropertyFileConfig {
value: String? = null, value: String? = null,
default: String? = null, default: String? = null,
type: Types = Types.STRING, type: Types = Types.STRING,
operation: Enum<Operations> = Operations.SET, operation: Operations = Operations.SET,
pattern: String = "", pattern: String = "",
unit: Units = Units.DAY) { unit: Units = Units.DAY) {
if (key.isNotEmpty()) entries.add(Entry(key, value, default, type, operation, pattern, unit)) if (key.isNotEmpty()) entries.add(Entry(key, value, default, type, operation, pattern, unit))