From a9cd262e60138e47e89de73b3dc808edd6bb63f1 Mon Sep 17 00:00:00 2001 From: Robert Morrison Date: Mon, 20 Jun 2022 02:53:16 +0100 Subject: [PATCH] Add dependency checking Function --- Program.cs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/Program.cs b/Program.cs index 6204d42..c65cdb4 100644 --- a/Program.cs +++ b/Program.cs @@ -113,4 +113,65 @@ class Program throw new NotImplementedException(); } + static Tuple?> CheckDeps(List? dependencies) + { + bool success = true; + + if (! (dependencies?.Count() > 0)) // assume success as no dependencies + { + return new Tuple?>(true,null); + } + + + var path = System.Environment.GetEnvironmentVariable("PATH")?.Split(':'); + if (path is null) + { + Log.Error("Cannot read system path."); + Environment.Exit(1); + } + Log.Debug("Path is: {path}",path); + + List executables = new(); + foreach (string dir in path) + { + if (Directory.Exists(dir)) // Sometimes People fuck up their path variables and include directories that don't exist. + { + executables.AddRange(GetAllFiles(dir)); + } + } + Log.Verbose("Executables are: {executables}",executables); + + // Do some regex magic to find an executable in the path. + Dictionary? result = new(); + Dictionary? failures = new(); + + foreach (string dependency in dependencies) + { + string regex = $"/(.*/)*{dependency}"; + Regex expr = new Regex(regex,RegexOptions.Compiled); + Log.Debug("Compiled regex: {expr}",expr); + string? match = executables.Find( x => expr.IsMatch(x)); + if (match is not null && match != "") + { + Log.Information("Dependency {dep} found at {path}",dependency,match); + result.Add(dependency,match); // Create a dictionary of paths to found executables + } + else + { + Log.Information("Dependency {dep} cannot be found",dependency); + success = false; + failures.Add(dependency,dependency); + } + } + + if (success) + { + return new Tuple?>(success,result); + } + else + { + return new Tuple?>(success,failures); + } + } + }