From cb69b41059472c8730528dac6c755580e56dea87 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 9 Jan 2018 11:19:15 +0300 Subject: [PATCH 1/4] Cleanup Add-Type Remove CSharp language versions and JScript from Language enum. Correct comments. Remove IgnoreWarnings parameter. --- .../commands/utility/AddType.cs | 238 +++++------------- .../Add-Type.Tests.ps1 | 2 +- 2 files changed, 68 insertions(+), 172 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 50d406dc934..662892957d3 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; @@ -525,11 +483,9 @@ public OutputAssemblyType OutputType [Parameter()] public SwitchParameter PassThru { get; set; } - /// - /// Flag to ignore warnings during compilation. - /// - [Parameter()] - public SwitchParameter IgnoreWarnings { get; set; } + #endregion Parameters + + #region GererateSource internal string GenerateTypeSource(string typeNamespace, string name, string sourceCode, Language language) { @@ -553,38 +509,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 +527,92 @@ 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 +877,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 +933,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,31 +1023,10 @@ 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; 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" { From f78c30e116e359813f991492572eb906bf0af06c Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 18 Jan 2018 07:42:50 +0300 Subject: [PATCH 2/4] Correct switches --- .../commands/utility/AddType.cs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 662892957d3..095b3e58536 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -602,9 +602,7 @@ private string GetUsingSet(Language language) } break; default: - { Diagnostics.Assert(false, "GetUsingSet: Unsupported language family."); - } break; } @@ -1025,15 +1023,7 @@ private void CompileSourceToAssembly(string source) CSharpParseOptions parseOptions; if (Language == Language.CSharp) { - switch (Language) - { - case Language.CSharp: - parseOptions = new CSharpParseOptions(); - break; - default: - parseOptions = null; - break; - } + parseOptions = new CSharpParseOptions(); } else { From 6af06bc40fa29054c9eb774593804166ae23d5bc Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 18 Jan 2018 14:49:06 -0800 Subject: [PATCH 3/4] Remove unneeded spaces --- .../commands/utility/AddType.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 095b3e58536..8ee2e19634d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -602,7 +602,7 @@ private string GetUsingSet(Language language) } break; default: - Diagnostics.Assert(false, "GetUsingSet: Unsupported language family."); + Diagnostics.Assert(false, "GetUsingSet: Unsupported language family."); break; } From 996f5e77e435f211c97dbe2294c346fe0a7ec012 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Wed, 31 Jan 2018 17:58:23 -0800 Subject: [PATCH 4/4] Add back the parameter "IgnoreWarnings" The parameter `-IgnoreWarnings` is implemented in Windows PowerShell. It should also be implemented in PowerShell Core. --- .../commands/utility/AddType.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 8ee2e19634d..4bba1753e39 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -483,6 +483,12 @@ public OutputAssemblyType OutputType [Parameter()] public SwitchParameter PassThru { get; set; } + /// + /// Flag to ignore warnings during compilation. + /// + [Parameter()] + public SwitchParameter IgnoreWarnings { get; set; } + #endregion Parameters #region GererateSource