Skip to content

Commit f4f8630

Browse files
authored
Generate ArmOperation for LRO in Azure plugin (Azure#48108)
1 parent df8c6b3 commit f4f8630

39 files changed

+1197
-98
lines changed

eng/Packages.Data.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@
261261
</ItemGroup>
262262

263263
<ItemGroup Condition="'$(IsGeneratorLibrary)' == 'true'">
264-
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20250127.1" />
264+
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20250205.1" />
265265
</ItemGroup>
266266

267267
<!--

eng/packages/http-client-csharp/generator/Azure.Generator/src/Azure.Generator.csproj

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="Azure.Core" />
10+
<PackageReference Include="Azure.ResourceManager" />
1011
<PackageReference Include="Microsoft.Generator.CSharp.ClientModel" />
1112
</ItemGroup>
1213

@@ -34,6 +35,57 @@
3435
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\RawRequestUriBuilder.cs" LinkBase="Shared/Core">
3536
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3637
</Compile>
38+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\OperationInternal.cs" LinkBase="Shared/Core">
39+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
40+
</Compile>
41+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\NextLinkOperationImplementation.cs" LinkBase="Shared/Core">
42+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
43+
</Compile>
44+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\ClientDiagnostics.cs" LinkBase="Shared/Core">
45+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
46+
</Compile>
47+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\DiagnosticScopeFactory.cs" LinkBase="Shared/Core">
48+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
49+
</Compile>
50+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\DiagnosticScope.cs" LinkBase="Shared/Core">
51+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
52+
</Compile>
53+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\HttpMessageSanitizer.cs" LinkBase="Shared/Core">
54+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
55+
</Compile>
56+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\AppContextSwitchHelper.cs" LinkBase="Shared/Core">
57+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
58+
</Compile>
59+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\OperationFinalStateVia.cs" LinkBase="Shared/Core">
60+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
61+
</Compile>
62+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\SequentialDelayStrategy.cs" LinkBase="Shared/Core">
63+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
64+
</Compile>
65+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\IOperationSource.cs" LinkBase="Shared/Core">
66+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
67+
</Compile>
68+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\OperationInternalOfT.cs" LinkBase="Shared/Core">
69+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
70+
</Compile>
71+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\OperationInternalBase.cs" LinkBase="Shared/Core">
72+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
73+
</Compile>
74+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\AsyncLockWithValue.cs" LinkBase="Shared/Core">
75+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
76+
</Compile>
77+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\OperationPoller.cs" LinkBase="Shared/Core">
78+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
79+
</Compile>
80+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\TaskExtensions.cs" LinkBase="Shared/Core">
81+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
82+
</Compile>
83+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\VoidValue.cs" LinkBase="Shared/Core">
84+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
85+
</Compile>
86+
<Compile Include="$(MSBuildThisFileDirectory)..\..\..\..\..\..\sdk\core\Azure.Core\src\Shared\FixedDelayWithNoJitterStrategy.cs" LinkBase="Shared/Core">
87+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
88+
</Compile>
3789
</ItemGroup>
3890

3991
</Project>

eng/packages/http-client-csharp/generator/Azure.Generator/src/AzureOutputLibrary.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,20 @@ private Dictionary<string, OperationSet> CategorizeClients()
7676

7777
/// <inheritdoc/>
7878
// TODO: generate resources and collections
79-
protected override TypeProvider[] BuildTypeProviders() => [.. base.BuildTypeProviders(), new RequestContextExtensionsDefinition()];
79+
protected override TypeProvider[] BuildTypeProviders()
80+
{
81+
if (AzureClientPlugin.Instance.IsAzureArm.Value == true)
82+
{
83+
var armOperation = new MgmtLongRunningOperationProvider(false);
84+
var genericArmOperation = new MgmtLongRunningOperationProvider(true);
85+
86+
// TODO: remove them once they are referenced in Resource operation implementation
87+
AzureClientPlugin.Instance.AddTypeToKeep(armOperation.Name);
88+
AzureClientPlugin.Instance.AddTypeToKeep(genericArmOperation.Name);
89+
return [.. base.BuildTypeProviders(), new RequestContextExtensionsDefinition(), armOperation, genericArmOperation];
90+
}
91+
return [.. base.BuildTypeProviders(), new RequestContextExtensionsDefinition()];
92+
}
8093

8194
internal bool IsResource(string name) => _resourceDataBySpecNameMap.ContainsKey(name);
8295
}

eng/packages/http-client-csharp/generator/Azure.Generator/src/Primitives/NewAzureProjectScaffolding.cs

Lines changed: 86 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,110 @@ protected override string GetSourceProjectFileContent()
2929
PackageTags = AzureClientPlugin.Instance.Configuration.RootNamespace,
3030
GenerateDocumentationFile = true,
3131
};
32-
foreach (var packages in _unbrandedDependencyPackages)
32+
33+
foreach (var packages in _azureDependencyPackages)
3334
{
3435
builder.PackageReferences.Add(packages);
3536
}
3637

38+
int pathSegmentCount = GetPathSegmentCount();
3739
if (AzureClientPlugin.Instance.InputLibrary.InputNamespace.Auth.ApiKey is not null)
3840
{
39-
ReadOnlySpan<char> text = AzureClientPlugin.Instance.Configuration.OutputDirectory.AsSpan();
40-
// we are either a spector project in the eng folder or a real sdk in the sdk folder
41-
int beginning = text.IndexOf("eng");
42-
if (beginning == -1)
43-
{
44-
beginning = text.IndexOf("sdk");
45-
}
46-
text = text.Slice(beginning);
47-
// starting with 2 to include eng at the beginning and src at the end
48-
int pathSegmentCount = 2 + text.Count('/');
49-
// count both path separators to normalize
50-
pathSegmentCount += text.Count('\\');
5141
builder.CompileIncludes.Add(new CSharpProjectWriter.CSProjCompileInclude(GetCompileInclude("AzureKeyCredentialPolicy.cs", pathSegmentCount), "Shared/Core"));
42+
}
43+
44+
TraverseInput(out bool hasOperation, out bool hasLongRunningOperation);
45+
if (hasOperation)
46+
{
5247
builder.CompileIncludes.Add(new CSharpProjectWriter.CSProjCompileInclude(GetCompileInclude("RawRequestUriBuilder.cs", pathSegmentCount), "Shared/Core"));
5348
}
5449

50+
if (hasLongRunningOperation)
51+
{
52+
foreach (var file in _lroSharedFiles)
53+
{
54+
builder.CompileIncludes.Add(new CSharpProjectWriter.CSProjCompileInclude(GetCompileInclude(file, pathSegmentCount), "Shared/Core"));
55+
}
56+
}
57+
5558
return builder.Write();
5659
}
5760

61+
private static readonly IReadOnlyList<string> _lroSharedFiles =
62+
[
63+
"AppContextSwitchHelper.cs",
64+
"AsyncLockWithValue.cs",
65+
"FixedDelayWithNoJitterStrategy.cs",
66+
"ClientDiagnostics.cs",
67+
"DiagnosticScopeFactory.cs",
68+
"DiagnosticScope.cs",
69+
"HttpMessageSanitizer.cs",
70+
"IOperationSource.cs",
71+
"NextLinkOperationImplementation.cs",
72+
"OperationFinalStateVia.cs",
73+
"OperationInternal.cs",
74+
"OperationInternalBase.cs",
75+
"OperationInternalOfT.cs",
76+
"OperationPoller.cs",
77+
"SequentialDelayStrategy.cs",
78+
"TaskExtensions.cs",
79+
"TrimmingAttribute.cs",
80+
"VoidValue.cs"
81+
];
82+
83+
private static void TraverseInput(out bool hasOperation, out bool hasLongRunningOperation)
84+
{
85+
hasOperation = false;
86+
hasLongRunningOperation = false;
87+
foreach (var inputClient in AzureClientPlugin.Instance.InputLibrary.InputNamespace.Clients)
88+
{
89+
foreach (var operation in inputClient.Operations)
90+
{
91+
hasOperation = true;
92+
if (operation.LongRunning != null)
93+
{
94+
hasLongRunningOperation = true;
95+
return;
96+
}
97+
}
98+
}
99+
}
100+
101+
private static int GetPathSegmentCount()
102+
{
103+
ReadOnlySpan<char> text = AzureClientPlugin.Instance.Configuration.OutputDirectory.AsSpan();
104+
// we are either a spector project in the eng folder or a real sdk in the sdk folder
105+
int beginning = text.IndexOf("eng");
106+
if (beginning == -1)
107+
{
108+
beginning = text.IndexOf("sdk");
109+
}
110+
text = text.Slice(beginning);
111+
// starting with 2 to include eng at the beginning and src at the end
112+
int pathSegmentCount = 2 + text.Count('/');
113+
// count both path separators to normalize
114+
pathSegmentCount += text.Count('\\');
115+
return pathSegmentCount;
116+
}
117+
58118
private string GetCompileInclude(string fileName, int pathSegmentCount)
59119
{
60120
return $"{MSBuildThisFileDirectory}{string.Concat(Enumerable.Repeat(ParentDirectory, pathSegmentCount))}{RelativeCoreSegment}{fileName}";
61121
}
62122

63-
private static readonly IReadOnlyList<CSharpProjectWriter.CSProjDependencyPackage> _unbrandedDependencyPackages =
64-
[
65-
new("Azure.Core"),
66-
new("System.Text.Json")
67-
];
123+
private static readonly IReadOnlyList<CSharpProjectWriter.CSProjDependencyPackage> _azureDependencyPackages =
124+
AzureClientPlugin.Instance.IsAzureArm.Value == true
125+
? [
126+
new("Azure.Core"),
127+
new("Azure.ResourceManager"),
128+
new("System.ClientModel"),
129+
new("System.Text.Json")
130+
]
131+
: [
132+
new("Azure.Core"),
133+
new("System.ClientModel"),
134+
new("System.Text.Json")
135+
];
68136

69137
/// <inheritdoc/>
70138
protected override string GetSolutionFileContent()

0 commit comments

Comments
 (0)