Skip to content

Commit 6f64ef4

Browse files
committed
refine new project scaffolding.
1 parent 0dc8c1f commit 6f64ef4

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

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

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public class NewAzureProjectScaffolding : NewProjectScaffolding
2020
private const string ParentDirectory = "../";
2121
private const string SharedSourceLinkBase = "Shared/Core";
2222

23+
private int PathSegmentCount => _pathSegmentCount ??= GetPathSegmentCount();
24+
private int? _pathSegmentCount;
25+
2326
/// <inheritdoc/>
2427
protected override string GetSourceProjectFileContent()
2528
{
@@ -37,33 +40,9 @@ protected override string GetSourceProjectFileContent()
3740
builder.PackageReferences.Add(packages);
3841
}
3942

40-
int pathSegmentCount = GetPathSegmentCount();
41-
if (AzureClientGenerator.Instance.InputLibrary.InputNamespace.Auth?.ApiKey is not null)
42-
{
43-
builder.CompileIncludes.Add(new CSharpProjectCompileInclude(GetCompileInclude("AzureKeyCredentialPolicy.cs", pathSegmentCount), SharedSourceLinkBase));
44-
}
45-
46-
bool hasOperation = false;
47-
bool hasLongRunningOperation = false;
48-
foreach (var client in AzureClientGenerator.Instance.InputLibrary.InputNamespace.Clients)
49-
{
50-
TraverseInput(client, ref hasOperation, ref hasLongRunningOperation);
51-
}
52-
53-
if (hasOperation)
54-
{
55-
foreach (var file in _operationSharedFiles)
56-
{
57-
builder.CompileIncludes.Add(new CSharpProjectCompileInclude(GetCompileInclude(file, pathSegmentCount), SharedSourceLinkBase));
58-
}
59-
}
60-
61-
if (hasLongRunningOperation)
43+
foreach (var compileInclude in BuildCompileIncludes())
6244
{
63-
foreach (var file in _lroSharedFiles)
64-
{
65-
builder.CompileIncludes.Add(new CSharpProjectCompileInclude(GetCompileInclude(file, pathSegmentCount), SharedSourceLinkBase));
66-
}
45+
builder.CompileIncludes.Add(compileInclude);
6746
}
6847

6948
return builder.Write();
@@ -141,9 +120,58 @@ private static int GetPathSegmentCount()
141120
return pathSegmentCount;
142121
}
143122

144-
private string GetCompileInclude(string fileName, int pathSegmentCount)
123+
/// <summary>
124+
/// Constructs a relative path for a compile include file based on the project structure.
125+
/// </summary>
126+
/// <param name="fileName">The name of the file to include.</param>
127+
/// <param name="relativeSegment">The relative path segment to the shared source files (defaults to RelativeCoreSegment).</param>
128+
/// <returns>A relative path string for the compile include file.</returns>
129+
protected string GetCompileInclude(string fileName, string relativeSegment = RelativeCoreSegment)
130+
{
131+
return $"{MSBuildThisFileDirectory}{string.Concat(Enumerable.Repeat(ParentDirectory, PathSegmentCount))}{relativeSegment}{fileName}";
132+
}
133+
134+
/// <summary>
135+
/// Gets the list of required CompileInclude files based on the project's requirements.
136+
/// </summary>
137+
/// <returns>A list of CSharpProjectCompileInclude files that should be included in the project.</returns>
138+
protected override IReadOnlyList<CSharpProjectCompileInclude> BuildCompileIncludes()
145139
{
146-
return $"{MSBuildThisFileDirectory}{string.Concat(Enumerable.Repeat(ParentDirectory, pathSegmentCount))}{RelativeCoreSegment}{fileName}";
140+
var compileIncludes = new List<CSharpProjectCompileInclude>();
141+
142+
// Add API key credential policy if API key authentication is configured
143+
if (AzureClientGenerator.Instance.InputLibrary.InputNamespace.Auth?.ApiKey is not null)
144+
{
145+
compileIncludes.Add(new CSharpProjectCompileInclude(GetCompileInclude("AzureKeyCredentialPolicy.cs"), SharedSourceLinkBase));
146+
}
147+
148+
// Analyze clients to determine what shared files are needed
149+
bool hasOperation = false;
150+
bool hasLongRunningOperation = false;
151+
foreach (var client in AzureClientGenerator.Instance.InputLibrary.InputNamespace.Clients)
152+
{
153+
TraverseInput(client, ref hasOperation, ref hasLongRunningOperation);
154+
}
155+
156+
// Add operation-related shared files if operations are present
157+
if (hasOperation)
158+
{
159+
foreach (var file in _operationSharedFiles)
160+
{
161+
compileIncludes.Add(new CSharpProjectCompileInclude(GetCompileInclude(file), SharedSourceLinkBase));
162+
}
163+
}
164+
165+
// Add long-running operation shared files if LRO operations are present
166+
if (hasLongRunningOperation)
167+
{
168+
foreach (var file in _lroSharedFiles)
169+
{
170+
compileIncludes.Add(new CSharpProjectCompileInclude(GetCompileInclude(file), SharedSourceLinkBase));
171+
}
172+
}
173+
174+
return compileIncludes;
147175
}
148176
}
149177
}

0 commit comments

Comments
 (0)