Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,7 @@ dotnet_diagnostic.CA1854.severity = suggestion

# JSON002: Probable JSON string detected
dotnet_diagnostic.JSON002.severity = suggestion

# Workaround for https://github.com/dotnet/roslyn-analyzers/issues/5628
[Program.cs]
dotnet_diagnostic.ca1812.severity = none
5 changes: 0 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
<PackageVersion Include="Polly" Version="7.2.3"/>
<PackageVersion Include="Semver" Version="2.2.0"/>
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435"/>
<PackageVersion Include="System.Composition.AttributedModel" Version="7.0.0"/>
<PackageVersion Include="System.Composition.Convention" Version="7.0.0"/>
<PackageVersion Include="System.Composition.Hosting" Version="7.0.0"/>
<PackageVersion Include="System.Composition.Runtime" Version="7.0.0"/>
<PackageVersion Include="System.Composition.TypedParts" Version="7.0.0"/>
<PackageVersion Include="System.Memory" Version="4.5.5"/>
<PackageVersion Include="System.Reactive" Version="5.0.0"/>
<PackageVersion Include="System.Runtime.Loader" Version="4.3.0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Composition;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.ComponentDetection.Common.Telemetry.Records;
using Microsoft.ComponentDetection.Contracts;

[Export(typeof(ICommandLineInvocationService))]
public class CommandLineInvocationService : ICommandLineInvocationService
{
private readonly IDictionary<string, string> commandLocatableCache = new ConcurrentDictionary<string, string>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using Microsoft.ComponentDetection.Contracts;

[Export(typeof(IComponentStreamEnumerableFactory))]
[Shared]
public class ComponentStreamEnumerableFactory : IComponentStreamEnumerableFactory
{
[Import]
public ILogger Logger { get; set; }
private readonly IPathUtilityService pathUtilityService;
private readonly ILogger logger;

[Import]
public IPathUtilityService PathUtilityService { get; set; }
public ComponentStreamEnumerableFactory(IPathUtilityService pathUtilityService, ILogger logger)
{
this.pathUtilityService = pathUtilityService;
this.logger = logger;
}

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

public IEnumerable<IComponentStream> GetComponentStreams(DirectoryInfo directory, Func<FileInfo, bool> fileMatchingPredicate, ExcludeDirectoryPredicate directoryExclusionPredicate, bool recursivelyScanDirectories = true)
{
var enumerable = new SafeFileEnumerable(directory, fileMatchingPredicate, this.Logger, this.PathUtilityService, directoryExclusionPredicate, recursivelyScanDirectories);
return new ComponentStreamEnumerable(enumerable, this.Logger);
var enumerable = new SafeFileEnumerable(directory, fileMatchingPredicate, this.logger, this.pathUtilityService, directoryExclusionPredicate, recursivelyScanDirectories);
return new ComponentStreamEnumerable(enumerable, this.logger);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace Microsoft.ComponentDetection.Common;
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Composition;

[Export(typeof(IConsoleWritingService))]
public class ConsoleWritingService : IConsoleWritingService
{
public void Write(string content)
Expand Down
31 changes: 0 additions & 31 deletions src/Microsoft.ComponentDetection.Common/DetectorDependencies.cs

This file was deleted.

9 changes: 4 additions & 5 deletions src/Microsoft.ComponentDetection.Common/DockerService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Linq;
using System.Threading;
Expand All @@ -13,7 +12,6 @@ namespace Microsoft.ComponentDetection.Common;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Newtonsoft.Json;

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

[Import]
public ILogger Logger { get; set; }
private readonly ILogger logger;

public DockerService(ILogger logger) => this.logger = logger;

public async Task<bool> CanPingDockerAsync(CancellationToken cancellationToken = default)
{
Expand All @@ -35,7 +34,7 @@ public async Task<bool> CanPingDockerAsync(CancellationToken cancellationToken =
}
catch (Exception e)
{
this.Logger.LogException(e, false);
this.logger.LogException(e, false);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Composition;
using System.Linq;
using Microsoft.ComponentDetection.Contracts;

[Export(typeof(IEnvironmentVariableService))]
public class EnvironmentVariableService : IEnvironmentVariableService
{
public bool DoesEnvironmentVariableExist(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Composition;
using System.Diagnostics;
using System.IO;
using System.IO.Enumeration;
Expand All @@ -14,26 +13,25 @@
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.Internal;

[Export(typeof(IObservableDirectoryWalkerFactory))]
[Export(typeof(FastDirectoryWalkerFactory))]
[Shared]
public class FastDirectoryWalkerFactory : IObservableDirectoryWalkerFactory
{
private readonly ConcurrentDictionary<DirectoryInfo, Lazy<IObservable<FileSystemInfo>>> pendingScans = new ConcurrentDictionary<DirectoryInfo, Lazy<IObservable<FileSystemInfo>>>();
private readonly IPathUtilityService pathUtilityService;
private readonly ILogger logger;

[Import]
public ILogger Logger { get; set; }

[Import]
public IPathUtilityService PathUtilityService { get; set; }
public FastDirectoryWalkerFactory(IPathUtilityService pathUtilityService, ILogger logger)
{
this.pathUtilityService = pathUtilityService;
this.logger = logger;
}

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

var sw = Stopwatch.StartNew();

this.Logger?.LogInfo($"Starting enumeration of {root.FullName}");
this.logger?.LogInfo($"Starting enumeration of {root.FullName}");

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

if (di.Attributes.HasFlag(FileAttributes.ReparsePoint))
{
var realPath = this.PathUtilityService.ResolvePhysicalPath(di.FullName);
var realPath = this.pathUtilityService.ResolvePhysicalPath(di.FullName);

realDirectory = new DirectoryInfo(realPath);
}
Expand Down Expand Up @@ -189,7 +187,7 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
() =>
{
sw.Stop();
this.Logger?.LogInfo($"Enumerated {fileCount} files and {directoryCount} directories in {sw.Elapsed}");
this.logger?.LogInfo($"Enumerated {fileCount} files and {directoryCount} directories in {sw.Elapsed}");
s.OnCompleted();
});
});
Expand All @@ -213,7 +211,7 @@ public IObservable<FileSystemInfo> Subscribe(DirectoryInfo root, IEnumerable<str

if (this.pendingScans.TryGetValue(root, out var scannerObservable))
{
this.Logger.LogVerbose(string.Join(":", patterns));
this.logger.LogVerbose(string.Join(":", patterns));

var inner = scannerObservable.Value.Where(fsi =>
{
Expand Down Expand Up @@ -244,11 +242,11 @@ public IObservable<ProcessRequest> GetFilteredComponentStreamObservable(Director
var searchPattern = x.SearchPattern;
var fileName = x.File.Name;

return this.PathUtilityService.MatchesPattern(searchPattern, fileName);
return this.pathUtilityService.MatchesPattern(searchPattern, fileName);
}).Where(x => x.File.Exists)
.Select(x =>
{
var lazyComponentStream = new LazyComponentStream(x.File, x.SearchPattern, this.Logger);
var lazyComponentStream = new LazyComponentStream(x.File, x.SearchPattern, this.logger);
return new ProcessRequest
{
ComponentStream = lazyComponentStream,
Expand Down Expand Up @@ -287,6 +285,6 @@ private IObservable<FileSystemInfo> CreateDirectoryWalker(DirectoryInfo di, Excl

private bool MatchesAnyPattern(FileInfo fi, params string[] searchPatterns)
{
return searchPatterns != null && searchPatterns.Any(sp => this.PathUtilityService.MatchesPattern(sp, fi.Name));
return searchPatterns != null && searchPatterns.Any(sp => this.pathUtilityService.MatchesPattern(sp, fi.Name));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
namespace Microsoft.ComponentDetection.Common;
using System.Composition;
namespace Microsoft.ComponentDetection.Common;
using System.IO;
using Microsoft.ComponentDetection.Contracts;

/// <summary>
/// Wraps some common file operations for easier testability. This interface is *only used by the command line driven app*.
/// </summary>
[Export(typeof(IFileUtilityService))]
[Export(typeof(FileUtilityService))]
[Shared]
public class FileUtilityService : IFileUtilityService
{
public string ReadAllText(string filePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
namespace Microsoft.ComponentDetection.Common;
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Composition;
using System.IO;
using Microsoft.ComponentDetection.Common.Exceptions;

[Export(typeof(IFileWritingService))]
[Export(typeof(FileWritingService))]
[Shared]
public class FileWritingService : IFileWritingService
{
public const string TimestampFormatString = "yyyyMMddHHmmssfff";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// All file paths are relative and will replace occurrences of {timestamp} with the shared file timestamp.
public interface IFileWritingService
{
void Init(string basePath);

void AppendToFile(string relativeFilePath, string text);

void WriteFile(string relativeFilePath, string text);
Expand Down
25 changes: 12 additions & 13 deletions src/Microsoft.ComponentDetection.Common/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
namespace Microsoft.ComponentDetection.Common;
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Composition;
using System.Runtime.CompilerServices;
using Microsoft.ComponentDetection.Common.Telemetry.Records;
using Microsoft.ComponentDetection.Contracts;

using static System.Environment;

[Export(typeof(ILogger))]
[Export(typeof(Logger))]
[Shared]
public class Logger : ILogger
{
public const string LogRelativePath = "GovCompDisc_Log_{timestamp}.log";

[Import]
public IFileWritingService FileWritingService { get; set; }
private readonly IConsoleWritingService consoleWriter;
private readonly IFileWritingService fileWritingService;

[Import]
public IConsoleWritingService ConsoleWriter { get; set; }
public Logger(IConsoleWritingService consoleWriter, IFileWritingService fileWritingService)
{
this.consoleWriter = consoleWriter;
this.fileWritingService = fileWritingService;
}

private VerbosityMode Verbosity { get; set; }

Expand All @@ -33,8 +32,8 @@ public void Init(VerbosityMode verbosity, bool writeLinePrefix = true)
this.WriteLinePrefix = writeLinePrefix;
try
{
this.FileWritingService.WriteFile(LogRelativePath, string.Empty);
this.LogInfo($"Log file: {this.FileWritingService.ResolveFilePath(LogRelativePath)}");
this.fileWritingService.WriteFile(LogRelativePath, string.Empty);
this.LogInfo($"Log file: {this.fileWritingService.ResolveFilePath(LogRelativePath)}");
}
catch (Exception)
{
Expand Down Expand Up @@ -132,15 +131,15 @@ private void AppendToFile(string text)
{
if (this.WriteToFile)
{
this.FileWritingService.AppendToFile(LogRelativePath, text);
this.fileWritingService.AppendToFile(LogRelativePath, text);
}
}

private void PrintToConsole(string text, VerbosityMode minVerbosity)
{
if (this.Verbosity >= minVerbosity)
{
this.ConsoleWriter.Write(text);
this.consoleWriter.Write(text);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
<PackageReference Include="Microsoft.AspNet.WebApi.Client" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Semver" />
<PackageReference Include="System.Composition.AttributedModel" />
<PackageReference Include="System.Composition.Convention" />
<PackageReference Include="System.Composition.Hosting" />
<PackageReference Include="System.Composition.Runtime" />
<PackageReference Include="System.Composition.TypedParts" />
<PackageReference Include="System.Reactive" />
<PackageReference Include="System.Threading.Tasks.Dataflow" />
</ItemGroup>
Expand Down
Loading