Skip to content

Commit

Permalink
Add support for files with namespaces
Browse files Browse the repository at this point in the history
Closes #17
  • Loading branch information
kant2002 committed Dec 5, 2024
1 parent b38fd4b commit 8017d3d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 13 deletions.
4 changes: 2 additions & 2 deletions FSharpPacker.FSharp/FSharpPacker.FSharp.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Argu" Version="6.1.1" />
<PackageReference Include="Argu" Version="6.2.4" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\"/>
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>


Expand Down
6 changes: 6 additions & 0 deletions FSharpPacker.FSharp/FsxPrepocessor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type FsxPreprocessor(verbose: bool) =
|> Option.map (fun x -> x.ReadProducedFile())
|> Option.defaultValue ""

member _.FindSource(matcher: Func<string, bool>) =
sourceFiles
|> Seq.tryFind (fun x -> matcher.Invoke(x.FileName))
|> Option.map (fun x -> x.ReadProducedFile())
|> Option.defaultValue ""


member _.GetSources() =
sourceFiles.ToArray()
Expand Down
14 changes: 12 additions & 2 deletions FSharpPacker.FSharp/Packer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,18 @@ let rec public ProcessLine verbose state sourceFile line =
()

and public ProcessFile state sourceFile verbose =
sourceFile.WriteLine($"module {GetModuleName(sourceFile)}")
sourceFile.ReadContent() |> Seq.iter (ProcessLine verbose state sourceFile )
let worker = ProcessLine verbose state sourceFile
let mutable insertedModule = false
sourceFile.ReadContent()
|> Seq.iteri (fun i line ->
if insertedModule || (String.IsNullOrWhiteSpace(line.Trim()))
then worker line
else
let trimmed = line.TrimStart()
insertedModule <- true
if not (trimmed.StartsWith("namespace ") || trimmed.StartsWith("namespace\t")) then
sourceFile.WriteLine($"module {GetModuleName(sourceFile)}")
worker line)
()


Expand Down
8 changes: 8 additions & 0 deletions FSharpPacker.FSharp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"FSharpPacker.FSharp": {
"commandName": "Project",
"commandLineArgs": "C:\\d\\my\\lesson1\\lesson15.fsx -sf -r win-x86 --framework net7.0 -o x86 -p:EnableCompressionInSingleFile=true"
}
}
}
15 changes: 7 additions & 8 deletions FSharpPacker.Tests/FSharpPacker.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.4" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<None Remove="Samples\**\*.fsx" />
<Content Include="Samples\**\*.fsx" CopyToOutputDirectory="Always" />
</ItemGroup>

<ItemGroup>
<Folder Include="Samples\Level1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FSharpPacker.FSharp\FSharpPacker.FSharp.fsproj" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions FSharpPacker.Tests/Samples/Level1/WithNamespace.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Asfaload.Collector
module Queue=
let f (s:string) = s
2 changes: 2 additions & 0 deletions FSharpPacker.Tests/Samples/Namespaces.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#load "Level1/WithNamespace.fsx"
printfn "Hello, world"
18 changes: 18 additions & 0 deletions FSharpPacker.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,22 @@ public void ConditionalCompilation()
var sources = preprocessor.GetSources();
Assert.AreEqual(1, sources.Length);
}
[TestMethod]
public void NamespacesInLoadFiles()
{
var sourceFile = "Samples/Namespaces.fsx";
var preprocessor = new FsxPreprocessor(verbose: true);
preprocessor.AddSource(sourceFile);

preprocessor.Process();

Assert.AreEqual(@"module Namespaces
printfn ""Hello, world""
".ReplaceLineEndings(), preprocessor.GetSource(sourceFile));

Assert.AreEqual(@"namespace Asfaload.Collector
module Queue=
let f (s:string) = s
".ReplaceLineEndings(), preprocessor.FindSource(_ => _.EndsWith("Level1/WithNamespace.fsx")));
}
}
2 changes: 1 addition & 1 deletion nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="dotnet7" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json" />
<!--<add key="dotnet7" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json" />-->
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<!--<add key="dotnet6" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json" />-->
</packageSources>
Expand Down

0 comments on commit 8017d3d

Please sign in to comment.