Skip to content

Commit

Permalink
Serialize compiler_info as json
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsogarciacaro committed Sep 27, 2021
1 parent 0715db7 commit aaf9d03
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
20 changes: 11 additions & 9 deletions src/Fable.AST/Plugins.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Cli/Fable.Cli.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Dotnet.ProjInfo" Version="0.44.0" />
<PackageReference Include="FSharp.SystemTextJson" Version="0.17.4" />
<PackageReference Include="source-map-sharp" Version="1.0.7" />
<PackageReference Include="FSharp.Core" Version="5.0.1" />
</ItemGroup>
Expand Down
23 changes: 8 additions & 15 deletions src/Fable.Cli/ProjectCracker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions src/Fable.Transforms/Global/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

[<RequireQualifiedAccess>]
type Severity =
Expand Down

0 comments on commit aaf9d03

Please sign in to comment.