Added the command name to warnings

This commit is contained in:
Erik C. Thauvin 2023-04-03 02:22:32 -07:00
parent 10c5dc7348
commit 00cd80894d
4 changed files with 62 additions and 55 deletions

View file

@ -1,4 +1,4 @@
package rife.bld.extension.propertyFile; package rife.bld.extension.propertyfile;
import rife.bld.Project; import rife.bld.Project;
import rife.bld.publish.PublishDeveloper; import rife.bld.publish.PublishDeveloper;
@ -35,7 +35,7 @@ public class PropertyFileBuild extends Project {
downloadSources = true; downloadSources = true;
repositories = List.of(MAVEN_CENTRAL, SONATYPE_SNAPSHOTS); repositories = List.of(MAVEN_CENTRAL, SONATYPE_SNAPSHOTS);
scope(compile) scope(compile)
.include(dependency("com.uwyn.rife2", "rife2", version(1, 5, 11))); .include(dependency("com.uwyn.rife2", "rife2", version(1, 5, 15)));
scope(test) scope(test)
.include(dependency("org.jsoup", "jsoup", version(1, 15, 4))) .include(dependency("org.jsoup", "jsoup", version(1, 15, 4)))
.include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 9, 2))) .include(dependency("org.junit.jupiter", "junit-jupiter", version(5, 9, 2)))

View file

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package rife.bld.extension.propertyFile; package rife.bld.extension.propertyfile;
/** /**
* <p>Declares the edits to be made to a {@link java.util.Properties Properties} file.</p> * <p>Declares the edits to be made to a {@link java.util.Properties Properties} file.</p>
@ -174,6 +174,8 @@ public class Entry {
* *
* @param key the {@link java.util.Properties property} key * @param key the {@link java.util.Properties property} key
*/ */
@SuppressWarnings("unused")
public Entry key(String key) { public Entry key(String key) {
setKey(key); setKey(key);
return this; return this;
@ -184,6 +186,7 @@ public class Entry {
* *
* @param value the {@link java.util.Properties property} value * @param value the {@link java.util.Properties property} value
*/ */
@SuppressWarnings("unused")
public Entry value(Object value) { public Entry value(Object value) {
if (value != null) { if (value != null) {
setValue(String.valueOf(value)); setValue(String.valueOf(value));
@ -200,6 +203,7 @@ public class Entry {
* *
* @param defaultValue the default value * @param defaultValue the default value
*/ */
@SuppressWarnings("unused")
public Entry defaultValue(Object defaultValue) { public Entry defaultValue(Object defaultValue) {
if (defaultValue != null) { if (defaultValue != null) {
setDefaultValue(String.valueOf(defaultValue)); setDefaultValue(String.valueOf(defaultValue));
@ -224,6 +228,7 @@ public class Entry {
* *
* @param operation the entry {@link Operations Operation} * @param operation the entry {@link Operations Operation}
*/ */
@SuppressWarnings("unused")
public Entry operation(Operations operation) { public Entry operation(Operations operation) {
setOperation(operation); setOperation(operation);
return this; return this;
@ -247,6 +252,7 @@ public class Entry {
* *
* @param unit the {@link Units unit} * @param unit the {@link Units unit}
*/ */
@SuppressWarnings("unused")
public Entry unit(Units unit) { public Entry unit(Units unit) {
setUnit(unit); setUnit(unit);
return this; return this;

View file

@ -14,11 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
package rife.bld.extension.propertyFile; package rife.bld.extension.propertyfile;
import rife.bld.Project; import rife.bld.Project;
import rife.bld.extension.propertyFile.Entry.Operations; import rife.bld.extension.propertyfile.Entry.Operations;
import rife.bld.extension.propertyFile.Entry.Types; import rife.bld.extension.propertyfile.Entry.Types;
import rife.bld.operations.AbstractOperation; import rife.bld.operations.AbstractOperation;
import java.io.File; import java.io.File;
@ -50,6 +50,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* *
* @param entry the {@link Entry entry} * @param entry the {@link Entry entry}
*/ */
@SuppressWarnings("unused")
public PropertyFileOperation entry(Entry entry) { public PropertyFileOperation entry(Entry entry) {
entries.add(entry); entries.add(entry);
return this; return this;
@ -60,6 +61,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* *
* @param file the file to be edited * @param file the file to be edited
*/ */
@SuppressWarnings("unused")
public PropertyFileOperation file(String file) { public PropertyFileOperation file(String file) {
this.file = new File(file); this.file = new File(file);
return this; return this;
@ -70,6 +72,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* *
* @param file the file to be edited * @param file the file to be edited
*/ */
@SuppressWarnings("unused")
public PropertyFileOperation file(File file) { public PropertyFileOperation file(File file) {
this.file = file; this.file = file;
return this; return this;
@ -80,6 +83,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* *
* @param failOnWarning if set to {@code true}, the task will fail on any warnings. * @param failOnWarning if set to {@code true}, the task will fail on any warnings.
*/ */
@SuppressWarnings("unused")
public PropertyFileOperation failOnWarning(boolean failOnWarning) { public PropertyFileOperation failOnWarning(boolean failOnWarning) {
this.failOnWarning = failOnWarning; this.failOnWarning = failOnWarning;
return this; return this;
@ -90,14 +94,16 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* *
* @param comment the header comment * @param comment the header comment
*/ */
@SuppressWarnings("unused")
public PropertyFileOperation comment(String comment) { public PropertyFileOperation comment(String comment) {
this.comment = comment; this.comment = comment;
return this; return this;
} }
/** /**
* Performs the edits to the {@link java.util.Properties properties} file. * Performs the modification(s) to the {@link java.util.Properties properties} file.
*/ */
@Override
public void execute() throws Exception { public void execute() throws Exception {
if (project == null) { if (project == null) {
throw new IOException("A project must be specified."); throw new IOException("A project must be specified.");
@ -105,13 +111,14 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
if (file == null) { if (file == null) {
throw new IOException("A properties file location must be specified."); throw new IOException("A properties file location must be specified.");
} }
var commandName = project.getCurrentCommandName();
var success = false; var success = false;
var properties = new Properties(); var properties = new Properties();
success = PropertyFileUtils.loadProperties(file, properties); success = PropertyFileUtils.loadProperties(commandName, file, properties);
if (success) { if (success) {
for (var entry : entries) { for (var entry : entries) {
if (entry.getKey().isBlank()) { if (entry.getKey().isBlank()) {
PropertyFileUtils.warn("At least one entry key must specified."); PropertyFileUtils.warn(commandName, "At least one entry key must specified.");
success = false; success = false;
} else { } else {
var key = entry.getKey(); var key = entry.getKey();
@ -119,17 +126,19 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
var defaultValue = entry.getDefaultValue(); var defaultValue = entry.getDefaultValue();
if ((value == null || value.isBlank()) && (defaultValue == null || defaultValue.isBlank()) if ((value == null || value.isBlank()) && (defaultValue == null || defaultValue.isBlank())
&& entry.getOperation() != Operations.DELETE) { && entry.getOperation() != Operations.DELETE) {
PropertyFileUtils.warn("An entry value or default must be specified: " + key); PropertyFileUtils.warn(commandName, "An entry value or default must be specified: " + key);
success = false; success = false;
} else if (entry.getType() == Types.STRING && entry.getOperation() == Operations.SUBTRACT) { } else if (entry.getType() == Types.STRING && entry.getOperation() == Operations.SUBTRACT) {
PropertyFileUtils.warn("Subtraction is not supported for String properties: " + key); PropertyFileUtils.warn(commandName, "Subtraction is not supported for String properties: " + key);
success = false; success = false;
} else if (entry.getOperation() == Operations.DELETE) { } else if (entry.getOperation() == Operations.DELETE) {
properties.remove(key); properties.remove(key);
} else { } else {
switch (entry.getType()) { switch (entry.getType()) {
case DATE -> success = PropertyFileUtils.processDate(properties, entry, failOnWarning); case DATE ->
case INT -> success = PropertyFileUtils.processInt(properties, entry, failOnWarning); success = PropertyFileUtils.processDate(commandName, properties, entry, failOnWarning);
case INT ->
success = PropertyFileUtils.processInt(commandName, properties, entry, failOnWarning);
default -> success = PropertyFileUtils.processString(properties, entry); default -> success = PropertyFileUtils.processString(properties, entry);
} }
} }

View file

@ -14,10 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
package rife.bld.extension.propertyFile; package rife.bld.extension.propertyfile;
import rife.bld.extension.propertyFile.Entry.Operations; import rife.bld.extension.propertyfile.Entry.Operations;
import rife.bld.extension.propertyFile.Entry.Units; import rife.bld.extension.propertyfile.Entry.Units;
import rife.tools.Localization; import rife.tools.Localization;
import javax.imageio.IIOException; import javax.imageio.IIOException;
@ -62,11 +62,12 @@ public final class PropertyFileUtils {
/** /**
* Processes a date {@link Properties property}. * Processes a date {@link Properties property}.
* *
* @param p the {@link Properties property} * @param command the issuing command
* @param entry the {@link Entry} containing the {@link Properties property} edits * @param p the {@link Properties property}
* @param entry the {@link Entry} containing the {@link Properties property} edits
* @return {@code true} if successful * @return {@code true} if successful
*/ */
public static boolean processDate(Properties p, Entry entry, boolean failOnWarning) { public static boolean processDate(String command, Properties p, Entry entry, boolean failOnWarning) {
var success = true; var success = true;
var cal = Calendar.getInstance(); var cal = Calendar.getInstance();
var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getValue(), var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getValue(),
@ -85,7 +86,8 @@ public final class PropertyFileUtils {
try { try {
cal.setTime(fmt.parse(value)); cal.setTime(fmt.parse(value));
} catch (ParseException pe) { } catch (ParseException pe) {
warn("Date parse exception for: " + entry.getKey() + " --> " + pe.getMessage(), pe, failOnWarning); warn(command, "Non-date value for \"" + entry.getKey() + "\" --> " + pe.getMessage(),
pe, failOnWarning);
success = false; success = false;
} }
} }
@ -99,10 +101,12 @@ public final class PropertyFileUtils {
offset *= -1; offset *= -1;
} }
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
warn("Non-integer value for: " + entry.getKey() + " --> " + nfe.getMessage(), nfe, failOnWarning); warn(command, "Non-date value for \"" + entry.getKey() + "\" --> " + nfe.getMessage(), nfe,
failOnWarning);
success = false; success = false;
} }
//noinspection MagicConstant
cal.add(calendarFields.getOrDefault(entry.getUnit(), Calendar.DATE), offset); cal.add(calendarFields.getOrDefault(entry.getUnit(), Calendar.DATE), offset);
} }
@ -158,25 +162,15 @@ public final class PropertyFileUtils {
return result; return result;
} }
/**
* Ensure that the given value is an integer.
*
* @param value the value
* @return the parsed value
* @throws NumberFormatException if the value could not be parsed as an integer
*/
static String parseInt(String value) throws NumberFormatException {
return String.valueOf(Integer.parseInt(value));
}
/** /**
* Processes an integer {@link Properties property}. * Processes an integer {@link Properties property}.
* *
* @param p the {@link Properties property} * @param command the issuing command
* @param entry the {@link Entry} containing the {@link Properties property} edits * @param p the {@link Properties property}
* @param entry the {@link Entry} containing the {@link Properties property} edits
* @return {@code true} if successful * @return {@code true} if successful
*/ */
public static boolean processInt(Properties p, Entry entry, boolean failOnWarning) { public static boolean processInt(String command, Properties p, Entry entry, boolean failOnWarning) {
var success = true; var success = true;
int intValue; int intValue;
try { try {
@ -187,13 +181,13 @@ public final class PropertyFileUtils {
if (value.isBlank()) { if (value.isBlank()) {
intValue = fmt.parse("0").intValue(); intValue = fmt.parse("0").intValue();
} else { } else {
intValue = fmt.parse(parseInt(value)).intValue(); intValue = fmt.parse(value).intValue();
} }
if (entry.getOperation() != Entry.Operations.SET) { if (entry.getOperation() != Entry.Operations.SET) {
var opValue = 1; var opValue = 1;
if (entry.getValue() != null) { if (entry.getValue() != null) {
opValue = fmt.parse(parseInt(entry.getValue())).intValue(); opValue = fmt.parse(entry.getValue()).intValue();
} }
if (entry.getOperation() == Entry.Operations.ADD) { if (entry.getOperation() == Entry.Operations.ADD) {
intValue += opValue; intValue += opValue;
@ -202,11 +196,9 @@ public final class PropertyFileUtils {
} }
} }
p.setProperty(entry.getKey(), fmt.format(intValue)); p.setProperty(entry.getKey(), fmt.format(intValue));
} catch (NumberFormatException nfe) { } catch (NumberFormatException | ParseException e) {
warn("Number format exception for: " + entry.getKey() + " --> " + nfe.getMessage(), nfe, failOnWarning); warn(command, "Non-integer value for \"" + entry.getKey() + "\" --> " + e.getMessage(), e,
success = false; failOnWarning);
} catch (ParseException pe) {
warn("Number parsing exception for: " + entry.getKey() + " --> " + pe.getMessage(), pe, failOnWarning);
success = false; success = false;
} }
@ -238,27 +230,29 @@ public final class PropertyFileUtils {
/** /**
* Logs a warning. * Logs a warning.
* *
* @param command the issuing command
* @param message the message to log * @param message the message to log
*/ */
static void warn(String message) { static void warn(String command, String message) {
if (LOGGER.isLoggable(Level.WARNING)) { if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning(message); LOGGER.warning('[' + command + "] " + message);
} }
} }
/** /**
* Logs a warning. * Logs a warning.
* *
* @param command The command name
* @param message the message log * @param message the message log
* @param e the related exception * @param e the related exception
* @param failOnWarning skips logging the exception if set to {@code false} * @param failOnWarning skips logging the exception if set to {@code false}
*/ */
static void warn(String message, Exception e, boolean failOnWarning) { static void warn(String command, String message, Exception e, boolean failOnWarning) {
if (LOGGER.isLoggable(Level.WARNING)) { if (LOGGER.isLoggable(Level.WARNING)) {
if (failOnWarning) { if (failOnWarning) {
LOGGER.log(Level.WARNING, message, e); LOGGER.log(Level.WARNING, '[' + command + "] " + message, e);
} else { } else {
LOGGER.warning(message); LOGGER.warning('[' + command + "] " + message);
} }
} }
} }
@ -266,26 +260,24 @@ public final class PropertyFileUtils {
/** /**
* Loads a {@link Properties properties} file. * Loads a {@link Properties properties} file.
* *
* @param file the file location. * @param command the issuing command
* @param p the {@link Properties properties} to load into. * @param file the file location.
* @param p the {@link Properties properties} to load into.
* @return {@code true} if successful * @return {@code true} if successful
*/ */
public static boolean loadProperties(File file, Properties p) { public static boolean loadProperties(String command, File file, Properties p) {
boolean success = true; boolean success = true;
if (file != null) { if (file != null) {
if (file.exists()) { if (file.exists()) {
try (var propStream = Files.newInputStream(file.toPath(), StandardOpenOption.READ)) { try (var propStream = Files.newInputStream(file.toPath(), StandardOpenOption.READ)) {
p.load(propStream); p.load(propStream);
} catch (IOException ioe) { } catch (IOException ioe) {
warn("Could not load properties file: " + ioe.getMessage(), ioe, true); warn(command, "Could not load properties file: " + ioe.getMessage(), ioe, true);
success = false; success = false;
} }
} else {
warn("The '" + file + "' properties file could not be found.");
success = false;
} }
} else { } else {
warn("Please specify the properties file location."); warn(command, "Please specify the properties file location.");
success = false; success = false;
} }
return success; return success;