Skip to content

Commit

Permalink
Apply latest fixes frim experiment branch
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jan 29, 2024
1 parent 48dfd2b commit a30f19c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 17 deletions.
65 changes: 50 additions & 15 deletions Fable.Daemon/CoolCatCracking.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,59 @@ let private dotnet_msbuild (fsproj : FullPath) (args : string) : Async<string> =
}
|> Async.AwaitTask

let mkOptionsFromDesignTimeBuildAux (fsproj : FileInfo) (additionalArguments : string) : Async<ProjectOptionsResponse> =
let mkOptionsFromDesignTimeBuildAux (fsproj : FileInfo) (options : CrackerOptions) : Async<ProjectOptionsResponse> =
async {
let! targetFrameworkJson =
dotnet_msbuild fsproj.FullName "--getProperty:TargetFrameworks --getProperty:TargetFramework"

let targetFramework =
let tf, tfs =
let configuration =
if String.IsNullOrWhiteSpace options.Configuration then
""
else
$"/p:Configuration=%s{options.Configuration}"

dotnet_msbuild
fsproj.FullName
$"{configuration} --getProperty:TargetFrameworks --getProperty:TargetFramework --getProperty:DefineConstants"

let defineConstants, targetFramework =
let properties =
JsonDocument.Parse targetFrameworkJson
|> fun json -> json.RootElement.GetProperty "Properties"
|> fun properties ->
properties.GetProperty("TargetFramework").GetString (),
properties.GetProperty("TargetFrameworks").GetString ()

let defineConstants =
properties.GetProperty("DefineConstants").GetString().Split ';'
|> Array.filter (fun c -> c <> "DEBUG" || c <> "RELEASE")

let tf, tfs =
properties.GetProperty("TargetFramework").GetString (),
properties.GetProperty("TargetFrameworks").GetString ()

if not (String.IsNullOrWhiteSpace tf) then
tf
defineConstants, tf
else
tfs.Split ';' |> Array.head
defineConstants, tfs.Split ';' |> Array.head

let defines =
[
"TRACE"
if not (String.IsNullOrWhiteSpace options.Configuration) then
options.Configuration.ToUpper ()
yield! defineConstants
yield! options.FableOptions.Define
]

|> List.map (fun s -> s.Trim ())
// Escaped `;`
|> String.concat "%3B"

let version = DateTime.UtcNow.Ticks % 3600L

let properties =
[
"/p:Telplin=True"
"/p:VitePlugin=True"
if not (String.IsNullOrWhiteSpace options.Configuration) then
$"/p:Configuration=%s{options.Configuration}"
if not (String.IsNullOrWhiteSpace defines) then
$"/p:DefineConstants=\"%s{defines}\""
$"/p:TargetFramework=%s{targetFramework}"
"/p:DesignTimeBuild=True"
"/p:SkipCompilerExecution=True"
Expand All @@ -90,7 +120,7 @@ let mkOptionsFromDesignTimeBuildAux (fsproj : FileInfo) (additionalArguments : s
"/p:RestoreLockedMode=false"
"/p:RestorePackagesWithLockFile=false"
// We trick NuGet into believing there is no lock file create, if it does exist it will try and create it.
" /p:NuGetLockFilePath=Telplin.lock"
" /p:NuGetLockFilePath=VitePlugin.lock"
// Pass in a fake version to avoid skipping the CoreCompile target
$"/p:Version=%i{version}"
]
Expand All @@ -101,7 +131,7 @@ let mkOptionsFromDesignTimeBuildAux (fsproj : FileInfo) (additionalArguments : s
"ResolveAssemblyReferencesDesignTime,ResolveProjectReferencesDesignTime,ResolvePackageDependenciesDesignTime,FindReferenceAssembliesForReferences,_GenerateCompileDependencyCache,_ComputeNonExistentFileProperty,BeforeBuild,BeforeCompile,CoreCompile"

let arguments =
$"/restore /t:%s{targets} %s{properties} --getItem:FscCommandLineArgs %s{additionalArguments} --getItem:ProjectReference --getProperty:OutputType"
$"/restore /t:%s{targets} %s{properties} -warnAsMessage:NU1608 --getItem:FscCommandLineArgs --getItem:ProjectReference --getProperty:OutputType"

let! json = dotnet_msbuild fsproj.FullName arguments
let jsonDocument = JsonDocument.Parse json
Expand All @@ -123,7 +153,11 @@ let mkOptionsFromDesignTimeBuildAux (fsproj : FileInfo) (additionalArguments : s

let projectReferences =
items.GetProperty("ProjectReference").EnumerateArray ()
|> Seq.map (fun arg -> arg.GetProperty("FullPath").GetString ())
|> Seq.map (fun arg ->
let relativePath = arg.GetProperty("Identity").GetString ()

Path.Combine (fsproj.DirectoryName, relativePath) |> Path.GetFullPath
)
|> Seq.toArray

let outputType = properties.GetProperty("OutputType").GetString ()
Expand All @@ -146,7 +180,8 @@ let coolCatResolver : ProjectCrackerResolver =
invalidArg (nameof fsproj) $"\"%s{fsproj.FullName}\" does not exist."

// Bad I know...
let result = mkOptionsFromDesignTimeBuildAux fsproj "" |> Async.RunSynchronously
let result =
mkOptionsFromDesignTimeBuildAux fsproj options |> Async.RunSynchronously

result
}
48 changes: 47 additions & 1 deletion cracking.fsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#I "./artifacts/bin/Fable.Daemon/debug"
#r "Fable.AST.dll"
#r "Fable.Compiler.dll"
#r "Fable.Daemon.dll"

open System.IO
open Fable.Compiler.Util
open Fable.Compiler.ProjectCracker
open Fable.Daemon.CoolCatCracking

Expand All @@ -11,8 +14,51 @@ fsi.AddPrinter (fun (x : ProjectOptionsResponse) ->

let fsproj = "/home/nojaf/projects/vite-plugin-fable/sample-project/App.fsproj"

let cliArgs : CliArgs =
{
ProjectFile = fsproj
RootDir = __SOURCE_DIRECTORY__
OutDir = None
IsWatch = false
Precompile = false
PrecompiledLib = None
PrintAst = false
FableLibraryPath = Some (Path.Combine (__SOURCE_DIRECTORY__, "sample-project/node_modules/fable-library"))
Configuration = "Debug"
NoRestore = false
NoCache = true
NoParallelTypeCheck = false
SourceMaps = false
SourceMapsRoot = None
Exclude = []
Replace = Map.empty
RunProcess = None
CompilerOptions =
{
TypedArrays = true
ClampByteArrays = false
Language = Fable.Language.JavaScript
Define = [ "FABLE_COMPILER" ; "FABLE_COMPILER_4" ; "FABLE_COMPILER_JAVASCRIPT" ]
DebugMode = true
OptimizeFSharpAst = false
Verbosity = Fable.Verbosity.Verbose
FileExtension = ".fs"
TriggeredByDependency = false
NoReflection = false
}
Verbosity = Fable.Verbosity.Verbose
}

let options : CrackerOptions = CrackerOptions cliArgs

#time "on"

let result =
coolCatResolver.GetProjectOptionsFromProjectFile (true, Unchecked.defaultof<CrackerOptions>, fsproj)
coolCatResolver.GetProjectOptionsFromProjectFile (true, options, fsproj)

#time "off"

result.ProjectReferences

for option in result.ProjectOptions do
printfn "%s" option
2 changes: 1 addition & 1 deletion sample-project/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open Thoth.Json
let r = sum 1 7

let someJsonString =
Encode.object [ "track", Encode.string "Smash Shit Up" ] |> Encode.toString 4
Encode.object [ "track", Encode.string "Scars" ] |> Encode.toString 4

let h1Element = document.querySelector "h1"
h1Element.textContent <- $"Dynamic Fable text %i{r}! %s{someJsonString}"

0 comments on commit a30f19c

Please sign in to comment.