Skip to content

Commit

Permalink
chore: debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
DaRacci committed May 16, 2024
1 parent 22a3da6 commit 4b93160
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Output/Debug/Compiler.dll",
"args": ["-i", "utils/test.ps1"],
"args": ["-i", ".\\utils\\test.ps1", "-v", "-o", "test.ps1", "-f"],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
Expand Down
12 changes: 11 additions & 1 deletion src/Compiler/CompiledScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
using System.Text;
using Compiler.Module;
using Compiler.Requirements;
using NLog;
using QuikGraph;

class CompiledScript : LocalFileModule
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public readonly AdjacencyGraph<ModuleSpec, Edge<ModuleSpec>> ModuleGraph = new();
public readonly Dictionary<string, Module> ResolvedModules = [];
public readonly ParamBlockAst ScriptParamBlockAst;
Expand Down Expand Up @@ -58,7 +60,7 @@ public string Compile()
{
var script = new StringBuilder();

Requirements.GetRequirements().ForEach(requirement =>
Requirements.GetRequirements().Where(requirement => requirement is not Compiler.Module.ModuleSpec).ToList().ForEach(requirement =>
{
script.AppendLine(requirement.GetInsertableLine());
});
Expand All @@ -84,30 +86,38 @@ private void ResolveRequirements()
var iterating = new Queue<Module>([this]);
while (iterating.TryDequeue(out Module? current) && current != null)
{
Logger.Debug($"Resolving requirements for {current.Name}");
if (localModules.Any(module => module.GetModuleMatchFor(current.ModuleSpec) == ModuleMatch.Exact) || downloadableModules.Any(module => module.GetModuleMatchFor(current.ModuleSpec) == ModuleMatch.Exact))
{
Logger.Debug($"Skipping {current.Name} because it is already resolved.");
continue;
}

switch (current)
{
case LocalFileModule local:
Logger.Debug($"Adding {local.Name} to local modules.");
localModules.Add(local);
break;
case RemoteModule remote:
Logger.Debug($"Adding {remote.Name} to downloadable modules.");
downloadableModules.Add(remote);
break;
}

if (!ModuleGraph.ContainsVertex(current.ModuleSpec))
{
Logger.Debug($"Adding {current.Name} to module graph.");
ModuleGraph.AddVertex(current.ModuleSpec);
}

Logger.Debug($"Adding edge from {ModuleSpec.Name} to {current.ModuleSpec.Name}.");
ModuleGraph.AddEdge(new Edge<ModuleSpec>(ModuleSpec, current.ModuleSpec));

current.Requirements.GetRequirements<ModuleSpec>().ForEach(module =>
{
Logger.Debug($"Adding {module.Name} to the queue.");
Module? resolved = null;
var resolvedPath = Path.GetFullPath(module.Name);
if (File.Exists(resolvedPath))
Expand Down
27 changes: 27 additions & 0 deletions src/Compiler/Requirements/ModuleSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Text;

namespace Compiler.Requirements;

public record ModuleSpec(
string Name,
Guid? Guid = null,
Version? MinimumVersion = null,
Version? MaximumVersion = null,
Version? RequiredVersion = null,
ModuleType Type = ModuleType.Downloadable
) : Requirement(true)
{
public override string GetInsertableLine()
{
var sb = new StringBuilder("#Requires -Modules @{");

sb.Append($"ModuleName = '{Name}';");
if (Guid != null) sb.Append($"GUID = {Guid};");
sb.Append($"ModuleVersion = '{(MinimumVersion != null ? MinimumVersion.ToString() : "0.0.0.0")}';");
if (MaximumVersion != null) sb.Append($"MaximumVersion = '{MaximumVersion}';");
if (RequiredVersion != null) sb.Append($"RequiredVersion = '{RequiredVersion}';");
sb.Append('}');

return sb.ToString();
}
}
25 changes: 0 additions & 25 deletions src/Compiler/Requirements/Requirements.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections;
using System.Text;
using CommandLine;

namespace Compiler.Requirements;
Expand Down Expand Up @@ -52,30 +51,6 @@ public abstract record Requirement(bool SupportsMultiple)
public abstract string GetInsertableLine();
}

public record ModuleSpec(
string Name,
Guid? Guid = null,
Version? MinimumVersion = null,
Version? MaximumVersion = null,
Version? RequiredVersion = null,
ModuleType Type = ModuleType.Downloadable
) : Requirement(true)
{
public override string GetInsertableLine()
{
var sb = new StringBuilder("#Requires -Modules @{");

sb.Append($"ModuleName = '{Name}';");
if (Guid != null) sb.Append($"GUID = {Guid};");
sb.Append($"ModuleVersion = '{(MinimumVersion != null ? MinimumVersion.ToString() : "0.0.0.0")}';");
if (MaximumVersion != null) sb.Append($"MaximumVersion = '{MaximumVersion}';");
if (RequiredVersion != null) sb.Append($"RequiredVersion = '{RequiredVersion}';");
sb.Append('}');

return sb.ToString();
}
}

public enum ModuleType
{
Downloadable,
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 4b93160

Please sign in to comment.