Skip to content

Commit 22dc8c3

Browse files
authored
Normalize newline in xml writer for csproj generation (#5768)
In support of Azure/azure-sdk-for-net#47071 we need to normalize the line endings in our xml writer to not toggle back and forth when sometimes generating on linux and other times on windows. Also added some missing public docs for the visitor.
1 parent 8ba2cc1 commit 22dc8c3

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/LibraryVisitor.cs

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

88
namespace Microsoft.Generator.CSharp
99
{
10+
/// <summary>
11+
/// Base class for visiting and potentially modifying the output library.
12+
/// </summary>
1013
public abstract class LibraryVisitor
1114
{
1215
internal virtual void Visit(OutputLibrary library)
@@ -100,46 +103,94 @@ internal virtual void Visit(OutputLibrary library)
100103
return type;
101104
}
102105

106+
/// <summary>
107+
/// Visits an <see cref="InputModelType"/> and the converted <see cref="ModelProvider"/> and returns a possibly modified version of the <see cref="ModelProvider"/>.
108+
/// </summary>
109+
/// <param name="model">The original input model.</param>
110+
/// <param name="type">The current conversion.</param>
111+
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="ModelProvider"/>.</returns>
103112
protected internal virtual ModelProvider? Visit(InputModelType model, ModelProvider? type)
104113
{
105114
return type;
106115
}
107116

117+
/// <summary>
118+
/// Visits an <see cref="InputModelProperty"/> and the converted <see cref="PropertyProvider"/> and returns a possibly modified version of the <see cref="PropertyProvider"/>.
119+
/// </summary>
120+
/// <param name="property">The original input model property.</param>
121+
/// <param name="propertyProvider">The current conversion.</param>
122+
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="PropertyProvider"/>.</returns>
108123
protected internal virtual PropertyProvider? Visit(InputModelProperty property, PropertyProvider? propertyProvider)
109124
{
110125
return propertyProvider;
111126
}
112127

128+
/// <summary>
129+
/// Visits an <see cref="InputEnumType"/> and the converted <see cref="TypeProvider"/> and returns a possibly modified version of the <see cref="TypeProvider"/>.
130+
/// </summary>
131+
/// <param name="enumType">The original input enum.</param>
132+
/// <param name="type">The current conversion.</param>
133+
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="TypeProvider"/>.</returns>
113134
protected internal virtual TypeProvider? Visit(InputEnumType enumType, TypeProvider? type)
114135
{
115136
return type;
116137
}
117138

139+
/// <summary>
140+
/// Visits a <see cref="TypeProvider"/> and returns a possibly modified version of it.
141+
/// </summary>
142+
/// <param name="type">The original <see cref="TypeProvider"/>.</param>
143+
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="TypeProvider"/>.</returns>
118144
protected virtual TypeProvider? Visit(TypeProvider type)
119145
{
120146
return type;
121147
}
122148

149+
/// <summary>
150+
/// Visits a <see cref="TypeProvider"/> after all its members have been visited and returns a possibly modified version of it.
151+
/// </summary>
152+
/// <param name="type">The original <see cref="TypeProvider"/>.</param>
153+
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="TypeProvider"/>.</returns>
123154
protected virtual TypeProvider? PostVisit(TypeProvider type)
124155
{
125156
return type;
126157
}
127158

159+
/// <summary>
160+
/// Visits a <see cref="ConstructorProvider"/> and returns a possibly modified version of it.
161+
/// </summary>
162+
/// <param name="constructor">The original <see cref="ConstructorProvider"/>.</param>
163+
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="ConstructorProvider"/>.</returns>
128164
protected virtual ConstructorProvider? Visit(ConstructorProvider constructor)
129165
{
130166
return constructor;
131167
}
132168

169+
/// <summary>
170+
/// Visits a <see cref="MethodProvider"/> and returns a possibly modified version of it.
171+
/// </summary>
172+
/// <param name="method">The original <see cref="MethodProvider"/>.</param>
173+
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="MethodProvider"/>.</returns>
133174
protected virtual MethodProvider? Visit(MethodProvider method)
134175
{
135176
return method;
136177
}
137178

179+
/// <summary>
180+
/// Visits a <see cref="PropertyProvider"/> and returns a possibly modified version of it.
181+
/// </summary>
182+
/// <param name="property">The original <see cref="PropertyProvider"/>.</param>
183+
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="PropertyProvider"/>.</returns>
138184
protected virtual PropertyProvider? Visit(PropertyProvider property)
139185
{
140186
return property;
141187
}
142188

189+
/// <summary>
190+
/// Visits a <see cref="FieldProvider"/> and returns a possibly modified version of it.
191+
/// </summary>
192+
/// <param name="field">The original <see cref="FieldProvider"/>.</param>
193+
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="FieldProvider"/>.</returns>
143194
protected virtual FieldProvider? Visit(FieldProvider field)
144195
{
145196
return field;

packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Writers/CSharpProjectWriter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Microsoft.Generator.CSharp;
1010

1111
public class CSharpProjectWriter
1212
{
13+
private const char NewLine = '\n';
14+
1315
public CSharpProjectWriter()
1416
{
1517
ProjectReferences = new List<CSProjDependencyPackage>();
@@ -64,7 +66,9 @@ public string Write()
6466
using var writer = XmlWriter.Create(builder, new XmlWriterSettings
6567
{
6668
OmitXmlDeclaration = true,
67-
Indent = true
69+
Indent = true,
70+
NewLineChars = "\n",
71+
NewLineHandling = NewLineHandling.Replace
6872
});
6973
writer.WriteStartDocument();
7074
// write the Project element
@@ -79,7 +83,7 @@ public string Write()
7983
// this is the only way I know to write a blank line in an xml document using APIs instead of just write raw strings
8084
// feel free to change this if other elegant way is found.
8185
writer.Flush();
82-
builder.AppendLine();
86+
builder.Append(NewLine);
8387
writer.WriteStartElement("ItemGroup");
8488
foreach (var compileInclude in CompileIncludes)
8589
{
@@ -92,7 +96,7 @@ public string Write()
9296
if (ProjectReferences.Count > 0)
9397
{
9498
writer.Flush();
95-
builder.AppendLine();
99+
builder.Append(NewLine);
96100
writer.WriteStartElement("ItemGroup");
97101
foreach (var package in ProjectReferences)
98102
{
@@ -105,7 +109,7 @@ public string Write()
105109
if (PackageReferences.Count > 0)
106110
{
107111
writer.Flush();
108-
builder.AppendLine();
112+
builder.Append(NewLine);
109113
writer.WriteStartElement("ItemGroup");
110114
foreach (var package in PackageReferences)
111115
{
@@ -118,7 +122,7 @@ public string Write()
118122
if (PrivatePackageReferences.Count > 0)
119123
{
120124
writer.Flush();
121-
builder.AppendLine();
125+
builder.Append(NewLine);
122126
writer.WriteStartElement("ItemGroup");
123127
foreach (var package in PrivatePackageReferences)
124128
{
@@ -132,7 +136,7 @@ public string Write()
132136
writer.Flush();
133137

134138
// add an empty on the end of file
135-
builder.AppendLine();
139+
builder.Append(NewLine);
136140

137141
return builder.ToString();
138142
}

0 commit comments

Comments
 (0)