From c870c78e1b9775ecc04a7c9899124d3a8dc1305d Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Sun, 13 Apr 2025 10:58:49 +0500 Subject: [PATCH] Suppport scripts with shebang Closes #22 --- FSharpPacker.FSharp/Packer.fs | 4 ++++ FSharpPacker.Tests/Samples/Shebang.fsx | 3 +++ FSharpPacker.Tests/UnitTest1.cs | 11 +++++++++++ 3 files changed, 18 insertions(+) create mode 100644 FSharpPacker.Tests/Samples/Shebang.fsx diff --git a/FSharpPacker.FSharp/Packer.fs b/FSharpPacker.FSharp/Packer.fs index 7544cd7..410aedf 100644 --- a/FSharpPacker.FSharp/Packer.fs +++ b/FSharpPacker.FSharp/Packer.fs @@ -93,6 +93,7 @@ let ParsePaths paths = type FsxLine = | SourceCode of string | Unsupported + | Skipped | IncludePath of seq | IncludeFiles of seq | IncludeReference of references: seq * packages: seq * projectReferences: seq @@ -151,6 +152,8 @@ let classifyLine (sourceFile: SourceFile) (normalizedLine: string) = let pathStrings = normalizedLine.Replace("#load ", ""); let files = ParsePaths(pathStrings) |> Seq.map sourceFile.ResolveRelativePath IncludeFiles(files) + elif normalizedLine.StartsWith("#!") then + Skipped else SourceCode(normalizedLine) else SourceCode(normalizedLine) @@ -161,6 +164,7 @@ let rec public ProcessLine verbose state sourceFile line = match lineResult with | SourceCode(code) -> sourceFile.WriteLine(code) | Unsupported -> () + | Skipped -> () | AddNugetFeed(code) -> state.nugetSources <- Seq.append state.nugetSources [code] | IncludePath(files) -> sourceFile.AddIncludePaths(files) | IncludeFiles(files) -> for file in files do diff --git a/FSharpPacker.Tests/Samples/Shebang.fsx b/FSharpPacker.Tests/Samples/Shebang.fsx new file mode 100644 index 0000000..a608cd2 --- /dev/null +++ b/FSharpPacker.Tests/Samples/Shebang.fsx @@ -0,0 +1,3 @@ +#!/usr/bin/env -S dotnet fsi + +printfn "Hello, world!" diff --git a/FSharpPacker.Tests/UnitTest1.cs b/FSharpPacker.Tests/UnitTest1.cs index ea0f134..d6a0290 100644 --- a/FSharpPacker.Tests/UnitTest1.cs +++ b/FSharpPacker.Tests/UnitTest1.cs @@ -245,4 +245,15 @@ public void MultipleLoadDirectivesFile() CollectionAssert.AreEqual(new string[] { "WithNamespace.fsx", "WithNamespaceAndComments.fsx", "Loader.fsx", "MultipleLoadDirectivesFile.fsx" }, preprocessor.GetSources().Select(_ => Path.GetFileName(_.FileName)).ToArray()); } + [TestMethod] + public void Shebang() + { + var sourceFile = "Samples/Shebang.fsx"; + var preprocessor = new FsxPreprocessor(verbose: false); + preprocessor.AddSource(sourceFile); + + preprocessor.Process(); + + Assert.AreEqual("module Shebang" + Environment.NewLine + Environment.NewLine + "printfn \"Hello, world!\"" + Environment.NewLine, preprocessor.GetSource(sourceFile)); + } } \ No newline at end of file