77using Buildalyzer . Workspaces ;
88using System . Collections . Generic ;
99using System ;
10+ using Strazh . Database ;
11+ using static Strazh . Analysis . AnalyzerConfig ;
12+ using System . IO ;
1013
1114namespace Strazh . Analysis
1215{
1316 public static class Analyzer
1417 {
15- private static string GetProjectName ( string fullName )
18+ public static async Task Analyze ( AnalyzerConfig config )
1619 {
17- return fullName . Split ( '\\ ' ) . Last ( ) . Replace ( ".csproj" , "" ) ;
18- }
20+ Console . WriteLine ( $ "Setup analyzer...") ;
1921
20- public static async Task < Triple [ ] > Analyze ( string path )
21- {
22- var triples = new List < Triple > ( ) ;
22+ var manager = config . IsSolutionBased
23+ ? new AnalyzerManager ( config . Solution )
24+ : new AnalyzerManager ( ) ;
2325
24- var manager = new AnalyzerManager ( ) ;
25- var analyzer = manager . GetProject ( path ) ;
26- var workspace = new AdhocWorkspace ( ) ;
27- var project = analyzer . AddToWorkspace ( workspace ) ;
26+ var projectAnalyzers = config . IsSolutionBased
27+ ? manager . Projects . Values
28+ : config . Projects . Select ( x => manager . GetProject ( x ) ) ;
2829
29- var projectBuild = analyzer . Build ( ) . FirstOrDefault ( ) ;
30- var currentNode = new ProjectNode ( GetProjectName ( project . Name ) ) ;
31- projectBuild . ProjectReferences . ToList ( ) . ForEach ( x =>
30+ Console . WriteLine ( $ "Analyzer ready to analyze { projectAnalyzers . Count ( ) } project/s.") ;
31+
32+ var workspace = new AdhocWorkspace ( ) ;
33+ var isDelete = config . IsDelete ;
34+ short index = 1 ;
35+ foreach ( var projectAnalyzer in projectAnalyzers )
3236 {
33- var node = new ProjectNode ( GetProjectName ( x ) ) ;
34- triples . Add ( new TripleDependsOnProject ( currentNode , node ) ) ;
35- } ) ;
36- projectBuild . PackageReferences . ToList ( ) . ForEach ( x =>
37+ var triples = await AnalyzeProject ( index , workspace , projectAnalyzer , config . Tier ) ;
38+ if ( triples . Count > 0 )
39+ {
40+ await DbManager . InsertData ( triples , config . Credentials , isDelete ) ;
41+ }
42+ index ++ ;
43+ isDelete = false ;
44+ }
45+ workspace . Dispose ( ) ;
46+ }
47+
48+ private static async Task < IList < Triple > > AnalyzeProject ( short index , AdhocWorkspace workspace , IProjectAnalyzer projectAnalyzer , Tiers mode )
49+ {
50+ Console . WriteLine ( $ "Project #{ index } :") ;
51+ var project = projectAnalyzer . AddToWorkspace ( workspace ) ;
52+ var root = GetRoot ( project . FilePath ) ;
53+ var rootNode = new FolderNode ( root , root ) ;
54+ var projectName = GetProjectName ( project . Name ) ;
55+ Console . WriteLine ( $ "Analyzing { projectName } project...") ;
56+
57+ var triples = new List < Triple > ( ) ;
58+ if ( mode == Tiers . All || mode == Tiers . Project )
3759 {
38- var version = x . Value . Values . FirstOrDefault ( x => x . Contains ( "." ) ) ?? "none" ;
39- var node = new PackageNode ( x . Key , x . Key , version ) ;
40- triples . Add ( new TripleDependsOnPackage ( currentNode , node ) ) ;
41- } ) ;
42-
43- var compilation = await project . GetCompilationAsync ( ) ;
44- var syntaxTreeRoot = compilation . SyntaxTrees ;
45- foreach ( var st in syntaxTreeRoot )
60+ Console . WriteLine ( $ "Analyzing Project tier...") ;
61+ var projectBuild = projectAnalyzer . Build ( ) . FirstOrDefault ( ) ;
62+ var projectNode = new ProjectNode ( projectName ) ;
63+ triples . Add ( new TripleIncludedIn ( projectNode , rootNode ) ) ;
64+ projectBuild . ProjectReferences . ToList ( ) . ForEach ( x =>
65+ {
66+ var node = new ProjectNode ( GetProjectName ( x ) ) ;
67+ triples . Add ( new TripleDependsOnProject ( projectNode , node ) ) ;
68+ } ) ;
69+ projectBuild . PackageReferences . ToList ( ) . ForEach ( x =>
70+ {
71+ var version = x . Value . Values . FirstOrDefault ( x => x . Contains ( "." ) ) ?? "none" ;
72+ var node = new PackageNode ( x . Key , x . Key , version ) ;
73+ triples . Add ( new TripleDependsOnPackage ( projectNode , node ) ) ;
74+ } ) ;
75+ Console . WriteLine ( $ "Analyzing Project tier complete.") ;
76+ }
77+
78+ if ( project . SupportsCompilation
79+ && ( mode == Tiers . All || mode == Tiers . Code ) )
4680 {
47- var sem = compilation . GetSemanticModel ( st ) ;
48- Extractor . AnalyzeTree < ClassDeclarationSyntax > ( triples , st , sem ) ;
49- Extractor . AnalyzeTree < InterfaceDeclarationSyntax > ( triples , st , sem ) ;
81+ Console . WriteLine ( $ "Analyzing Code tier...") ;
82+ var compilation = await project . GetCompilationAsync ( ) ;
83+ var syntaxTreeRoot = compilation . SyntaxTrees ;
84+ foreach ( var st in syntaxTreeRoot )
85+ {
86+ var sem = compilation . GetSemanticModel ( st ) ;
87+ Extractor . AnalyzeTree < ClassDeclarationSyntax > ( triples , st , sem , rootNode ) ;
88+ Extractor . AnalyzeTree < InterfaceDeclarationSyntax > ( triples , st , sem , rootNode ) ;
89+ }
90+ Console . WriteLine ( $ "Analyzing Code tier complete.") ;
91+ triples = triples . GroupBy ( x => x . ToString ( ) ) . Select ( x => x . First ( ) ) . ToList ( ) ;
5092 }
51- var result = triples . GroupBy ( x => x . ToString ( ) ) . Select ( x => x . First ( ) ) . ToArray ( ) ;
52- Console . WriteLine ( $ "Codebase of project \" { currentNode . Name } \" analyzed with result of { result . Length } triples .") ;
53- return result ;
93+
94+ Console . WriteLine ( $ "Analyzing { projectName } project complete .") ;
95+ return triples ;
5496 }
97+
98+ private static string GetProjectName ( string fullName )
99+ => fullName . Split ( Path . DirectorySeparatorChar ) . Last ( ) . Replace ( ".csproj" , "" ) ;
100+
101+ private static string GetRoot ( string filePath )
102+ => filePath . Split ( Path . DirectorySeparatorChar ) . Reverse ( ) . Skip ( 1 ) . FirstOrDefault ( ) ;
55103 }
56104}
0 commit comments