Skip to content
4 changes: 0 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,6 @@ dotnet_diagnostic.SA1200.severity = suggestion
# A field should not follow a property
dotnet_diagnostic.SA1201.severity = suggestion

# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md
# Constant fields should appear before non-constant fields
dotnet_diagnostic.SA1202.severity = suggestion

# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1203.md
# 'public' members should come before 'private' members
dotnet_diagnostic.SA1203.severity = suggestion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -88,6 +88,16 @@ public bool IsCommandLineExecution()
return true;
}

public async Task<bool> CanCommandBeLocated(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
{
return await this.CanCommandBeLocated(command, additionalCandidateCommands, workingDirectory: null, parameters);
}

public async Task<CommandLineExecutionResult> ExecuteCommand(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
{
return await this.ExecuteCommand(command, additionalCandidateCommands, workingDirectory: null, parameters);
}

private static Task<CommandLineExecutionResult> RunProcessAsync(string fileName, string parameters, DirectoryInfo workingDirectory = null)
{
var tcs = new TaskCompletionSource<CommandLineExecutionResult>();
Expand Down Expand Up @@ -144,15 +154,5 @@ private static Task<CommandLineExecutionResult> RunProcessAsync(string fileName,

return tcs.Task;
}

public async Task<bool> CanCommandBeLocated(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
{
return await this.CanCommandBeLocated(command, additionalCandidateCommands, workingDirectory: null, parameters);
}

public async Task<CommandLineExecutionResult> ExecuteCommand(string command, IEnumerable<string> additionalCandidateCommands = null, params string[] parameters)
{
return await this.ExecuteCommand(command, additionalCandidateCommands, workingDirectory: null, parameters);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -79,27 +79,27 @@ public ISingleFileComponentRecorder CreateSingleFileComponentRecorder(string loc
return matching;
}

internal DependencyGraph GetDependencyGraphForLocation(string location)
{
return this.singleFileRecorders.Single(x => x.ManifestFileLocation == location).DependencyGraph;
}

public IReadOnlyDictionary<string, IDependencyGraph> GetDependencyGraphsByLocation()
{
return this.singleFileRecorders.Where(x => x.DependencyGraph.HasComponents())
.ToImmutableDictionary(x => x.ManifestFileLocation, x => x.DependencyGraph as IDependencyGraph);
}

internal DependencyGraph GetDependencyGraphForLocation(string location)
{
return this.singleFileRecorders.Single(x => x.ManifestFileLocation == location).DependencyGraph;
}

public class SingleFileComponentRecorder : ISingleFileComponentRecorder
{
private readonly ILogger log;

public string ManifestFileLocation { get; }

internal DependencyGraph DependencyGraph { get; }

IDependencyGraph ISingleFileComponentRecorder.DependencyGraph => this.DependencyGraph;

internal DependencyGraph DependencyGraph { get; }

private readonly ConcurrentDictionary<string, DetectedComponent> detectedComponentsInternal = new ConcurrentDictionary<string, DetectedComponent>();

private readonly ComponentRecorder recorder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -119,6 +119,42 @@ public IEnumerable<string> GetAllExplicitlyReferencedComponents()
.Select(componentRefNode => componentRefNode.Id);
}

IEnumerable<string> IDependencyGraph.GetDependenciesForComponent(string componentId)
{
return this.GetDependenciesForComponent(componentId).ToImmutableList();
}

IEnumerable<string> IDependencyGraph.GetComponents()
{
return this.componentNodes.Keys.ToImmutableList();
}

bool IDependencyGraph.IsComponentExplicitlyReferenced(string componentId)
{
return this.IsExplicitReferencedDependency(this.componentNodes[componentId]);
}

internal class ComponentRefNode
{
internal bool IsExplicitReferencedDependency { get; set; }

internal string Id { get; set; }

internal ISet<string> DependencyIds { get; private set; }

internal ISet<string> DependedOnByIds { get; private set; }

internal bool? IsDevelopmentDependency { get; set; }

internal DependencyScope? DependencyScope { get; set; }

internal ComponentRefNode()
{
this.DependencyIds = new HashSet<string>();
this.DependedOnByIds = new HashSet<string>();
}
}

private void GetExplicitReferencedDependencies(ComponentRefNode component, IList<string> explicitReferencedDependencyIds, ISet<string> visited)
{
if (this.IsExplicitReferencedDependency(component))
Expand Down Expand Up @@ -158,41 +194,5 @@ private void AddDependency(string componentId, string parentComponentId)
parentComponentRefNode.DependencyIds.Add(componentId);
this.componentNodes[componentId].DependedOnByIds.Add(parentComponentId);
}

IEnumerable<string> IDependencyGraph.GetDependenciesForComponent(string componentId)
{
return this.GetDependenciesForComponent(componentId).ToImmutableList();
}

IEnumerable<string> IDependencyGraph.GetComponents()
{
return this.componentNodes.Keys.ToImmutableList();
}

bool IDependencyGraph.IsComponentExplicitlyReferenced(string componentId)
{
return this.IsExplicitReferencedDependency(this.componentNodes[componentId]);
}

internal class ComponentRefNode
{
internal bool IsExplicitReferencedDependency { get; set; }

internal string Id { get; set; }

internal ISet<string> DependencyIds { get; private set; }

internal ISet<string> DependedOnByIds { get; private set; }

internal bool? IsDevelopmentDependency { get; set; }

internal DependencyScope? DependencyScope { get; set; }

internal ComponentRefNode()
{
this.DependencyIds = new HashSet<string>();
this.DependedOnByIds = new HashSet<string>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,6 @@ public IObservable<FileSystemInfo> GetDirectoryScanner(DirectoryInfo root, Concu
});
}

private FileSystemInfo Transform(ref FileSystemEntry entry)
{
return entry.ToFileSystemInfo();
}

private IObservable<FileSystemInfo> CreateDirectoryWalker(DirectoryInfo di, ExcludeDirectoryPredicate directoryExclusionPredicate, int minimumConnectionCount, IEnumerable<string> filePatterns)
{
return this.GetDirectoryScanner(di, new ConcurrentDictionary<string, bool>(), directoryExclusionPredicate, filePatterns, true).Replay() // Returns a replay subject which will republish anything found to new subscribers.
.AutoConnect(minimumConnectionCount); // Specifies that this connectable observable should start when minimumConnectionCount subscribe.
}

private bool MatchesAnyPattern(FileInfo fi, params string[] searchPatterns)
{
return searchPatterns != null && searchPatterns.Any(sp => this.PathUtilityService.MatchesPattern(sp, fi.Name));
}

/// <summary>
/// Initialized an observable file enumerator.
/// </summary>
Expand Down Expand Up @@ -290,5 +274,21 @@ public void Reset()
{
this.pendingScans.Clear();
}

private FileSystemInfo Transform(ref FileSystemEntry entry)
{
return entry.ToFileSystemInfo();
}

private IObservable<FileSystemInfo> CreateDirectoryWalker(DirectoryInfo di, ExcludeDirectoryPredicate directoryExclusionPredicate, int minimumConnectionCount, IEnumerable<string> filePatterns)
{
return this.GetDirectoryScanner(di, new ConcurrentDictionary<string, bool>(), directoryExclusionPredicate, filePatterns, true).Replay() // Returns a replay subject which will republish anything found to new subscribers.
.AutoConnect(minimumConnectionCount); // Specifies that this connectable observable should start when minimumConnectionCount subscribe.
}

private bool MatchesAnyPattern(FileInfo fi, params string[] searchPatterns)
{
return searchPatterns != null && searchPatterns.Any(sp => this.PathUtilityService.MatchesPattern(sp, fi.Name));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Composition;
using System.IO;
using Microsoft.ComponentDetection.Common.Exceptions;
Expand All @@ -10,9 +10,10 @@ namespace Microsoft.ComponentDetection.Common
[Shared]
public class FileWritingService : IFileWritingService
{
public const string TimestampFormatString = "yyyyMMddHHmmss";

private object lockObject = new object();
private string timestamp = DateTime.Now.ToString(TimestampFormatString);
public const string TimestampFormatString = "yyyyMMddHHmmss";

public string BasePath { get; private set; }

Expand Down
20 changes: 10 additions & 10 deletions src/Microsoft.ComponentDetection.Common/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Composition;
using System.Runtime.CompilerServices;
using Microsoft.ComponentDetection.Common.Telemetry.Records;
Expand Down Expand Up @@ -68,15 +68,6 @@ public void LogError(string message)
this.LogInternal("ERROR", message, VerbosityMode.Quiet);
}

private void LogInternal(string prefix, string message, VerbosityMode verbosity = VerbosityMode.Normal)
{
var formattedPrefix = string.IsNullOrWhiteSpace(prefix) ? string.Empty : $"[{prefix}] ";
var text = $"{formattedPrefix}{message} {NewLine}";

this.PrintToConsole(text, verbosity);
this.AppendToFile(text);
}

public void LogFailedReadingFile(string filePath, Exception e)
{
this.PrintToConsole(NewLine, VerbosityMode.Verbose);
Expand Down Expand Up @@ -150,5 +141,14 @@ private void PrintToConsole(string text, VerbosityMode minVerbosity)
this.ConsoleWriter.Write(text);
}
}

private void LogInternal(string prefix, string message, VerbosityMode verbosity = VerbosityMode.Normal)
{
var formattedPrefix = string.IsNullOrWhiteSpace(prefix) ? string.Empty : $"[{prefix}] ";
var text = $"{formattedPrefix}{message} {NewLine}";

this.PrintToConsole(text, verbosity);
this.AppendToFile(text);
}
}
}
26 changes: 13 additions & 13 deletions src/Microsoft.ComponentDetection.Common/PathUtilityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ namespace Microsoft.ComponentDetection.Common
[Shared]
public class PathUtilityService : IPathUtilityService
{
[DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern SafeFileHandle CreateFile(
[In] string lpFileName,
[In] uint dwDesiredAccess,
[In] uint dwShareMode,
[In] IntPtr lpSecurityAttributes,
[In] uint dwCreationDisposition,
[In] uint dwFlagsAndAttributes,
[In] IntPtr hTemplateFile);

[DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetFinalPathNameByHandle([In] IntPtr hFile, [Out] StringBuilder lpszFilePath, [In] int cchFilePath, [In] int dwFlags);

/// <summary>
/// This call can be made on a linux system to get the absolute path of a file. It will resolve nested layers.
/// Note: You may pass IntPtr.Zero to the output parameter. You MUST then free the IntPtr that RealPathLinux returns
Expand Down Expand Up @@ -242,6 +229,19 @@ public string ResolvePhysicalPathLinux(string path)
}
}

[DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern SafeFileHandle CreateFile(
[In] string lpFileName,
[In] uint dwDesiredAccess,
[In] uint dwShareMode,
[In] IntPtr lpSecurityAttributes,
[In] uint dwCreationDisposition,
[In] uint dwFlagsAndAttributes,
[In] IntPtr hTemplateFile);

[DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetFinalPathNameByHandle([In] IntPtr hFile, [Out] StringBuilder lpszFilePath, [In] int cchFilePath, [In] int dwFlags);

private bool CheckIfRunningOnWindowsContainer()
{
if (IsLinux)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -7,15 +7,15 @@ namespace Microsoft.ComponentDetection.Common
{
public class TabularStringFormat
{
public const char DefaultVerticalLineChar = '|';
public const char DefaultHorizontalLineChar = '_';

private IList<Column> columns;
private int totalWidth;
private char horizontalLineChar;
private char verticalLineChar;
private string tableTitle;

public const char DefaultVerticalLineChar = '|';
public const char DefaultHorizontalLineChar = '_';

public TabularStringFormat(IList<Column> columns, char horizontalLineChar = DefaultHorizontalLineChar, char verticalLineChar = DefaultVerticalLineChar, string tableTitle = null)
{
this.columns = columns;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using Microsoft.ComponentDetection.Common.Telemetry.Attributes;

Expand Down Expand Up @@ -27,6 +27,11 @@ public void StopExecutionTimer()
}
}

public void Dispose()
{
this.Dispose(true);
}

private bool disposedValue = false;

protected virtual void Dispose(bool disposing)
Expand All @@ -42,10 +47,5 @@ protected virtual void Dispose(bool disposing)
this.disposedValue = true;
}
}

public void Dispose()
{
this.Dispose(true);
}
}
}
Loading