Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mnash 5154 skeleton operations #1011

Merged
24 changes: 19 additions & 5 deletions src/AutoRest.CSharp.V3/AutoRest/Plugins/CSharpGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(Task<CodeModel> codeModel
var restClientWriter = new RestClientWriter();
var serializeWriter = new SerializationWriter();
var headerModelModelWriter = new ResponseHeaderGroupWriter();
var resourceOperationWriter = new ResourceOperationWriter();

foreach (var model in context.Library.Models)
{
Expand Down Expand Up @@ -71,12 +72,26 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(Task<CodeModel> codeModel
project.AddGeneratedFile($"{responseHeaderModel.Type.Name}.cs", headerModelCodeWriter.ToString());
}

foreach (var client in context.Library.Clients)
if (!context.Configuration.AzureArm)
m-nash marked this conversation as resolved.
Show resolved Hide resolved
{
var codeWriter = new CodeWriter();
clientWriter.WriteClient(codeWriter, client, context.Configuration);
foreach (var client in context.Library.Clients)
{
var codeWriter = new CodeWriter();
clientWriter.WriteClient(codeWriter, client, context.Configuration);

project.AddGeneratedFile($"{client.Type.Name}.cs", codeWriter.ToString());
project.AddGeneratedFile($"{client.Type.Name}.cs", codeWriter.ToString());
}
}

if (context.Configuration.AzureArm)
{
foreach (var resourceOperation in context.Library.ResourceOperations)
{
var codeWriter = new CodeWriter();
resourceOperationWriter.WriteClient(codeWriter, resourceOperation);

project.AddGeneratedFile($"{resourceOperation.Type.Name}.cs", codeWriter.ToString());
}
}

foreach (var operation in context.Library.LongRunningOperations)
Expand All @@ -96,7 +111,6 @@ public async Task<GeneratedCodeWorkspace> ExecuteAsync(Task<CodeModel> codeModel
var clientCodeWriter = new CodeWriter();
ManagementClientWriter.WriteAggregateClient(clientCodeWriter, context);
project.AddGeneratedFile($"{context.Configuration.LibraryName}ManagementClient.cs", clientCodeWriter.ToString());

}

return project;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.Collections.Generic;
using System.Text;
using AutoRest.CSharp.V3.Output.Models;

namespace AutoRest.CSharp.V3.Generation.Writers
{
internal class ResourceOperationWriter
{
public void WriteClient(CodeWriter writer, ResourceOperation resourceOperation)
{
var cs = resourceOperation.Type;
var @namespace = cs.Namespace;
using (writer.Namespace(@namespace))
{
writer.WriteXmlDocumentationSummary(resourceOperation.Description);
using (writer.Scope($"{resourceOperation.Declaration.Accessibility} partial class {cs.Name}"))
{
}
}
}
}
}
4 changes: 1 addition & 3 deletions src/AutoRest.CSharp.V3/Output/Models/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace AutoRest.CSharp.V3.Output.Models
internal class Client: ClientBase
{
private readonly OperationGroup _operationGroup;
private readonly BuildContext _context;
private PagingMethod[]? _pagingMethods;
private ClientMethod[]? _methods;
private LongRunningOperationMethod[]? _longRunningOperationMethods;
Expand All @@ -25,7 +24,6 @@ internal class Client: ClientBase
public Client(OperationGroup operationGroup, BuildContext context): base(context)
{
_operationGroup = operationGroup;
_context = context;

var clientPrefix = GetClientPrefix(operationGroup.Language.Default.Name);
DefaultName = clientPrefix + ClientSuffix;
Expand Down Expand Up @@ -120,4 +118,4 @@ private IEnumerable<ClientMethod> BuildMethods()
}
}
}
}
}
2 changes: 0 additions & 2 deletions src/AutoRest.CSharp.V3/Output/Models/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ internal abstract class ClientBase: TypeProvider
private const string OperationsSuffixValue = "Operations";
protected string ClientSuffix { get; }

private readonly BuildContext _context;
private readonly TypeFactory _typeFactory;

protected ClientBase(BuildContext context): base(context)
{
ClientSuffix = context.Configuration.AzureArm ? OperationsSuffixValue : ClientSuffixValue;
_typeFactory = context.TypeFactory;
_context = context;
}

protected Parameter BuildParameter(RequestParameter requestParameter)
Expand Down
59 changes: 59 additions & 0 deletions src/AutoRest.CSharp.V3/Output/Models/ResourceOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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.Collections.Generic;
using System.Text;
using AutoRest.CSharp.V3.Input;
using AutoRest.CSharp.V3.Output.Builders;
using AutoRest.CSharp.V3.Output.Models.Types;
using AutoRest.CSharp.V3.Utilities;

namespace AutoRest.CSharp.V3.Output.Models
{
internal class ResourceOperation : TypeProvider
{
private const string ClientSuffixValue = "Client";
private const string OperationsSuffixValue = "Operations";
private OperationGroup _operationGroup;

public ResourceOperation(OperationGroup operationGroup, BuildContext context)
: base(context)
{
_operationGroup = operationGroup;
var clientPrefix = GetClientPrefix(operationGroup.Language.Default.Name);
m-nash marked this conversation as resolved.
Show resolved Hide resolved
DefaultName = clientPrefix + OperationsSuffixValue;
}

protected override string DefaultName { get; }

protected override string DefaultAccessibility { get; } = "public";

public string Description => BuilderHelpers.EscapeXmlDescription(CreateDescription(_operationGroup, GetClientPrefix(Declaration.Name)));

protected string GetClientPrefix(string name)
{
name = string.IsNullOrEmpty(name) ? _context.CodeModel.Language.Default.Name : name.ToCleanName();

if (name.EndsWith(OperationsSuffixValue) && name.Length >= OperationsSuffixValue.Length)
{
name = name.Substring(0, name.Length - OperationsSuffixValue.Length);
}

if (name.EndsWith(ClientSuffixValue) && name.Length >= ClientSuffixValue.Length)
{
name = name.Substring(0, name.Length - ClientSuffixValue.Length);
}

return name;
}

protected static string CreateDescription(OperationGroup operationGroup, string clientPrefix)
{
StringBuilder summary = new StringBuilder();
return string.IsNullOrWhiteSpace(operationGroup.Language.Default.Description) ?
$"A class representing the operations that can be performed over a specific {clientPrefix}." :
BuilderHelpers.EscapeXmlDescription(operationGroup.Language.Default.Description);
}
}
}
2 changes: 0 additions & 2 deletions src/AutoRest.CSharp.V3/Output/Models/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ internal class RestClient : ClientBase
protected string RestClientSuffix { get; }

private readonly OperationGroup _operationGroup;
private readonly BuildContext _context;
private readonly Dictionary<string, Parameter> _parameters;
private readonly SerializationBuilder _serializationBuilder;
private Dictionary<ServiceRequest, RestClientMethod>? _requestMethods;
Expand All @@ -43,7 +42,6 @@ internal class RestClient : ClientBase
public RestClient(OperationGroup operationGroup, BuildContext context) : base(context)
{
_operationGroup = operationGroup;
_context = context;
_parameters = operationGroup.Operations
.SelectMany(op => op.Parameters.Concat(op.Requests.SelectMany(r => r.Parameters)))
.Where(p => p.Implementation == ImplementationLocation.Client)
Expand Down
4 changes: 1 addition & 3 deletions src/AutoRest.CSharp.V3/Output/Models/Types/EnumType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace AutoRest.CSharp.V3.Output.Models.Types
{
internal class EnumType : TypeProvider
{
private readonly BuildContext _context;
private readonly IEnumerable<ChoiceValue> _choices;
private readonly ModelTypeMapping? _typeMapping;
private IList<EnumTypeValue>? _values;
Expand All @@ -31,7 +30,6 @@ public EnumType(SealedChoiceSchema schema, BuildContext context)

private EnumType(Schema schema, BuildContext context, Schema baseType, IEnumerable<ChoiceValue> choices, bool isExtendable) : base(context)
{
_context = context;
_choices = choices;

DefaultName = schema.CSharpName();
Expand Down Expand Up @@ -102,4 +100,4 @@ private static string CreateDescription(ChoiceValue choiceValue)
return string.IsNullOrWhiteSpace(choiceValue.Language.Default.Description) ? choiceValue.Value : BuilderHelpers.EscapeXmlDescription(choiceValue.Language.Default.Description);
}
}
}
}
19 changes: 19 additions & 0 deletions src/AutoRest.CSharp.V3/Output/Models/Types/OutputLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class OutputLibrary
private Dictionary<Schema, TypeProvider>? _models;
private Dictionary<OperationGroup, Client>? _clients;
private Dictionary<OperationGroup, RestClient>? _restClients;
private Dictionary<OperationGroup, ResourceOperation>? _resourceOperations;
private Dictionary<Operation, LongRunningOperation>? _operations;
private Dictionary<Operation, ResponseHeaderGroupType>? _headerModels;

Expand All @@ -31,6 +32,8 @@ public OutputLibrary(CodeModel codeModel, BuildContext context)

public IEnumerable<RestClient> RestClients => EnsureRestClients().Values;

public IEnumerable<ResourceOperation> ResourceOperations => EnsureResourceOperation().Values;

public IEnumerable<Client> Clients => EnsureClients().Values;

public IEnumerable<LongRunningOperation> LongRunningOperations => EnsureLongRunningOperations().Values;
Expand Down Expand Up @@ -122,6 +125,22 @@ private Dictionary<OperationGroup, RestClient> EnsureRestClients()
return _restClients;
}

private Dictionary<OperationGroup, ResourceOperation> EnsureResourceOperation()
{
if (_resourceOperations != null)
{
return _resourceOperations;
}

_resourceOperations = new Dictionary<OperationGroup, ResourceOperation>();
foreach (var operationGroup in _codeModel.OperationGroups)
{
_resourceOperations.Add(operationGroup, new ResourceOperation(operationGroup, _context));
}

return _resourceOperations;
}

public TypeProvider FindTypeForSchema(Schema schema)
{
return SchemaMap[schema];
Expand Down
2 changes: 1 addition & 1 deletion src/AutoRest.CSharp.V3/Output/Models/Types/TypeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace AutoRest.CSharp.V3.Output.Models.Types
{
internal abstract class TypeProvider
{
private readonly BuildContext _context;
protected readonly BuildContext _context;
private readonly Lazy<INamedTypeSymbol?> _existingType;
private TypeDeclarationOptions? _type;

Expand Down
4 changes: 4 additions & 0 deletions src/AutoRest.CSharp.V3/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@
"xms-error-responses": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\TestServerProjects\\xms-error-responses"
},
"compute": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\TestProjects\\compute"
}
}
}