cssitegen/Utils/Utils.PathSearch.cs
Robert Morrison 18ed0534b7
refactor(EVERYTHING): Refactor all the things.
With a little bit of OOP and a monster commit, I too can make a an ssg,
Ka-chow...

Changes:
-  added class to represent SiteFile
-  enabled conversion semi-automatic based on file type.
-  added template to Testing.
-  Removed awful code for dependency search
-  Removed awful code for enumerating directory
-  arguments to a class to allow for easier passing to other
  parts of the code.

TODO:
- 🐞Test and debug with a copy of a live site,
- ✍️ Add handling for Pandoc errors on stderr
- Look into parallelising as much as possible.
2024-01-16 11:00:35 +00:00

53 lines
1.3 KiB
C#

using Serilog;
namespace csSiteGen;
public static partial class Utils {
static Dictionary<string,string> PathSearchMemo = new();
///<summary>
/// Find executable in path
///</summary>
public static string PathSearch(string Program){
if (PathSearchMemo.TryGetValue(Program, out string? result))
{
Log.Debug("Memo Hit for {program}", Program);
return result;
}
var path = System.Environment.GetEnvironmentVariable("PATH")?.Split(':');
if (path is null)
{
Log.Error("Failed to read PATH environment variable.");
return string.Empty;
}
List<string> candidateExecutables = new();
foreach (var dir in path)
{
// Directories do not need to exist for them to be in path
// This check avoids attempting to look in directories that
// Do not exist.
if (Directory.Exists(dir))
{
Log.Information("Searching for {Program} in {dir}", Program, dir);
candidateExecutables.AddRange(Directory.GetFiles(dir,$"{Program}"));
}
}
if (candidateExecutables.Count == 0)
{
Log.Warning("Dependency {Program} not found",Program);
return string.Empty;
}
result = candidateExecutables.First();
PathSearchMemo.Add(Program,result);
Log.Information("Found {program} at {path}",Program, result);
Log.Debug("Adding {@entry} to PathSearchMemo", PathSearchMemo.Last());
return result;
}
}