|
| 1 | +# API Usage Analyzer |
| 2 | + |
| 3 | +Produces a report of API usage across code repositories, with version and target framework statistics, listing unused/removed APIs, and links to each usage. The output file format is [KDL](https://kdl.dev) for ease of human browsing and easily clickable links when viewing. The file is rewritten in real time as the analysis proceeds. |
| 4 | + |
| 5 | +See: [Prerelease status](#prerelease-status), [How to use](#how-to-use), [Command-line arguments](#command-line-arguments) |
| 6 | + |
| 7 | +Sample output: |
| 8 | + |
| 9 | +```kdl |
| 10 | +assembly SomeLibrary { |
| 11 | + current-api-source { |
| 12 | + repo "https://somewhere/SomeLibrary" |
| 13 | + branch main |
| 14 | + commit b7e651d52c9b19e6d6e0ca6b4389bc06ac3e7fde |
| 15 | + } |
| 16 | + stats { |
| 17 | + unused-api-count 4_000 |
| 18 | + removed-api-count 66 |
| 19 | + used-api-count 749 |
| 20 | + version "1.0.2.0" { |
| 21 | + repo "https://somewhere/ConsumingRepo1" { tfm net462 } |
| 22 | + } |
| 23 | + version "2.0.0.0" { |
| 24 | + repo "https://somewhere/ConsumingRepo2" { tfm net8.0 } |
| 25 | + repo "https://somewhere/ConsumingRepo3" { tfm net48 } |
| 26 | + } |
| 27 | + version "3.0.0.0" { |
| 28 | + repo "https://somewhere/ConsumingRepo2" { tfm net48; tfm net8.0-windows; tfm net9.0-windows } |
| 29 | + } |
| 30 | + } |
| 31 | +
|
| 32 | + unused-apis { |
| 33 | + api "SomeLibrary.AverageAccumulator.AverageAccumulator(double initialTotal, double initialCount)" url="https://somewhere/SomeLibrary?version=GBmain&path=src/SomeLibrary/AverageAccumulator.cs&line=13&lineEnd=17&lineStartColumn=9&lineEndColumn=10" { tfm net48; tfm net8.0 } |
| 34 | + api "SomeLibrary.AverageAccumulator.Count.get" url="https://somewhere/SomeLibrary?version=GBmain&path=src/SomeLibrary/AverageAccumulator.cs&line=51&lineEnd=51&lineStartColumn=29&lineEndColumn=37" { tfm net48; tfm net8.0 } |
| 35 | + api "SomeLibrary.AverageAccumulator.implicit operator double?(SomeLibrary.AverageAccumulator accumulator)" url="https://somewhere/SomeLibrary?version=GBmain&path=src/SomeLibrary/AverageAccumulator.cs&line=53&lineEnd=56&lineStartColumn=9&lineEndColumn=10" { tfm net48; tfm net8.0 } |
| 36 | + } |
| 37 | +
|
| 38 | + removed-apis { |
| 39 | + api "SomeLibrary.IAsyncDisposable" { |
| 40 | + version "2.0.0.0" { |
| 41 | + repo "https://somewhere/ConsumingRepo1" { |
| 42 | + reference "https://somewhere/ConsumingRepo1?version=GBmain&path=src/ConsumingProject1/AsyncDisposable.cs&line=12&lineEnd=12&lineStartColumn=5&lineEndColumn=112" { tfm net48 } |
| 43 | + reference "https://somewhere/ConsumingRepo1?version=GBmain&path=src/ConsumingProject1/AsyncDisposable.cs&line=6&lineEnd=6&lineStartColumn=39&lineEndColumn=64" { tfm net48 } |
| 44 | + // ... |
| 45 | + } |
| 46 | + repo "https://somewhere/ConsumingRepo2" { |
| 47 | + reference "https://somewhere/ConsumingRepo2?version=GBmain&path=src/ConsumingProject2/BatchReporting/Domain/MailingClaim.cs&line=5&lineEnd=5&lineStartColumn=40&lineEndColumn=56" { tfm net48 } |
| 48 | + reference "https://somewhere/ConsumingRepo2?version=GBmain&path=src/ConsumingProject2/BatchReporting/Domain/MailingClaimLifetime.cs&line=5&lineEnd=5&lineStartColumn=50&lineEndColumn=66" { tfm net48 } |
| 49 | + // ... |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + // ... |
| 54 | + } |
| 55 | +
|
| 56 | + api "SomeLibrary.AverageAccumulator" url="https://somewhere/SomeLibrary?version=GBmain&path=src/SomeLibrary/AverageAccumulator.cs&line=7&lineEnd=57&lineStartColumn=5&lineEndColumn=6" { |
| 57 | + version "2.0.0.0" { |
| 58 | + repo "https://somewhere/ConsumingRepo2" { |
| 59 | + reference "https://somewhere/ConsumingRepo2?version=GBmain&path=src/ConsumingProject2/PerfDataEntry/ReportHandler.cs&line=447&lineEnd=447&lineStartColumn=90&lineEndColumn=107" { tfm net48 } |
| 60 | + reference "https://somewhere/ConsumingRepo2?version=GBmain&path=src/ConsumingProject2/PerfDataEntry/ReportHandler.cs&line=451&lineEnd=451&lineStartColumn=90&lineEndColumn=107" { tfm net48 } |
| 61 | + // ... |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + api "SomeLibrary.AverageAccumulator.Add(double value, double weight)" url="https://somewhere/SomeLibrary?version=GBmain&path=src/SomeLibrary/AverageAccumulator.cs&line=35&lineEnd=47&lineStartColumn=9&lineEndColumn=10" { |
| 66 | + version "2.0.0.0" { |
| 67 | + repo "https://somewhere/ConsumingRepo2" { |
| 68 | + reference "https://somewhere/ConsumingRepo2?version=GBmain&path=src/ConsumingProject2/PerfDataEntry/DataEntrySummaryCalculator.cs&line=135&lineEnd=135&lineStartColumn=40&lineEndColumn=43" { tfm net48 } |
| 69 | + reference "https://somewhere/ConsumingRepo2?version=GBmain&path=src/ConsumingProject2/PerfDataEntry/DataEntrySummaryCalculator.cs&line=92&lineEnd=92&lineStartColumn=56&lineEndColumn=59" { tfm net48 } |
| 70 | + // ... |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + // ... |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## Prerelease status |
| 79 | + |
| 80 | +You may hit a NotImplementedException if there's syntax that the tool hasn't seen before. If you don't mind getting a little grease on your hands, this will pose no obstacle to you, and we would be delighted if you're inspired to share back as a pull request. Issues are welcome too. |
| 81 | + |
| 82 | +## How to use |
| 83 | + |
| 84 | +1. Clone this repository (example: `git clone https://github.com/Techsola/api-usage-analyzer`) |
| 85 | +2. Navigate to the `src\ApiUsageAnalyzer` subdirectory (example: `cd api-usage-analyzer\src\ApiUsageAnalyzer`) |
| 86 | +3. Type `dotnet run` followed by the arguments below. `--open-in-vs-code` is recommended. |
| 87 | +4. Wait a bit as repositories are cloned, restored, and analyzed. |
| 88 | +5. Peruse the `<assembly-name>.kdl` file in the working directory. |
| 89 | + |
| 90 | +## Command-line arguments |
| 91 | + |
| 92 | +``` |
| 93 | +USAGE: |
| 94 | + ApiUsageAnalyzer <assembly-name> [OPTIONS] |
| 95 | +
|
| 96 | +ARGUMENTS: |
| 97 | + <assembly-name> The name of the assembly defining the APIs whose usage will be reported |
| 98 | +
|
| 99 | +OPTIONS: |
| 100 | + -h, --help Prints help information |
| 101 | + --azdo <URLS> The Azure DevOps project collection URLs from which |
| 102 | + repositories are discovered. Repeat --azdo to specify |
| 103 | + more than one URL. Currently, only Git repositories and |
| 104 | + only the Azure DevOps platform are supported |
| 105 | + --defining-repo-name <NAME> If specified, only repositories with this name will be |
| 106 | + searched for the project that defines the API assembly |
| 107 | + --defining-repo-url <URL> The remote URL of an repository containing the project |
| 108 | + that defines the API assembly. The repository's current |
| 109 | + HEAD will be used to define the set of available APIs in |
| 110 | + the API assembly which will be used for the |
| 111 | + unused/removed API comparison |
| 112 | + --repo-package-id-filter <REGEX> If specified, repositories will be skipped unless they |
| 113 | + contain a project with a direct reference to a NuGet |
| 114 | + package with an ID that matches this regular expression |
| 115 | + --open-in-vs-code If specified, the output file will automatically open in |
| 116 | + VS Code. VS Code can monitor the live updates to the |
| 117 | + results |
| 118 | +``` |
0 commit comments