|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Angouri. |
| 3 | +// AngouriMath is licensed under MIT. |
| 4 | +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
| 5 | +// Website: https://am.angouri.org. |
| 6 | +// |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.IO; |
| 11 | +using System.Linq; |
| 12 | +using System.Text; |
| 13 | + |
| 14 | +namespace DocSamples; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// The F# side of the same check. `AngouriMath.FSharp` is a published package, and the wiki |
| 18 | +/// page for it is the only documentation of the wrapper's names, so a wrong name there -- |
| 19 | +/// `dy/dx` for `d/dx` -- has nothing else to catch it. |
| 20 | +/// |
| 21 | +/// One file, one module per sample, so each sample keeps its own `open` directives. F# |
| 22 | +/// wants `open` before anything else in a module, so they are hoisted out of the body. |
| 23 | +/// </summary> |
| 24 | +static class FSharpGenerator |
| 25 | +{ |
| 26 | + public static Dictionary<string, int> Write(string dir, string wrapperFsproj, |
| 27 | + IEnumerable<Snippet> snippets, ISet<string> exclude) |
| 28 | + { |
| 29 | + var wanted = snippets.Where(s => s.Mode != Mode.Skip && !exclude.Contains(s.Id)).ToList(); |
| 30 | + Directory.CreateDirectory(dir); |
| 31 | + |
| 32 | + var sb = new StringBuilder(); |
| 33 | + var offsets = new Dictionary<string, int>(); |
| 34 | + sb.AppendLine("module DocSamples.Generated.FSharpSamples"); |
| 35 | + sb.AppendLine(); |
| 36 | + |
| 37 | + foreach (var s in wanted) |
| 38 | + { |
| 39 | + var opens = s.Body.Where(l => l.TrimStart().StartsWith("open ")).ToList(); |
| 40 | + sb.AppendLine($"module {s.Id} ="); |
| 41 | + foreach (var o in opens) sb.AppendLine(" " + o.Trim()); |
| 42 | + sb.AppendLine(" let run () ="); |
| 43 | + offsets[s.Id] = sb.ToString().Count(c => c == '\n') + 1; |
| 44 | + var wrote = false; |
| 45 | + foreach (var line in s.Body) |
| 46 | + { |
| 47 | + if (opens.Contains(line)) { sb.AppendLine(); continue; } |
| 48 | + sb.AppendLine(line.Trim().Length == 0 ? "" : " " + line); |
| 49 | + if (line.Trim().Length > 0) wrote = true; |
| 50 | + } |
| 51 | + if (!wrote) sb.AppendLine(" ()"); |
| 52 | + sb.AppendLine(); |
| 53 | + } |
| 54 | + |
| 55 | + sb.AppendLine("module Runner ="); |
| 56 | + sb.AppendLine(" open System"); |
| 57 | + sb.AppendLine(" open System.IO"); |
| 58 | + sb.AppendLine(" open System.Text.Json"); |
| 59 | + sb.AppendLine(); |
| 60 | + sb.AppendLine(" type Result = { Id: string; Status: string; Output: string }"); |
| 61 | + sb.AppendLine(); |
| 62 | + sb.AppendLine(" let samples : (string * (unit -> unit) * bool) list ="); |
| 63 | + if (wanted.Count == 0) sb.AppendLine(" []"); |
| 64 | + else |
| 65 | + sb.AppendLine(" [ " + string.Join("\n ", |
| 66 | + wanted.Select(s => $"(\"{s.Id}\", {s.Id}.run, {(s.Mode == Mode.Run ? "true" : "false")})")) |
| 67 | + + " ]"); |
| 68 | + sb.AppendLine(""" |
| 69 | +
|
| 70 | + [<EntryPoint>] |
| 71 | + let main argv = |
| 72 | + let real = Console.Out |
| 73 | + let results = |
| 74 | + samples |
| 75 | + |> List.map (fun (id, run, execute) -> |
| 76 | + if not execute then { Id = id; Status = "compiled"; Output = "" } |
| 77 | + else |
| 78 | + let captured = new StringWriter() |
| 79 | + Console.SetOut(captured) |
| 80 | + let status = |
| 81 | + try |
| 82 | + run () |
| 83 | + "ok" |
| 84 | + with e -> "threw " + e.GetType().Name + ": " + e.Message.Split('\n').[0] |
| 85 | + Console.SetOut(real) |
| 86 | + { Id = id; Status = status; Output = captured.ToString() }) |
| 87 | + let path = if argv.Length > 0 then argv.[0] else "results.json" |
| 88 | + File.WriteAllText(path, JsonSerializer.Serialize(results)) |
| 89 | + 0 |
| 90 | + """); |
| 91 | + |
| 92 | + File.WriteAllText(Path.Combine(dir, "Samples.fs"), sb.ToString()); |
| 93 | + File.WriteAllText(Path.Combine(dir, "generated-fsharp.fsproj"), $""" |
| 94 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 95 | + <PropertyGroup> |
| 96 | + <OutputType>Exe</OutputType> |
| 97 | + <TargetFramework>net10.0</TargetFramework> |
| 98 | + <AssemblyName>generatedfsharp</AssemblyName> |
| 99 | + <TreatWarningsAsErrors>false</TreatWarningsAsErrors> |
| 100 | + <WarningLevel>0</WarningLevel> |
| 101 | + <NoWarn>FS0025;FS0049;FS0064;FS0193;FS1182</NoWarn> |
| 102 | + </PropertyGroup> |
| 103 | + <ItemGroup> |
| 104 | + <Compile Include="Samples.fs" /> |
| 105 | + </ItemGroup> |
| 106 | + <ItemGroup> |
| 107 | + <ProjectReference Include="{wrapperFsproj}" /> |
| 108 | + </ItemGroup> |
| 109 | + </Project> |
| 110 | + """); |
| 111 | + return offsets; |
| 112 | + } |
| 113 | +} |
0 commit comments