From aaf9d031b728e23bec2674d4fdd40cada1f79ecc Mon Sep 17 00:00:00 2001 From: Alfonso Garcia-Caro Date: Mon, 27 Sep 2021 10:50:38 +0900 Subject: [PATCH] Serialize compiler_info as json --- src/Fable.AST/Plugins.fs | 20 +++++++++++--------- src/Fable.Cli/Fable.Cli.fsproj | 1 + src/Fable.Cli/ProjectCracker.fs | 23 ++++++++--------------- src/Fable.Transforms/Global/Compiler.fs | 22 +++++++++++----------- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/Fable.AST/Plugins.fs b/src/Fable.AST/Plugins.fs index 3201bcdcac..31091dffe2 100644 --- a/src/Fable.AST/Plugins.fs +++ b/src/Fable.AST/Plugins.fs @@ -14,15 +14,17 @@ type Language = | TypeScript type CompilerOptions = - abstract TypedArrays: bool - abstract ClampByteArrays: bool - abstract Language: Language - abstract Define: string list - abstract DebugMode: bool - abstract OptimizeFSharpAst: bool - abstract RootModule: bool - abstract Verbosity: Verbosity - abstract FileExtension: string + { + TypedArrays: bool + ClampByteArrays: bool + Language: Language + Define: string list + DebugMode: bool + OptimizeFSharpAst: bool + RootModule: bool + Verbosity: Verbosity + FileExtension: string + } type PluginHelper = abstract LibraryDir: string diff --git a/src/Fable.Cli/Fable.Cli.fsproj b/src/Fable.Cli/Fable.Cli.fsproj index 0a7c8d2250..469f03597a 100644 --- a/src/Fable.Cli/Fable.Cli.fsproj +++ b/src/Fable.Cli/Fable.Cli.fsproj @@ -43,6 +43,7 @@ + diff --git a/src/Fable.Cli/ProjectCracker.fs b/src/Fable.Cli/ProjectCracker.fs index b522881bb4..5854b3a222 100644 --- a/src/Fable.Cli/ProjectCracker.fs +++ b/src/Fable.Cli/ProjectCracker.fs @@ -4,6 +4,7 @@ module Fable.Cli.ProjectCracker open System open System.Xml.Linq +open System.Text.Json open System.Collections.Generic open FSharp.Compiler.CodeAnalysis open Fable @@ -425,26 +426,18 @@ let createFableDir (opts: CrackerOptions) = let baseDir = opts.OutDir |> Option.defaultWith (fun () -> IO.Path.GetDirectoryName(opts.ProjFile)) IO.Path.Combine(baseDir, Naming.fableHiddenDir) - let compilerInfo = IO.Path.Combine(fableDir, "compiler_info.txt") - let newInfo = - Map [ - "version", Literals.VERSION - "define", opts.FableOptions.Define |> List.sort |> String.concat "," - "debug", opts.FableOptions.DebugMode.ToString() - "typedArrays", opts.FableOptions.TypedArrays.ToString() - "clampByteArrays", opts.FableOptions.ClampByteArrays.ToString() - "optimize", opts.FableOptions.OptimizeFSharpAst.ToString() - ] + let jsonOptions = JsonSerializerOptions() + jsonOptions.Converters.Add(Serialization.JsonFSharpConverter()) + let compilerInfoPath = IO.Path.Combine(fableDir, "compiler_info.txt") + let newInfo = {| version = Literals.VERSION; options = opts.FableOptions |} let isEmptyOrOutdated = if opts.NoCache || isDirectoryEmpty fableDir then true else let isOutdated = try - IO.File.ReadLines(compilerInfo) - |> Seq.map (fun line -> let parts = line.Split("=") in parts.[0], parts.[1]) - |> Map - |> fun oldInfo -> oldInfo <> newInfo + let oldInfo = IO.File.ReadAllText(compilerInfoPath) + JsonSerializer.Deserialize(oldInfo, jsonOptions) <> newInfo with _ -> true if isOutdated then IO.Directory.Delete(fableDir, true) @@ -454,7 +447,7 @@ let createFableDir (opts: CrackerOptions) = if IO.Directory.Exists(fableDir) then IO.Directory.Delete(fableDir, true) IO.Directory.CreateDirectory(fableDir) |> ignore - IO.File.WriteAllLines(compilerInfo, newInfo |> Seq.map (fun kv -> kv.Key + "=" + kv.Value)) + IO.File.WriteAllText(compilerInfoPath, JsonSerializer.Serialize(newInfo, jsonOptions)) IO.File.WriteAllText(IO.Path.Combine(fableDir, ".gitignore"), "**/*") fableDir diff --git a/src/Fable.Transforms/Global/Compiler.fs b/src/Fable.Transforms/Global/Compiler.fs index 3733b7cfad..98e9e88cf9 100644 --- a/src/Fable.Transforms/Global/Compiler.fs +++ b/src/Fable.Transforms/Global/Compiler.fs @@ -14,17 +14,17 @@ type CompilerOptionsHelper = ?fileExtension, ?clampByteArrays, ?rootModule) = - let define = defaultArg define [] - { new CompilerOptions with - member _.Define = define - member _.DebugMode = defaultArg debugMode true - member _.Language = defaultArg language JavaScript - member _.TypedArrays = defaultArg typedArrays true - member _.OptimizeFSharpAst = defaultArg optimizeFSharpAst false - member _.RootModule = defaultArg rootModule false - member _.Verbosity = defaultArg verbosity Verbosity.Normal - member _.FileExtension = defaultArg fileExtension CompilerOptionsHelper.DefaultExtension - member _.ClampByteArrays = defaultArg clampByteArrays false } + { + CompilerOptions.Define = defaultArg define [] + DebugMode = defaultArg debugMode true + Language = defaultArg language JavaScript + TypedArrays = defaultArg typedArrays true + OptimizeFSharpAst = defaultArg optimizeFSharpAst false + RootModule = defaultArg rootModule false + Verbosity = defaultArg verbosity Verbosity.Normal + FileExtension = defaultArg fileExtension CompilerOptionsHelper.DefaultExtension + ClampByteArrays = defaultArg clampByteArrays false + } [] type Severity =