Skip to content

Commit f6912c0

Browse files
authored
refactor: migrate away from MEF to Dependency Injection (#412)
1 parent 8031814 commit f6912c0

File tree

119 files changed

+1560
-2081
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+1560
-2081
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,7 @@ dotnet_diagnostic.CA1854.severity = suggestion
671671

672672
# JSON002: Probable JSON string detected
673673
dotnet_diagnostic.JSON002.severity = suggestion
674+
675+
# Workaround for https://github.com/dotnet/roslyn-analyzers/issues/5628
676+
[Program.cs]
677+
dotnet_diagnostic.ca1812.severity = none

Directory.Packages.props

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
<PackageVersion Include="Polly" Version="7.2.3"/>
3333
<PackageVersion Include="Semver" Version="2.2.0"/>
3434
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435"/>
35-
<PackageVersion Include="System.Composition.AttributedModel" Version="7.0.0"/>
36-
<PackageVersion Include="System.Composition.Convention" Version="7.0.0"/>
37-
<PackageVersion Include="System.Composition.Hosting" Version="7.0.0"/>
38-
<PackageVersion Include="System.Composition.Runtime" Version="7.0.0"/>
39-
<PackageVersion Include="System.Composition.TypedParts" Version="7.0.0"/>
4035
<PackageVersion Include="System.Memory" Version="4.5.5"/>
4136
<PackageVersion Include="System.Reactive" Version="5.0.0"/>
4237
<PackageVersion Include="System.Runtime.Loader" Version="4.3.0"/>

src/Microsoft.ComponentDetection.Common/CommandLineInvocationService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
55
using System.ComponentModel;
6-
using System.Composition;
76
using System.Diagnostics;
87
using System.IO;
98
using System.Linq;
109
using System.Threading.Tasks;
1110
using Microsoft.ComponentDetection.Common.Telemetry.Records;
1211
using Microsoft.ComponentDetection.Contracts;
1312

14-
[Export(typeof(ICommandLineInvocationService))]
1513
public class CommandLineInvocationService : ICommandLineInvocationService
1614
{
1715
private readonly IDictionary<string, string> commandLocatableCache = new ConcurrentDictionary<string, string>();
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
namespace Microsoft.ComponentDetection.Common;
22
using System;
33
using System.Collections.Generic;
4-
using System.Composition;
54
using System.IO;
65
using Microsoft.ComponentDetection.Contracts;
76

8-
[Export(typeof(IComponentStreamEnumerableFactory))]
9-
[Shared]
107
public class ComponentStreamEnumerableFactory : IComponentStreamEnumerableFactory
118
{
12-
[Import]
13-
public ILogger Logger { get; set; }
9+
private readonly IPathUtilityService pathUtilityService;
10+
private readonly ILogger logger;
1411

15-
[Import]
16-
public IPathUtilityService PathUtilityService { get; set; }
12+
public ComponentStreamEnumerableFactory(IPathUtilityService pathUtilityService, ILogger logger)
13+
{
14+
this.pathUtilityService = pathUtilityService;
15+
this.logger = logger;
16+
}
1717

1818
public IEnumerable<IComponentStream> GetComponentStreams(DirectoryInfo directory, IEnumerable<string> searchPatterns, ExcludeDirectoryPredicate directoryExclusionPredicate, bool recursivelyScanDirectories = true)
1919
{
20-
var enumerable = new SafeFileEnumerable(directory, searchPatterns, this.Logger, this.PathUtilityService, directoryExclusionPredicate, recursivelyScanDirectories);
21-
return new ComponentStreamEnumerable(enumerable, this.Logger);
20+
var enumerable = new SafeFileEnumerable(directory, searchPatterns, this.logger, this.pathUtilityService, directoryExclusionPredicate, recursivelyScanDirectories);
21+
return new ComponentStreamEnumerable(enumerable, this.logger);
2222
}
2323

2424
public IEnumerable<IComponentStream> GetComponentStreams(DirectoryInfo directory, Func<FileInfo, bool> fileMatchingPredicate, ExcludeDirectoryPredicate directoryExclusionPredicate, bool recursivelyScanDirectories = true)
2525
{
26-
var enumerable = new SafeFileEnumerable(directory, fileMatchingPredicate, this.Logger, this.PathUtilityService, directoryExclusionPredicate, recursivelyScanDirectories);
27-
return new ComponentStreamEnumerable(enumerable, this.Logger);
26+
var enumerable = new SafeFileEnumerable(directory, fileMatchingPredicate, this.logger, this.pathUtilityService, directoryExclusionPredicate, recursivelyScanDirectories);
27+
return new ComponentStreamEnumerable(enumerable, this.logger);
2828
}
2929
}

src/Microsoft.ComponentDetection.Common/ConsoleWritingService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
namespace Microsoft.ComponentDetection.Common;
1+
namespace Microsoft.ComponentDetection.Common;
22
using System;
3-
using System.Composition;
43

5-
[Export(typeof(IConsoleWritingService))]
64
public class ConsoleWritingService : IConsoleWritingService
75
{
86
public void Write(string content)

src/Microsoft.ComponentDetection.Common/DetectorDependencies.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Microsoft.ComponentDetection.Common/DockerService.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace Microsoft.ComponentDetection.Common;
22
using System;
33
using System.Collections.Generic;
4-
using System.Composition;
54
using System.IO;
65
using System.Linq;
76
using System.Threading;
@@ -13,7 +12,6 @@ namespace Microsoft.ComponentDetection.Common;
1312
using Microsoft.ComponentDetection.Contracts.BcdeModels;
1413
using Newtonsoft.Json;
1514

16-
[Export(typeof(IDockerService))]
1715
public class DockerService : IDockerService
1816
{
1917
// Base image annotations from ADO dockerTask
@@ -23,8 +21,9 @@ public class DockerService : IDockerService
2321
private static readonly DockerClient Client = new DockerClientConfiguration().CreateClient();
2422
private static int incrementingContainerId;
2523

26-
[Import]
27-
public ILogger Logger { get; set; }
24+
private readonly ILogger logger;
25+
26+
public DockerService(ILogger logger) => this.logger = logger;
2827

2928
public async Task<bool> CanPingDockerAsync(CancellationToken cancellationToken = default)
3029
{
@@ -35,7 +34,7 @@ public async Task<bool> CanPingDockerAsync(CancellationToken cancellationToken =
3534
}
3635
catch (Exception e)
3736
{
38-
this.Logger.LogException(e, false);
37+
this.logger.LogException(e, false);
3938
return false;
4039
}
4140
}

src/Microsoft.ComponentDetection.Common/EnvironmentVariableService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
namespace Microsoft.ComponentDetection.Common;
22
using System;
3-
using System.Composition;
43
using System.Linq;
54
using Microsoft.ComponentDetection.Contracts;
65

7-
[Export(typeof(IEnvironmentVariableService))]
86
public class EnvironmentVariableService : IEnvironmentVariableService
97
{
108
public bool DoesEnvironmentVariableExist(string name)

src/Microsoft.ComponentDetection.Common/FastDirectoryWalkerFactory.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
5-
using System.Composition;
65
using System.Diagnostics;
76
using System.IO;
87
using System.IO.Enumeration;
@@ -14,26 +13,25 @@
1413
using Microsoft.ComponentDetection.Contracts;
1514
using Microsoft.ComponentDetection.Contracts.Internal;
1615

17-
[Export(typeof(IObservableDirectoryWalkerFactory))]
18-
[Export(typeof(FastDirectoryWalkerFactory))]
19-
[Shared]
2016
public class FastDirectoryWalkerFactory : IObservableDirectoryWalkerFactory
2117
{
2218
private readonly ConcurrentDictionary<DirectoryInfo, Lazy<IObservable<FileSystemInfo>>> pendingScans = new ConcurrentDictionary<DirectoryInfo, Lazy<IObservable<FileSystemInfo>>>();
19+
private readonly IPathUtilityService pathUtilityService;
20+
private readonly ILogger logger;
2321

24-
[Import]
25-
public ILogger Logger { get; set; }
26-
27-
[Import]
28-
public IPathUtilityService PathUtilityService { get; set; }
22+
public FastDirectoryWalkerFactory(IPathUtilityService pathUtilityService, ILogger logger)
23+
{
24+
this.pathUtilityService = pathUtilityService;
25+
this.logger = logger;
26+
}
2927

3028
public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, ConcurrentDictionary<string, bool> scannedDirectories, ExcludeDirectoryPredicate directoryExclusionPredicate, IEnumerable<string> filePatterns = null, bool recurse = true)
3129
{
3230
return Observable.Create<FileSystemInfo>(s =>
3331
{
3432
if (!root.Exists)
3533
{
36-
this.Logger?.LogError($"Root directory doesn't exist: {root.FullName}");
34+
this.logger?.LogError($"Root directory doesn't exist: {root.FullName}");
3735
s.OnCompleted();
3836
return Task.CompletedTask;
3937
}
@@ -51,7 +49,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
5149

5250
var sw = Stopwatch.StartNew();
5351

54-
this.Logger?.LogInfo($"Starting enumeration of {root.FullName}");
52+
this.logger?.LogInfo($"Starting enumeration of {root.FullName}");
5553

5654
var fileCount = 0;
5755
var directoryCount = 0;
@@ -72,7 +70,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
7270

7371
if (di.Attributes.HasFlag(FileAttributes.ReparsePoint))
7472
{
75-
var realPath = this.PathUtilityService.ResolvePhysicalPath(di.FullName);
73+
var realPath = this.pathUtilityService.ResolvePhysicalPath(di.FullName);
7674

7775
realDirectory = new DirectoryInfo(realPath);
7876
}
@@ -189,7 +187,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
189187
() =>
190188
{
191189
sw.Stop();
192-
this.Logger?.LogInfo($"Enumerated {fileCount} files and {directoryCount} directories in {sw.Elapsed}");
190+
this.logger?.LogInfo($"Enumerated {fileCount} files and {directoryCount} directories in {sw.Elapsed}");
193191
s.OnCompleted();
194192
});
195193
});
@@ -213,7 +211,7 @@ public IObservable<FileSystemInfo> Subscribe(DirectoryInfo root, IEnumerable<str
213211

214212
if (this.pendingScans.TryGetValue(root, out var scannerObservable))
215213
{
216-
this.Logger.LogVerbose(string.Join(":", patterns));
214+
this.logger.LogVerbose(string.Join(":", patterns));
217215

218216
var inner = scannerObservable.Value.Where(fsi =>
219217
{
@@ -244,11 +242,11 @@ public IObservable<ProcessRequest> GetFilteredComponentStreamObservable(Director
244242
var searchPattern = x.SearchPattern;
245243
var fileName = x.File.Name;
246244

247-
return this.PathUtilityService.MatchesPattern(searchPattern, fileName);
245+
return this.pathUtilityService.MatchesPattern(searchPattern, fileName);
248246
}).Where(x => x.File.Exists)
249247
.Select(x =>
250248
{
251-
var lazyComponentStream = new LazyComponentStream(x.File, x.SearchPattern, this.Logger);
249+
var lazyComponentStream = new LazyComponentStream(x.File, x.SearchPattern, this.logger);
252250
return new ProcessRequest
253251
{
254252
ComponentStream = lazyComponentStream,
@@ -287,6 +285,6 @@ private IObservable<FileSystemInfo> CreateDirectoryWalker(DirectoryInfo di, Excl
287285

288286
private bool MatchesAnyPattern(FileInfo fi, params string[] searchPatterns)
289287
{
290-
return searchPatterns != null && searchPatterns.Any(sp => this.PathUtilityService.MatchesPattern(sp, fi.Name));
288+
return searchPatterns != null && searchPatterns.Any(sp => this.pathUtilityService.MatchesPattern(sp, fi.Name));
291289
}
292290
}

src/Microsoft.ComponentDetection.Common/FileUtilityService.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
namespace Microsoft.ComponentDetection.Common;
2-
using System.Composition;
1+
namespace Microsoft.ComponentDetection.Common;
32
using System.IO;
43
using Microsoft.ComponentDetection.Contracts;
54

65
/// <summary>
76
/// Wraps some common file operations for easier testability. This interface is *only used by the command line driven app*.
87
/// </summary>
9-
[Export(typeof(IFileUtilityService))]
10-
[Export(typeof(FileUtilityService))]
11-
[Shared]
128
public class FileUtilityService : IFileUtilityService
139
{
1410
public string ReadAllText(string filePath)

0 commit comments

Comments
 (0)