Skip to content

Commit b1fc40d

Browse files
committed
Add support for asterisk is filters. Update libs
1 parent edc2084 commit b1fc40d

File tree

9 files changed

+74
-44
lines changed

9 files changed

+74
-44
lines changed

SDV.App/MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
</WrapPanel>
2727

2828
<WrapPanel HorizontalAlignment="Left" Margin="0,10,0,0">
29-
<Label Content="Filtered package prefixes" />
30-
<TextBox x:Name="PackagePrefixes" Width="200" />
29+
<Label Content="Filtered packages" />
30+
<TextBox x:Name="PackagePrefixes" Width="465" />
3131
</WrapPanel>
3232
<WrapPanel HorizontalAlignment="Center" Margin="0,0,0,10">
3333
<TextBlock Text="Comma separated values" FontSize="10" />

SDV.App/MainWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public MainWindow(ILogger<MainWindow> logger, WindowLogSink logSink, IGraphBuild
3636
{
3737
Filter = "solution files (*.sln)|*.sln"
3838
};
39-
PackagePrefixes.Text = "Microsoft., System.";
39+
PackagePrefixes.Text = "Microsoft.*, System.*";
4040
IncludeDependentProjects.IsChecked = true;
4141
ClearSelectionButton.IsEnabled = false;
4242
SetupModeComboBox();
@@ -77,7 +77,7 @@ private async void btnBuild_Click(object sender, RoutedEventArgs e)
7777
var request = new GraphBuilderRequest(_slnFilePaths)
7878
{
7979
Mode = (PackageFilterMode)Mode.SelectedValue,
80-
PackagePrefixes = PackagePrefixes.Text.Split(",").Select(t => t.Trim()).Where(t => !string.IsNullOrEmpty(t)).ToArray(),
80+
PackageFilters = PackagePrefixes.Text.Split(",").Select(t => t.Trim()).Where(t => !string.IsNullOrEmpty(t)).ToArray(),
8181
IncludeDependentProjects = IncludeDependentProjects.IsChecked ?? false,
8282
MergeProjects = MergeProjects.IsChecked ?? false
8383
};

SDV.DependenciesAnalyzer/Dependencies/DependenciesUtils.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class DependenciesUtils
1212
public DependenciesUtils(ILogger log)
1313
{
1414
_log = log;
15-
_configuration = new TreeGeneratorConfiguration(string.Empty);
15+
_configuration = new TreeGeneratorConfiguration(string.Empty, []);
1616
}
1717

1818
public DependencyTree[] CreateDependencyTree(IExtractor extractor, TreeGeneratorConfiguration configuration)
@@ -74,22 +74,7 @@ private IEnumerable<DependencyTree> FindChildrenDependencies(string id,
7474
}
7575
}
7676
}
77-
78-
79-
private bool CanBeProcessed(string name)
80-
{
81-
if ( _configuration.Mode == PackageFilterMode.None || _configuration.PackagePrefixes.Length == 0)
82-
{
83-
return true;
84-
}
8577

86-
var isMatched = _configuration.PackagePrefixes.Any(name.StartsWith);
87-
var shouldBeProcessed = _configuration.Mode == PackageFilterMode.Include ? isMatched : !isMatched;
88-
if (!shouldBeProcessed)
89-
{
90-
_log.LogDebug("Processing of package {Name} and its dependencies is skipped due to configuration", name);
91-
}
9278

93-
return shouldBeProcessed;
94-
}
79+
private bool CanBeProcessed(string name) => _configuration.IsPackageEnabled(name);
9580
}

SDV.DependenciesAnalyzer/Interfaces/INugetDependenciesGenerator.cs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,74 @@ public interface INugetDependenciesGenerator
1717
public class TreeGeneratorConfiguration
1818
{
1919
public string SlnFilePath { get; }
20-
public string[] PackagePrefixes { get; init; }
2120
public PackageFilterMode Mode { get; init; }
2221
public bool IncludeDependentProjects { get; init; }
22+
23+
private readonly List<Func<string, bool>> _filters;
24+
private const char Mask = '*';
2325

24-
public TreeGeneratorConfiguration(string slnFilePath)
26+
public TreeGeneratorConfiguration(string slnFilePath, string[] packageFilters)
2527
{
28+
_filters = new List<Func<string, bool>>();
2629
SlnFilePath = slnFilePath;
27-
PackagePrefixes = Array.Empty<string>();
2830
Mode = PackageFilterMode.None;
2931
IncludeDependentProjects = false;
32+
BuildFilters(packageFilters);
33+
}
34+
35+
public bool IsPackageEnabled(string packageName)
36+
{
37+
if (Mode == PackageFilterMode.None || _filters.Count == 0)
38+
return true;
39+
40+
var isMatched = _filters.Any(f => f(packageName));
41+
var shouldBeProcessed = Mode == PackageFilterMode.Include ? isMatched : !isMatched;
42+
43+
return shouldBeProcessed;
44+
}
45+
46+
private void BuildFilters(string[] packagePrefixes)
47+
{
48+
foreach (var filter in packagePrefixes.Where(p => !string.IsNullOrWhiteSpace(p)).Select(p => p.Trim()))
49+
{
50+
var split = filter.Split(Mask);
51+
if (split.Length == 1) // filter
52+
{
53+
_filters.Add(s => split[0].Equals(s));
54+
return;
55+
}
56+
57+
if (split.Length == 2)
58+
{
59+
if(split.First() != string.Empty && split.Last() != string.Empty) // fil*ter
60+
{
61+
_filters.Add(s => s.StartsWith(split.First()) && filter.EndsWith(split.Last()));
62+
63+
}
64+
else if (split.First() == string.Empty && split.Last() == string.Empty) // *
65+
{
66+
throw new ArgumentException($"Unknown filter specified: {filter}");
67+
}
68+
else if (split.First() == string.Empty) // *filter
69+
{
70+
_filters.Add(s => s.EndsWith(split.Last()));
71+
}
72+
else // filter*
73+
{
74+
_filters.Add(s => s.StartsWith(split.First()));
75+
}
76+
77+
return;
78+
}
79+
80+
if (split.Length == 3 && split.First() == string.Empty && split.Last() == string.Empty)
81+
{
82+
_filters.Add(s => s.Contains(split[1]));
83+
return;
84+
}
85+
86+
throw new ArgumentException($"Unknown filter specified: {filter}");
87+
}
3088
}
3189
}
3290

SDV.DependenciesAnalyzer/NugetDependenciesGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public NugetDependenciesGenerator(ILogger<NugetDependenciesGenerator> log)
2020

2121
public Tree Generate(string slnFilePath)
2222
{
23-
return Generate(new TreeGeneratorConfiguration(slnFilePath));
23+
return Generate(new TreeGeneratorConfiguration(slnFilePath, []));
2424
}
2525

2626
public Tree Generate(TreeGeneratorConfiguration configuration)

SDV.DependenciesAnalyzer/SDV.DependenciesAnalyzer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
13-
<PackageReference Include="NuGet.Packaging" Version="6.8.0" />
13+
<PackageReference Include="NuGet.Packaging" Version="6.10.0" />
1414
</ItemGroup>
1515

1616
</Project>

SDV.DependenciesAnalyzer/Structure/SolutionBuilder.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public SolutionBuilder(ILogger logger, ProjectBuilder projectBuilder)
1919
{
2020
_logger = logger;
2121
_projectBuilder = projectBuilder;
22-
_configuration = new TreeGeneratorConfiguration(string.Empty);
22+
_configuration = new TreeGeneratorConfiguration(string.Empty, []);
2323
_dependenciesSources = new List<string>();
2424
}
2525

@@ -97,19 +97,7 @@ private bool CanProjectBeProcessed(string name, string path)
9797
return false;
9898
}
9999

100-
if ( _configuration.Mode == PackageFilterMode.None || _configuration.PackagePrefixes.Length == 0)
101-
{
102-
return true;
103-
}
104-
105-
var isMatched = _configuration.PackagePrefixes.Any(name.StartsWith);
106-
var shouldBeProcessed = _configuration.Mode == PackageFilterMode.Include ? isMatched : !isMatched;
107-
if (!shouldBeProcessed)
108-
{
109-
_logger.LogDebug("Processing of project {ProjectName} and its dependencies is skipped due to configuration", name);
110-
}
111-
112-
return shouldBeProcessed;
100+
return _configuration.IsPackageEnabled(name);
113101
}
114102

115103
private IEnumerable<ProjectBuilder.Project> LoadSingleProjectFromDir()

SDV.GraphGenerator/Interfaces/IGraphBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public interface IGraphBuilder
1010
public class GraphBuilderRequest
1111
{
1212
public IEnumerable<string> SlnFilePaths { get; }
13-
public string[] PackagePrefixes { get; init; }
13+
public string[] PackageFilters { get; init; }
1414
public PackageFilterMode Mode { get; init; }
1515
public bool IncludeDependentProjects { get; init; }
1616
public bool MergeProjects { get; init; }
1717

1818
public GraphBuilderRequest(IEnumerable<string> slnFilePaths)
1919
{
2020
SlnFilePaths = slnFilePaths;
21-
PackagePrefixes = Array.Empty<string>();
21+
PackageFilters = Array.Empty<string>();
2222
}
2323
}

SDV.GraphGenerator/Services/GraphBuilder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ public string BuildGraphAndGetFilePath(GraphBuilderRequest request)
2828

2929
foreach (var slnFilePath in request.SlnFilePaths)
3030
{
31-
var config = new TreeGeneratorConfiguration(slnFilePath)
31+
var config = new TreeGeneratorConfiguration(slnFilePath, request.PackageFilters)
3232
{
3333
Mode = request.Mode,
34-
PackagePrefixes = request.PackagePrefixes,
3534
IncludeDependentProjects = request is { MergeProjects: false, IncludeDependentProjects: true }
3635
};
3736

0 commit comments

Comments
 (0)