Skip to content

Commit

Permalink
fsxc: use external file for fsiStub
Browse files Browse the repository at this point in the history
This way we will not add 3 extra lines to the beginning
of the file, which would make line numbers to be off in
case of errors.
  • Loading branch information
knocte committed Sep 5, 2024
1 parent ac9cfaf commit 2cab8f3
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions fsxc/Fsxc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type ProgramInvocationType =
| FsxLauncherScript
| FsxcPureInvocation

type ExtraExtension = string

exception NoScriptProvided

module Program =
Expand Down Expand Up @@ -210,7 +212,7 @@ module Program =
let GetBinFolderForAScript(script: FileInfo) =
DirectoryInfo(Path.Combine(script.Directory.FullName, "bin"))

let GetAutoGenerationTargets (orig: FileInfo) (extension: string) =
let GetAutoGenerationTargets (orig: FileInfo) (extension: ExtraExtension) =
let binDir = GetBinFolderForAScript(orig)

let autogeneratedFileName =
Expand Down Expand Up @@ -477,7 +479,7 @@ module Program =
let preprocessScriptContents
(origScript: FileInfo)
(contents: List<LineAction>)
: List<CompilerInput> * FileInfo =
: List<CompilerInput> * List<ExtraExtension> * FileInfo =

#if LEGACY_FRAMEWORK
// from "Microsoft.Build", "16.11.0" to .../packages/Microsoft.Build.16.11.0/lib/net472/Microsoft.Build.dll
Expand Down Expand Up @@ -580,13 +582,17 @@ module Program =
| Some location -> location
#endif

let initialInjectedContents =
"""module FsxScript
let initialFileContent =
"""[<AutoOpen>]
module FsxScriptInit
type FsiStub = { CommandLineArgs: array<string> }
let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() }
"""

let autogeneratedFile = GetAutoGenerationTargets origScript "fs"
let extraExtensionForMain: ExtraExtension = "fs"

let autogeneratedFile =
GetAutoGenerationTargets origScript extraExtensionForMain

let binFolder = autogeneratedFile.Directory

Expand All @@ -597,11 +603,28 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() }

File.WriteAllText(
autogeneratedFile.FullName,
initialInjectedContents
"""module FsxScript
"""
)

seq {
let extraExtensionForInitFile: ExtraExtension = "init.fs"

let initFile =
GetAutoGenerationTargets origScript extraExtensionForInitFile

File.WriteAllText(initFile.FullName, initialFileContent)

let extraExtensions =
[
extraExtensionForInitFile
extraExtensionForMain
]

seq {
// for !LEGACY_FRAMEWORK, this init file will be added to the fsproj file
#if LEGACY_FRAMEWORK
yield CompilerInput.SourceFile initFile
#endif
let startCommentInFSharp = "// "

for maybeDep in contents do
Expand Down Expand Up @@ -682,6 +705,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() }
)
}
|> List.ofSeq,
extraExtensions,
autogeneratedFile

#if LEGACY_FRAMEWORK
Expand All @@ -707,6 +731,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() }
let generateProjectFile
(origScript: FileInfo)
(contents: List<LineAction>)
(extraExtensions: seq<ExtraExtension>)
: FileInfo =
let projectFile = GetAutoGenerationTargets origScript "fsproj"

Expand Down Expand Up @@ -805,7 +830,8 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() }

iterate contents

addFile(sprintf "%s.fs" origScript.Name)
for ext in extraExtensions do
addFile(sprintf "%s.%s" origScript.Name ext)

File.AppendAllText(projectFile.FullName, "</Project>")

Expand All @@ -817,7 +843,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() }

let binFolder = GetBinFolderForAScript script

let compilerInputs, autoGenFile =
let compilerInputs, extraExtensions, autoGenFile =
preprocessScriptContents script contents

Console.WriteLine(
Expand All @@ -829,7 +855,10 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() }

Console.WriteLine("_________________END AUTOGEN FILE_________________")
#if !LEGACY_FRAMEWORK
let projectFile = generateProjectFile script contents
let projectFile = generateProjectFile script contents extraExtensions
#else // dummy for to just prevent "unused" warning, meh
for _ext in extraExtensions do
()
#endif

let exitCode, exeTarget =
Expand Down

0 comments on commit 2cab8f3

Please sign in to comment.