Skip to content

Commit

Permalink
fix(Compiler): Some small fixes and a working hex zip extractor
Browse files Browse the repository at this point in the history
Disable indenting the localfiles text so the exported modules aren't weirdly indented, also saves some space.
Fix script embedding itself as a module
Refactor and greatly improve the extraction process for modules
  • Loading branch information
DaRacci committed Jun 24, 2024
1 parent d530c7f commit 5a31b13
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
Empty file removed TODO.md
Empty file.
14 changes: 7 additions & 7 deletions src/Compiler/Module/CompiledModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static CompiledModule From(Module module, int indentBy = 0)
ContentType.UTF8String,
localFileModule.ModuleSpec,
localFileModule.Requirements,
CompiledDocument.FromBuilder(localFileModule.Document, indentBy + 4).GetContent(),
CompiledDocument.FromBuilder(localFileModule.Document, 0).GetContent(),
indentBy
),
RemoteModule remoteModule => new CompiledModule(
Expand Down Expand Up @@ -63,33 +63,33 @@ public override string ToString()

Requirements.GetRequirements().Where(requirement => requirement is not Compiler.Requirements.ModuleSpec).ToList().ForEach(requirement =>
{
sb.Append(contentIndentStr);
// sb.Append(contentIndentStr);
sb.AppendLine(requirement.GetInsertableLine());
});
Requirements.GetRequirements<ModuleSpec>().ToList().ForEach(requirement =>
{
sb.Append(contentIndentStr);
// sb.Append(contentIndentStr);
sb.AppendLine(requirement.GetInsertableLine());
});

sb.AppendLine(Content);

sb.AppendLine("'@");
sb.AppendLine("'@;");
contentObject = sb.ToString();
break;
}
case ContentType.ZipHex:
contentObject = $"'${Content}'";
contentObject = $"'{Content}'";
break;
default:
throw new NotImplementedException();
}

return $$"""
{{indentStr}}'{{ModuleSpec.Name}}' = @{
{{indentStr}} Type = '{{GetType().Name}}';
{{indentStr}} Type = '{{ContentType}}';
{{indentStr}} Hash = '{{ContentHash}}';
{{indentStr}} Content = {{contentObject}};
{{indentStr}} Content = {{contentObject}}
{{indentStr}}};
""";
}
Expand Down
42 changes: 36 additions & 6 deletions src/Compiler/Module/CompiledScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,41 @@ public string Compile()
ResolvedModules.ToList().ForEach(module => script.AppendLine(module.ToString()));
script.AppendLine("};");

script.AppendLine("""
$Global:EmbeddedModules | ForEach-Object {
if ($_.Type -eq 'UTF8String') {
$Local:Path = Join-Path $env:TEMP $("($_.Name)-($_.ContentHash).ps1");
if (-not (Test-Path $Local:Path)) {
Set-Content -Path $Local:Path -Value $_.Content;
script.AppendLine(/*ps1*/ """
$Local:PrivatePSModulePath = $env:ProgramData | Join-Path -ChildPath 'AMT/PowerShell/Modules';
if (-not (Test-Path -Path $Local:PrivatePSModulePath)) {
Write-Host "Creating module root folder: $Local:PrivatePSModulePath";
New-Item -Path $Local:PrivatePSModulePath -ItemType Directory | Out-Null;
}
$Local:PSModulePath = $env:PSModulePath -split ';' | Select-Object -First 1;
$Global:EmbeddedModules.GetEnumerator() | ForEach-Object {
$Local:Content = $_.Value.Content;
$Local:NameHash = "$($_.Key)-$($_.Value.Hash)";
$Local:ModuleFolderPath = Join-Path -Path $Local:PSModulePath -ChildPath $Local:NameHash;
if (-not (Test-Path -Path $Local:ModuleFolderPath)) {
Write-Host "Creating module folder: $Local:ModuleFolderPath";
New-Item -Path $Local:ModuleFolderPath -ItemType Directory | Out-Null;
}
switch ($_.Value.Type) {
'UTF8String' {
$Local:InnerModulePath = Join-Path -Path $Local:ModuleFolderPath -ChildPath "$Local:NameHash.psm1";
if (-not (Test-Path -Path $Local:InnerModulePath)) {
Write-Host "Writing content to module file: $Local:InnerModulePath"
Set-Content -Path $Local:InnerModulePath -Value $Content;
}
}
'ZipHex' {
if (Test-Path -Path $Local:ModuleFolderPath) {
return;
}
[String]$Local:TempFile = [System.IO.Path]::GetTempFileName();
[Byte[]]$Local:Bytes = [System.Convert]::FromHexString($Content);
[System.IO.File]::WriteAllBytes($Local:TempFile, $Local:Bytes);
Write-Host "Expanding module file: $Local:TempFile"
Expand-Archive -Path $Local:TempFile -DestinationPath $Local:ModuleFolderPath -Force;
}
Default {
Write-Warning "Unknown module type: $($_)";
}
}
}
Expand Down Expand Up @@ -211,6 +240,7 @@ private void ResolveRequirements()
});
}

ModuleGraph.RemoveVertex(ModuleSpec); // Remove the script from the graph so it doesn't try to resolve itself.
var sortedModules = ModuleGraph.TopologicalSort() ?? throw new Exception("Cyclic dependency detected.");
sortedModules.ToList().ForEach(moduleSpec =>
{
Expand Down
4 changes: 3 additions & 1 deletion src/Compiler/Module/Local.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void FixLines()
80,
MultilineStringOpenRegex(),
MultilineStringCloseRegex(),
UpdateOptions.InsertInline,
(lines) =>
{
// Get the multiline indent level from the last line of the string.
Expand Down Expand Up @@ -196,7 +197,8 @@ public override ModuleMatch GetModuleMatchFor(ModuleSpec requirement)

public static LocalFileModule? TryFromFile(string relativeFrom, ModuleSpec spec)
{
var relativePath = spec switch {
var relativePath = spec switch
{
PathedModuleSpec pathedSpec => pathedSpec.RelativePath,
_ => spec.Name
};
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/Requirements/ModuleSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public ModuleSpec MergeSpecs(ModuleSpec[] merge)

public override string GetInsertableLine()
{
if (Guid == null && RequiredVersion == null && MinimumVersion == null && MaximumVersion == null)
{
return $"Using module '{Path.GetFileNameWithoutExtension(Name)}'";
}

var sb = new StringBuilder("Using module @{");
sb.Append($"ModuleName = '{Path.GetFileNameWithoutExtension(Name)}';");
if (Guid != null) sb.Append($"GUID = {Guid};");
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Text/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public override string ToString()
var sb = new StringBuilder()
.AppendLine("TextEditor:")
.AppendLine(" Document:")
.AppendLine('\t' + string.Join("\n\t", Document.Lines))
// .AppendLine('\t' + string.Join("\n\t", Document.Lines))
.AppendLine(" TextUpdaters:")
.AppendLine('\t' + string.Join("\n\t", TextUpdaters.Select(updater => updater.ToString())));
return sb.ToString();
Expand Down

0 comments on commit 5a31b13

Please sign in to comment.