Skip to content

Commit

Permalink
Merge pull request #14 from TechieGuy12/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
TechieGuy12 authored Feb 3, 2022
2 parents 42ef558 + fdc398e commit 90c0cfe
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 77 deletions.
26 changes: 13 additions & 13 deletions FileWatcher/Configuration/Actions/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ namespace TE.FileWatcher.Configuration.Actions
/// </summary>
public class Action : RunnableBase
{
// The placeholders for the destination path
//private Dictionary<string, string> _destinationPlaceholders
// = new Dictionary<string, string>();

// The placeholders for the source path
//private Dictionary<string, string> _sourcePlaceholders
// = new Dictionary<string, string>();

/// <summary>
/// The type of action to perform.
/// </summary>
Expand Down Expand Up @@ -118,7 +110,9 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
{
if (string.IsNullOrWhiteSpace(destination))
{
Logger.WriteLine($"The file '{source}' could not be copied because the destination file was not specified.");
Logger.WriteLine(
$"The file '{source}' could not be copied because the destination file was not specified.",
LogLevel.ERROR);
return;
}

Expand All @@ -127,7 +121,9 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
}
else
{
Logger.WriteLine($"The file '{source}' could not be copied because the path was not valid, the file doesn't exists, or it was in use.");
Logger.WriteLine(
$"The file '{source}' could not be copied because the path was not valid, the file doesn't exists, or it was in use.",
LogLevel.ERROR);
}
break;
case ActionType.Move:
Expand All @@ -144,7 +140,9 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
}
else
{
Logger.WriteLine($"The file '{source}' could not be moved because the path was not valid, the file doesn't exists, or it was in use.");
Logger.WriteLine(
$"The file '{source}' could not be moved because the path was not valid, the file doesn't exists, or it was in use.",
LogLevel.ERROR);
}
break;
case ActionType.Delete:
Expand All @@ -155,15 +153,17 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
}
else
{
Logger.WriteLine($"The file '{source}' could not be deleted because the path was not valid, the file doesn't exists, or it was in use.");
Logger.WriteLine(
$"The file '{source}' could not be deleted because the path was not valid, the file doesn't exists, or it was in use.",
LogLevel.ERROR);
}
break;
}
}
catch (Exception ex)
{
string message = (ex.InnerException == null) ? ex.Message : ex.InnerException.Message;
Logger.WriteLine($"Could not {Type.ToString().ToLower()} file {source}. Reason: {message}");
Logger.WriteLine($"Could not {Type.ToString().ToLower()} file {source}. Reason: {message}", LogLevel.ERROR);
return;
}
}
Expand Down
23 changes: 17 additions & 6 deletions FileWatcher/Configuration/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)

if (string.IsNullOrWhiteSpace(commandPath))
{
Logger.WriteLine($"The command was not provided. Command was not run.");
Logger.WriteLine($"The command was not provided. Command was not run.",
LogLevel.ERROR);
return;
}

if (!File.Exists(commandPath))
{
Logger.WriteLine($"The command '{commandPath}' was not found. Command was not run.");
Logger.WriteLine(
$"The command '{commandPath}' was not found. Command was not run.",
LogLevel.ERROR);
return;
}

Expand Down Expand Up @@ -111,7 +114,9 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
}
catch (Exception ex)
{
Logger.WriteLine($"Could not run the command '{commandPath} {arguments}'. Reason: {ex.Message}");
Logger.WriteLine(
$"Could not run the command '{commandPath} {arguments}'. Reason: {ex.Message}",
LogLevel.ERROR);
}
}

Expand Down Expand Up @@ -148,7 +153,9 @@ private void Execute()
}
else
{
Logger.WriteLine($"The command '{startInfo.FileName}' was not found. Command was not run.");
Logger.WriteLine(
$"The command '{startInfo.FileName}' was not found. Command was not run.",
LogLevel.ERROR);

// Execute the next process in the queue
Execute();
Expand All @@ -159,11 +166,15 @@ private void Execute()
{
if (_process != null)
{
Logger.WriteLine($"Could not run the command '{_process.StartInfo.FileName} {_process.StartInfo.Arguments}'. Reason: {ex.Message}");
Logger.WriteLine(
$"Could not run the command '{_process.StartInfo.FileName} {_process.StartInfo.Arguments}'. Reason: {ex.Message}",
LogLevel.ERROR);
}
else
{
Logger.WriteLine($"Could not run the command. Reason: {ex.Message}");
Logger.WriteLine(
$"Could not run the command. Reason: {ex.Message}",
LogLevel.ERROR);
}
}
}
Expand Down
44 changes: 40 additions & 4 deletions FileWatcher/Configuration/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,25 @@ namespace TE.FileWatcher.Configuration
/// </summary>
public class Logging
{
/// <summary>
/// The default log size.
/// </summary>
public const int DEFAULT_LOG_SIZE = 5;

/// <summary>
/// The default log number.
/// </summary>
public const int DEFAULT_LOG_NUMBER = 10;

// The log path
private string? _logPath;

// The size of the log file
private int _logSize = DEFAULT_LOG_SIZE;

// The number of log files to retain
private int _logNumber = DEFAULT_LOG_NUMBER;

/// <summary>
/// Gets or sets the path of the log file.
/// </summary>
Expand All @@ -39,13 +55,33 @@ public string? LogPath
/// backed up and a new log file is created.
/// </summary>
[XmlElement("size")]
public int Size { get; set; }
public int Size
{
get
{
return _logSize;
}
set
{
_logSize = value > 0 ? value : DEFAULT_LOG_SIZE;
}
}

/// <summary>
/// Gets or sets the number of log file to retain.
/// </summary>
[XmlElement("number")]
public int Number { get; set; }
public int Number
{
get
{
return _logNumber;
}
set
{
_logNumber = value > 0 ? value : DEFAULT_LOG_NUMBER;
}
}

/// <summary>
/// Initializes an instance of the <see cref="Logging"/> class.
Expand All @@ -62,8 +98,8 @@ public string? LogPath
public Logging()
{
LogPath = Path.Combine(Path.GetTempPath(), Logger.DEFAULT_LOG_NAME);
Size = 5;
Number = 10;
Size = DEFAULT_LOG_SIZE;
Number = DEFAULT_LOG_NUMBER;
}
}
}
8 changes: 6 additions & 2 deletions FileWatcher/Configuration/Notifications/Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@ private async void OnElapsed(object? source, ElapsedEventArgs e)
foreach (Exception ex in aex.Flatten().InnerExceptions)
{
Logger.WriteLine(ex.Message, LogLevel.ERROR);
Logger.WriteLine($"StackTrace:{Environment.NewLine}{ex.StackTrace}");
Logger.WriteLine(
$"StackTrace:{Environment.NewLine}{ex.StackTrace}",
LogLevel.ERROR);
}
}
catch (NullReferenceException ex)
{
Logger.WriteLine(ex.Message, LogLevel.ERROR);
Logger.WriteLine($"StackTrace:{Environment.NewLine}{ex.StackTrace}");
Logger.WriteLine(
$"StackTrace:{Environment.NewLine}{ex.StackTrace}",
LogLevel.ERROR);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions FileWatcher/Configuration/RunnableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static string GetRelativeFullPath(string watchPath, string fullPath)
/// </returns>
private static string? GetRelativePath(string watchPath, string fullPath)
{
string? relativeFullPath = IO.Path.GetDirectoryName(fullPath);
string? relativeFullPath = Path.GetDirectoryName(fullPath);
if (relativeFullPath == null)
{
return null;
Expand All @@ -150,12 +150,12 @@ private static string GetRelativeFullPath(string watchPath, string fullPath)
/// </returns>
private static string? GetFilename(string fullPath, bool includeExtension)
{
if (string.IsNullOrEmpty(fullPath) || !IO.File.Exists(fullPath))
if (string.IsNullOrEmpty(fullPath) || !File.Exists(fullPath))
{
return null;
}

return includeExtension ? IO.Path.GetFileNameWithoutExtension(fullPath) : IO.Path.GetFileName(fullPath);
return includeExtension ? Path.GetFileNameWithoutExtension(fullPath) : Path.GetFileName(fullPath);
}

/// <summary>
Expand All @@ -169,12 +169,12 @@ private static string GetRelativeFullPath(string watchPath, string fullPath)
/// </returns>
private static string? GetFileExtension(string fullPath)
{
if (string.IsNullOrEmpty(fullPath) || !IO.File.Exists(fullPath))
if (string.IsNullOrEmpty(fullPath) || !File.Exists(fullPath))
{
return null;
}

return IO.Path.GetExtension(fullPath);
return Path.GetExtension(fullPath);
}
}
}
6 changes: 4 additions & 2 deletions FileWatcher/Configuration/Watch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ private void CreateFileSystemWatcher()
{
if (string.IsNullOrWhiteSpace(Path))
{
Logger.WriteLine("The path to watch was not specified.");
Logger.WriteLine(
"The path to watch was not specified.",
LogLevel.ERROR);
return;
}

Expand Down Expand Up @@ -461,7 +463,7 @@ private static void NotAccessibleError(FileSystemWatcher source, ErrorEventArgs
catch
{
source.EnableRaisingEvents = false;
System.Threading.Thread.Sleep(iTimeOut);
Thread.Sleep(iTimeOut);
}
}

Expand Down
3 changes: 2 additions & 1 deletion FileWatcher/Configuration/Watches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class Watches
/// </summary>
public void Start()
{
if (WatchList == null)
if (WatchList == null || WatchList.Count <= 0)
{
Logger.WriteLine("No watches were specified.", LogLevel.ERROR);
return;
}

Expand Down
9 changes: 6 additions & 3 deletions FileWatcher/Configuration/XmlFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace TE.FileWatcher.Configuration
{
/// <summary>
/// The XML configuration file.
/// </summary>
public class XmlFile : IConfigurationFile
{
// The default configuration file name
Expand Down Expand Up @@ -47,7 +50,7 @@ public XmlFile(string path, string name)
/// The folder path.
/// </param>
/// <returns>
/// The folder path of the files, otherwise null.
/// The folder path of the files, otherwise <c>null</c>.
/// </returns>
private static string? GetFolderPath(string? path)
{
Expand Down Expand Up @@ -85,7 +88,7 @@ public XmlFile(string path, string name)
/// The name of the configuration file.
/// </param>
/// <returns>
/// The full path to the configuration file, otherwise null.
/// The full path to the configuration file, otherwise <c>null</c>.
/// </returns>
private static string? GetFullPath(string path, string name)
{
Expand Down Expand Up @@ -126,7 +129,7 @@ public XmlFile(string path, string name)
/// </summary>
/// <returns>
/// A <see cref="Watches"/> object if the file was read successfully,
/// otherwise null.
/// otherwise <c>null</c>.
/// </returns>
[RequiresUnreferencedCode("Calls XmlSerializer")]
public Watches? Read()
Expand Down
19 changes: 18 additions & 1 deletion FileWatcher/FileSystem/Directory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ public static class Directory
/// <param name="path">
/// The path that includes the folder structure to create.
/// </param>
/// <exception cref="ArgumentNullException">
/// Thrown when an argument is null or empty.
/// </exception>
/// <exception cref="FileWatcherException">
/// Thrown when the directory could not be created.
/// </exception>
/// <exception cref="NullReferenceException">
/// Thrown when the directory from the path is null.
/// </exception>
public static void Create(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return;
throw new ArgumentNullException(nameof(path));
}

string? folders = Path.GetDirectoryName(path);
Expand All @@ -34,8 +43,16 @@ public static void Create(string path)
if (!IO.Directory.Exists(folders))
{
IO.Directory.CreateDirectory(folders);
if (!IO.Directory.Exists(folders))
{
throw new FileWatcherException($"The directory {folders} could not be created.");
}
}
}
else
{
throw new NullReferenceException("The directory path could not be determined.");
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion FileWatcher/FileSystem/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static void Copy(string source, string destination, bool verify)
/// <returns>
/// <c>true</c> if the file is valid, otherwise <c>false</c>.
/// </returns>
public static bool IsValid(string? path)
public static bool IsValid(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
Expand Down
Loading

0 comments on commit 90c0cfe

Please sign in to comment.