Skip to content

Commit

Permalink
Merge pull request #2305 from microsoftgraph/dev
Browse files Browse the repository at this point in the history
Merge Dev Branch into Master
  • Loading branch information
andrueastman authored Jan 27, 2025
2 parents 5102e6a + cdafba3 commit d107f86
Show file tree
Hide file tree
Showing 89 changed files with 2,283 additions and 1,510 deletions.
18 changes: 7 additions & 11 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@ jobs:
with:
distribution: 'adopt'
java-version: 17
- name: Setup .NET 5 # At the moment the scanner requires dotnet 5 https://www.nuget.org/packages/dotnet-sonarscanner
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 5.0.x
- name: Setup .NET 6
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.x
- name: Setup .NET 7
uses: actions/setup-dotnet@v4
with:
dotnet-version: 7.0.x
with: # At the moment the scanner requires dotnet 5 https://www.nuget.org/packages/dotnet-sonarscanner
dotnet-version: |
5.x
6.x
7.x
8.x
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
Expand Down
10 changes: 7 additions & 3 deletions CodeSnippetsPipeline.Test/CodeSnippetsPipeline.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0">
<PackageReference Include="NUnit.Analyzers" Version="4.6.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PackageReference Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
6 changes: 5 additions & 1 deletion CodeSnippetsReflection.App/CodeSnippetsReflection.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="9.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
47 changes: 20 additions & 27 deletions CodeSnippetsReflection.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
Expand All @@ -24,7 +25,7 @@ namespace CodeSnippetsReflection.App
/// </summary>
class Program
{
static void Main(string[] args)
static async Task Main(string[] args)
{
IConfiguration config = new ConfigurationBuilder()
.AddCommandLine(args)
Expand All @@ -36,7 +37,7 @@ static void Main(string[] args)
var generationArg = config.GetSection("Generation");
if (!snippetsPathArg.Exists() || !languagesArg.Exists())
{
Console.Error.WriteLine("Http snippets directory and languages should be specified");
await Console.Error.WriteLineAsync("Http snippets directory and languages should be specified");
Console.WriteLine(@"Example usage:
.\CodeSnippetReflection.App.exe --SnippetsPath C:\snippets --Languages c#,javascript --Generation odata|openapi");
return;
Expand All @@ -45,13 +46,13 @@ static void Main(string[] args)
var httpSnippetsDir = snippetsPathArg.Value;
if (!Directory.Exists(httpSnippetsDir))
{
Console.Error.WriteLine($@"Directory {httpSnippetsDir} does not exist!");
await Console.Error.WriteLineAsync($@"Directory {httpSnippetsDir} does not exist!");
return;
}

if (customMetadataPathArg.Exists() && !File.Exists(customMetadataPathArg.Value))
{
Console.Error.WriteLine($@"Metadata file {customMetadataPathArg.Value} does not exist!");
await Console.Error.WriteLineAsync($@"Metadata file {customMetadataPathArg.Value} does not exist!");
return;
}

Expand All @@ -67,12 +68,12 @@ static void Main(string[] args)
.GroupBy(l => ODataSnippetsGenerator.SupportedLanguages.Contains(l) || OpenApiSnippetsGenerator.SupportedLanguages.Contains(l))
.ToDictionary(g => g.Key, g => g.ToList());

var supportedLanguages = languageGroups.ContainsKey(true) ? languageGroups[true] : null;
var unsupportedLanguages = languageGroups.ContainsKey(false) ? languageGroups[false] : null;
var supportedLanguages = languageGroups.GetValueOrDefault(true, null);
var unsupportedLanguages = languageGroups.GetValueOrDefault(false, null);

if (supportedLanguages == null)
{
Console.Error.WriteLine($"None of the given languages are supported. Supported languages: {string.Join(" ", ODataSnippetsGenerator.SupportedLanguages)}");
await Console.Error.WriteLineAsync($"None of the given languages are supported. Supported languages: {string.Join(" ", ODataSnippetsGenerator.SupportedLanguages)}");
return;
}

Expand All @@ -94,18 +95,15 @@ static void Main(string[] args)

// cache the generators by generation rather than creating a new one on each generation to avoid multiple loads of the metadata.
var snippetGenerators = new ConcurrentDictionary<string, ISnippetsGenerator>();
Parallel.ForEach(supportedLanguages, language =>
await Task.WhenAll(supportedLanguages.Select(language =>
{
//Generation will still be originalGeneration if language is java since it is not stable
//Remove the condition when java is stable
generation = (OpenApiSnippetsGenerator.SupportedLanguages.Contains(language)) ? "openapi" : originalGeneration;

var generator = snippetGenerators.GetOrAdd(generation, generationKey => GetSnippetsGenerator(generationKey, customMetadataPathArg));
Parallel.ForEach(files, file =>
{
ProcessFile(generator, language, file);
});
});
return files.Select(file => ProcessFileAsync(generator, language, file));
}).SelectMany(static t => t));
Console.WriteLine($"Processed {files.Count()} files.");
}

Expand All @@ -118,43 +116,38 @@ private static ISnippetsGenerator GetSnippetsGenerator(string generation, IConfi
};
}

private static void ProcessFile(ISnippetsGenerator generator, string language, string file)
private static async Task ProcessFileAsync(ISnippetsGenerator generator, string language, string file)
{
// convert http request into a type that works with SnippetGenerator.ProcessPayloadRequest()
// we are not altering the types as it should continue serving the HTTP endpoint as well
using var streamContent = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(file))));
using var streamContent = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(await File.ReadAllTextAsync(file))));
streamContent.Headers.Add("Content-Type", "application/http;msgtype=request");

string snippet;
var filePath = file.Replace("-httpSnippet", $"---{language.ToLowerInvariant()}");
try
{
// This is a very fast operation, it is fine to make is synchronuous.
// With the parallel foreach in the main method, processing all snippets for C# in both Beta and V1 takes about 7 seconds.
// As of this writing, the code was processing 2650 snippets
// Using async-await is costlier as this operation is all in-memory and task creation and scheduling overhead is high for that.
// With async-await, the same operation takes 1 minute 7 seconds.
using var message = streamContent.ReadAsHttpRequestMessageAsync().Result;
snippet = generator.ProcessPayloadRequest(message, language);
using var message = await streamContent.ReadAsHttpRequestMessageAsync();
snippet = await generator.ProcessPayloadRequestAsync(message, language);
}
catch (Exception e)
{
var message = $"Exception while processing {file}.{Environment.NewLine}{e.Message}{Environment.NewLine}{e.StackTrace}";
Console.Error.WriteLine(message);
File.WriteAllText(filePath + "-error", message);
await Console.Error.WriteLineAsync(message);
await File.WriteAllTextAsync(filePath + "-error", message);
return;
}

if (!string.IsNullOrWhiteSpace(snippet))
{
Console.WriteLine($"Writing snippet: {filePath}");
File.WriteAllText(filePath, snippet);
await File.WriteAllTextAsync(filePath, snippet);
}
else
{
var message = $"Failed to generate {language} snippets for {file}.";
File.WriteAllText(filePath + "-error", message);
Console.Error.WriteLine(message);
await File.WriteAllTextAsync(filePath + "-error", message);
await Console.Error.WriteLineAsync(message);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<PackageReference Include="coverlet.msbuild" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit d107f86

Please sign in to comment.