diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 50d406dc934..4bba1753e39 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -33,73 +33,33 @@ namespace Microsoft.PowerShell.Commands public enum Language { /// - /// The C# programming language: latest version. + /// The C# programming language. /// CSharp, /// - /// The C# programming language v7 + /// The Visual Basic programming language. /// - CSharpVersion7, - - /// - /// The C# programming language v6 - /// - CSharpVersion6, - - /// - /// The C# programming language v5 - /// - CSharpVersion5, - - /// - /// The C# programming language v4 - /// - CSharpVersion4, - - /// - /// The C# programming language v3 (for Linq, etc) - /// - CSharpVersion3, - - /// - /// The C# programming language v2 - /// - CSharpVersion2, - - /// - /// The C# programming language v1 - /// - CSharpVersion1, - - /// - /// The Visual Basic programming language - /// - VisualBasic, - - /// - /// The Managed JScript programming language - /// - JScript, + VisualBasic } /// - /// Types supported for the OutputAssembly parameter + /// Types supported for the OutputAssembly parameter. /// public enum OutputAssemblyType { /// - /// A Dynamically linked library (DLL) + /// A Dynamically linked library (DLL). /// Library, /// - /// An executable application that targets the console subsystem + /// An executable application that targets the console subsystem. /// ConsoleApplication, /// - /// An executable application that targets the graphical subsystem + /// An executable application that targets the graphical subsystem. /// WindowsApplication } @@ -148,6 +108,8 @@ public class AddTypeCompilerError [OutputType(typeof(Type))] public sealed class AddTypeCommand : PSCmdlet { + #region Parameters + /// /// The source code of this type. /// @@ -322,10 +284,6 @@ private void ProcessPaths(List resolvedPaths) Language = Language.VisualBasic; break; - case ".JS": - Language = Language.JScript; - break; - case ".DLL": loadAssembly = true; break; @@ -531,6 +489,10 @@ public OutputAssemblyType OutputType [Parameter()] public SwitchParameter IgnoreWarnings { get; set; } + #endregion Parameters + + #region GererateSource + internal string GenerateTypeSource(string typeNamespace, string name, string sourceCode, Language language) { string usingSource = String.Format( @@ -553,38 +515,17 @@ internal string GenerateTypeSource(string typeNamespace, string name, string sou } } - internal bool IsCSharp(Language language) - { - switch (language) - { - case Language.CSharp: - case Language.CSharpVersion2: - case Language.CSharpVersion3: - case Language.CSharpVersion1: - case Language.CSharpVersion4: - case Language.CSharpVersion5: - case Language.CSharpVersion6: - case Language.CSharpVersion7: - return true; - default: - return false; - } - } - // Get the -FromMember template for a given language - internal string GetMethodTemplate(Language language) + private string GetMethodTemplate(Language language) { - if (IsCSharp(language)) - { - return - " public class {0}\n" + - " {{\n" + - " {1}\n" + - " }}\n"; - } - switch (language) { + case Language.CSharp: + return + " public class {0}\n" + + " {{\n" + + " {1}\n" + + " }}\n"; case Language.VisualBasic: return " public Class {0}\n" + @@ -592,110 +533,90 @@ internal string GetMethodTemplate(Language language) " {1}\n" + " \n" + " End Class\n"; - case Language.JScript: - return - " public class {0}\n" + - " {{\n" + - " {1}\n" + - " }}\n"; } + + Diagnostics.Assert(false, "GetMethodTemplate: Unsupported language family."); + return null; } // Get the -FromMember namespace template for a given language - internal string GetNamespaceTemplate(Language language) + private string GetNamespaceTemplate(Language language) { - if (IsCSharp(language)) - { - return - "namespace {0}\n" + - "{{\n" + - "{1}\n" + - "}}\n"; - } - switch (language) { + case Language.CSharp: + return + "namespace {0}\n" + + "{{\n" + + "{1}\n" + + "}}\n"; case Language.VisualBasic: return "Namespace {0}\n" + "\n" + "{1}\n" + "End Namespace\n"; - case Language.JScript: - return - "package {0}\n" + - "{{\n" + - "{1}\n" + - "}}\n"; } + + Diagnostics.Assert(false, "GetNamespaceTemplate: Unsupported language family."); + return null; } // Get the -FromMember namespace template for a given language - internal string GetUsingTemplate(Language language) + private string GetUsingTemplate(Language language) { - if (IsCSharp(language)) - { - return - "using System;\n" + - "using System.Runtime.InteropServices;\n" + - "{0}" + - "\n"; - } - switch (language) { - case Language.VisualBasic: + case Language.CSharp: return - "Imports System\n" + - "Imports System.Runtime.InteropServices\n" + + "using System;\n" + + "using System.Runtime.InteropServices;\n" + "{0}" + "\n"; - case Language.JScript: + case Language.VisualBasic: return - "import System;\n" + - "import System.Runtime.InteropServices;\n" + + "Imports System\n" + + "Imports System.Runtime.InteropServices\n" + "{0}" + "\n"; } + + Diagnostics.Assert(false, "GetUsingTemplate: Unsupported language family."); + return null; } // Generate the code for the using statements - internal string GetUsingSet(Language language) + private string GetUsingSet(Language language) { StringBuilder usingNamespaceSet = new StringBuilder(); - if (IsCSharp(language)) - { - foreach (string namespaceValue in UsingNamespace) - { - usingNamespaceSet.Append("using " + namespaceValue + ";\n"); - } - } - else - { - switch (language) - { - case Language.VisualBasic: - foreach (string namespaceValue in UsingNamespace) - { - usingNamespaceSet.Append("Imports " + namespaceValue + "\n"); - } - break; - case Language.JScript: - foreach (string namespaceValue in UsingNamespace) - { - usingNamespaceSet.Append("import " + namespaceValue + ";\n"); - } - break; - } + switch (language) + { + case Language.CSharp: + foreach (string namespaceValue in UsingNamespace) + { + usingNamespaceSet.Append("using " + namespaceValue + ";\n"); + } + break; + case Language.VisualBasic: + foreach (string namespaceValue in UsingNamespace) + { + usingNamespaceSet.Append("Imports " + namespaceValue + "\n"); + } + break; + default: + Diagnostics.Assert(false, "GetUsingSet: Unsupported language family."); + break; } return usingNamespaceSet.ToString(); } + #endregion GererateSource + internal void HandleCompilerErrors(AddTypeCompilerError[] compilerErrors) { // Get the source code that corresponds to their type in the case of errors @@ -960,7 +881,7 @@ private static PortableExecutableReference[] InitDefaultRefAssemblies() /// /// Initialize the set of assembly names that should be ignored when they are specified in '-ReferencedAssemblies'. - /// - System.Private.CoreLib.ni.dll - the runtim dll that contains most core/primitive types + /// - System.Private.CoreLib.ni.dll - the runtime dll that contains most core/primitive types /// - System.Private.Uri.dll - the runtime dll that contains 'System.Uri' and related types /// Referencing these runtime dlls may cause ambiguous type identity or other issues. /// - System.Runtime.dll - the corresponding reference dll will be automatically included @@ -1016,7 +937,7 @@ private string ResolveAssemblyName(string assembly, bool isForReferenceAssembly) if (!assembly.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { // It could be a short assembly name or a full assembly name, but we - // alwasy want the short name to find the corresponding assembly file. + // always want the short name to find the corresponding assembly file. var assemblyName = new AssemblyName(assembly); refAssemblyDll = assemblyName.Name + ".dll"; } @@ -1106,38 +1027,9 @@ private void WriteTypes(Assembly assembly) private void CompileSourceToAssembly(string source) { CSharpParseOptions parseOptions; - if (IsCSharp(Language)) + if (Language == Language.CSharp) { - switch (Language) - { - case Language.CSharpVersion1: - parseOptions = new CSharpParseOptions(LanguageVersion.CSharp1); - break; - case Language.CSharpVersion2: - parseOptions = new CSharpParseOptions(LanguageVersion.CSharp2); - break; - case Language.CSharpVersion3: - parseOptions = new CSharpParseOptions(LanguageVersion.CSharp3); - break; - case Language.CSharpVersion4: - parseOptions = new CSharpParseOptions(LanguageVersion.CSharp4); - break; - case Language.CSharpVersion5: - parseOptions = new CSharpParseOptions(LanguageVersion.CSharp5); - break; - case Language.CSharpVersion6: - parseOptions = new CSharpParseOptions(LanguageVersion.CSharp6); - break; - case Language.CSharpVersion7: - parseOptions = new CSharpParseOptions(LanguageVersion.CSharp7); - break; - case Language.CSharp: - parseOptions = new CSharpParseOptions(); - break; - default: - parseOptions = null; - break; - } + parseOptions = new CSharpParseOptions(); } else { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 index b36ce180d51..e5f7d933def 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 @@ -34,7 +34,7 @@ Describe "Add-Type" -Tags "CI" { } It "Public 'Language' enumeration contains all members" { - [Enum]::GetNames("Microsoft.PowerShell.Commands.Language") -join "," | Should Be "CSharp,CSharpVersion7,CSharpVersion6,CSharpVersion5,CSharpVersion4,CSharpVersion3,CSharpVersion2,CSharpVersion1,VisualBasic,JScript" + [Enum]::GetNames("Microsoft.PowerShell.Commands.Language") -join "," | Should Be "CSharp,VisualBasic" } It "Should not throw given a simple class definition" {