|
| 1 | +open System.IO |
| 2 | +open System.Text.RegularExpressions |
| 3 | +open FSharp.Compiler.CodeAnalysis |
| 4 | +open Buildalyzer |
| 5 | + |
| 6 | +let getProjectOptionsFromProjectFile (isMain: bool) (projFile: string) = |
| 7 | + |
| 8 | + let tryGetResult (isMain: bool) (manager: AnalyzerManager) (maybeCsprojFile: string) = |
| 9 | + |
| 10 | + let analyzer = manager.GetProject(maybeCsprojFile) |
| 11 | + let env = analyzer.EnvironmentFactory.GetBuildEnvironment(Environment.EnvironmentOptions(DesignTime=true,Restore=false)) |
| 12 | + // If System.the project targets multiple frameworks, multiple results will be returned |
| 13 | + // For now we just take the first one with non-empty command |
| 14 | + let results = analyzer.Build(env) |
| 15 | + results |
| 16 | + |> Seq.tryFind (fun r -> System.String.IsNullOrEmpty(r.Command) |> not) |
| 17 | + |
| 18 | + let manager = |
| 19 | + let log = new StringWriter() |
| 20 | + let options = AnalyzerManagerOptions(LogWriter = log) |
| 21 | + let m = AnalyzerManager(options) |
| 22 | + m |
| 23 | + |
| 24 | + // Because Buildalyzer works better with .csproj, we first "dress up" the project as if it were a C# one |
| 25 | + // and try to adapt the results. If it doesn't work, we try again to analyze the .fsproj directly |
| 26 | + let csprojResult = |
| 27 | + let csprojFile = projFile.Replace(".fsproj", ".csproj") |
| 28 | + if File.Exists(csprojFile) then |
| 29 | + None |
| 30 | + else |
| 31 | + try |
| 32 | + File.Copy(projFile, csprojFile) |
| 33 | + tryGetResult isMain manager csprojFile |
| 34 | + |> Option.map (fun (r: IAnalyzerResult) -> |
| 35 | + // Careful, options for .csproj start with / but so do root paths in unix |
| 36 | + let reg = Regex(@"^\/[^\/]+?(:?:|$)") |
| 37 | + let comArgs = |
| 38 | + r.CompilerArguments |
| 39 | + |> Array.map (fun line -> |
| 40 | + if reg.IsMatch(line) then |
| 41 | + if line.StartsWith("/reference") then "-r" + line.Substring(10) |
| 42 | + else "--" + line.Substring(1) |
| 43 | + else line) |
| 44 | + let comArgs = |
| 45 | + match r.Properties.TryGetValue("OtherFlags") with |
| 46 | + | false, _ -> comArgs |
| 47 | + | true, otherFlags -> |
| 48 | + let otherFlags = otherFlags.Split(' ', System.StringSplitOptions.RemoveEmptyEntries) |
| 49 | + Array.append otherFlags comArgs |
| 50 | + comArgs, r) |
| 51 | + finally |
| 52 | + File.Delete(csprojFile) |
| 53 | + |
| 54 | + let compilerArgs, result = |
| 55 | + csprojResult |
| 56 | + |> Option.orElseWith (fun () -> |
| 57 | + tryGetResult isMain manager projFile |
| 58 | + |> Option.map (fun r -> |
| 59 | + // result.CompilerArguments doesn't seem to work well in Linux |
| 60 | + let comArgs = Regex.Split(r.Command, @"\r?\n") |
| 61 | + comArgs, r)) |
| 62 | + |> function |
| 63 | + | Some result -> result |
| 64 | + // TODO: Get Buildalyzer errors from the log |
| 65 | + | None -> failwith $"Cannot parse {projFile}" |
| 66 | + |
| 67 | + let projDir = Path.GetDirectoryName(projFile) |
| 68 | + let projOpts = |
| 69 | + compilerArgs |
| 70 | + |> Array.skipWhile (fun line -> not(line.StartsWith("-"))) |
| 71 | + |> Array.map (fun f -> |
| 72 | + if f.EndsWith(".fs") || f.EndsWith(".fsi") then |
| 73 | + if Path.IsPathRooted f then f else Path.Combine(projDir, f) |
| 74 | + else f) |
| 75 | + projOpts, |
| 76 | + Seq.toArray result.ProjectReferences, |
| 77 | + result.Properties, |
| 78 | + result.TargetFramework |
| 79 | + |
| 80 | +let mkStandardProjectReferences () = |
| 81 | + let file = "fcs-export.fsproj" |
| 82 | + let projDir = __SOURCE_DIRECTORY__ |
| 83 | + let projFile = Path.Combine(projDir, file) |
| 84 | + let (args, _, _, _) = getProjectOptionsFromProjectFile true projFile |
| 85 | + args |
| 86 | + |> Array.filter (fun s -> s.StartsWith("-r:")) |
| 87 | + |
| 88 | +let mkProjectCommandLineArgsForScript (dllName, fileNames) = |
| 89 | + [| yield "--simpleresolution" |
| 90 | + yield "--noframework" |
| 91 | + yield "--debug:full" |
| 92 | + yield "--define:DEBUG" |
| 93 | + yield "--targetprofile:netcore" |
| 94 | + yield "--optimize-" |
| 95 | + yield "--out:" + dllName |
| 96 | + yield "--doc:test.xml" |
| 97 | + yield "--warn:3" |
| 98 | + yield "--fullpaths" |
| 99 | + yield "--flaterrors" |
| 100 | + yield "--target:library" |
| 101 | + for x in fileNames do |
| 102 | + yield x |
| 103 | + let references = mkStandardProjectReferences () |
| 104 | + for r in references do |
| 105 | + yield r |
| 106 | + |] |
| 107 | + |
| 108 | +let checker = FSharpChecker.Create() |
| 109 | + |
| 110 | +let parseAndCheckScript (file, input) = |
| 111 | + let dllName = Path.ChangeExtension(file, ".dll") |
| 112 | + let projName = Path.ChangeExtension(file, ".fsproj") |
| 113 | + let args = mkProjectCommandLineArgsForScript (dllName, [file]) |
| 114 | + printfn "file: %s" file |
| 115 | + args |> Array.iter (printfn "args: %s") |
| 116 | + let projectOptions = checker.GetProjectOptionsFromCommandLineArgs (projName, args) |
| 117 | + let parseRes, typedRes = checker.ParseAndCheckFileInProject(file, 0, input, projectOptions) |> Async.RunSynchronously |
| 118 | + |
| 119 | + if parseRes.Diagnostics.Length > 0 then |
| 120 | + printfn "---> Parse Input = %A" input |
| 121 | + printfn "---> Parse Error = %A" parseRes.Diagnostics |
| 122 | + |
| 123 | + match typedRes with |
| 124 | + | FSharpCheckFileAnswer.Succeeded(res) -> parseRes, res |
| 125 | + | res -> failwithf "Parsing did not finish... (%A)" res |
| 126 | + |
| 127 | +[<EntryPoint>] |
| 128 | +let main argv = |
| 129 | + ignore argv |
| 130 | + printfn "Exporting metadata..." |
| 131 | + let file = "/temp/test.fsx" |
| 132 | + let input = "let a = 42" |
| 133 | + let sourceText = FSharp.Compiler.Text.SourceText.ofString input |
| 134 | + // parse script just to export metadata |
| 135 | + let parseRes, typedRes = parseAndCheckScript(file, sourceText) |
| 136 | + printfn "Exporting is done. Binaries can be found in ./temp/metadata/" |
| 137 | + 0 |
0 commit comments