Skip to content

Commit

Permalink
GetDependentFiles from Compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Oct 28, 2023
1 parent 65029a7 commit 5abe564
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 42 deletions.
29 changes: 5 additions & 24 deletions src/Fable.Cli/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,6 @@ type FsWatcher(delayMs: int) =
|> Observable.throttle delayMs
|> Observable.map caseInsensitiveSet

// TODO: Check the path is actually normalized?
type File(normalizedFullPath: string) =
let mutable sourceHash = None
member _.NormalizedFullPath = normalizedFullPath
member _.ReadSource() =
match sourceHash with
| Some h -> h, lazy File.readAllTextNonBlocking normalizedFullPath
| _ ->
let source = File.readAllTextNonBlocking normalizedFullPath
let h = hash source
sourceHash <- Some h
h, lazy source

static member MakeSourceReader (files: File[]) =
let fileDic =
files
|> Seq.map (fun f -> f.NormalizedFullPath, f) |> dict
let sourceReader f = fileDic[f].ReadSource()
files |> Array.map (fun file -> file.NormalizedFullPath), sourceReader

type ProjectCracked(cliArgs: CliArgs, crackerResponse: CrackerResponse, sourceFiles: File array) =

member _.CliArgs = cliArgs
Expand All @@ -311,7 +291,7 @@ type ProjectCracked(cliArgs: CliArgs, crackerResponse: CrackerResponse, sourceFi
member _.FableLibDir = crackerResponse.FableLibDir
member _.FableModulesDir = crackerResponse.FableModulesDir

member _.MakeCompiler(currentFile, project, ?triggeredByDependency) =
member _.MakeCompiler(checker: InteractiveChecker, currentFile, project, ?triggeredByDependency) =
let opts =
match triggeredByDependency with
| Some t -> { cliArgs.CompilerOptions with TriggeredByDependency = t }
Expand All @@ -320,7 +300,7 @@ type ProjectCracked(cliArgs: CliArgs, crackerResponse: CrackerResponse, sourceFi
let fableLibDir = Path.getRelativePath currentFile crackerResponse.FableLibDir
let watchDependencies = if cliArgs.IsWatch then Some(HashSet()) else None

CompilerImpl(currentFile, project, opts, fableLibDir, crackerResponse.OutputType,
CompilerImpl(checker, currentFile, project, opts, fableLibDir, crackerResponse.OutputType,
?outDir=cliArgs.OutDir, ?watchDependencies=watchDependencies)

member _.MapSourceFiles(f) =
Expand Down Expand Up @@ -399,7 +379,7 @@ type FableCompilerState = {
ReplyChannel = replyChannel
}

and FableCompiler(projCracked: ProjectCracked, fableProj: Project, checker: InteractiveChecker) =
and FableCompiler(checker: InteractiveChecker, projCracked: ProjectCracked, fableProj: Project) =
let agent =
MailboxProcessor<FableCompilerMsg>.Start(fun agent ->
let startInThreadPool toMsg work =
Expand All @@ -415,6 +395,7 @@ and FableCompiler(projCracked: ProjectCracked, fableProj: Project, checker: Inte
let fableProj = state.FableProj
startInThreadPool FableFileCompiled (fun () -> async {
let com = projCracked.MakeCompiler(
checker,
fileName,
fableProj,
triggeredByDependency = state.TriggeredByDependency(fileName)
Expand Down Expand Up @@ -559,7 +540,7 @@ and FableCompiler(projCracked: ProjectCracked, fableProj: Project, checker: Inte
?precompiledInfo = (projCracked.PrecompiledInfo |> Option.map (fun i -> i :> _)),
getPlugin = loadType projCracked.CliArgs
)
return FableCompiler(projCracked, fableProj, checker)
return FableCompiler(checker, projCracked, fableProj)
}

member _.CompileToFile(outFile: string) =
Expand Down
14 changes: 4 additions & 10 deletions src/Fable.Compiler.Service/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,13 @@ type BabelWriter
// let sourcePath = defaultArg file sourcePath |> Path.getRelativeFileOrDirPath false targetPath false
// mapGenerator.Force().AddMapping(generated, original, source=sourcePath, ?name=displayName)

// TODO: real bad code, refactor
let mutable checker = Unchecked.defaultof<InteractiveChecker>

let mkCompilerForFile
(checker: InteractiveChecker)
(cliArgs: CliArgs)
(crackerResponse: CrackerResponse)
(currentFile: string)
: Async<Compiler> =
async {
checker <- InteractiveChecker.Create(crackerResponse.ProjectOptions)
let! assemblies = checker.GetImportedAssemblies()

let sourceReader fileName =
Expand Down Expand Up @@ -144,18 +141,15 @@ let mkCompilerForFile
let fableLibDir =
Path.getRelativePath currentFile crackerResponse.FableLibDir

let watchDependencies = None
// if cliArgs.IsWatch then Some(HashSet()) else None

return
CompilerImpl(
checker,
currentFile,
fableProj,
opts,
fableLibDir,
crackerResponse.OutputType,
?outDir = cliArgs.OutDir,
?watchDependencies = watchDependencies
?outDir = cliArgs.OutDir
)
}

Expand Down Expand Up @@ -187,7 +181,7 @@ let compileFile (com: Compiler) (pathResolver: PathResolver) (outPath: string) =

1, lazy source

let! dependentFiles = checker.GetDependentFiles(com.CurrentFile, com.SourceFiles, sourceReader)
let! dependentFiles = com.GetDependentFiles()

return output, dependentFiles
}
15 changes: 13 additions & 2 deletions src/Fable.Compiler.Service/Library.fsi
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
module Fable.Compiler.Service.Compiler

open FSharp.Compiler.SourceCodeServices
open Fable
open Fable.Compiler.Service.Util
open Fable.Compiler.Service.ProjectCracker

val mkCompilerForFile: cliArgs: CliArgs -> crackerResponse: CrackerResponse -> currentFile: string -> Async<Compiler>
val compileFile: com: Compiler -> pathResolver: PathResolver -> outPath: string -> Async<string * string array>
val mkCompilerForFile:
checker: InteractiveChecker ->
cliArgs: CliArgs ->
crackerResponse: CrackerResponse ->
currentFile: string ->
Async<Compiler>

val compileFile:
com: Compiler ->
pathResolver: PathResolver ->
outPath: string ->
Async<string * string array>
1 change: 1 addition & 0 deletions src/Fable.Transforms/Dart/Fable2Dart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2284,6 +2284,7 @@ module Compiler =
member _.AddWatchDependency(fileName) = com.AddWatchDependency(fileName)
member _.AddLog(msg, severity, ?range, ?fileName:string, ?tag: string) =
com.AddLog(msg, severity, ?range=range, ?fileName=fileName, ?tag=tag)
member _.GetDependentFiles () = com.GetDependentFiles()

let makeCompiler com = DartCompiler(com)

Expand Down
1 change: 1 addition & 0 deletions src/Fable.Transforms/FSharp2Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,6 +1934,7 @@ type FableCompiler(com: Compiler) =
member _.AddWatchDependency(fileName) = com.AddWatchDependency(fileName)
member _.AddLog(msg, severity, ?range, ?fileName:string, ?tag: string) =
com.AddLog(msg, severity, ?range=range, ?fileName=fileName, ?tag=tag)
member _.GetDependentFiles () = com.GetDependentFiles()


let rec attachClassMembers (com: FableCompiler) = function
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Transforms/Fable.Transforms.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<OtherFlags>$(OtherFlags) --nowarn:3536</OtherFlags>
</PropertyGroup>
<ItemGroup>
<Compile Include="File.fs" />
<Compile Include="Global/Babel.fs" />
<Compile Include="Global/Fable.Core.fs" />
<Compile Include="Global/Metadata.fs" />
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3164,6 +3164,7 @@ module Compiler =
member _.AddWatchDependency(fileName) = com.AddWatchDependency(fileName)
member _.AddLog(msg, severity, ?range, ?fileName:string, ?tag: string) =
com.AddLog(msg, severity, ?range=range, ?fileName=fileName, ?tag=tag)
member _.GetDependentFiles () = com.GetDependentFiles()

let makeCompiler com = BabelCompiler(com)

Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/FableTransforms.fs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ let rec transformDeclaration transformations (com: Compiler) file decl =
AttachedMembers = attachedMembers }
|> ClassDeclaration

let transformFile (com: Compiler) (file: File) =
let transformFile (com: Compiler) (file: Fable.AST.Fable.File) =
let transformations = getTransformations com
let newDecls = List.map (transformDeclaration transformations com file) file.Declarations
File(newDecls, usedRootNames=file.UsedNamesInRootScope)
Fable.AST.Fable.File(newDecls, usedRootNames=file.UsedNamesInRootScope)
41 changes: 41 additions & 0 deletions src/Fable.Transforms/File.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Fable.Transforms

open System.IO

// TODO: Check the path is actually normalized?
type File(normalizedFullPath: string) =
let mutable sourceHash = None

let readAllTextNonBlocking (path: string) =
if File.Exists(path) then
use fileStream =
new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite
)

use textReader = new StreamReader(fileStream)
textReader.ReadToEnd()
else
// Log.always("File does not exist: " + path)
""

member _.NormalizedFullPath = normalizedFullPath

member _.ReadSource() =
match sourceHash with
| Some h -> h, lazy readAllTextNonBlocking normalizedFullPath
| _ ->
let source = readAllTextNonBlocking normalizedFullPath
let h = hash source
sourceHash <- Some h
h, lazy source

static member MakeSourceReader(files: File array) =
let fileDic =
files |> Seq.map (fun f -> f.NormalizedFullPath, f) |> dict

let sourceReader f = fileDic[f].ReadSource()
files |> Array.map (fun file -> file.NormalizedFullPath), sourceReader
4 changes: 4 additions & 0 deletions src/Fable.Transforms/Global/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ type Compiler =
abstract AddLog: msg:string * severity: Severity * ?range: SourceLocation
* ?fileName:string * ?tag: string -> unit

/// Invokes InteractiveChecker.GetDependentFiles
/// This will find dependent file via the untyped tree graph.
abstract GetDependentFiles: unit -> Async<string array>

type InlineExprLazy(f: Compiler -> InlineExpr) =
let mutable value: InlineExpr voption = ValueNone
member this.Calculate(com: Compiler) =
Expand Down
2 changes: 2 additions & 0 deletions src/Fable.Transforms/Php/Fable2Php.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,8 @@ type PhpCompiler(com: Fable.Compiler) =
member this.FindLableLevel(label) =
List.findIndex(function Some v when v = label -> true | _ -> false) breakable

member _.GetDependentFiles () = com.GetDependentFiles()

module Compiler =

let transformFile com (file: Fable.File) =
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4182,6 +4182,7 @@ module Compiler =

member _.AddLog(msg, severity, ?range, ?fileName: string, ?tag: string) =
com.AddLog(msg, severity, ?range = range, ?fileName = fileName, ?tag = tag)
member _.GetDependentFiles () = com.GetDependentFiles()

let makeCompiler com = PythonCompiler(com)

Expand Down
2 changes: 2 additions & 0 deletions src/Fable.Transforms/Rust/Fable2Rust.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4315,6 +4315,8 @@ module Compiler =
member _.AddLog(msg, severity, ?range, ?fileName:string, ?tag: string) =
com.AddLog(msg, severity, ?range=range, ?fileName=fileName, ?tag=tag)

member _.GetDependentFiles () = com.GetDependentFiles()

let makeCompiler com = RustCompiler(com)

let transformFile (com: Fable.Compiler) (file: Fable.File) =
Expand Down
24 changes: 20 additions & 4 deletions src/Fable.Transforms/State.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Fable.Transforms.State

open FSharp.Compiler.SourceCodeServices
open Fable
open Fable.AST
open System.Collections.Concurrent
Expand Down Expand Up @@ -237,9 +238,20 @@ type Log =

/// Type with utilities for compiling F# files to JS.
/// Not thread-safe, an instance must be created per file
type CompilerImpl(currentFile, project: Project, options, fableLibDir: string, ?outType: OutputType, ?outDir: string,
?watchDependencies: HashSet<string>, ?logs: ResizeArray<Log>, ?isPrecompilingInlineFunction: bool) =

type CompilerImpl
(
checker: InteractiveChecker,
currentFile,
project: Project,
options,
fableLibDir: string,
?outType: OutputType,
?outDir: string,
?watchDependencies: HashSet<string>,
?logs: ResizeArray<Log>,
?isPrecompilingInlineFunction: bool
)
=
let mutable counter = -1
let outType = defaultArg outType OutputType.Exe
let logs = Option.defaultWith ResizeArray logs
Expand Down Expand Up @@ -272,7 +284,7 @@ type CompilerImpl(currentFile, project: Project, options, fableLibDir: string, ?
Path.Combine(Path.GetDirectoryName(currentFile), fableLibraryDir)
else fableLibraryDir
|> Path.getRelativeFileOrDirPath false file true
CompilerImpl(file, project, options, fableLibraryDir, outType, ?outDir=outDir,
CompilerImpl(checker, file, project, options, fableLibraryDir, outType, ?outDir=outDir,
?watchDependencies=watchDependencies, logs=logs, isPrecompilingInlineFunction=true)

member _.GetImplementationFile(fileName) =
Expand Down Expand Up @@ -324,3 +336,7 @@ type CompilerImpl(currentFile, project: Project, options, fableLibDir: string, ?
member _.AddLog(msg, severity, ?range, ?fileName:string, ?tag: string) =
Log.Make(severity, msg, ?range=range, ?fileName=fileName, ?tag=tag)
|> logs.Add

member _.GetDependentFiles () =
let sourceReader = File.MakeSourceReader (Array.map File project.SourceFiles) |> snd
checker.GetDependentFiles (currentFile, project.SourceFiles, sourceReader)
4 changes: 4 additions & 0 deletions tests/FCSTest/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
open System.Diagnostics
open System.IO
open System.Text.Json
open FSharp.Compiler.SourceCodeServices
open FSharp.Compiler.CodeAnalysis
open Fable.Compiler.Service.Compiler
open Fable
Expand Down Expand Up @@ -170,8 +171,11 @@ let cliArgs: CliArgs = {
}
}

let checker = InteractiveChecker.Create(crackerResponse.ProjectOptions)

let compilerForFile =
mkCompilerForFile
checker
cliArgs
crackerResponse
(Path.normalizeFullPath @"C:\Users\nojaf\Projects\MyFableApp\Lib.fs")
Expand Down

0 comments on commit 5abe564

Please sign in to comment.