Merge pull request #7284 from daxian-dbw/removeVB

Remove the 'VisualBasic' support from Add-Type
This commit is contained in:
Aditya Patwardhan 2018-07-13 13:48:50 -07:00 committed by GitHub
commit ab07b85f01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 150 deletions

View file

@ -58,9 +58,6 @@
<Component Id="cmp3CC027D3F160412C9F0044EBED3115DD" Guid="{8D7CAA67-8F28-422C-85FB-BDE04902E64F}">
<File Id="filFD2EF6BC74AF459D1BB52CA1E8C6E33B" KeyPath="yes" Source="$(env.ProductSourcePath)\NJsonSchema.dll" />
</Component>
<Component Id="cmp98A11B3F71BACCA2DD0A4856577CD343" Guid="{4EFB61AC-361B-42D0-B9A1-C1FEE287CD13}">
<File Id="fil9286A659888A5C12030A9CF0B215D062" KeyPath="yes" Source="$(env.ProductSourcePath)\Microsoft.CodeAnalysis.VisualBasic.dll" />
</Component>
<Component Id="cmp3B130879A26D2E954251BB81E8948069" Guid="{CD268615-4603-4A5F-B126-340ADF2EDDD5}">
<File Id="filAACDEEE28FEA076C73D082A0AD21B8E0" KeyPath="yes" Source="$(env.ProductSourcePath)\Microsoft.CSharp.dll" />
</Component>
@ -1937,7 +1934,6 @@
<ComponentRef Id="cmp30C10EC477714A76B2A8D0A957D1D6F3" />
<ComponentRef Id="cmp31176C5A3BDB4A9F95171A4780D7D4D2" />
<ComponentRef Id="cmp3CC027D3F160412C9F0044EBED3115DD" />
<ComponentRef Id="cmp98A11B3F71BACCA2DD0A4856577CD343" />
<ComponentRef Id="cmp3B130879A26D2E954251BB81E8948069" />
<ComponentRef Id="cmp02ABBE4A3EDBEBFD05DC17A009A2B79D" />
<ComponentRef Id="cmpAA10498DF244C013CB5043C62E3AA83A" />

View file

@ -66,7 +66,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.8.2" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="2.8.2" />
<PackageReference Include="NJsonSchema" Version="9.10.63" />
</ItemGroup>

View file

@ -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
/// <summary>
/// The C# programming language.
/// </summary>
CSharp,
/// <summary>
/// The Visual Basic programming language.
/// </summary>
VisualBasic
CSharp
}
/// <summary>
@ -166,8 +160,7 @@ namespace Microsoft.PowerShell.Commands
foreach (string path in pathValue)
{
// Try to resolve the path
ProviderInfo provider = null;
Collection<string> newPaths = SessionState.Path.GetResolvedProviderPathFromPSPath(path, out provider);
Collection<string> 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<string> 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
/// <summary>
/// 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<SyntaxTree> 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<String> 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);
}

View file

@ -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"
}
}