Skip to content

Commit

Permalink
Pass sourceReader as dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Oct 28, 2023
1 parent 5abe564 commit de10464
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 35 deletions.
31 changes: 8 additions & 23 deletions src/Fable.Compiler.Service/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ open System
open System.Text
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.SourceCodeServices
open FSharp.Compiler.Text
open Fable
open Fable.Compiler.Service.Util
open Fable.Transforms.State
Expand Down Expand Up @@ -98,6 +97,7 @@ type BabelWriter
// mapGenerator.Force().AddMapping(generated, original, source=sourcePath, ?name=displayName)

let mkCompilerForFile
(sourceReader: SourceReader)
(checker: InteractiveChecker)
(cliArgs: CliArgs)
(crackerResponse: CrackerResponse)
Expand All @@ -106,15 +106,6 @@ let mkCompilerForFile
async {
let! assemblies = checker.GetImportedAssemblies()

let sourceReader fileName =
let source =
Array.find
(fun sourceFile -> sourceFile = fileName)
crackerResponse.ProjectOptions.SourceFiles
|> System.IO.File.ReadAllText

1, lazy source

let! checkProjectResult =
checker.ParseAndCheckProject(
cliArgs.ProjectFile,
Expand Down Expand Up @@ -153,7 +144,12 @@ let mkCompilerForFile
)
}

let compileFile (com: Compiler) (pathResolver: PathResolver) (outPath: string) =
let compileFile
(sourceReader: SourceReader)
(com: Compiler)
(pathResolver: PathResolver)
(outPath: string)
=
async {
let babel =
FSharp2Fable.Compiler.transformFile com
Expand All @@ -171,17 +167,6 @@ let compileFile (com: Compiler) (pathResolver: PathResolver) (outPath: string) =

do! BabelPrinter.run writer babel
let output = writer.ToString()

let sourceReader fileName =
let source =
Array.find
(fun sourceFile -> sourceFile = fileName)
com.SourceFiles
|> System.IO.File.ReadAllText

1, lazy source

let! dependentFiles = com.GetDependentFiles()

let! dependentFiles = com.GetDependentFiles sourceReader
return output, dependentFiles
}
2 changes: 2 additions & 0 deletions src/Fable.Compiler.Service/Library.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ open Fable.Compiler.Service.Util
open Fable.Compiler.Service.ProjectCracker

val mkCompilerForFile:
sourceReader:SourceReader ->
checker: InteractiveChecker ->
cliArgs: CliArgs ->
crackerResponse: CrackerResponse ->
currentFile: string ->
Async<Compiler>

val compileFile:
sourceReader:SourceReader ->
com: Compiler ->
pathResolver: PathResolver ->
outPath: string ->
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Dart/Fable2Dart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2284,7 +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()
member _.GetDependentFiles sourceReader = com.GetDependentFiles sourceReader

let makeCompiler com = DartCompiler(com)

Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/FSharp2Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +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()
member _.GetDependentFiles sourceReader = com.GetDependentFiles sourceReader


let rec attachClassMembers (com: FableCompiler) = function
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3164,7 +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()
member _.GetDependentFiles sourceReader = com.GetDependentFiles sourceReader

let makeCompiler com = BabelCompiler(com)

Expand Down
4 changes: 3 additions & 1 deletion src/Fable.Transforms/Global/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type InlineExpr =
type CompilerPlugins =
{ MemberDeclarationPlugins: Map<Fable.EntityRef, System.Type> }

type SourceReader = string -> int * Lazy<string>

type Compiler =
abstract LibraryDir: string
abstract CurrentFile: string
Expand All @@ -73,7 +75,7 @@ type Compiler =

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

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

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

module Compiler =

Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4182,7 +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()
member _.GetDependentFiles sourceReader = com.GetDependentFiles sourceReader

let makeCompiler com = PythonCompiler(com)

Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Rust/Fable2Rust.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4315,7 +4315,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()
member _.GetDependentFiles sourceReader = com.GetDependentFiles sourceReader

let makeCompiler com = RustCompiler(com)

Expand Down
3 changes: 1 addition & 2 deletions src/Fable.Transforms/State.fs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,5 @@ type CompilerImpl
Log.Make(severity, msg, ?range=range, ?fileName=fileName, ?tag=tag)
|> logs.Add

member _.GetDependentFiles () =
let sourceReader = File.MakeSourceReader (Array.map File project.SourceFiles) |> snd
member _.GetDependentFiles sourceReader =
checker.GetDependentFiles (currentFile, project.SourceFiles, sourceReader)
27 changes: 24 additions & 3 deletions tests/FCSTest/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module CoolCatProjectCracking =
&& File.Exists filePath
&& not (filePath.Contains "obj")
then
Some (Path.normalizeFullPath filePath)
Some(Path.normalizeFullPath filePath)
else
None
)
Expand Down Expand Up @@ -122,7 +122,15 @@ module CoolCatProjectCracking =
Directory.Delete(pwd, true)

let fsprojOptions =
CoolCatProjectCracking.mkOptionsFromDesignTimeBuild fsprojPath ""
let sw = Stopwatch()
sw.Start()

let options =
CoolCatProjectCracking.mkOptionsFromDesignTimeBuild fsprojPath ""

sw.Stop()
printfn "Cracking took: %A" sw.Elapsed
options

let crackerResponse: CrackerResponse = {
FableLibDir =
Expand Down Expand Up @@ -173,8 +181,17 @@ let cliArgs: CliArgs = {

let checker = InteractiveChecker.Create(crackerResponse.ProjectOptions)

let sourceReader =
Fable.Transforms.File.MakeSourceReader(
Array.map
Fable.Transforms.File
crackerResponse.ProjectOptions.SourceFiles
)
|> snd

let compilerForFile =
mkCompilerForFile
sourceReader
checker
cliArgs
crackerResponse
Expand All @@ -189,10 +206,14 @@ let dummyPathResolver =

let javascript, dependentFiles =
compileFile
sourceReader
compilerForFile
dummyPathResolver
@"C:\Users\nojaf\Projects\MyFableApp\Lib.js"
|> Async.RunSynchronously

printfn "this is javascript:\n%s" javascript
printfn "these files need to be reprocessed: %s" (String.concat "," dependentFiles)

printfn
"these files need to be reprocessed: %s"
(String.concat "," dependentFiles)

0 comments on commit de10464

Please sign in to comment.