Skip to content

Commit

Permalink
Find dependent files after compilation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Oct 25, 2023
1 parent b12508d commit 9bbf460
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/Fable.Compiler.Service/Fable.Compiler.Service.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
</ItemGroup>

<ItemGroup>
<Reference Include="../../lib/fcs/FSharp.Core.dll"/>
<Reference Include="../../lib/fcs/FSharp.Compiler.Service.dll"/>
<Reference Include="C:\Users\nojaf\Projects\fsharp\artifacts\bin\FSharp.Core\Release\netstandard2.0\FSharp.Core.dll" />
<Reference Include="C:\Users\nojaf\Projects\fsharp\artifacts\bin\FSharp.Compiler.Service\Release\netstandard2.0\FSharp.Compiler.Service.dll" />
<!-- <Reference Include="../../lib/fcs/FSharp.Core.dll"/>-->
<!-- <Reference Include="../../lib/fcs/FSharp.Compiler.Service.dll"/>-->
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 24 additions & 7 deletions src/Fable.Compiler.Service/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ type BabelWriter
let sb = StringBuilder()
// let mapGenerator = lazy (SourceMapSharp.SourceMapGenerator(?sourceRoot = cliArgs.SourceMapsRoot))

override x.ToString() =
sb.ToString()
override x.ToString() = sb.ToString()

interface Printer.Writer with
// Don't dispose the stream here because we need to access the memory stream to check if file has changed
Expand Down Expand Up @@ -98,18 +97,23 @@ 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
(cliArgs: CliArgs)
(crackerResponse: CrackerResponse)
(currentFile: string)
: Async<Compiler> =
async {
let checker = InteractiveChecker.Create(crackerResponse.ProjectOptions)
checker <- InteractiveChecker.Create(crackerResponse.ProjectOptions)
let! assemblies = checker.GetImportedAssemblies()

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

1, lazy source
Expand All @@ -119,7 +123,7 @@ let mkCompilerForFile
cliArgs.ProjectFile,
crackerResponse.ProjectOptions.SourceFiles,
sourceReader,
Array.last crackerResponse.ProjectOptions.SourceFiles
currentFile
)

ignore checkProjectResult.Diagnostics
Expand Down Expand Up @@ -172,5 +176,18 @@ let compileFile (com: Compiler) (pathResolver: PathResolver) (outPath: string) =
)

do! BabelPrinter.run writer babel
return writer.ToString()
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 = checker.GetDependentFiles(com.CurrentFile, com.SourceFiles, sourceReader)

return output, dependentFiles
}
2 changes: 1 addition & 1 deletion src/Fable.Compiler.Service/Library.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ 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>
val compileFile: com: Compiler -> pathResolver: PathResolver -> outPath: string -> Async<string * string array>
6 changes: 4 additions & 2 deletions tests/FCSTest/FCSTest.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
</ItemGroup>

<ItemGroup>
<Reference Include="../../lib/fcs/FSharp.Core.dll"/>
<Reference Include="../../lib/fcs/FSharp.Compiler.Service.dll"/>
<Reference Include="C:\Users\nojaf\Projects\fsharp\artifacts\bin\FSharp.Core\Release\netstandard2.0\FSharp.Core.dll" />
<Reference Include="C:\Users\nojaf\Projects\fsharp\artifacts\bin\FSharp.Compiler.Service\Release\netstandard2.0\FSharp.Compiler.Service.dll" />
<!-- <Reference Include="../../lib/fcs/FSharp.Core.dll"/>-->
<!-- <Reference Include="../../lib/fcs/FSharp.Compiler.Service.dll"/>-->
</ItemGroup>

</Project>
15 changes: 10 additions & 5 deletions tests/FCSTest/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ module CoolCatProjectCracking =
|> Array.choose (fun (line: string) ->
let filePath = Path.Combine(projectDir, line)

if isFSharpFile line && File.Exists filePath then
Some filePath
if
isFSharpFile line
&& File.Exists filePath
&& not (filePath.Contains "obj")
then
Some (Path.normalizeFullPath filePath)
else
None
)
Expand Down Expand Up @@ -170,7 +174,7 @@ let compilerForFile =
mkCompilerForFile
cliArgs
crackerResponse
@"C:\Users\nojaf\Projects\MyFableApp\App.fs"
(Path.normalizeFullPath @"C:\Users\nojaf\Projects\MyFableApp\Lib.fs")
|> Async.RunSynchronously

let dummyPathResolver =
Expand All @@ -179,11 +183,12 @@ let dummyPathResolver =
member _.GetOrAddDeduplicateTargetDir(_importDir, _addTargetDir) = ""
}

let javascript =
let javascript, dependentFiles =
compileFile
compilerForFile
dummyPathResolver
@"C:\Users\nojaf\Projects\MyFableApp\App.js"
@"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)

0 comments on commit 9bbf460

Please sign in to comment.