ps/Modules/Cole.PowerShell.Developer/Scratch/PowershellFindParentModules.linq

113 lines
4.1 KiB
Plaintext
Raw Normal View History

2023-05-30 22:55:40 -07:00
<Query Kind="Program" />
public static Dictionary<string,int> WeightedList = new Dictionary<string,int>();
void Main()
{
var r1 = new Regex(@"<#.+?#>", RegexOptions.Singleline);
var r2 = new Regex("#.+?[\r|\n]");
var directoryPath = Path.Combine(GetExecutingDirectory(),"Modules");
var di = new DirectoryInfo(directoryPath);
var files = di.GetFiles("*.ps1",SearchOption.AllDirectories);
var functionNames = new List<string>();
var excludeNames = new List<string>{"Module", "profile", "Shredded Function Names", "ShredModules", "VariableDeclarations", "ConfigurationValues", "chocolateybeforemodify", "chocolateyInstall", "chocolateyUninstall"};
var d = new Dictionary<string, HashSet<string>>();
var df = files.Select(x => {
var fm = new FolderModule();
fm.FunctionName = Path.GetFileNameWithoutExtension(x.FullName);
var pathParent = x.Directory;
fm.PublicPrivate = x.Directory.Name;
fm.ModuleName = x.Directory.Parent.Name;
return fm;
}).Where(x => !excludeNames.Contains(x.FunctionName) && x.FunctionName.IndexOf(".Test", StringComparison.OrdinalIgnoreCase) < 0).Distinct().ToDictionary(x => x.FunctionName, x => x);
var fmod = new FolderModule();
fmod.FunctionName = "Alkami.Ops.Common";
fmod.PublicPrivate = "OPS";
fmod.ModuleName = "Alkami.Ops.Common";
df.Add(fmod.FunctionName,fmod);
fmod = new FolderModule();
fmod.FunctionName = "Alkami.Ops.SecretServer";
fmod.PublicPrivate = "OPS";
fmod.ModuleName = "Alkami.Ops.SecretServer";
df.Add(fmod.FunctionName,fmod);
//df.Where(x => !new[]{"Public","Private"}.Contains(x.Value.PublicPrivate)).Dump();
//df.Where(x => new[]{"Public","Private"}.Contains(x.Value.PublicPrivate)).Dump();
// Dictionary<ModuleName,Dictionary<FileName,HashSet<ModuleName>>>
var fd = new Dictionary<string,Dictionary<string,HashSet<string>>>();
foreach(var file in files) {
var fileName = Path.GetFileNameWithoutExtension(file.FullName);
if (excludeNames.Contains(fileName) || fileName.IndexOf(".Test", StringComparison.OrdinalIgnoreCase) >= 0) {
continue;
}
var moduleName = file.Directory.Parent.Name;
if (!d.ContainsKey(moduleName)) {
d.Add(moduleName, new HashSet<string>());
}
if (!fd.ContainsKey(moduleName)) {
fd.Add(moduleName, new Dictionary<string,HashSet<string>>());
}
var text = r2.Replace(r1.Replace(File.ReadAllText(file.FullName),""), "");
foreach (var kvp in df) {
if (text.Contains(kvp.Key) && kvp.Value.ModuleName != moduleName) {
if (!fd[moduleName].ContainsKey(fileName)) {
fd[moduleName].Add(fileName, new HashSet<string>());
}
if (kvp.Value.PublicPrivate.ToUpper() == "PRIVATE") {
fd[moduleName][fileName].Add("!!PRIVATE - " + kvp.Value.ModuleName + " - " + kvp.Value.FunctionName);
} else if (kvp.Value.PublicPrivate.ToUpper() == "OPS" && !moduleName.ToUpper().Contains("DEVOPS")) {
fd[moduleName][fileName].Add("!!OPS - " + kvp.Value.ModuleName + " - " + kvp.Value.FunctionName);
} else {
fd[moduleName][fileName].Add(kvp.Value.ModuleName);
}
d[moduleName].Add(kvp.Value.ModuleName);
}
}
}
// d.Dump();
foreach(var kvp in d) {
$"{kvp.Key}".Dump();
kvp.Value.ToList().ForEach(x => $" {x}".Dump());
"".Dump();
}
"".Dump();
"===============================================".Dump();
"".Dump();
foreach(var kvp in fd) {
$"{kvp.Key}".Dump();
foreach(var kkvp in kvp.Value) {
$" {kkvp.Key}".Dump();
kkvp.Value.ToList().ForEach(x => $" {x}".Dump());
}
"".Dump();
}
/*
I need to know that a given module has a dependency on another module.
I start with the file.Directory.Parent.Name as the module. I then find each file in that folder, and check all lines to match each item in the list.
If any line matches any key:
add the module dependency from the value.ModuleName for that key to the list under the module
continue to the next line
*/
}
public class FolderModule {
public string ModuleName {get;set;}
public string PublicPrivate {get;set;}
public string FunctionName {get;set;}
}
public static int CalculateWeightedValue(Dictionary<string,string> inputs, string key) {
return 0;
}
public static string GetExecutingDirectory()
{
return Path.GetDirectoryName (Util.CurrentQueryPath);
}