|
| 1 | +open System.IO |
| 2 | +open System.Collections.Generic |
| 3 | +open FSharp.Compiler |
| 4 | +open FSharp.Compiler.SourceCodeServices |
| 5 | + |
| 6 | +let readRefs (folder : string) (projectFile: string) = |
| 7 | + let runProcess (workingDir: string) (exePath: string) (args: string) = |
| 8 | + let psi = System.Diagnostics.ProcessStartInfo() |
| 9 | + psi.FileName <- exePath |
| 10 | + psi.WorkingDirectory <- workingDir |
| 11 | + psi.RedirectStandardOutput <- false |
| 12 | + psi.RedirectStandardError <- false |
| 13 | + psi.Arguments <- args |
| 14 | + psi.CreateNoWindow <- true |
| 15 | + psi.UseShellExecute <- false |
| 16 | + |
| 17 | + use p = new System.Diagnostics.Process() |
| 18 | + p.StartInfo <- psi |
| 19 | + p.Start() |> ignore |
| 20 | + p.WaitForExit() |
| 21 | + |
| 22 | + let exitCode = p.ExitCode |
| 23 | + exitCode, () |
| 24 | + |
| 25 | + let runCmd exePath args = runProcess folder exePath (args |> String.concat " ") |
| 26 | + let msbuildExec = Dotnet.ProjInfo.Inspect.dotnetMsbuild runCmd |
| 27 | + let result = Dotnet.ProjInfo.Inspect.getProjectInfo ignore msbuildExec Dotnet.ProjInfo.Inspect.getFscArgs [] projectFile |
| 28 | + match result with |
| 29 | + | Ok(Dotnet.ProjInfo.Inspect.GetResult.FscArgs x) -> |
| 30 | + x |
| 31 | + |> List.filter (fun s -> s.StartsWith("-r:")) |
| 32 | + |> List.map (fun s -> s.Replace("-r:", "")) |
| 33 | + | _ -> [] |
| 34 | + |
| 35 | +let mkStandardProjectReferences () = |
| 36 | + let file = "fcs-export.fsproj" |
| 37 | + let projDir = __SOURCE_DIRECTORY__ |
| 38 | + readRefs projDir file |
| 39 | + |
| 40 | +let mkProjectCommandLineArgsForScript (dllName, fileNames) = |
| 41 | + [| yield "--simpleresolution" |
| 42 | + yield "--noframework" |
| 43 | + yield "--debug:full" |
| 44 | + yield "--define:DEBUG" |
| 45 | + yield "--optimize-" |
| 46 | + yield "--out:" + dllName |
| 47 | + yield "--doc:test.xml" |
| 48 | + yield "--warn:3" |
| 49 | + yield "--fullpaths" |
| 50 | + yield "--flaterrors" |
| 51 | + yield "--target:library" |
| 52 | + for x in fileNames do |
| 53 | + yield x |
| 54 | + let references = mkStandardProjectReferences () |
| 55 | + for r in references do |
| 56 | + yield "-r:" + r |
| 57 | + |] |
| 58 | + |
| 59 | +let checker = FSharpChecker.Create() |
| 60 | + |
| 61 | +let parseAndCheckScript (file, input) = |
| 62 | + let dllName = Path.ChangeExtension(file, ".dll") |
| 63 | + let projName = Path.ChangeExtension(file, ".fsproj") |
| 64 | + let args = mkProjectCommandLineArgsForScript (dllName, [file]) |
| 65 | + // printfn "file: %s" file |
| 66 | + // args |> Array.iter (printfn "args: %s") |
| 67 | + let projectOptions = checker.GetProjectOptionsFromCommandLineArgs (projName, args) |
| 68 | + let parseRes, typedRes = checker.ParseAndCheckFileInProject(file, 0, input, projectOptions) |> Async.RunSynchronously |
| 69 | + |
| 70 | + if parseRes.Errors.Length > 0 then |
| 71 | + printfn "---> Parse Input = %A" input |
| 72 | + printfn "---> Parse Error = %A" parseRes.Errors |
| 73 | + |
| 74 | + match typedRes with |
| 75 | + | FSharpCheckFileAnswer.Succeeded(res) -> parseRes, res |
| 76 | + | res -> failwithf "Parsing did not finish... (%A)" res |
| 77 | + |
| 78 | +[<EntryPoint>] |
| 79 | +let main argv = |
| 80 | + ignore argv |
| 81 | + printfn "Exporting metadata..." |
| 82 | + let file = "/temp/test.fsx" |
| 83 | + let input = "let a = 42" |
| 84 | + let sourceText = FSharp.Compiler.Text.SourceText.ofString input |
| 85 | + // parse script just to export metadata |
| 86 | + let parseRes, typedRes = parseAndCheckScript(file, sourceText) |
| 87 | + printfn "Exporting is done." |
| 88 | + 0 |
0 commit comments