-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa18260
commit 62786df
Showing
6 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using static Roslynator.Logger; | ||
|
||
namespace Roslynator.CommandLine | ||
{ | ||
internal class ListReferencesCommand : MSBuildWorkspaceCommand | ||
{ | ||
public ListReferencesCommand( | ||
ListReferencesCommandLineOptions options, | ||
MetadataReferenceDisplay display, | ||
MetadataReferenceFilter filter, | ||
in ProjectFilter projectFilter) : base(projectFilter) | ||
{ | ||
Options = options; | ||
Display = display; | ||
Filter = filter; | ||
} | ||
|
||
public ListReferencesCommandLineOptions Options { get; } | ||
|
||
public MetadataReferenceDisplay Display { get; } | ||
|
||
public MetadataReferenceFilter Filter { get; } | ||
|
||
public override async Task<CommandResult> ExecuteAsync(ProjectOrSolution projectOrSolution, CancellationToken cancellationToken = default) | ||
{ | ||
AssemblyResolver.Register(); | ||
|
||
ImmutableArray<Compilation> compilations = await GetCompilationsAsync(projectOrSolution, cancellationToken); | ||
|
||
int count = 0; | ||
|
||
foreach (string display in compilations | ||
.SelectMany(compilation => compilation.ExternalReferences.Select(reference => (compilation, reference))) | ||
.Select(f => GetDisplay(f.compilation, f.reference)) | ||
.Where(f => f != null) | ||
.Distinct() | ||
.OrderBy(f => f, StringComparer.InvariantCulture)) | ||
{ | ||
WriteLine(display); | ||
count++; | ||
} | ||
|
||
if (ShouldWrite(Verbosity.Normal)) | ||
{ | ||
WriteLine(Verbosity.Normal); | ||
WriteLine($"{count} assembl{((count == 1) ? "y" : "ies")} found", ConsoleColor.Green, Verbosity.Normal); | ||
} | ||
|
||
return CommandResult.Success; | ||
|
||
string GetDisplay(Compilation compilation, MetadataReference reference) | ||
{ | ||
switch (reference) | ||
{ | ||
case PortableExecutableReference portableReference: | ||
{ | ||
if ((Filter & MetadataReferenceFilter.Dll) == 0) | ||
return null; | ||
|
||
string path = portableReference.FilePath; | ||
|
||
switch (Display) | ||
{ | ||
case MetadataReferenceDisplay.Path: | ||
{ | ||
return path; | ||
} | ||
case MetadataReferenceDisplay.FileName: | ||
{ | ||
return Path.GetFileName(path); | ||
} | ||
case MetadataReferenceDisplay.FileNameWithoutExtension: | ||
{ | ||
return Path.GetFileNameWithoutExtension(path); | ||
} | ||
case MetadataReferenceDisplay.AssemblyName: | ||
{ | ||
var assembly = (IAssemblySymbol)compilation.GetAssemblyOrModuleSymbol(reference); | ||
|
||
return assembly.Identity.ToString(); | ||
} | ||
default: | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
} | ||
case CompilationReference compilationReference: | ||
{ | ||
if ((Filter & MetadataReferenceFilter.Project) == 0) | ||
return null; | ||
|
||
return compilationReference.Display; | ||
} | ||
default: | ||
{ | ||
#if DEBUG | ||
WriteLine(reference.GetType().FullName, ConsoleColor.Yellow); | ||
#endif | ||
return reference.Display; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Roslynator.CommandLine | ||
{ | ||
internal enum MetadataReferenceDisplay | ||
{ | ||
Path = 0, | ||
FileName = 1, | ||
FileNameWithoutExtension = 2, | ||
AssemblyName = 3, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Roslynator.CommandLine | ||
{ | ||
[Flags] | ||
internal enum MetadataReferenceFilter | ||
{ | ||
None = 0, | ||
Dll = 1, | ||
Project = 1 << 1, | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/CommandLine/Options/ListReferencesCommandLineOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using CommandLine; | ||
|
||
namespace Roslynator.CommandLine | ||
{ | ||
#if DEBUG | ||
[Verb("list-references", HelpText = "Lists assembly references from the specified project or solution.")] | ||
#endif | ||
public class ListReferencesCommandLineOptions : MSBuildCommandLineOptions | ||
{ | ||
[Option( | ||
longName: "display", | ||
HelpText = "Defines how the assembly is displayed. Allowed values are path (default), file-name, file-name-without-extension or assembly-name.")] | ||
public string Display { get; set; } | ||
|
||
[Option( | ||
longName: ParameterNames.Type, | ||
HelpText = "Defines a type of a reference. Allowed values are dll and project.")] | ||
public IEnumerable<string> Type { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters