Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
safesparrow committed Oct 16, 2022
1 parent b1ebfdb commit b461abb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
52 changes: 48 additions & 4 deletions tests/FSharp.Compiler.Service.Tests2/DepResolving.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
module FSharp.Compiler.Service.Tests.DepResolving

open System
open System.Collections.Generic
open Buildalyzer
open FSharp.Compiler.Service.Tests2.SyntaxTreeTests.TypeTests
open FSharp.Compiler.Syntax
open NUnit.Framework

let log (msg : string) =
let d = DateTime.Now.ToString("HH:mm:ss.fff")
printfn $"{d} {msg}"

/// File * AST
type FileAST = string * ParsedInput

Expand Down Expand Up @@ -122,12 +128,13 @@ let rec searchInTrie (trie : TrieNode) (path : LongIdent) : TrieNode option =
| false, _ ->
None

let detectFileDependencies (nodes : FileAST list) : Graph =
let detectFileDependencies (nodes : FileAST[]) : Graph =
// Create ASTs, extract module refs
let nodes =
nodes
|> List.mapi (fun i (name, ast) ->
let typeAndModuleRefs = visit ast
|> Array.mapi (fun i (name, ast) ->
printfn $"Visiting {name}"
let typeAndModuleRefs = visit ast
let top = topModuleOrNamespace ast
let moduleRefs = extractModuleSegments typeAndModuleRefs
{
Expand All @@ -138,7 +145,6 @@ let detectFileDependencies (nodes : FileAST list) : Graph =
Idx = i
}
)
|> List.toArray

let trie = buildTrie nodes

Expand Down Expand Up @@ -282,9 +288,47 @@ type B = int
let nodes =
files
|> List.map (fun (name, code) -> name, getParseResults code)
|> List.toArray

let graph = detectFileDependencies nodes

printfn "Detected file dependencies:"
graph
|> Array.iter (fun (file, deps) -> printfn $"{file} -> %+A{deps}")

[<Test>]
let Test () =
log "start"
let m = AnalyzerManager()
let analyzer = m.GetProject(@"C:\projekty\fsharp\fsharp_main\src\Compiler\FSharp.Compiler.Service.fsproj")
let results = analyzer.Build()
log "built"

let res = results.Results |> Seq.head
let files = res.SourceFiles
let files =
files
|> Array.take 3
|> Array.Parallel.map (fun f -> f, System.IO.File.ReadAllText(f))
|> Array.chunkBySize 5
|> Array.map (fun chunk ->
chunk
|> Array.Parallel.map (fun (f, code) ->
printfn $"Parsing {f}"
let ast = getParseResults code
f, ast
)
)
|> Array.concat
let N = files.Length
log $"{N} files read"

let graph = detectFileDependencies files
log "deps detected"

let totalDeps = graph |> Array.sumBy (fun (f, deps) -> deps.Length)
let maxPossibleDeps = (N * (N-1)) / 2
printfn $"Analysed {N} files, detected {totalDeps}/{maxPossibleDeps} file dependencies:"
graph
|> Array.iter (fun (file, deps) -> printfn $"{file} -> %+A{deps}")
()
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<!-- Force a newer Newtonsoft.Json version to avoid conflicts. -->
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" />
<PackageReference Include="Buildalyzer" Version="4.1.2" />
<ProjectReference Include="..\..\src\Compiler\FSharp.Compiler.Service.fsproj" />
<ProjectReference Include="..\..\tests\FSharp.Test.Utilities\FSharp.Test.Utilities.fsproj" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion tests/FSharp.Compiler.Service.Tests2/TypeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,12 +1018,13 @@ and visitSynModuleOrNamespace (x : SynModuleOrNamespace) : Stuff =
yield! visitSynAttributeLists synAttributeLists
}

and visit (input : ParsedInput) : Stuff =
and visit (input : ParsedInput) =
match input with
| ParsedInput.SigFile _ -> failwith "Signature files are not currently supported"
| ParsedInput.ImplFile(ParsedImplFileInput(fileName, isScript, qualifiedNameOfFile, scopedPragmas, parsedHashDirectives, synModuleOrNamespaces, flags, parsedImplFileInputTrivia)) ->
synModuleOrNamespaces
|> Seq.collect visitSynModuleOrNamespace
|> Seq.toArray

let topModuleOrNamespace (input : ParsedInput) =
match input with
Expand Down
3 changes: 2 additions & 1 deletion tests/service/Program.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@


[<EntryPoint>]
let main argv =
printfn "Dotnet Core NUnit Tests..."
FSharp.Compiler.Service.Tests.DepResolving.Test()
0

0 comments on commit b461abb

Please sign in to comment.