Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 051f53b

Browse files
authored
Update sgen usage and --parameters (#27332)
* update usage and --parameters * update -- parameter in package * Update short format * Format code blocks * Add comments and spaces * add short form argument match method * resovle merge conflict * update help message and other fixes according to pr comments * remove a period in usage message
1 parent 5608a9f commit 051f53b

File tree

5 files changed

+48
-44
lines changed

5 files changed

+48
-44
lines changed

src/Microsoft.XmlSerializer.Generator/pkg/build/Microsoft.XmlSerializer.Generator.targets

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<Delete Condition="Exists('$(_SerializerPdbIntermediateFolder)') == 'true'" Files="$(_SerializerPdbIntermediateFolder)" ContinueOnError="true"/>
1313
<Delete Condition="Exists('$(_SerializerCsIntermediateFolder)') == 'true'" Files="$(_SerializerCsIntermediateFolder)" ContinueOnError="true"/>
1414
<Message Text="Running Serialization Tool" Importance="normal" />
15-
<Exec Command="dotnet Microsoft.XmlSerializer.Generator $(IntermediateOutputPath)$(AssemblyName)$(TargetExt) /force /quiet" ContinueOnError="true"/>
15+
<Exec Command="dotnet Microsoft.XmlSerializer.Generator $(IntermediateOutputPath)$(AssemblyName)$(TargetExt) --force --quiet" ContinueOnError="true"/>
1616
<Warning Condition="Exists('$(_SerializerCsIntermediateFolder)') != 'true'" Text="$(_SGenWarningText)" />
1717
<Csc Condition="Exists('$(_SerializerCsIntermediateFolder)') == 'true'" ContinueOnError="true" OutputAssembly="$(_SerializerDllIntermediateFolder)" References="@(ReferencePath);@(IntermediateAssembly)" EmitDebugInformation="$(DebugSymbols)" Sources="$(_SerializerCsIntermediateFolder)" TargetType="Library" ToolExe="$(CscToolExe)" ToolPath="$(CscToolPath)" DisabledWarnings="$(_SerializationAssemblyDisabledWarnings)"/>
1818
<Warning Condition="Exists('$(_SerializerDllIntermediateFolder)') != 'true' And Exists('$(_SerializerCsIntermediateFolder)') == 'true'" Text="$(_SGenWarningText)"/>
@@ -43,7 +43,7 @@
4343
<Delete Files="%(_ReferenceSerializerIntermediateFolder.Identity).cs" ContinueOnError="true"/>
4444
<Delete Files="%(_ReferenceSerializerIntermediateFolder.Identity).pdb" ContinueOnError="true"/>
4545
<Message Text="Running Serialization Tool for Reference Assembly" Importance="normal" />
46-
<Exec Command="dotnet Microsoft.XmlSerializer.Generator /force /quiet /assembly:%(_TargetSerializationAssembly.Identity) /type:%(_TargetSerializationAssembly.SerializationTypes) /out:$(IntermediateOutputPath)" ContinueOnError="true" />
46+
<Exec Command="dotnet Microsoft.XmlSerializer.Generator --force --quiet --assembly:%(_TargetSerializationAssembly.Identity) --type:%(_TargetSerializationAssembly.SerializationTypes) --out:$(IntermediateOutputPath)" ContinueOnError="true" />
4747
<Warning Condition="Exists('$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).cs') != 'true'" Text="SGEN: Fail to generate %(_ReferenceSerializationAssemblyName.Identity)'. Please follow the instructions at https://go.microsoft.com/fwlink/?linkid=858594 and try again." />
4848
<Csc Condition="Exists('$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).cs') == 'true'" ContinueOnError="true" OutputAssembly="$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).dll" References="@(ReferencePath);@(IntermediateAssembly)" EmitDebugInformation="$(DebugSymbols)" Sources="$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).cs" TargetType="Library" ToolExe="$(CscToolExe)" ToolPath="$(CscToolPath)" DisabledWarnings="$(_SerializationAssemblyDisabledWarnings)"/>
4949
<Warning Condition="Exists('$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).dll') != 'true' And Exists('$(IntermediateOutputPath)%(_ReferenceSerializationAssemblyName.Identity).cs') == 'true'" Text="SGEN: Fail to compile %(_ReferenceSerializationAssemblyName.Identity).cs. Please follow the instructions at https://go.microsoft.com/fwlink/?linkid=858594 and try again." />
@@ -58,4 +58,4 @@
5858
<Target Name="CopySerializerForReferenceAssemblies" AfterTargets="PrepareForPublish" Condition="@(SerializationAssembly)!=''">
5959
<Copy Condition="Exists('$(OutputPath)%(SerializationAssembly.Identity).XmlSerializers.dll') == 'true'" SourceFiles="$(OutputPath)%(SerializationAssembly.Identity).XmlSerializers.dll" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
6060
</Target>
61-
</Project>
61+
</Project>

src/Microsoft.XmlSerializer.Generator/src/Sgen.cs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private int Run(string[] args)
4141
{
4242
string arg = args[i];
4343
string value = string.Empty;
44-
45-
if (arg.StartsWith("/") || arg.StartsWith("-"))
44+
45+
if (arg.StartsWith("-"))
4646
{
4747
int colonPos = arg.IndexOf(":");
4848
if (colonPos != -1)
@@ -51,11 +51,8 @@ private int Run(string[] args)
5151
arg = arg.Substring(0, colonPos).Trim();
5252
}
5353
}
54-
55-
string originalArg = arg;
56-
arg = arg.ToLower(CultureInfo.InvariantCulture);
57-
58-
if (ArgumentMatch(arg, "?") || ArgumentMatch(arg, "help"))
54+
55+
if (ArgumentMatch(arg, "help") || ShortNameArgumentMatch(arg, "h"))
5956
{
6057
WriteHeader();
6158
WriteHelp();
@@ -69,11 +66,11 @@ private int Run(string[] args)
6966
{
7067
proxyOnly = true;
7168
}
72-
else if (ArgumentMatch(arg, "out"))
69+
else if (ArgumentMatch(arg, "out") || ShortNameArgumentMatch(arg, "o"))
7370
{
7471
if (codePath != null)
7572
{
76-
errs.Add(SR.Format(SR.ErrInvalidArgument, "/out", arg));
73+
errs.Add(SR.Format(SR.ErrInvalidArgument, "--out", arg));
7774
}
7875

7976
codePath = value;
@@ -89,11 +86,11 @@ private int Run(string[] args)
8986
}
9087
}
9188
}
92-
else if (ArgumentMatch(arg, "assembly"))
89+
else if (ArgumentMatch(arg, "assembly") || ShortNameArgumentMatch(arg, "a"))
9390
{
9491
if (assembly != null)
9592
{
96-
errs.Add(SR.Format(SR.ErrInvalidArgument, "/assembly", arg));
93+
errs.Add(SR.Format(SR.ErrInvalidArgument, "--assembly", arg));
9794
}
9895

9996
assembly = value;
@@ -120,14 +117,14 @@ private int Run(string[] args)
120117
}
121118
else
122119
{
123-
if (arg.EndsWith(".dll") || arg.EndsWith(".exe"))
120+
if (arg.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) || arg.EndsWith(".exe", StringComparison.InvariantCultureIgnoreCase))
124121
{
125122
if (assembly != null)
126123
{
127-
errs.Add(SR.Format(SR.ErrInvalidArgument, "/assembly", arg));
124+
errs.Add(SR.Format(SR.ErrInvalidArgument, "--assembly", arg));
128125
}
129126

130-
assembly = originalArg;
127+
assembly = arg;
131128
}
132129
else
133130
{
@@ -352,16 +349,27 @@ private void GenerateFile(List<string> typeNames, string assemblyName, bool prox
352349
}
353350
}
354351

355-
// assumes all same case.
352+
356353
private bool ArgumentMatch(string arg, string formal)
357354
{
358-
if (arg[0] != '/' && arg[0] != '-')
355+
// Full name format, eg: --assembly
356+
if (arg.Length < 3 || arg[0] != '-' || arg[1] != '-' )
359357
{
360358
return false;
361359
}
360+
arg = arg.Substring(2);
361+
return arg.Equals(formal, StringComparison.InvariantCultureIgnoreCase);
362+
}
362363

364+
public bool ShortNameArgumentMatch(string arg, string shortName)
365+
{
366+
// Short name format, eg: -a
367+
if (arg.Length < 2 || arg[0] != '-')
368+
{
369+
return false;
370+
}
363371
arg = arg.Substring(1);
364-
return (arg == formal || (arg.Length == 1 && arg[0] == formal[0]));
372+
return arg.Equals(shortName, StringComparison.InvariantCultureIgnoreCase);
365373
}
366374

367375
private void ImportType(Type type, ArrayList mappings, ArrayList importedTypes, bool verbose, XmlReflectionImporter importer, bool parsableerrors)
@@ -418,16 +426,16 @@ private void WriteHeader()
418426
private void WriteHelp()
419427
{
420428
Console.Out.WriteLine(SR.Format(SR.HelpDescription));
421-
Console.Out.WriteLine(SR.Format(SR.HelpUsage, this.GetType().Assembly.GetName().Name));
429+
Console.Out.WriteLine(SR.Format(SR.HelpUsage, this.GetType().Assembly.GetName().Name.Substring("dotnet-".Length)));
422430
Console.Out.WriteLine(SR.Format(SR.HelpDevOptions));
423-
Console.Out.WriteLine(SR.Format(SR.HelpAssembly, "/assembly:", "/a:"));
424-
Console.Out.WriteLine(SR.Format(SR.HelpType, "/type:", "/t:"));
425-
Console.Out.WriteLine(SR.Format(SR.HelpProxy, "/proxytypes", "/p"));
426-
Console.Out.WriteLine(SR.Format(SR.HelpForce, "/force", "/f"));
427-
Console.Out.WriteLine(SR.Format(SR.HelpOut, "/out:", "/o:"));
431+
Console.Out.WriteLine(SR.Format(SR.HelpAssembly, "-a", "--assembly"));
432+
Console.Out.WriteLine(SR.Format(SR.HelpType, "--type"));
433+
Console.Out.WriteLine(SR.Format(SR.HelpProxy, "--proxytypes"));
434+
Console.Out.WriteLine(SR.Format(SR.HelpForce, "--force"));
435+
Console.Out.WriteLine(SR.Format(SR.HelpOut, "-o", "--out"));
428436

429437
Console.Out.WriteLine(SR.Format(SR.HelpMiscOptions));
430-
Console.Out.WriteLine(SR.Format(SR.HelpHelp, "/?", "/help"));
438+
Console.Out.WriteLine(SR.Format(SR.HelpHelp, "-h", "--help"));
431439
}
432440

433441
private static string FormatMessage(bool parsableerrors, bool warning, string message)

src/Microsoft.XmlSerializer.Generator/tests/Microsoft.XmlSerializer.Generator.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<SerializerName>$(AssemblyName).XmlSerializers</SerializerName>
5757
</PropertyGroup>
5858
<Message Text="Running Serialization Tool" Importance="normal" />
59-
<Exec Command="$(GeneratorCliPath)dotnet $(OutputPath)dotnet-Microsoft.XmlSerializer.Generator.dll $(OutputPath)Microsoft.XmlSerializer.Generator.Tests.dll /force /quiet" />
59+
<Exec Command="$(GeneratorCliPath)dotnet $(OutputPath)dotnet-Microsoft.XmlSerializer.Generator.dll $(OutputPath)Microsoft.XmlSerializer.Generator.Tests.dll --force --quiet" />
6060
<Warning Condition="Exists('$(OutputPath)$(SerializerName).cs') != 'true'" Text="Fail to generate $(OutputPath)$(SerializerName).cs"/>
6161
<Csc Condition="Exists('$(OutputPath)$(SerializerName).cs') == 'true'"
6262
OutputAssembly="$(OutputPath)$(SerializerName).dll"

src/Microsoft.XmlSerializer.Generator/tests/SGenTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void SgenCommandTest()
1919
string codefile = "Microsoft.XmlSerializer.Generator.Tests.XmlSerializers.cs";
2020
var type = Type.GetType("Microsoft.XmlSerializer.Generator.Sgen, dotnet-Microsoft.XmlSerializer.Generator");
2121
MethodInfo md = type.GetMethod("Main", BindingFlags.Static | BindingFlags.Public);
22-
string[] args = new string[] { "Microsoft.XmlSerializer.Generator.Tests.dll", "/force", "/quiet" };
22+
string[] args = new string[] { "Microsoft.XmlSerializer.Generator.Tests.dll", "--force", "--quiet" };
2323
int n = (int)md.Invoke(null, new object[] { args });
2424
Assert.Equal(0, n);
2525
Assert.True(File.Exists(codefile), string.Format("Fail to generate {0}.", codefile));

src/System.Private.Xml/src/Resources/Strings.resx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3413,37 +3413,33 @@ building and deploying the assemblies with the application.
34133413
</data>
34143414
<data name="HelpUsage" xml:space="preserve">
34153415
<value>
3416-
Usage: dotnet {0} [[/assembly:&lt;assembly name&gt;] | [&lt;assembly file location&gt;]]
3417-
[/type:] [/debug].
3418-
</value>
3416+
Usage: dotnet {0} [[--assembly &lt;assembly name&gt;] | [&lt;assembly file location&gt;]] [--type] [--debug]</value>
34193417
</data>
34203418
<data name="HelpDevOptions" xml:space="preserve">
3421-
<value> Developer options:</value>
3419+
<value>
3420+
Developer options:</value>
34223421
</data>
34233422
<data name="HelpAssembly" xml:space="preserve">
3424-
<value> {0} Assembly location or display name. Short form is '{1}'.</value>
3423+
<value> {0}|{1} Assembly location or display name.</value>
34253424
</data>
34263425
<data name="HelpType" xml:space="preserve">
3427-
<value> {0} Generate code for serialization/deserialization of the
3428-
specified type from the input assembly. Short form is '{1}'.</value>
3426+
<value> {0} Generate code for serialization/deserialization of the specified type from the input assembly.</value>
34293427
</data>
34303428
<data name="HelpForce" xml:space="preserve">
3431-
<value> {0} Forces overwrite of a previously generated assembly.
3432-
Short form is '{1}'.</value>
3429+
<value> {0} Forces overwrite of a previously generated assembly.</value>
34333430
</data>
34343431
<data name="HelpProxy" xml:space="preserve">
3435-
<value> {0} Generate serialization code only for proxy classes and web
3436-
method parameters. Short form is '{1}'.</value>
3432+
<value> {0} Generate serialization code only for proxy classes and web method parameters.</value>
34373433
</data>
34383434
<data name="HelpOut" xml:space="preserve">
3439-
<value> {0} Output directory name (default: target assembly location).
3440-
Short form is '{1}'.</value>
3435+
<value> {0}|{1} Output directory name (default: target assembly location).</value>
34413436
</data>
34423437
<data name="HelpMiscOptions" xml:space="preserve">
3443-
<value> Miscellaneous options:</value>
3438+
<value>
3439+
Miscellaneous options:</value>
34443440
</data>
34453441
<data name="HelpHelp" xml:space="preserve">
3446-
<value> {0} or {1} Show this message</value>
3442+
<value> {0}|{1} Show help.</value>
34473443
</data>
34483444
<data name="MoreHelp" xml:space="preserve">
34493445
<value>If you would like more help, please type "sgen {0}".</value>

0 commit comments

Comments
 (0)