Skip to content

Commit bd8088b

Browse files
Merge pull request #3 from vladbatushkov/alpha3
Version Alpha3
2 parents 03062b2 + 585a7c5 commit bd8088b

15 files changed

Lines changed: 636 additions & 471 deletions

File tree

Dockerfile

Lines changed: 6 additions & 4 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
@@ -8,6 +8,8 @@ WORKDIR /src
88
COPY Strazh Strazh/
99
RUN dotnet build /src/Strazh/Strazh.csproj -c Release -o /app
1010
WORKDIR /app
11-
ENV cs=neo4j:neo4j:neo4j
12-
ENV pl=Project.csproj
13-
CMD ["sh", "-c", "dotnet Strazh.dll --credentials $cs --pathlist $pl"]
11+
ENV c="neo4j:neo4j:neo4j"
12+
ENV t="all"
13+
ENV s="none"
14+
ENV p=""
15+
CMD ["sh", "-c", "dotnet Strazh.dll -c $c -t $t -s $s -p $p"]

README.md

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,6 @@ dotnet ./app/Strazh.dll -c neo4j:neo4j:strazh -p ./Strazh/Strazh.csproj
2424

2525
### docker
2626

27-
**Dockerfile**
28-
```
29-
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS sdk
30-
WORKDIR /src
31-
COPY Strazh/Strazh.csproj Strazh/Strazh.csproj
32-
RUN dotnet restore /src/Strazh/Strazh.csproj
33-
34-
FROM sdk as build
35-
WORKDIR /src
36-
COPY Strazh Strazh/
37-
RUN dotnet build /src/Strazh/Strazh.csproj -c Release -o /app
38-
WORKDIR /app
39-
ENV cs=neo4j:neo4j:neo4j
40-
ENV pl=Project.csproj
41-
CMD ["sh", "-c", "dotnet Strazh.dll --cs $cs --pl $pl"]
42-
```
43-
4427
In case you want to create a local `strazh:dev` image:
4528

4629
```
@@ -50,19 +33,21 @@ docker build . -t strazh:dev
5033
Example how to run created `strazh:dev` container against the `Strazh.csproj` project (strazh can explore strazh codebase O_o )
5134

5235
```
53-
docker run -it --rm --network=host -v $(pwd)/Strazh:/dest -e cs=neo4j:neo4j:strazh -e pl=/dest/Strazh.csproj strazh:dev
36+
docker run -it --rm --network=host -v $(pwd)/Strazh:/dest -e c=neo4j:neo4j:strazh -e p=/dest/Strazh.csproj strazh:dev
5437
```
5538

56-
Run with cli from `Strazh` folder: `dotnet Strazh.dll -cs neo4j:neo4j:strazh -pl ../../../Strazh.csproj`
39+
Run with cli from `Strazh` folder: `dotnet Strazh.dll -c neo4j:neo4j:strazh -p ../../../Strazh.csproj`
5740

5841
Run using `dotnet run` from `Strazh` folder:
5942
```
60-
dotnet run -cs "neo4j:neo4j:strazh" -pl "./Strazh.csproj"
43+
dotnet run -c "neo4j:neo4j:strazh" -p "./Strazh.csproj"
6144
```
6245

63-
_- docker volume used to map folder `/Strazh` to folder `/dest` inside docker._
64-
_- environment value `cs` used to connect to Neo4j with `database:user:password` credentials._
65-
_- environment value `pl` used to point to list of project path inside docker container._
46+
- docker volume used to map folder `/Strazh` to folder `/dest` inside docker.
47+
- environment value `c` used to connect to Neo4j with `database:user:password` credentials.
48+
- environment value `m` used to point to mode of analysis used for run.
49+
- environment value `s` used to point to solution file inside docker container.
50+
- environment value `p` used to point to project files inside docker container.
6651

6752
**docker-compose.yml**
6853

@@ -73,14 +58,14 @@ services:
7358
7459
strazh:
7560
build: .
76-
image: vladbatushkov/strazh:1.0.0-alpha.1
61+
image: vladbatushkov/strazh:1.0.0-alpha.3
7762
container_name: strazh
7863
network_mode: host
7964
volumes:
8065
- ./Strazh:/dest
8166
environment:
82-
- cs=neo4j:neo4j:strazh
83-
- pl=/dest/Strazh.csproj
67+
- c=neo4j:neo4j:strazh
68+
- p=/dest/Strazh.csproj
8469
depends_on:
8570
- neo4j
8671

Strazh/Analysis/Analyzer.cs

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,98 @@
77
using Buildalyzer.Workspaces;
88
using System.Collections.Generic;
99
using System;
10+
using Strazh.Database;
11+
using static Strazh.Analysis.AnalyzerConfig;
12+
using System.IO;
1013

1114
namespace 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
}

Strazh/Analysis/AnalyzerConfig.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace Strazh.Analysis
2+
{
3+
public class AnalyzerConfig
4+
{
5+
public class CredentialsConfig
6+
{
7+
public string Database { get; }
8+
public string User { get; }
9+
public string Password { get; }
10+
11+
public CredentialsConfig(string credentials)
12+
{
13+
if (!string.IsNullOrEmpty(credentials))
14+
{
15+
var args = credentials.Split(":");
16+
if (args.Length == 3)
17+
{
18+
Database = args[0];
19+
User = args[1];
20+
Password = args[2];
21+
}
22+
}
23+
}
24+
}
25+
26+
public enum Tiers : int
27+
{
28+
All = 0,
29+
Project = 1,
30+
Code = 2
31+
}
32+
33+
public CredentialsConfig Credentials { get; }
34+
public Tiers Tier { get; }
35+
public string Solution { get; }
36+
public string[] Projects { get; }
37+
public bool IsDelete { get; }
38+
39+
public bool IsSolutionBased => !string.IsNullOrEmpty(Solution);
40+
41+
public bool IsValid => (!string.IsNullOrEmpty(Solution) && Projects.Length == 0)
42+
|| (string.IsNullOrEmpty(Solution) && Projects.Length > 0);
43+
44+
public AnalyzerConfig(string credentials, string tier, string delete, string solution, string[] projects)
45+
{
46+
solution = solution == "none" ? "" : solution;
47+
Credentials = new CredentialsConfig(credentials);
48+
Tier = MapTier(tier);
49+
IsDelete = delete != "false";
50+
Solution = solution;
51+
Projects = projects ?? new string[] { };
52+
}
53+
54+
private Tiers MapTier(string mode)
55+
=> (mode ?? "").ToLowerInvariant() switch
56+
{
57+
"project" => Tiers.Project,
58+
"code" => Tiers.Code,
59+
_ => Tiers.All,
60+
};
61+
}
62+
}

0 commit comments

Comments
 (0)