Skip to content

Commit

Permalink
Merge pull request #23 from TechieGuy12/updates
Browse files Browse the repository at this point in the history
Date placeholder and keep timestamps
  • Loading branch information
TechieGuy12 authored Jun 4, 2022
2 parents 7456521 + 7e8f202 commit 2a106b3
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 202 deletions.
192 changes: 14 additions & 178 deletions FileWatcher/Configuration/Actions/Action.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.Xml.Serialization;
using TE.FileWatcher.Logging;
using TEFS = TE.FileWatcher.FileSystem;

Expand All @@ -10,22 +9,6 @@ namespace TE.FileWatcher.Configuration.Actions
/// </summary>
public class Action : RunnableBase
{
// The regular expresson pattern for extracting the date type and the
// specified date format to be used
const string PATTERN = @"\[(?<datetype>.*):(?<format>.*)\]";

// The created date placeholder value
const string CREATED_DATE = "createddate";

// The modified date placholder value
const string MODIFIED_DATE = "modifieddate";

// The current date placeholder value
const string CURRENT_DATE = "currentdate";

// The regular expression
private readonly Regex _regex;

/// <summary>
/// The type of action to perform.
/// </summary>
Expand Down Expand Up @@ -77,12 +60,10 @@ public enum ActionType
public bool Verify { get; set; }

/// <summary>
/// Creates an instance of the <see cref="Action"/> class.
/// Gets or sets the keep timestamps flag.
/// </summary>
public Action()
{
_regex = new Regex(PATTERN, RegexOptions.Compiled);
}
[XmlElement(ElementName = "keepTimestamps", DataType = "boolean")]
public bool KeepTimestamps { get; set; }

/// <summary>
/// Runs the action.
Expand Down Expand Up @@ -145,8 +126,8 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
return;
}

TEFS.File.Copy(source, destination, Verify);
Logger.WriteLine($"Copied {source} to {destination}.");
TEFS.File.Copy(source, destination, Verify, KeepTimestamps);
Logger.WriteLine($"Copied {source} to {destination}. Verify: {Verify}. Keep timestamps: {KeepTimestamps}.");
break;

case ActionType.Move:
Expand All @@ -158,8 +139,8 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
return;
}

TEFS.File.Move(source, destination, Verify);
Logger.WriteLine($"Moved {source} to {destination}.");
TEFS.File.Move(source, destination, Verify, KeepTimestamps);
Logger.WriteLine($"Moved {source} to {destination}. Verify: {Verify}. Keep timestamps: {KeepTimestamps}.");
break;

case ActionType.Delete:
Expand Down Expand Up @@ -195,83 +176,6 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
};
}

/// <summary>
/// Gets the date value for the specified date type using the full path
/// of the changed file.
/// </summary>
/// <param name="dateType">
/// The type of date.
/// </param>
/// <param name="fullPath">
/// The full path to the changed file.
/// </param>
/// <returns>
/// The <see cref="DateTime"/> value for the type, otherwise <c>null</c>.
/// </returns>
/// <exception cref="FileWatcherException">
/// Thrown when the date could not be determined.
/// </exception>
private static DateTime? GetDate(string dateType, string fullPath)
{
// Determine the type of date type, and then get
// the value for the date
return dateType switch
{
CREATED_DATE => TEFS.File.GetCreatedDate(fullPath),
MODIFIED_DATE => TEFS.File.GetModifiedDate(fullPath),
CURRENT_DATE => DateTime.Now,
_ => null
};
}

/// <summary>
/// Gets the date string value using the specified date and format.
/// </summary>
/// <param name="date">
/// The date to be formatted.
/// </param>
/// <param name="format">
/// The format string.
/// </param>
/// <returns>
/// The formatted string value
/// </returns>
/// <exception cref="FileWatcherException">
/// Thrown when the date string value can not be created.
/// </exception>
private static string? GetDateString(DateTime date, string format)
{
if (string.IsNullOrEmpty(format))
{
Logger.WriteLine("The date format was not provided.");
return null;
}

try
{
// Format the date, or return null if there is an
// issue trying to format the date
string? dateString = date.ToString(format);
if (string.IsNullOrWhiteSpace(dateString))
{
// There was an issue formatting the date, and
// the date string value was null or contained
// no value, so write a log message, and then
// continue to the next match
throw new FileWatcherException(
$"The date could not be formatted. Format: {format}, date: {date}.");
}

return dateString;
}
catch (Exception ex)
when (ex is ArgumentException || ex is FormatException)
{
throw new FileWatcherException(
$"The date could not be formatted properly using '{format}'. Reason: {ex.Message}");
}
}

/// <summary>
/// Gets the destination value by replacing any placeholders with the
/// actual string values.
Expand Down Expand Up @@ -319,84 +223,16 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
{
return null;
}

return ReplacePlaceholders(Source, watchPath, fullPath);
}

/// <summary>
/// Replaces the date placeholders in a string with the actual values.
/// </summary>
/// <param name="value">
/// The value containing the placeholders.
/// </param>
/// <param name="watchPath">
/// The watch path.
/// </param>
/// <param name="fullPath">
/// The full path of the changed file.
/// </param>
/// <returns>
/// The value with the placeholders replaced with the actual strings,
/// otherwise <c>null</c>.
/// </returns>
private string? ReplaceDatePlaceholders(string value, string watchPath, string fullPath)
{
// Re
if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(watchPath) || string.IsNullOrWhiteSpace(fullPath))

string? source = ReplacePlaceholders(Source, watchPath, fullPath);
if (!string.IsNullOrWhiteSpace(source))
{
return null;
source = ReplaceDatePlaceholders(source, watchPath, fullPath);
}

string replacedValue = value;

if (_regex.IsMatch(value))
{
// Find all the regex matches that are in the string since there
// could be multiple date matches
MatchCollection matches = _regex.Matches(value);
if (matches.Count > 0)
{
// Loop through each of the matches so the placeholder can
// be replaced with the actual date values
foreach (Match match in matches)
{
// Store the date type (createddate, modifieddate,
// or currentdate) and change it to lowercase so it can
// be easily compared later
string dateType = match.Groups["datetype"].Value.ToLower();
// Store the specified date format
string format = match.Groups["format"].Value;

try
{
// Get the date for the specified date type
DateTime? date = GetDate(dateType, fullPath);
if (date != null)
{
// The string value for the date time using the date type
// and format
string? dateString = GetDateString((DateTime)date, format);
return source;
}

// Replace the date placeholder with the formatted date
// value
replacedValue = replacedValue.Replace(match.Value, dateString);
} else
{
Logger.WriteLine(
$"The date value is null. Date type: {dateType}, changed: {fullPath}, value: {value}, watch path: {watchPath}.",
LogLevel.WARNING);
}
}
catch (FileWatcherException ex)
{
Logger.WriteLine(ex.Message, LogLevel.ERROR);
continue;
}
}
}
}

return replacedValue;
}
}
}
21 changes: 18 additions & 3 deletions FileWatcher/Configuration/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Command : RunnableBase
public Triggers Triggers { get; set; } = new Triggers();

/// <summary>
/// Runs the command.
/// Queues the command process to be run.
/// </summary>
/// <param name="watchPath">
/// The watch path.
Expand Down Expand Up @@ -114,6 +114,9 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
}
}

/// <summary>
/// Executes the next command process from the queue.
/// </summary>
private void Execute()
{
// If the queue is null or empty, then no command is waiting to nbe
Expand Down Expand Up @@ -193,7 +196,13 @@ private void Execute()
return null;
}

return ReplacePlaceholders(Arguments, watchPath, fullPath);
string? arguments = ReplacePlaceholders(Arguments, watchPath, fullPath);
if (!string.IsNullOrWhiteSpace(arguments))
{
arguments = ReplaceDatePlaceholders(arguments, watchPath, fullPath);
}

return arguments;
}

/// <summary>
Expand All @@ -216,7 +225,13 @@ private void Execute()
return null;
}

return ReplacePlaceholders(Path, watchPath, fullPath);
string? path = ReplacePlaceholders(Path, watchPath, fullPath);
if (!string.IsNullOrWhiteSpace(path))
{
path = ReplaceDatePlaceholders(path, watchPath, fullPath);
}

return path;
}

/// <summary>
Expand Down
Loading

0 comments on commit 2a106b3

Please sign in to comment.