diff --git a/assets/files.wxs b/assets/files.wxs index f861da02d..e503fac3a 100644 --- a/assets/files.wxs +++ b/assets/files.wxs @@ -58,9 +58,6 @@ - - - @@ -1937,7 +1934,6 @@ - diff --git a/src/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj b/src/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj index 4dfe12837..b8e19ad48 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj +++ b/src/Microsoft.PowerShell.Commands.Utility/Microsoft.PowerShell.Commands.Utility.csproj @@ -66,7 +66,6 @@ - diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index 6fe439a82..dbe6ed0c0 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -22,7 +22,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Emit; using Microsoft.CodeAnalysis.Text; -using Microsoft.CodeAnalysis.VisualBasic; using PathType = System.IO.Path; @@ -37,12 +36,7 @@ namespace Microsoft.PowerShell.Commands /// /// The C# programming language. /// - CSharp, - - /// - /// The Visual Basic programming language. - /// - VisualBasic + CSharp } /// @@ -166,8 +160,7 @@ namespace Microsoft.PowerShell.Commands foreach (string path in pathValue) { // Try to resolve the path - ProviderInfo provider = null; - Collection newPaths = SessionState.Path.GetResolvedProviderPathFromPSPath(path, out provider); + Collection newPaths = SessionState.Path.GetResolvedProviderPathFromPSPath(path, out ProviderInfo _); // If it didn't resolve, add the original back // for a better error message. @@ -218,12 +211,8 @@ namespace Microsoft.PowerShell.Commands private void ProcessPaths(List resolvedPaths) { - // Now, get the file type. At the same time, make sure - // we aren't attempting to mix languages, as that is - // not supported by the Roslyn. While it - // would be possible to partition the files into - // languages, that would be much too complex to - // describe. + // Validate file extensions. + // Make sure we don't mix source files from different languages (if we support any other languages in future). string activeExtension = null; foreach (string path in resolvedPaths) { @@ -235,10 +224,6 @@ namespace Microsoft.PowerShell.Commands Language = Language.CSharp; break; - case ".VB": - Language = Language.VisualBasic; - break; - case ".DLL": loadAssembly = true; break; @@ -253,7 +238,6 @@ namespace Microsoft.PowerShell.Commands currentExtension); ThrowTerminatingError(errorRecord); - break; } @@ -273,9 +257,9 @@ namespace Microsoft.PowerShell.Commands ThrowTerminatingError(errorRecord); } - - _paths = resolvedPaths.ToArray(); } + + _paths = resolvedPaths.ToArray(); } private string[] _paths; @@ -425,15 +409,12 @@ namespace Microsoft.PowerShell.Commands /// /// Roslyn command line parameters. /// https://github.com/dotnet/roslyn/blob/master/docs/compilers/CSharp/CommandLine.md - /// https://github.com/dotnet/roslyn/blob/master/docs/compilers/Visual%20Basic/CommandLine.md /// /// Parser options: /// langversion:string - language version from: /// [enum]::GetNames([Microsoft.CodeAnalysis.CSharp.LanguageVersion]) - /// [enum]::GetNames([Microsoft.CodeAnalysis.VisualBasic.LanguageVersion]) /// define:symbol list - preprocessor symbols: /// /define:UNIX,DEBUG - CSharp - /// /define:UNIX=1,DEBUG=1 - VisualBasic /// /// Compilation options: /// optimize{+|-} - optimization level @@ -444,7 +425,6 @@ namespace Microsoft.PowerShell.Commands /// nowarn - disable all warnings /// nowarn:strings - disable a list of individual warnings /// usings:strings - ';'-delimited usings for CSharp - /// imports:strings - ';'-delimited imports for VisualBasic /// /// Emit options: /// platform:string - limit which platforms this code can run on; must be x86, x64, Itanium, arm, AnyCPU32BitPreferred or anycpu (default) @@ -497,18 +477,9 @@ namespace Microsoft.PowerShell.Commands " {{\n" + " {1}\n" + " }}\n"; - case Language.VisualBasic: - return - " public Class {0}\n" + - " \n" + - " {1}\n" + - " \n" + - " End Class\n"; } - Diagnostics.Assert(false, "GetMethodTemplate: Unsupported language."); - - return null; + throw PSTraceSource.NewNotSupportedException(); } // Get the -FromMember namespace template for a given language @@ -522,17 +493,9 @@ namespace Microsoft.PowerShell.Commands "{{\n" + "{1}\n" + "}}\n"; - case Language.VisualBasic: - return - "Namespace {0}\n" + - "\n" + - "{1}\n" + - "End Namespace\n"; } - Diagnostics.Assert(false, "GetNamespaceTemplate: Unsupported language."); - - return null; + throw PSTraceSource.NewNotSupportedException(); } // Get the -FromMember namespace template for a given language @@ -546,17 +509,9 @@ namespace Microsoft.PowerShell.Commands "using System.Runtime.InteropServices;\n" + "{0}" + "\n"; - case Language.VisualBasic: - return - "Imports System\n" + - "Imports System.Runtime.InteropServices\n" + - "{0}" + - "\n"; } - Diagnostics.Assert(false, "GetUsingTemplate: Unsupported language."); - - return null; + throw PSTraceSource.NewNotSupportedException(); } // Generate the code for the using statements @@ -572,15 +527,9 @@ namespace Microsoft.PowerShell.Commands 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."); - break; + throw PSTraceSource.NewNotSupportedException(); } return usingNamespaceSet.ToString(); @@ -910,14 +859,10 @@ namespace Microsoft.PowerShell.Commands { case Language.CSharp: return CSharpCommandLineParser.Default.Parse(args, baseDirectory, sdkDirectory, additionalReferenceDirectories); - case Language.VisualBasic: - return VisualBasicCommandLineParser.Default.Parse(args, baseDirectory, sdkDirectory, additionalReferenceDirectories); - default: - Diagnostics.Assert(false, "ParseCompilerOption: Unsupported language family."); - break; - } - return null; + default: + throw PSTraceSource.NewNotSupportedException(); + } } private SyntaxTree ParseSourceText(SourceText sourceText, ParseOptions parseOptions, string path = "") @@ -927,15 +872,9 @@ namespace Microsoft.PowerShell.Commands case Language.CSharp: return CSharpSyntaxTree.ParseText(sourceText, (CSharpParseOptions) parseOptions, path); - case Language.VisualBasic: - return VisualBasicSyntaxTree.ParseText(sourceText, (VisualBasicParseOptions) parseOptions, path); - default: - Diagnostics.Assert(false, "ParseSourceText: Unsupported language family."); - break; + throw PSTraceSource.NewNotSupportedException(); } - - return null; } private CompilationOptions GetDefaultCompilationOptions() @@ -945,15 +884,9 @@ namespace Microsoft.PowerShell.Commands case Language.CSharp: return new CSharpCompilationOptions(OutputAssemblyTypeToOutputKind(OutputType)); - case Language.VisualBasic: - return new VisualBasicCompilationOptions(outputKind: OutputAssemblyTypeToOutputKind(OutputType)); - default: - Diagnostics.Assert(false, "GetDefaultCompilationOptions: Unsupported language family."); - break; + throw PSTraceSource.NewNotSupportedException(); } - - return null; } private bool isSourceCodeUpdated(List syntaxTrees, out Assembly assembly) @@ -1069,16 +1002,12 @@ namespace Microsoft.PowerShell.Commands references: references, options: (CSharpCompilationOptions)compilationOptions); break; - case Language.VisualBasic: - compilation = VisualBasicCompilation.Create( - PathType.GetRandomFileName(), - syntaxTrees: syntaxTrees, - references: references, - options: (VisualBasicCompilationOptions)compilationOptions); - break; + + default: + throw PSTraceSource.NewNotSupportedException(); } - DoEmitAndLoadAssemply(compilation, emitOptions); + DoEmitAndLoadAssembly(compilation, emitOptions); } private void CheckDuplicateTypes(Compilation compilation, out ConcurrentBag newTypes) @@ -1162,12 +1091,12 @@ namespace Microsoft.PowerShell.Commands } } - private void CacheAssemply(Assembly assembly) + private void CacheAssembly(Assembly assembly) { s_sourceAssemblyCache.Add(_syntaxTreesHash, assembly); } - private void DoEmitAndLoadAssemply(Compilation compilation, EmitOptions emitOptions) + private void DoEmitAndLoadAssembly(Compilation compilation, EmitOptions emitOptions) { EmitResult emitResult; @@ -1189,7 +1118,7 @@ namespace Microsoft.PowerShell.Commands Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(ms); CacheNewTypes(newTypes); - CacheAssemply(assembly); + CacheAssembly(assembly); if (PassThru) { @@ -1212,7 +1141,7 @@ namespace Microsoft.PowerShell.Commands Assembly assembly = Assembly.LoadFrom(_outputAssembly); CacheNewTypes(newTypes); - CacheAssemply(assembly); + CacheAssembly(assembly); WriteTypes(assembly); } 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 b43ae15b4..525722849 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Add-Type.Tests.ps1 @@ -34,30 +34,6 @@ Describe "Add-Type" -Tags "CI" { Set-Content -Path $CSharpFile1 -Value $CSharpCode1 -Force Set-Content -Path $CSharpFile2 -Value $CSharpCode2 -Force - $VBCode1 = @" - Namespace Test.AddType - Public Class VBTest1$guid - Public Shared Function Add1(a As Integer, b As Integer) As String - return (a + b) - End Function - End Class - End Namespace -"@ - $VBCode2 = @" - Namespace Test.AddType - Public Class VBTest2$guid - Public Shared Function Add2(a As Integer, b As Integer) As String - return (a + b) - End Function - End Class - End Namespace -"@ - $VBFile1 = Join-Path -Path $TestDrive -ChildPath "VBFile1.vb" - $VBFile2 = Join-Path -Path $TestDrive -ChildPath "VBFile2.vb" - - Set-Content -Path $VBFile1 -Value $VBCode1 -Force - Set-Content -Path $VBFile2 -Value $VBCode2 -Force - $codeWarning = @" namespace Test.AddType { @@ -74,7 +50,7 @@ Describe "Add-Type" -Tags "CI" { } It "Public 'Language' enumeration contains all members" { - [Enum]::GetNames("Microsoft.PowerShell.Commands.Language") -join "," | Should -BeExactly "CSharp,VisualBasic" + [Enum]::GetNames("Microsoft.PowerShell.Commands.Language") -join "," | Should -BeExactly "CSharp" } It "Should not throw given a simple C# class definition" { @@ -85,13 +61,6 @@ Describe "Add-Type" -Tags "CI" { [CSharpfooType].Name | Should BeExactly "CSharpfooType" } - It "Should not throw given a simple VisualBasic class definition" { - # In subsequent launches from the same session - # the test will be passed without real compile - it will return an assembly previously compiled. - { Add-Type -TypeDefinition "Public Class VBfooType `n End Class" -Language VisualBasic } | Should Not Throw - [VBfooType].Name | Should BeExactly "VBfooType" - } - It "Can use System.Management.Automation.CmdletAttribute" { $code = @" using System.Management.Automation; @@ -127,13 +96,6 @@ public class AttributeTest$guid : PSCmdlet file2 = $CSharpFile2 sourceLanguage = "CSharp" } - @{ - type1 = "[Test.AddType.VBTest1$guid]" - type2 = "[Test.AddType.VBTest2$guid]" - file1 = $VBFile1 - file2 = $VBFile2 - sourceLanguage = "VisualBasic" - } ) { param($type1, $type2, $file1, $file2, $sourceLanguage) @@ -167,16 +129,6 @@ public class AttributeTest$guid : PSCmdlet expectedResult = "System.Text.UTF8Encoding+UTF8EncodingSealed" sourceLanguage = "CSharp" } - @{ - sourceCode = "Public Shared Function TestString() As String `n Return UTF8Encoding.UTF8.ToString() `n End Function" - sourceType = "TestVisualBasicType1" - sourceNS = "TestVisualBasicNS" - sourceUsingNS = "System.Text" - sourceRunType = "TestVisualBasicNS.TestVisualBasicType1" - sourceDefaultNSRunType = "Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes.TestVisualBasicType1" - expectedResult = "System.Text.UTF8Encoding+UTF8EncodingSealed" - sourceLanguage = "VisualBasic" - } ) { param($sourceCode, $sourceType, $sourceNS, $sourceUsingNS, $sourceRunType, $sourceDefaultNSRunType, $expectedResult, $sourceLanguage) @@ -253,7 +205,6 @@ public class AttributeTest$guid : PSCmdlet # Catch non-termination information error for CompilerOptions. { Add-Type -CompilerOptions "/platform:anycpuERROR" -Language CSharp -MemberDefinition "public static string TestString() { return ""}" -Name "TestType1" -Namespace "TestNS" -ErrorAction Stop } | Should -Throw -ErrorId "SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand" - { Add-Type -CompilerOptions "/platform:anycpuERROR" -Language VisualBasic -MemberDefinition "Public Shared Function TestString() As String `n Return `"`" `n End Function" -Name "TestType1" -Namespace "TestNS" -ErrorAction Stop } | Should -Throw -ErrorId "SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand" } It "OutputType parameter requires that the OutputAssembly parameter be specified." { @@ -269,4 +220,10 @@ public class AttributeTest$guid : PSCmdlet Add-Type -TypeDefinition $codeWarning -IgnoreWarnings -WarningVariable warnVar -WarningAction SilentlyContinue $warnVar.Count | Should -Be 1 } + + It "Throw terminating error when file with non-supported extension is passed to -Path" { + $VBFile = Join-Path -Path $TestDrive -ChildPath "VBFile.vb" + New-Item -Path $VBFile -ItemType File -Force > $null + { Add-Type -Path $VBFile } | Should -Throw -ErrorId "EXTENSION_NOT_SUPPORTED,Microsoft.PowerShell.Commands.AddTypeCommand" + } }