Skip to content

Commit

Permalink
Merge pull request #13 from TechieGuy12/fixes
Browse files Browse the repository at this point in the history
Code fixes
  • Loading branch information
TechieGuy12 authored Jan 31, 2022
2 parents 6f023fa + 27238de commit 42ef558
Show file tree
Hide file tree
Showing 28 changed files with 414 additions and 261 deletions.
47 changes: 38 additions & 9 deletions FileWatcher/Configuration/Actions/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ namespace TE.FileWatcher.Configuration.Actions
public class Action : RunnableBase
{
// The placeholders for the destination path
private Dictionary<string, string> _destinationPlaceholders
= new Dictionary<string, string>();
//private Dictionary<string, string> _destinationPlaceholders
// = new Dictionary<string, string>();

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

/// <summary>
/// The type of action to perform.
Expand Down Expand Up @@ -64,7 +64,7 @@ public enum ActionType
/// Gets or sets the destination of the action.
/// </summary>
[XmlElement("destination")]
public string Destination { get; set; }
public string? Destination { get; set; }

/// <summary>
/// Gets or sets the verify flag.
Expand Down Expand Up @@ -101,8 +101,13 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
return;
}

string source = GetSource(watchPath, fullPath);
string destination = GetDestination(watchPath, fullPath);
string? source = GetSource(watchPath, fullPath);
string? destination = GetDestination(watchPath, fullPath);

if (string.IsNullOrWhiteSpace(source))
{
return;
}

try
{
Expand All @@ -111,23 +116,47 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
case ActionType.Copy:
if (TEFS.File.IsValid(source))
{
if (string.IsNullOrWhiteSpace(destination))
{
Logger.WriteLine($"The file '{source}' could not be copied because the destination file was not specified.");
return;
}

File.Copy(source, destination, Verify);
Logger.WriteLine($"Copied {source} to {destination}.");
}
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.");
}
break;
case ActionType.Move:
if (TEFS.File.IsValid(source))
{
if (string.IsNullOrWhiteSpace(destination))
{
Logger.WriteLine($"The file '{source}' could not be moved because the destination file was not specified.");
return;
}

File.Move(source, destination, Verify);
Logger.WriteLine($"Moved {source} to {destination}.");
}
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.");
}
break;
case ActionType.Delete:
if (TEFS.File.IsValid(source))
{
File.Delete(source);
Logger.WriteLine($"Deleted {source}.");
}
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.");
}
break;
}
}
Expand All @@ -152,7 +181,7 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
/// <returns>
/// The destination string value.
/// </returns>
private string GetDestination(string watchPath, string fullPath)
private string? GetDestination(string watchPath, string fullPath)
{
if (string.IsNullOrWhiteSpace(Destination))
{
Expand All @@ -175,7 +204,7 @@ private string GetDestination(string watchPath, string fullPath)
/// <returns>
/// The source string value.
/// </returns>
private string GetSource(string watchPath, string fullPath)
private string? GetSource(string watchPath, string fullPath)
{
if (string.IsNullOrWhiteSpace(Source))
{
Expand Down
2 changes: 1 addition & 1 deletion FileWatcher/Configuration/Actions/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Actions
/// Gets or sets the list of actions to perform.
/// </summary>
[XmlElement("action")]
public List<Action> ActionList { get; set; }
public List<Action>? ActionList { get; set; }

/// <summary>
/// Runs all the actions for the watch.
Expand Down
54 changes: 38 additions & 16 deletions FileWatcher/Configuration/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace TE.FileWatcher.Configuration.Commands
public class Command : RunnableBase
{
// The process that will run the command
private Process _process;
private Process? _process;

// A queue containing the information to start the command process
private ConcurrentQueue<ProcessStartInfo> _processInfo;
private ConcurrentQueue<ProcessStartInfo>? _processInfo;

// Flag indicating that a process is running
private bool _isProcessRunning = false;
Expand All @@ -29,13 +29,13 @@ public class Command : RunnableBase
/// Gets or sets the arguments associated with the file to execute.
/// </summary>
[XmlElement("arguments")]
public string Arguments { get; set; }
public string? Arguments { get; set; }

/// <summary>
/// Gets or sets the full path to the file to executed.
/// </summary>
[XmlElement("path")]
public string Path { get; set; }
public string? Path { get; set; }

/// <summary>
/// Gets or sets the triggers of the action.
Expand Down Expand Up @@ -72,8 +72,14 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
return;
}

string commandPath = GetCommand(watchPath, fullPath);
string arguments = GetArguments(watchPath, fullPath);
string? commandPath = GetCommand(watchPath, fullPath);
string? arguments = GetArguments(watchPath, fullPath);

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

if (!File.Exists(commandPath))
{
Expand All @@ -88,9 +94,16 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
_processInfo = new ConcurrentQueue<ProcessStartInfo>();
}

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = commandPath;
startInfo.Arguments = arguments;
ProcessStartInfo startInfo = new()
{
FileName = commandPath
};

if (arguments != null)
{
startInfo.Arguments = arguments;
}

_processInfo.Enqueue(startInfo);

// Execute the next process in the queue
Expand Down Expand Up @@ -119,12 +132,14 @@ private void Execute()

try
{
if (_processInfo.TryDequeue(out ProcessStartInfo startInfo))
if (_processInfo.TryDequeue(out ProcessStartInfo? startInfo))
{
if (File.Exists(startInfo.FileName))
{
_process = new Process();
_process.StartInfo = startInfo;
_process = new Process
{
StartInfo = startInfo
};
_process.StartInfo.CreateNoWindow = true;
_process.StartInfo.UseShellExecute = false;
_process.EnableRaisingEvents = true;
Expand All @@ -142,7 +157,14 @@ private void Execute()
}
catch (Exception ex)
{
Logger.WriteLine($"Could not run the command '{_process.StartInfo.FileName} {_process.StartInfo.Arguments}'. Reason: {ex.Message}");
if (_process != null)
{
Logger.WriteLine($"Could not run the command '{_process.StartInfo.FileName} {_process.StartInfo.Arguments}'. Reason: {ex.Message}");
}
else
{
Logger.WriteLine($"Could not run the command. Reason: {ex.Message}");
}
}
}

Expand All @@ -159,7 +181,7 @@ private void Execute()
/// <returns>
/// The command path string value.
/// </returns>
private string GetArguments(string watchPath, string fullPath)
private string? GetArguments(string watchPath, string fullPath)
{
if (string.IsNullOrWhiteSpace(Arguments))
{
Expand All @@ -182,7 +204,7 @@ private string GetArguments(string watchPath, string fullPath)
/// <returns>
/// The command path string value.
/// </returns>
private string GetCommand(string watchPath, string fullPath)
private string? GetCommand(string watchPath, string fullPath)
{
if (string.IsNullOrWhiteSpace(Path))
{
Expand All @@ -201,7 +223,7 @@ private string GetCommand(string watchPath, string fullPath)
/// <param name="args">
/// The event arguments.
/// </param>
private void OnProcessExit(object sender, EventArgs args)
private void OnProcessExit(object? sender, EventArgs args)
{
_isProcessRunning = false;

Expand Down
2 changes: 1 addition & 1 deletion FileWatcher/Configuration/Commands/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Commands
/// Gets or sets the list of actions to perform.
/// </summary>
[XmlElement("command")]
public List<Command> CommandList { get; set; }
public List<Command>? CommandList { get; set; }

/// <summary>
/// Runs all the commands for the watch.
Expand Down
2 changes: 1 addition & 1 deletion FileWatcher/Configuration/Data/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public HashSet<FileAttributes> Attribute
{
get
{
HashSet<FileAttributes> attributes = new HashSet<FileAttributes>(AttributeStrings.Count);
HashSet<FileAttributes> attributes = new(AttributeStrings.Count);
foreach (string attribute in AttributeStrings)
{
try
Expand Down
Loading

0 comments on commit 42ef558

Please sign in to comment.