未验证 提交 80a76ea0 编写于 作者: J Joey Robichaud 提交者: GitHub

Merge pull request #47855 from JoeRobich/action-telemetry-builder

Add tool to generate table of hashes for CodeActions
此差异已折叠。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<!--
Since the old hash function was framework and runtime dependent and VS runs 32-bit, we need to
run FullFramework 32-bit in order to generate the same hashes.
-->
<TargetFrameworks>net472</TargetFrameworks>
<Platforms>x86</Platforms>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Compilers\Core\Portable\Microsoft.CodeAnalysis.csproj" />
<ProjectReference Include="..\..\Compilers\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.csproj" />
<ProjectReference Include="..\..\Compilers\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.vbproj" />
<ProjectReference Include="..\..\Features\Core\Portable\Microsoft.CodeAnalysis.Features.csproj" />
<ProjectReference Include="..\..\Features\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.Features.csproj" />
<ProjectReference Include="..\..\Features\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.Features.vbproj" />
<ProjectReference Include="..\..\Workspaces\Core\Portable\Microsoft.CodeAnalysis.Workspaces.csproj" />
<ProjectReference Include="..\..\Workspaces\CSharp\Portable\Microsoft.CodeAnalysis.CSharp.Workspaces.csproj" />
<ProjectReference Include="..\..\Workspaces\VisualBasic\Portable\Microsoft.CodeAnalysis.VisualBasic.Workspaces.vbproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Compilers\Core\Portable\InternalUtilities\Hash.cs" Link="Utilities\Hash.cs" />
<Compile Include="..\..\EditorFeatures\Core\Shared\Extensions\TelemetryExtensions.cs" Link="Utilities\TelemetryExtensions.cs" />
</ItemGroup>
</Project>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using TelemetryInfo = System.Tuple<string, string, string>;
namespace BuildActionTelemetryTable
{
public class Program
{
private static readonly string s_executingPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
public static void Main(string[] args)
{
Console.WriteLine("Loading assemblies and finding CodeActions ...");
var assemblies = GetAssemblies(args);
var codeActionTypes = GetCodeActionTypes(assemblies);
Console.WriteLine($"Generating Kusto datatable of {codeActionTypes.Length} CodeAction hashes ...");
var telemetryInfos = GetTelemetryInfos(codeActionTypes);
var datatable = GenerateKustoDatatable(telemetryInfos);
var filepath = Path.GetFullPath(".\\ActionTable.txt");
Console.WriteLine($"Writing datatable to {filepath} ...");
File.WriteAllText(filepath, datatable);
Console.WriteLine("Complete.");
}
internal static ImmutableArray<Assembly> GetAssemblies(string[] paths)
{
if (paths.Length == 0)
{
// By default inspect the Roslyn assemblies
paths = Directory.EnumerateFiles(s_executingPath, "Microsoft.CodeAnalysis*.dll")
.ToArray();
}
var currentDirectory = new Uri(Environment.CurrentDirectory + "\\");
return paths.Select(path =>
{
Console.WriteLine($"Loading assembly from {GetRelativePath(path, currentDirectory)}.");
return Assembly.LoadFrom(path);
}).ToImmutableArray();
static string GetRelativePath(string path, Uri baseUri)
{
var rootedPath = Path.IsPathRooted(path)
? path
: Path.GetFullPath(path);
var relativePath = baseUri.MakeRelativeUri(new Uri(rootedPath));
return relativePath.ToString();
}
}
internal static ImmutableArray<Type> GetCodeActionTypes(IEnumerable<Assembly> assemblies)
{
var types = assemblies.SelectMany(
assembly => assembly.GetTypes().Where(
type => !type.GetTypeInfo().IsInterface && !type.GetTypeInfo().IsAbstract));
return types
.Where(t => typeof(CodeAction).IsAssignableFrom(t))
.ToImmutableArray();
}
internal static ImmutableArray<TelemetryInfo> GetTelemetryInfos(ImmutableArray<Type> codeActionTypes)
{
return codeActionTypes.Select(GetTelemetryInfo)
.ToImmutableArray();
static TelemetryInfo GetTelemetryInfo(Type type)
{
var telemetryId = type.GetTelemetryId().ToString();
return Tuple.Create(type.FullName, telemetryId.Substring(0, 8), telemetryId.Substring(19));
}
}
internal static string GenerateKustoDatatable(ImmutableArray<TelemetryInfo> telemetryInfos)
{
var table = new StringBuilder();
table.AppendLine("let actions = datatable(ActionName: string, Prefix: string, Suffix: string)");
table.AppendLine("[");
foreach (var (actionTypeName, prefix, suffix) in telemetryInfos)
{
table.AppendLine(@$" ""{actionTypeName}"", ""{prefix}"", ""{suffix}"",");
}
table.Append("];");
return table.ToString();
}
}
}
# BuildActionTelemetryTable
This tool generates a Kusto datatable containing CodeAction names and telemetry hashes for analyzing VS LightBulbSessions.
## How to use
By default this tool will generate the table for the Roslyn CodeActions.
```
BuildActionTelemetryTable
```
To generate a table for custom CodeActions you can specify the assembly names to inspect as space separated arguments on the commandline.
```
BuildActionTelemetryTable ./path/StyleCop.Analyzers.dll ./path/StyleCop.Analyzer.CodeFixes.dll
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册