Skip to content

Commit

Permalink
Include sample-gen by default when generating sdk code (mgmt) (#3673)
Browse files Browse the repository at this point in the history
* update to use testmodeler2.6.1

* Include sample-gen in when generating code

* fix some naming

* fix test project

* fix path format

* avoid include original file for MgmtMockAndSample project

---------

Co-authored-by: Arthur Ma <arthurma@microsoft.com>
  • Loading branch information
RodgeFu and ArthurMa1978 authored Sep 7, 2023
1 parent f529082 commit 4d25396
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 52 deletions.
23 changes: 14 additions & 9 deletions src/AutoRest.CSharp/Common/AutoRest/Plugins/CSharpGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
using System.Threading.Tasks;
using AutoRest.CSharp.AutoRest.Communication;
using AutoRest.CSharp.Common.Input;
using AutoRest.CSharp.Common.Utilities;
using AutoRest.CSharp.Input;
using AutoRest.CSharp.Input.Source;
using AutoRest.CSharp.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace AutoRest.CSharp.AutoRest.Plugins
{
Expand All @@ -32,14 +32,17 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(CodeModel codeModel)
}
else if (Configuration.AzureArm)
{
if (Configuration.MgmtTestConfiguration is not null)
if (Configuration.MgmtConfiguration.MgmtDebug.SkipCodeGen)
{
// we currently do not need this sourceInputModel when generating the test code because it only has information about the "non-generated" test code.
await MgmtTestTarget.ExecuteAsync(project, codeModel);
await AutoRestLogger.Warning("skip generating sdk code because 'mgmt-debug.skip-codegen' is true.");
if (Configuration.MgmtTestConfiguration is not null)
await MgmtTestTarget.ExecuteAsync(project, codeModel, null);
}
else
{
await MgmtTarget.ExecuteAsync(project, codeModel, sourceInputModel);
if (Configuration.MgmtTestConfiguration is not null)
await MgmtTestTarget.ExecuteAsync (project, codeModel, sourceInputModel);
}
}
else
Expand All @@ -60,7 +63,7 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(InputNamespace rootNamesp
return project;
}

private static void ValidateConfiguration ()
private static void ValidateConfiguration()
{
if (Configuration.Generation1ConvenienceClient && Configuration.AzureArm)
{
Expand All @@ -82,7 +85,7 @@ public async Task<bool> Execute(IPluginCommunication autoRest)

if (!Path.IsPathRooted(Configuration.OutputFolder))
{
await autoRest.Warning("output-folder path should be an absolute path");
await AutoRestLogger.Warning("output-folder path should be an absolute path");
}
if (Configuration.SaveInputs)
{
Expand All @@ -95,12 +98,14 @@ public async Task<bool> Execute(IPluginCommunication autoRest)
var project = await ExecuteAsync(codeModel);
await foreach (var file in project.GetGeneratedFilesAsync())
{
await autoRest.WriteFile(file.Name, file.Text, "source-file-csharp");
// format all \ to / in filename, otherwise they will be treated as escape char when sending to autorest service
var filename = file.Name.Replace('\\', '/');
await autoRest.WriteFile(filename, file.Text, "source-file-csharp");
}
}
catch (ErrorHelpers.ErrorException e)
{
await autoRest.Fatal(e.ErrorText);
await AutoRestLogger.Fatal(e.ErrorText);
return false;
}
catch (Exception e)
Expand All @@ -118,7 +123,7 @@ public async Task<bool> Execute(IPluginCommunication autoRest)
{
// Ignore any errors while trying to output crash information
}
await autoRest.Fatal($"Internal error in AutoRest.CSharp{ErrorHelpers.FileIssueText}\nException: {e.Message}\n{e.StackTrace}");
await AutoRestLogger.Fatal($"Internal error in AutoRest.CSharp{ErrorHelpers.FileIssueText}\nException: {e.Message}\n{e.StackTrace}");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ private static T GetRequiredOption<T>(IPluginCommunication autoRest, string name
return autoRest.GetValue<T>(name).GetAwaiter().GetResult() ?? throw new InvalidOperationException($"{name} configuration parameter is required");
}

private static string TrimFileSuffix(string path)
internal static string TrimFileSuffix(string path)
{
if (Uri.IsWellFormedUriString(path, UriKind.Absolute))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json;
using System.Threading.Tasks;
using AutoRest.CSharp.AutoRest.Communication;
using AutoRest.CSharp.Common.Utilities;
using AutoRest.CSharp.Input;
using AutoRest.CSharp.Utilities;

Expand All @@ -34,6 +35,7 @@ public static async Task<bool> Start(IPluginCommunication autoRest)
Console.Error.WriteLine("Attempting to attach debugger.");
System.Diagnostics.Debugger.Launch();
}
AutoRestLogger.Initialize(autoRest);
return await plugin.Execute(autoRest);
}
catch (Exception e)
Expand Down
34 changes: 34 additions & 0 deletions src/AutoRest.CSharp/Common/Utilities/AutoRestLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using AutoRest.CSharp.AutoRest.Communication;

namespace AutoRest.CSharp.Common.Utilities
{
internal static class AutoRestLogger
{
private static IPluginCommunication? _autoRest = null;
public static void Initialize(IPluginCommunication autoRest)
{
_autoRest = autoRest;
}

public static bool IsInitialized => _autoRest != null;

public static async Task Warning(string message)
{
if (!IsInitialized)
throw new InvalidOperationException("AutoRestLogger.Warning is called before initialized");
await _autoRest!.Warning(message);
}

public static async Task Fatal(string message)
{
if (!IsInitialized)
throw new InvalidOperationException("AutoRestLogger.Fatal is called before initialized");
await _autoRest!.Fatal(message);
}
}
}
13 changes: 10 additions & 3 deletions src/AutoRest.CSharp/Mgmt/AutoRest/MgmtConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ public class MgmtDebugConfiguration

public bool ShowSerializedNames { get; }

public bool SkipCodeGen { get; }

public MgmtDebugConfiguration(
JsonElement? suppressListException = default,
JsonElement? showSerializedNames = default
JsonElement? showSerializedNames = default,
JsonElement? skipCodeGen = default
)
{
SuppressListException = Configuration.DeserializeBoolean(suppressListException, false);
ShowSerializedNames = Configuration.DeserializeBoolean(showSerializedNames, false);
SkipCodeGen = Configuration.DeserializeBoolean(skipCodeGen, false);
}

internal static MgmtDebugConfiguration LoadConfiguration(JsonElement root)
Expand All @@ -37,18 +41,21 @@ internal static MgmtDebugConfiguration LoadConfiguration(JsonElement root)

root.TryGetProperty(nameof(SuppressListException), out var suppressListException);
root.TryGetProperty(nameof(ShowSerializedNames), out var showSerializedNames);
root.TryGetProperty(nameof(SkipCodeGen), out var skipCodeGen);

return new MgmtDebugConfiguration(
suppressListException: suppressListException,
showSerializedNames: showSerializedNames
showSerializedNames: showSerializedNames,
skipCodeGen: skipCodeGen
);
}

internal static MgmtDebugConfiguration GetConfiguration(IPluginCommunication autoRest)
{
return new MgmtDebugConfiguration(
suppressListException: autoRest.GetValue<JsonElement?>(string.Format(MgmtDebugOptionsFormat, "suppress-list-exception")).GetAwaiter().GetResult(),
showSerializedNames: autoRest.GetValue<JsonElement?>(string.Format(MgmtDebugOptionsFormat, "show-serialized-names")).GetAwaiter().GetResult()
showSerializedNames: autoRest.GetValue<JsonElement?>(string.Format(MgmtDebugOptionsFormat, "show-serialized-names")).GetAwaiter().GetResult(),
skipCodeGen: autoRest.GetValue<JsonElement?>(string.Format(MgmtDebugOptionsFormat, "skip-codegen")).GetAwaiter().GetResult()
);
}

Expand Down
22 changes: 17 additions & 5 deletions src/AutoRest.CSharp/MgmtTest/AutoRest/MgmtTestConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,30 @@ namespace AutoRest.CSharp.Input
{
internal class MgmtTestConfiguration
{
internal const string TestGenOptionsRoot = "testgen";
internal const string TestGenOptionsRoot = "sample-gen";
private const string TestGenOptionsFormat = $"{TestGenOptionsRoot}.{{0}}";

public string? SourceCodePath { get; }
public string? OutputFolder { get; }
public bool Mock { get; }
public bool Sample { get; }
public IReadOnlyList<string> SkippedOperations { get; }
public bool ClearOutputFolder { get; }

public MgmtTestConfiguration(
IReadOnlyList<string> skippedOperations,
JsonElement? sourceCodePath = default,
JsonElement? mock = default,
JsonElement? sample = default)
JsonElement? sample = default,
JsonElement? outputFolder = default,
JsonElement? clearOutputFolder = default)
{
SkippedOperations = skippedOperations;
SourceCodePath = !Configuration.IsValidJsonElement(sourceCodePath) ? null : sourceCodePath.ToString();
Mock = Configuration.DeserializeBoolean(mock, false);
Sample = Configuration.DeserializeBoolean(sample, false);
Sample = Configuration.DeserializeBoolean(sample, true);
OutputFolder = !Configuration.IsValidJsonElement(outputFolder) ? null : Configuration.TrimFileSuffix(outputFolder.ToString() ?? "");
ClearOutputFolder = Configuration.DeserializeBoolean(clearOutputFolder, false);
}

internal static MgmtTestConfiguration? LoadConfiguration(JsonElement root)
Expand All @@ -42,14 +48,18 @@ public MgmtTestConfiguration(
testGenRoot.TryGetProperty(nameof(SourceCodePath), out var sourceCodePath);
testGenRoot.TryGetProperty(nameof(Mock), out var mock);
testGenRoot.TryGetProperty(nameof(Sample), out var sample);
testGenRoot.TryGetProperty(nameof(OutputFolder), out var testGenOutputFolder);
testGenRoot.TryGetProperty(nameof(ClearOutputFolder), out var testGenClearOutputFolder);

var skippedOperations = Configuration.DeserializeArray(skippedOperationsElement);

return new MgmtTestConfiguration(
skippedOperations,
sourceCodePath: sourceCodePath,
mock: mock,
sample: sample);
sample: sample,
outputFolder: testGenOutputFolder,
clearOutputFolder: testGenClearOutputFolder);
}

internal static MgmtTestConfiguration? GetConfiguration(IPluginCommunication autoRest)
Expand All @@ -61,7 +71,9 @@ public MgmtTestConfiguration(
skippedOperations: autoRest.GetValue<string[]?>(string.Format(TestGenOptionsFormat, "skipped-operations")).GetAwaiter().GetResult() ?? Array.Empty<string>(),
sourceCodePath: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "source-path")).GetAwaiter().GetResult(),
mock: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "mock")).GetAwaiter().GetResult(),
sample: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "sample")).GetAwaiter().GetResult());
sample: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "sample")).GetAwaiter().GetResult(),
outputFolder: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "output-folder")).GetAwaiter().GetResult(),
clearOutputFolder: autoRest.GetValue<JsonElement?>(string.Format(TestGenOptionsFormat, "clear-output-folder")).GetAwaiter().GetResult());
}

internal void SaveConfiguration(Utf8JsonWriter writer)
Expand Down
11 changes: 7 additions & 4 deletions src/AutoRest.CSharp/MgmtTest/AutoRest/MgmtTestOutputLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ internal class MgmtTestOutputLibrary
private readonly MgmtTestConfiguration _mgmtTestConfiguration;
public MgmtTestOutputLibrary(CodeModel codeModel, SourceInputModel sourceInputModel)
{
MgmtContext.Initialize(new BuildContext<MgmtOutputLibrary>(codeModel, sourceInputModel));

// force trigger the model initialization
foreach (var _ in MgmtContext.Library.ResourceSchemaMap)
if (!MgmtContext.IsInitialized)
{
MgmtContext.Initialize(new BuildContext<MgmtOutputLibrary>(codeModel, sourceInputModel));

// force trigger the model initialization
foreach (var _ in MgmtContext.Library.ResourceSchemaMap)
{
}
}

_mockTestModel = MgmtContext.CodeModel.TestModel!.MockTest;
Expand Down
Loading

0 comments on commit 4d25396

Please sign in to comment.