Skip to content

Commit

Permalink
Filter out unnecessary input files on 'make' command
Browse files Browse the repository at this point in the history
Adapt the 'make' command to discard 'elm-stuff' and '.git' directories when loading from a local file system.
  • Loading branch information
Viir committed Dec 5, 2023
1 parent 3516fdd commit 66eb719
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
32 changes: 32 additions & 0 deletions implement/elm-time/Pine/LoadFromLocalFilesystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;

namespace Pine;

Expand All @@ -16,4 +17,35 @@ public static class LoadFromLocalFilesystem

return PineValueComposition.SortedTreeFromSetOfBlobsWithStringPath(blobs);
}

public static TreeNodeWithStringPath RemoveNoiseFromTree(
TreeNodeWithStringPath originalTree,
bool discardGitDirectory)
{
if (originalTree is not TreeNodeWithStringPath.TreeNode tree)
return originalTree;

TreeNodeWithStringPath? getValueFromStringName(string name) =>
tree.Elements.FirstOrDefault(c => c.name == name).component;

var elmJson = getValueFromStringName("elm.json");

bool keepNode((string name, TreeNodeWithStringPath component) node)
{
if (elmJson != null && node.name is "elm-stuff")
return false;

if (discardGitDirectory && node.component is TreeNodeWithStringPath.TreeNode && node.name is ".git")
return false;

return true;
}

return TreeNodeWithStringPath.SortedTree(
treeContent:
[.. tree.Elements
.Where(keepNode)
.Select(child => (child.name, RemoveNoiseFromTree(child.component, discardGitDirectory)))
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Pine;
using System;
using System.Collections.Immutable;
using System.Linq;

namespace ElmTime.Platform.WebService;
Expand All @@ -26,7 +25,7 @@ public static
var filteredSourceTree =
loadCompositionResult.origin is LoadCompositionOrigin.FromLocalFileSystem
?
RemoveNoiseFromTreeComingFromLocalFileSystem(sourceTree)
LoadFromLocalFilesystem.RemoveNoiseFromTree(sourceTree, discardGitDirectory: true)
:
sourceTree;

Expand All @@ -42,32 +41,6 @@ loadCompositionResult.origin is LoadCompositionOrigin.FromLocalFileSystem
return (sourceTree, filteredSourceCompositionId, configZipArchive);
}

public static TreeNodeWithStringPath RemoveNoiseFromTreeComingFromLocalFileSystem(
TreeNodeWithStringPath originalTree)
{
if (originalTree is not TreeNodeWithStringPath.TreeNode tree)
return originalTree;

TreeNodeWithStringPath? getValueFromStringName(string name) =>
tree.Elements.FirstOrDefault(c => c.name == name).component;

var elmJson = getValueFromStringName("elm.json");

bool keepNode((string name, TreeNodeWithStringPath component) node)
{
if (elmJson != null && node.name == "elm-stuff")
return false;

return true;
}

return TreeNodeWithStringPath.SortedTree(
treeContent:
tree.Elements
.Where(keepNode)
.Select(child => (child.name, RemoveNoiseFromTreeComingFromLocalFileSystem(child.component))).ToImmutableList());
}

public static byte[] BuildConfigurationZipArchive(PineValue sourceComposition)
{
var parseSourceAsTree =
Expand Down
28 changes: 23 additions & 5 deletions implement/elm-time/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ElmTime;

public class Program
{
public static string AppVersionId => "2023-12-04";
public static string AppVersionId => "2023-12-05";

private static int AdminInterfaceDefaultPort => 4000;

Expand Down Expand Up @@ -1392,7 +1392,26 @@ private static CommandLineApplication AddMakeCommand(CommandLineApplication app)
loadInputDirectoryResult
.AndThen(loadInputDirectoryOk =>
{
if (loadInputDirectoryOk.tree.GetNodeAtPath(ImmutableList.Create("elm.json")) is not
var filteredSourceTree =
loadInputDirectoryOk.origin is LoadCompositionOrigin.FromLocalFileSystem
?
LoadFromLocalFilesystem.RemoveNoiseFromTree(
loadInputDirectoryOk.tree, discardGitDirectory: true)
:
loadInputDirectoryOk.tree;
var discardedBlobs =
loadInputDirectoryOk.tree
.EnumerateBlobsTransitive()
.Where(originalBlob => filteredSourceTree.GetNodeAtPath(originalBlob.path) is not TreeNodeWithStringPath.BlobNode)
.ToImmutableArray();
if (0 < discardedBlobs.Length)
{
Console.WriteLine("Discarded " + discardedBlobs.Length + " blobs from the input directory.");
}
if (filteredSourceTree.GetNodeAtPath(ImmutableList.Create("elm.json")) is not
TreeNodeWithStringPath.BlobNode elmJsonFile)
return Result<string, LoadForMakeResult>.err(
"Did not find elm.json file in that directory.");
Expand All @@ -1416,7 +1435,7 @@ private static CommandLineApplication AddMakeCommand(CommandLineApplication app)
{
return
Result<string, LoadForMakeResult>.ok(
new LoadForMakeResult(loadInputDirectoryOk.tree,
new LoadForMakeResult(filteredSourceTree,
[],
pathToElmFile.Replace('\\', '/').Split('/')));
}
Expand Down Expand Up @@ -1485,8 +1504,7 @@ IReadOnlyList<string> pathRelativeToCommonParentFromAbsolute(string absolutePath
.Aggregate(
seed:
TreeNodeWithStringPath.EmptyTree
.SetNodeAtPathSorted(workingDirectoryRelative,
loadInputDirectoryOk.tree),
.SetNodeAtPathSorted(workingDirectoryRelative, filteredSourceTree),
func:
(aggregate, nextSourceDir) =>
aggregate.SetNodeAtPathSorted(nextSourceDir.relativePath,
Expand Down
4 changes: 2 additions & 2 deletions implement/elm-time/elm-time.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>ElmTime</RootNamespace>
<AssemblyName>elm-time</AssemblyName>
<AssemblyVersion>2023.1204.0.0</AssemblyVersion>
<FileVersion>2023.1204.0.0</FileVersion>
<AssemblyVersion>2023.1205.0.0</AssemblyVersion>
<FileVersion>2023.1205.0.0</FileVersion>
<Nullable>enable</Nullable>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
Expand Down

0 comments on commit 66eb719

Please sign in to comment.