Skip to content

Commit c17f3da

Browse files
committed
Base
1 parent dd7fa8f commit c17f3da

File tree

10 files changed

+458
-1
lines changed

10 files changed

+458
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.vs
2+
/bin
3+
/obj

DateiHelper.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Text.RegularExpressions;
7+
8+
namespace DependencieResolver
9+
{
10+
static class DateiHelper
11+
{
12+
public static Dictionary<string, IList<string>> DepsAufDatei = new Dictionary<string, IList<string>>();
13+
14+
public static string LiesGanzeImportZeile(string aktuelleZeile, StreamReader sr)
15+
{
16+
var istEinzeiligerImport = DateiHelper.IstImportZeile(aktuelleZeile) && DateiHelper.IstImportZeileEnde(aktuelleZeile) || IstZeileUnspezifischerImport(aktuelleZeile);
17+
if (istEinzeiligerImport)
18+
{
19+
return aktuelleZeile;
20+
}
21+
22+
var zusammengesetzteZeile = new StringBuilder();
23+
while (aktuelleZeile != null && !DateiHelper.IstImportZeileEnde(aktuelleZeile))
24+
{
25+
zusammengesetzteZeile.Append(aktuelleZeile);
26+
try
27+
{
28+
aktuelleZeile = sr.ReadLine();
29+
}
30+
catch (IOException)
31+
{
32+
break;
33+
}
34+
}
35+
zusammengesetzteZeile.Append(aktuelleZeile);
36+
37+
return zusammengesetzteZeile.ToString();
38+
}
39+
40+
public static bool IstImportZeile(string zeile)
41+
{
42+
return zeile?.Trim().StartsWith("import ") ?? false;
43+
}
44+
public static bool IstImportZeileEnde(string zeile)
45+
{
46+
var fromIstEnthalten = zeile?.Contains(" from ") ?? false;
47+
return fromIstEnthalten;
48+
}
49+
50+
private static readonly string RegexImport = "^import ['|\"]([^'\"]+)['|\"][;]*$";
51+
52+
public static bool IstZeileUnspezifischerImport(string zeile)
53+
{
54+
var importRegel = new Regex(RegexImport);
55+
return importRegel.IsMatch(zeile);
56+
}
57+
58+
public static string LiesImportZielAus(string zeile)
59+
{
60+
if (IstZeileUnspezifischerImport(zeile))
61+
{
62+
var match = new Regex(RegexImport).Match(zeile);
63+
return match.Groups.Values.Last().Value;
64+
}
65+
var split = zeile.Split(" from ");
66+
67+
var fromPfad = split[^1]
68+
.Replace("'", string.Empty)
69+
.Replace("\"", string.Empty)
70+
.Replace(";", string.Empty);
71+
72+
return fromPfad;
73+
}
74+
75+
public static string ErsetzeRelativePfade(string zeile, IList<string> orderBisZurDatei)
76+
{
77+
var zeileEnthaeltRelativePfade = zeile.Contains('.') || zeile.Contains("..");
78+
if (!zeileEnthaeltRelativePfade)
79+
{
80+
return zeile;
81+
}
82+
83+
var zeileMitRichtigenSeperatoren = zeile.UmwandelnZuRichtigenSeperatoren();
84+
var zeileOhneRelativesHier = ErsetzeRelativesHier(zeileMitRichtigenSeperatoren, orderBisZurDatei);
85+
var zeileOhneRelativeOrdnerHoch = ErsetzeRelativesOrderHoch(zeileOhneRelativesHier, orderBisZurDatei);
86+
87+
return zeileOhneRelativeOrdnerHoch;
88+
}
89+
90+
private static string ErsetzeRelativesHier(string zeile, IList<string> orderBisZurDatei)
91+
{
92+
if (zeile.StartsWith($".{Path.DirectorySeparatorChar}"))
93+
{
94+
var aktuellerPfad = String.Join(Path.DirectorySeparatorChar, orderBisZurDatei) + Path.DirectorySeparatorChar;
95+
zeile = zeile.Replace($".{Path.DirectorySeparatorChar}", aktuellerPfad); // Was wenn mehrere Punkte da sind?
96+
}
97+
98+
return zeile;
99+
}
100+
101+
private static string ErsetzeRelativesOrderHoch(string zeile, IList<string> orderBisZurDatei)
102+
{
103+
var anzahlEbenenHoch = new Regex(Regex.Escape("..")).Matches(zeile).Count;
104+
105+
if (anzahlEbenenHoch == 0)
106+
{
107+
return zeile;
108+
}
109+
110+
while (anzahlEbenenHoch > 0)
111+
{
112+
orderBisZurDatei.RemoveAt(orderBisZurDatei.Count - 1);
113+
anzahlEbenenHoch--;
114+
}
115+
116+
var zeileOhneRelativePfade = zeile.Replace($"..{Path.DirectorySeparatorChar}", ""); // Ebenen hoch löschen
117+
var zeileMitErsetztemPfad = String.Join(Path.DirectorySeparatorChar, orderBisZurDatei) + Path.DirectorySeparatorChar + zeileOhneRelativePfade;
118+
119+
return zeileMitErsetztemPfad;
120+
}
121+
122+
public static string UmwandelnZuRichtigenSeperatoren(this string zeileMitPfad)
123+
{
124+
return zeileMitPfad
125+
.Replace('/', Path.DirectorySeparatorChar)
126+
.Replace('\\', Path.DirectorySeparatorChar);
127+
}
128+
}
129+
}

DateiNode.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace DependencieResolver
7+
{
8+
class DateiNode
9+
{
10+
public string Pfad { get; }
11+
public IList<string> DependencyPfade { get; set; } // => [root/Foo/Bar.ts]
12+
13+
private IList<string> OrderHierarchie
14+
{
15+
get
16+
{
17+
var pfadeInklDatei = Pfad.Split(Path.DirectorySeparatorChar).ToList();
18+
pfadeInklDatei.RemoveAt(pfadeInklDatei.Count - 1); // Aktuelle Datei löschen
19+
return pfadeInklDatei;
20+
}
21+
}
22+
23+
public DateiNode(string pfad)
24+
{
25+
DependencyPfade = new List<string>();
26+
Pfad = pfad;
27+
28+
if (!File.Exists(Pfad))
29+
{
30+
throw new IOException("Pfad existiert nicht!");
31+
}
32+
}
33+
34+
public void WerteImportPfadeAus()
35+
{
36+
DependencyPfade = new List<string>();
37+
38+
using StreamReader sr = new StreamReader(Pfad);
39+
var line = sr.ReadLine();
40+
41+
while (line != null)
42+
{
43+
var zeileIstLeer = String.IsNullOrEmpty(line.Trim());
44+
if (zeileIstLeer)
45+
{
46+
line = sr.ReadLine();
47+
continue;
48+
}
49+
50+
if (!DateiHelper.IstImportZeile(line))
51+
{
52+
break;
53+
}
54+
55+
var importZeile = DateiHelper.LiesGanzeImportZeile(line, sr);
56+
var importZiel = DateiHelper.LiesImportZielAus(importZeile);
57+
58+
importZiel = DateiHelper.ErsetzeRelativePfade(importZiel, OrderHierarchie);
59+
60+
DependencyPfade.Add(importZiel);
61+
62+
line = sr.ReadLine();
63+
}
64+
}
65+
66+
public void BaueDepGraph(bool rekursivLaden = false)
67+
{
68+
if (!DependencyPfade.Any())
69+
{
70+
WerteImportPfadeAus();
71+
}
72+
73+
FuelleDepGraph();
74+
75+
if (!rekursivLaden)
76+
{
77+
return;
78+
}
79+
80+
foreach (var dependencyPfad in DependencyPfade)
81+
{
82+
var depAlsDatei = new DateiNode(dependencyPfad);
83+
depAlsDatei.BaueDepGraph(true);
84+
}
85+
}
86+
87+
private void FuelleDepGraph()
88+
{
89+
foreach (var dependency in DependencyPfade)
90+
{
91+
if (DateiHelper.DepsAufDatei.TryGetValue(dependency, out var externeDep))
92+
{
93+
if (!externeDep.Contains(Pfad))
94+
{
95+
externeDep.Add(Pfad);
96+
}
97+
}
98+
else
99+
{
100+
DateiHelper.DepsAufDatei.Add(dependency, new List<string> { Pfad });
101+
}
102+
}
103+
104+
}
105+
}
106+
}

DependencieResolver.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
10+
</ItemGroup>
11+
12+
</Project>

DependencieResolver.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30011.22
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencieResolver", "DependencieResolver.csproj", "{DB1614E1-DD5F-4943-8D31-8F5A5BE3945D}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{DB1614E1-DD5F-4943-8D31-8F5A5BE3945D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{DB1614E1-DD5F-4943-8D31-8F5A5BE3945D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{DB1614E1-DD5F-4943-8D31-8F5A5BE3945D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{DB1614E1-DD5F-4943-8D31-8F5A5BE3945D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {E150ECA4-7441-4663-977D-76F417E123AE}
24+
EndGlobalSection
25+
EndGlobal

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 owehmer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

OrdnerNode.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace DependencieResolver
6+
{
7+
class OrdnerNode
8+
{
9+
public string Pfad { get; set; }
10+
public List<DateiNode> Dateien { get; set; }
11+
public List<OrdnerNode> Unterordner { get; set; }
12+
13+
public IList<string> AlleDependencies
14+
{
15+
get
16+
{
17+
var liste = new List<string>();
18+
liste.AddRange(Dateien.SelectMany(d => d.DependencyPfade).ToList());
19+
liste.AddRange(Unterordner.SelectMany(u => u.AlleDependencies.ToList()));
20+
return liste;
21+
}
22+
}
23+
24+
public IList<DateiNode> AlleDateienRekuriv
25+
{
26+
get
27+
{
28+
var liste = new List<DateiNode>();
29+
liste.AddRange(Dateien.ToList());
30+
liste.AddRange(Unterordner.SelectMany(u => u.AlleDateienRekuriv.ToList()));
31+
return liste;
32+
}
33+
}
34+
35+
public OrdnerNode(string pfad)
36+
{
37+
Pfad = pfad;
38+
Dateien = new List<DateiNode>();
39+
Unterordner = new List<OrdnerNode>();
40+
41+
if (!Directory.Exists(pfad))
42+
{
43+
throw new IOException("Pfad des Ordners exitiert nicht.");
44+
}
45+
}
46+
47+
public void DateienAuswerten(bool unterordnerLesen = true)
48+
{
49+
string[] fileEntries = Directory.GetFiles(Pfad);
50+
51+
var fileNodes = fileEntries
52+
.Select(filePath =>
53+
{
54+
var node = new DateiNode(filePath);
55+
node.BaueDepGraph();
56+
return node;
57+
})
58+
.ToList();
59+
60+
Dateien.AddRange(fileNodes);
61+
62+
if (!unterordnerLesen)
63+
{
64+
return;
65+
}
66+
67+
IList<string> subdirectoryEntries = Directory.GetDirectories(Pfad)
68+
.Where(e => !e.Contains('.') && !e.Contains("node_modules"))
69+
.ToList();
70+
71+
var unterordnerNodes = subdirectoryEntries.Select(subDirPath =>
72+
{
73+
var node = new OrdnerNode(subDirPath);
74+
node.DateienAuswerten();
75+
return node;
76+
}).ToList();
77+
78+
Unterordner.AddRange(unterordnerNodes);
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)