Skip to content

Commit

Permalink
dotnet-svcutil VB support and add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
imcarolwang committed Dec 14, 2023
1 parent b5867a1 commit dce51c8
Show file tree
Hide file tree
Showing 27 changed files with 4,203 additions and 16 deletions.
43 changes: 30 additions & 13 deletions src/dotnet-svcutil/lib/src/CommandProcessorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public bool NoTypeReuse
internal const string WCFCSParamsFileName = "ConnectedService.json";
internal const string BaseServiceReferenceName = "ServiceReference";

private static readonly List<string> s_cmdLineOverwriteSwitches = new List<string> { Switches.NoLogo.Name, Switches.Verbosity.Name, Switches.ToolContext.Name, Switches.ProjectFile.Name, Switches.AcceptCertificate.Name, Switches.ServiceContract.Name };
private static readonly List<string> s_cmdLineOverwriteSwitches = new List<string> { Switches.NoLogo.Name, Switches.Verbosity.Name, Switches.ToolContext.Name, Switches.ProjectFile.Name, Switches.AcceptCertificate.Name, Switches.ServiceContract.Name, Switches.Language.Name };

internal class CommandSwitches
{
Expand Down Expand Up @@ -92,6 +92,7 @@ internal class CommandSwitches
public readonly CommandSwitch Wrapped = new CommandSwitch(WrappedKey, "wr", SwitchType.Flag);
public readonly CommandSwitch AcceptCertificate = new CommandSwitch(AccecptCertificateKey, "ac", SwitchType.Flag);
public readonly CommandSwitch ServiceContract = new CommandSwitch(ServiceContractKey, "sc", SwitchType.Flag);
public readonly CommandSwitch Language = new CommandSwitch(LanguageKey, "l", SwitchType.SingletonValue, OperationalContext.Global);

public void Init() { } // provided as a way to get the static class Switches loaded early.
}
Expand Down Expand Up @@ -198,13 +199,13 @@ public async Task ResolveAsync(CancellationToken cancellationToken)
{
if (this.Help != true && this.Errors.Count() == 0)
{
ProcessLanguageOption();

ProcessSerializerOption();

// process project file first as it can define the working directory.
await ProcessProjectFileOptionAsync(cancellationToken).ConfigureAwait(false);

ProcessLanguageOption();

// next update option as the options may change.
await ProcessUpdateOptionAsync(cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -330,27 +331,32 @@ private async Task ProcessProjectFileOptionAsync(CancellationToken cancellationT
using (SafeLogger logger = await SafeLogger.WriteStartOperationAsync(this.Logger, $"Resolving {ProjectFileKey} option ...").ConfigureAwait(false))
{
// Resolve the project in the current directory.

var workingDirectory = Directory.GetCurrentDirectory();
var projects = Directory.GetFiles(workingDirectory, "*.csproj", SearchOption.TopDirectoryOnly);
var csProjects = Directory.GetFiles(workingDirectory, "*.csproj", SearchOption.TopDirectoryOnly);
var vbProjects = Directory.GetFiles(workingDirectory, "*.vbproj", SearchOption.TopDirectoryOnly);

if (projects.Length == 1)
if(csProjects.Length == 1 && vbProjects.Length ==0 )
{
projectFile = csProjects[0];
}
else if (csProjects.Length == 0 && vbProjects.Length == 1)
{
projectFile = projects[0];
projectFile = vbProjects[0];
this.Language = "VisualBasic";
}
else if (projects.Length == 0)
else if(csProjects.Length == 0 && vbProjects.Length == 0)
{
if (this.ToolContext == OperationalContext.Project)
{
throw new ToolArgumentException(string.Format(CultureInfo.CurrentCulture, SR.ErrInvalidOperationNoProjectFileFoundUnderFolderFormat, workingDirectory));
}
}
else if (projects.Length > 1)
else
{
var moreThanOneProjectMsg = string.Format(CultureInfo.CurrentCulture, SR.ErrMoreThanOneProjectFoundFormat, workingDirectory);
if (this.ToolContext != OperationalContext.Project)
{
var projectItems = projects.Aggregate((projectMsg, projectItem) => $"{projectMsg}, {projectItem}").Trim(',').Trim();
var projectItems = csProjects.Concat(vbProjects).ToArray().Aggregate((projectMsg, projectItem) => $"{projectMsg}, {projectItem}").Trim(',').Trim();
var useProjectOptions = string.Format(CultureInfo.CurrentCulture, SR.UseProjectFileOptionOnMultipleFilesMsgFormat, Switches.ProjectFile.Name, projectItems);
throw new ToolArgumentException($"{moreThanOneProjectMsg}{Environment.NewLine}{useProjectOptions}");
}
Expand Down Expand Up @@ -417,7 +423,7 @@ private async Task ProcessOutputFileOptionAsync(string workingDirectory, Cancell
var outputFile = this.OutputFile?.OriginalPath();
if (outputFile == null)
{
outputFile = "Reference.cs";
outputFile = "Reference";
}

if (!outputFile.EndsWith(this.CodeProvider.FileExtension, RuntimeEnvironmentHelper.FileStringComparison))
Expand Down Expand Up @@ -740,9 +746,20 @@ private void ProcessSerializerOption()

private void ProcessLanguageOption()
{
if (this.CodeProvider == null)
if (!string.IsNullOrEmpty(Language))
{
try
{
CodeProvider = CodeDomProvider.CreateProvider(Language);
}
catch (Exception e)
{
throw new ToolArgumentException(string.Format(SR.ErrCouldNotCreateCodeProvider, Language, Switches.Language.Abbreviation), e);
}
}
else
{
this.CodeProvider = CodeDomProvider.CreateProvider("csharp");
CodeProvider = CodeDomProvider.CreateProvider("csharp");
}
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ internal CodeDomCompilationConfiguration()
compilerInfo._providerOptions = new Dictionary<string, string>();
compilerInfo._providerOptions[RedistVersionInfo.NameTag] = RedistVersionInfo.DefaultVersion;
AddCompilerInfo(compilerInfo);

// VB
compilerParameters = new CompilerParameters();
compilerParameters.WarningLevel = 4;
typeName = "Microsoft.VisualBasic.VBCodeProvider";
compilerInfo = new CompilerInfo(compilerParameters, typeName);
compilerInfo._compilerLanguages = new string[] { "vb", "vbs", "visualbasic", "vbscript" };
compilerInfo._compilerExtensions = new string[] { ".vb", "vb" };
compilerInfo._providerOptions = new Dictionary<string, string>();
compilerInfo._providerOptions[RedistVersionInfo.NameTag] = RedistVersionInfo.DefaultVersion;
AddCompilerInfo(compilerInfo);
}

private void AddCompilerInfo(CompilerInfo compilerInfo)
Expand Down
Loading

0 comments on commit dce51c8

Please sign in to comment.