Skip to content

Commit 585a7c5

Browse files
Vlad BatushkovVlad Batushkov
authored andcommitted
mode change to tiers and add type nodes
1 parent fa29af3 commit 585a7c5

9 files changed

Lines changed: 98 additions & 124 deletions

File tree

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS sdk
1+
FROM mcr.microsoft.com/dotnet/sdk:3.1-alpine AS sdk
22
WORKDIR /src
33
COPY Strazh/Strazh.csproj Strazh/Strazh.csproj
44
RUN dotnet restore /src/Strazh/Strazh.csproj
@@ -9,7 +9,7 @@ COPY Strazh Strazh/
99
RUN dotnet build /src/Strazh/Strazh.csproj -c Release -o /app
1010
WORKDIR /app
1111
ENV c="neo4j:neo4j:neo4j"
12-
ENV m="all"
12+
ENV t="all"
1313
ENV s="none"
1414
ENV p=""
15-
CMD ["sh", "-c", "dotnet Strazh.dll -c $c -m $m -s $s -p $p"]
15+
CMD ["sh", "-c", "dotnet Strazh.dll -c $c -t $t -s $s -p $p"]

Strazh/Analysis/Analyzer.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using Strazh.Database;
1111
using static Strazh.Analysis.AnalyzerConfig;
12+
using System.IO;
1213

1314
namespace Strazh.Analysis
1415
{
@@ -33,7 +34,7 @@ public static async Task Analyze(AnalyzerConfig config)
3334
short index = 1;
3435
foreach (var projectAnalyzer in projectAnalyzers)
3536
{
36-
var triples = await AnalyzeProject(index, workspace, projectAnalyzer, config.Mode);
37+
var triples = await AnalyzeProject(index, workspace, projectAnalyzer, config.Tier);
3738
if (triples.Count > 0)
3839
{
3940
await DbManager.InsertData(triples, config.Credentials, isDelete);
@@ -44,7 +45,7 @@ public static async Task Analyze(AnalyzerConfig config)
4445
workspace.Dispose();
4546
}
4647

47-
private static async Task<IList<Triple>> AnalyzeProject(short index, AdhocWorkspace workspace, IProjectAnalyzer projectAnalyzer, AnalyzeMode mode)
48+
private static async Task<IList<Triple>> AnalyzeProject(short index, AdhocWorkspace workspace, IProjectAnalyzer projectAnalyzer, Tiers mode)
4849
{
4950
Console.WriteLine($"Project #{index}:");
5051
var project = projectAnalyzer.AddToWorkspace(workspace);
@@ -54,9 +55,9 @@ private static async Task<IList<Triple>> AnalyzeProject(short index, AdhocWorksp
5455
Console.WriteLine($"Analyzing {projectName} project...");
5556

5657
var triples = new List<Triple>();
57-
if (mode == AnalyzeMode.All || mode == AnalyzeMode.Structure)
58+
if (mode == Tiers.All || mode == Tiers.Project)
5859
{
59-
Console.WriteLine($"Analyzing Structure level...");
60+
Console.WriteLine($"Analyzing Project tier...");
6061
var projectBuild = projectAnalyzer.Build().FirstOrDefault();
6162
var projectNode = new ProjectNode(projectName);
6263
triples.Add(new TripleIncludedIn(projectNode, rootNode));
@@ -71,13 +72,13 @@ private static async Task<IList<Triple>> AnalyzeProject(short index, AdhocWorksp
7172
var node = new PackageNode(x.Key, x.Key, version);
7273
triples.Add(new TripleDependsOnPackage(projectNode, node));
7374
});
74-
Console.WriteLine($"Analyzing Structure level complete.");
75+
Console.WriteLine($"Analyzing Project tier complete.");
7576
}
7677

7778
if (project.SupportsCompilation
78-
&& (mode == AnalyzeMode.All || mode == AnalyzeMode.Code))
79+
&& (mode == Tiers.All || mode == Tiers.Code))
7980
{
80-
Console.WriteLine($"Analyzing Code level...");
81+
Console.WriteLine($"Analyzing Code tier...");
8182
var compilation = await project.GetCompilationAsync();
8283
var syntaxTreeRoot = compilation.SyntaxTrees;
8384
foreach (var st in syntaxTreeRoot)
@@ -86,7 +87,7 @@ private static async Task<IList<Triple>> AnalyzeProject(short index, AdhocWorksp
8687
Extractor.AnalyzeTree<ClassDeclarationSyntax>(triples, st, sem, rootNode);
8788
Extractor.AnalyzeTree<InterfaceDeclarationSyntax>(triples, st, sem, rootNode);
8889
}
89-
Console.WriteLine($"Analyzing Code level complete.");
90+
Console.WriteLine($"Analyzing Code tier complete.");
9091
triples = triples.GroupBy(x => x.ToString()).Select(x => x.First()).ToList();
9192
}
9293

@@ -95,9 +96,9 @@ private static async Task<IList<Triple>> AnalyzeProject(short index, AdhocWorksp
9596
}
9697

9798
private static string GetProjectName(string fullName)
98-
=> fullName.Split('\\').Last().Replace(".csproj", "");
99+
=> fullName.Split(Path.DirectorySeparatorChar).Last().Replace(".csproj", "");
99100

100101
private static string GetRoot(string filePath)
101-
=> filePath.Split("\\").Reverse().Skip(1).FirstOrDefault();
102+
=> filePath.Split(Path.DirectorySeparatorChar).Reverse().Skip(1).FirstOrDefault();
102103
}
103104
}

Strazh/Analysis/AnalyzerConfig.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public CredentialsConfig(string credentials)
2323
}
2424
}
2525

26-
public enum AnalyzeMode : int
26+
public enum Tiers : int
2727
{
2828
All = 0,
29-
Structure = 1,
29+
Project = 1,
3030
Code = 2
3131
}
3232

3333
public CredentialsConfig Credentials { get; }
34-
public AnalyzeMode Mode { get; }
34+
public Tiers Tier { get; }
3535
public string Solution { get; }
3636
public string[] Projects { get; }
3737
public bool IsDelete { get; }
@@ -41,22 +41,22 @@ public enum AnalyzeMode : int
4141
public bool IsValid => (!string.IsNullOrEmpty(Solution) && Projects.Length == 0)
4242
|| (string.IsNullOrEmpty(Solution) && Projects.Length > 0);
4343

44-
public AnalyzerConfig(string credentials, string mode, string delete, string solution, string[] projects)
44+
public AnalyzerConfig(string credentials, string tier, string delete, string solution, string[] projects)
4545
{
4646
solution = solution == "none" ? "" : solution;
4747
Credentials = new CredentialsConfig(credentials);
48-
Mode = MapMode(mode);
48+
Tier = MapTier(tier);
4949
IsDelete = delete != "false";
5050
Solution = solution;
5151
Projects = projects ?? new string[] { };
5252
}
5353

54-
private AnalyzeMode MapMode(string mode)
54+
private Tiers MapTier(string mode)
5555
=> (mode ?? "").ToLowerInvariant() switch
5656
{
57-
"structure" => AnalyzeMode.Structure,
58-
"code" => AnalyzeMode.Code,
59-
_ => AnalyzeMode.All,
57+
"project" => Tiers.Project,
58+
"code" => Tiers.Code,
59+
_ => Tiers.All,
6060
};
6161
}
6262
}

Strazh/Analysis/Extractor.cs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
using Microsoft.CodeAnalysis.CSharp;
55
using System.Collections.Generic;
66
using Strazh.Domain;
7+
using System.IO;
78

89
namespace Strazh.Analysis
910
{
1011
public static class Extractor
1112
{
12-
private static CodeNode CreateNode(this ISymbol symbol, TypeDeclarationSyntax declaration)
13+
private static TypeNode CreateTypeNode(this ISymbol symbol, TypeDeclarationSyntax declaration)
1314
{
1415
(string fullName, string name) = (symbol.ContainingNamespace.ToString() + '.' + symbol.Name, symbol.Name);
1516
switch (declaration)
@@ -31,7 +32,7 @@ private static InterfaceNode CreateInterfaceNode(this TypeInfo typeInfo)
3132
private static string[] MapModifiers(this SyntaxTokenList syntaxTokens)
3233
=> syntaxTokens.Select(x => x.ValueText).ToArray();
3334

34-
private static Node CreateNode(this TypeInfo typeInfo)
35+
private static TypeNode CreateTypeNode(this TypeInfo typeInfo)
3536
{
3637
switch (typeInfo.ConvertedType.TypeKind)
3738
{
@@ -68,20 +69,20 @@ private static MethodNode CreateMethodNode(this IMethodSymbol symbol, MethodDecl
6869
var fullName = symbol.ContainingNamespace.GetNamespaceName($"{symbol.ContainingType.Name}.{symbol.Name}");
6970
var args = symbol.Parameters.Select(x => (name: x.Name, type: x.Type.ToString())).ToArray();
7071
var returnType = symbol.ReturnType.ToString();
71-
return new MethodNode(fullName,
72+
return new MethodNode(fullName,
7273
symbol.Name,
7374
args,
7475
returnType,
7576
declaration?.Modifiers.MapModifiers());
7677
}
7778

7879
private static string GetName(string filePath)
79-
=> filePath.Split("\\").Reverse().FirstOrDefault();
80+
=> filePath.Split(Path.DirectorySeparatorChar).Reverse().FirstOrDefault();
8081

8182
private static List<TripleIncludedIn> GetFolderChain(string filePath, FileNode file)
8283
{
8384
var triples = new List<TripleIncludedIn>();
84-
var chain = filePath.Split("\\");
85+
var chain = filePath.Split(Path.DirectorySeparatorChar);
8586
FolderNode prev = null;
8687
var path = string.Empty;
8788
foreach (var item in chain)
@@ -99,7 +100,7 @@ private static List<TripleIncludedIn> GetFolderChain(string filePath, FileNode f
99100
}
100101
else
101102
{
102-
path = $"{path}\\{item}";
103+
path = Path.DirectorySeparatorChar == '/' ? $"{path}/{item}" : $"{path}\\{item}";
103104
triples.Add(new TripleIncludedIn(new FolderNode(path, item), new FolderNode(prev.FullName, prev.Name)));
104105
prev = new FolderNode(path, item);
105106
}
@@ -116,14 +117,14 @@ public static void AnalyzeTree<T>(IList<Triple> triples, SyntaxTree st, Semantic
116117
var root = st.GetRoot();
117118
var filePath = root.SyntaxTree.FilePath;
118119
var index = filePath.IndexOf(rootFolder.Name);
119-
filePath = index < 0 ? filePath : filePath.Substring(index);
120+
filePath = index < 0 ? filePath : filePath[index..];
120121
var fileName = GetName(filePath);
121122
var fileNode = new FileNode(filePath, fileName);
122123
GetFolderChain(filePath, fileNode).ForEach(triples.Add);
123124
var declarations = root.DescendantNodes().OfType<T>();
124125
foreach (var declaration in declarations)
125126
{
126-
var node = sem.GetDeclaredSymbol(declaration).CreateNode(declaration);
127+
var node = sem.GetDeclaredSymbol(declaration).CreateTypeNode(declaration);
127128
if (node != null)
128129
{
129130
triples.Add(new TripleDeclaredAt(node, fileNode));
@@ -136,29 +137,33 @@ public static void AnalyzeTree<T>(IList<Triple> triples, SyntaxTree st, Semantic
136137
/// <summary>
137138
/// Member (field, property) initialization
138139
/// </summary>
139-
public static void GetConstructsWithinClass(IList<Triple> triples, ClassDeclarationSyntax declaration, SemanticModel sem, ClassNode classNode)
140-
{
141-
var creates = declaration.DescendantNodes().OfType<ObjectCreationExpressionSyntax>();
142-
foreach (var creation in creates)
143-
{
144-
var node = sem.GetTypeInfo(creation).CreateClassNode();
145-
triples.Add(new TripleConstruct(classNode, node));
146-
}
147-
}
140+
//public static void GetConstructsWithinClass(IList<Triple> triples, ClassDeclarationSyntax declaration, SemanticModel sem, ClassNode classNode)
141+
//{
142+
// var creates = declaration.DescendantNodes().OfType<ObjectCreationExpressionSyntax>();
143+
// foreach (var creation in creates)
144+
// {
145+
// var node = sem.GetTypeInfo(creation).CreateClassNode();
146+
// triples.Add(new TripleConstruct(classNode, node));
147+
// }
148+
//}
148149

149150
/// <summary>
150151
/// Type inherited from BaseType
151152
/// </summary>
152-
public static void GetInherits(IList<Triple> triples, TypeDeclarationSyntax declaration, SemanticModel sem, Node node)
153+
public static void GetInherits(IList<Triple> triples, TypeDeclarationSyntax declaration, SemanticModel sem, TypeNode node)
153154
{
154155
if (declaration.BaseList != null)
155156
{
156157
foreach (var baseTypeSyntax in declaration.BaseList.Types)
157158
{
158-
var parentNode = sem.GetTypeInfo(baseTypeSyntax.Type).CreateNode();
159-
if (parentNode != null)
159+
var parentNode = sem.GetTypeInfo(baseTypeSyntax.Type).CreateTypeNode();
160+
if (node is ClassNode classNode)
161+
{
162+
triples.Add(new TripleOfType(classNode, parentNode));
163+
}
164+
if (node is InterfaceNode interfaceNode && parentNode is InterfaceNode parentInterfaceNode)
160165
{
161-
triples.Add(new TripleInherit(node, parentNode));
166+
triples.Add(new TripleOfType(interfaceNode, parentInterfaceNode));
162167
}
163168
}
164169
}
@@ -167,7 +172,7 @@ public static void GetInherits(IList<Triple> triples, TypeDeclarationSyntax decl
167172
/// <summary>
168173
/// Class or Interface have some method AND some method can call another method AND some method can creates an object of class
169174
/// </summary>
170-
public static void GetMethodsAll(IList<Triple> triples, TypeDeclarationSyntax declaration, SemanticModel sem, Node node)
175+
public static void GetMethodsAll(IList<Triple> triples, TypeDeclarationSyntax declaration, SemanticModel sem, TypeNode node)
171176
{
172177
var methods = declaration.DescendantNodes().OfType<MethodDeclarationSyntax>();
173178
foreach (var method in methods)
@@ -185,8 +190,7 @@ public static void GetMethodsAll(IList<Triple> triples, TypeDeclarationSyntax de
185190
break;
186191

187192
case InvocationExpressionSyntax invocation:
188-
var invokedSymbol = sem.GetSymbolInfo(invocation).Symbol as IMethodSymbol;
189-
if (invokedSymbol != null)
193+
if (sem.GetSymbolInfo(invocation).Symbol is IMethodSymbol invokedSymbol)
190194
{
191195
var invokedMethod = invokedSymbol.CreateMethodNode();
192196
triples.Add(new TripleInvoke(methodNode, invokedMethod));

Strazh/Domain/Nodes.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Strazh.Domain
44
{
55
public abstract class Node
66
{
7-
public virtual string Label { get; }
7+
public abstract string Label { get; }
88

99
public virtual string FullName { get; }
1010

@@ -27,9 +27,6 @@ protected virtual void SetPrimaryKey()
2727
Pk = FullName.GetHashCode().ToString();
2828
}
2929

30-
public string Match()
31-
=> $"pk: \"{Pk}\"";
32-
3330
public virtual string Set(string node)
3431
=> $"{node}.pk = \"{Pk}\", {node}.fullName = \"{FullName}\", {node}.name = \"{Name}\"";
3532
}
@@ -51,7 +48,15 @@ public override string Set(string node)
5148
=> $"{base.Set(node)}{(string.IsNullOrEmpty(Modifiers) ? "" : $", {node}.modifiers = \"{Modifiers}\"")}";
5249
}
5350

54-
public class ClassNode : CodeNode
51+
public abstract class TypeNode : CodeNode
52+
{
53+
public TypeNode(string fullName, string name, string[] modifiers = null)
54+
: base(fullName, name, modifiers)
55+
{
56+
}
57+
}
58+
59+
public class ClassNode : TypeNode
5560
{
5661
public ClassNode(string fullName, string name, string[] modifiers = null)
5762
: base(fullName, name, modifiers)
@@ -61,7 +66,7 @@ public ClassNode(string fullName, string name, string[] modifiers = null)
6166
public override string Label { get; } = "Class";
6267
}
6368

64-
public class InterfaceNode : CodeNode
69+
public class InterfaceNode : TypeNode
6570
{
6671
public InterfaceNode(string fullName, string name, string[] modifiers = null)
6772
: base(fullName, name, modifiers)

Strazh/Domain/Relationships.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace Strazh.Domain
22
{
33
public abstract class Relationship
44
{
5-
public virtual string Type { get; }
5+
public abstract string Type { get; }
66
}
77

88
public class HaveRelationship : Relationship

0 commit comments

Comments
 (0)