-
Notifications
You must be signed in to change notification settings - Fork 574
[dotnet-svcutil] Implement command switch for separating type files #5896
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
base: main
Are you sure you want to change the base?
Changes from all commits
0f5f0be
9b5ca9b
863dd3a
03a51af
dab071c
2cfda84
270c289
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,8 @@ | |
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.Tools.ServiceModel.Svcutil.Metadata; | ||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Globalization; | ||
|
|
@@ -13,6 +13,10 @@ | |
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeDom; | ||
| using Microsoft.Tools.ServiceModel.Svcutil.Metadata; | ||
| using Newtonsoft.Json.Linq; | ||
| using static System.ServiceModel.Channels.RequestReplyCorrelator; | ||
| using DcNS = System.Runtime.Serialization; | ||
|
|
||
| namespace Microsoft.Tools.ServiceModel.Svcutil | ||
|
|
@@ -244,17 +248,75 @@ await serviceDescriptor.ImportMetadataAsync( | |
| using (await SafeLogger.WriteStartOperationAsync(options.Logger, "Processing Code DOM ...").ConfigureAwait(false)) | ||
| { | ||
| ToolConsole.WriteLine(SR.GeneratingFiles); | ||
| if (options.SeparateFiles == true) | ||
| { | ||
| var originalOutputFile = options.OutputFile; | ||
| try | ||
| { | ||
| var generatedOutputPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase); | ||
| foreach (CodeNamespace @namespace in importModule.CodeCompileUnit.Namespaces) | ||
| { | ||
| foreach (CodeTypeDeclaration type in @namespace.Types) | ||
| { | ||
| var namespacePrefix = GetSafeNamespaceFilePrefix(@namespace.Name); | ||
| var outputFileName = $"{namespacePrefix}.{type.Name}{CodeSerializer.GetOutputFileExtension(options)}"; | ||
| var outputPath = Path.Combine(options.OutputDir.FullName, outputFileName); | ||
| if (!generatedOutputPaths.Add(outputPath)) | ||
| { | ||
| throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, | ||
| "A generated output file collision was detected for type '{0}' in namespace '{1}'. The file path '{2}' is already assigned to another generated type.", | ||
| type.Name, @namespace.Name, outputPath)); | ||
| } | ||
| options.OutputFile = new FileInfo(outputPath); | ||
| CodeSerializer codeSerializer = new CodeSerializer(options, serviceDescriptor.MetadataDocuments); | ||
| CodeCompileUnit compileUnit = new CodeCompileUnit(); | ||
| CodeNamespace splitNamespace = new CodeNamespace(@namespace.Name); | ||
|
|
||
| // Transfer the assembly attributes, referenced assemblies, user data and directives to each split compile unit to ensure the generated code is correct. | ||
| compileUnit.AssemblyCustomAttributes.AddRange(importModule.CodeCompileUnit.AssemblyCustomAttributes); | ||
| compileUnit.StartDirectives.AddRange(importModule.CodeCompileUnit.StartDirectives); | ||
| compileUnit.EndDirectives.AddRange(importModule.CodeCompileUnit.EndDirectives); | ||
| compileUnit.ReferencedAssemblies.AddRange(importModule.CodeCompileUnit.ReferencedAssemblies.Cast<string>().ToArray()); | ||
| foreach (DictionaryEntry pair in importModule.CodeCompileUnit.UserData) | ||
| { | ||
| compileUnit.UserData.Add(pair.Key, pair.Value); | ||
| } | ||
|
|
||
| compileUnit.Namespaces.Add(splitNamespace); | ||
| splitNamespace.Types.Add(type); | ||
| var filePath = codeSerializer.Save(compileUnit); | ||
| ToolConsole.WriteLineIf(options.ToolContext != OperationalContext.Infrastructure, filePath, LogTag.Important); | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| options.OutputFile = originalOutputFile; | ||
| } | ||
| } | ||
|
Comment on lines
+251
to
+296
|
||
| else | ||
| { | ||
| CodeSerializer codeSerializer = new CodeSerializer(options, serviceDescriptor.MetadataDocuments); | ||
| var filePath = codeSerializer.Save(importModule.CodeCompileUnit); | ||
|
|
||
| CodeSerializer codeSerializer = new CodeSerializer(options, serviceDescriptor.MetadataDocuments); | ||
| var filePath = codeSerializer.Save(importModule.CodeCompileUnit); | ||
|
|
||
| // When in Infrastructure mode (WCF CS) it is assumed the output file path have been provided so no need to display it. | ||
| ToolConsole.WriteLineIf(options.ToolContext != OperationalContext.Infrastructure, filePath, LogTag.Important); | ||
| // When in Infrastructure mode (WCF CS) it is assumed the output file path have been provided so no need to display it. | ||
| ToolConsole.WriteLineIf(options.ToolContext != OperationalContext.Infrastructure, filePath, LogTag.Important); | ||
| } | ||
| } | ||
|
|
||
| return ToolConsole.ExitCode; | ||
| } | ||
|
|
||
| private static string GetSafeNamespaceFilePrefix(string namespaceName) | ||
| { | ||
| var prefix = string.IsNullOrWhiteSpace(namespaceName) ? "Global" : namespaceName; | ||
| foreach (var invalidChar in Path.GetInvalidFileNameChars()) | ||
| { | ||
| prefix = prefix.Replace(invalidChar, '_'); | ||
| } | ||
| return prefix.Replace('.', '_'); | ||
| } | ||
|
|
||
| private static bool IsSuccess(int result) | ||
| { | ||
| return result == (int)ToolExitCode.Success || result == (int)ToolExitCode.ValidationErrorTurnedWarning; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
SeparateFilesis enabled, the tool writes many.csfiles but does not appear to clean up previously generated type files in the output directory. This can leave stale types behind on reruns/updates and cause compilation conflicts. Consider deleting previously generated type files (or generating into a dedicated subfolder and managing it) before writing the new set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mconnew I'm not sure about this, cleaning up the output directory is nothing I would recommend. That should be a user task.