Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions assets/files.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
117 changes: 23 additions & 94 deletions src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VisualBasic;

using PathType = System.IO.Path;

Expand All @@ -37,12 +36,7 @@ public enum Language
/// <summary>
/// The C# programming language.
/// </summary>
CSharp,

/// <summary>
/// The Visual Basic programming language.
/// </summary>
VisualBasic
CSharp
}

/// <summary>
Expand Down Expand Up @@ -166,8 +160,7 @@ public string[] Path
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.
Expand Down Expand Up @@ -218,12 +211,8 @@ public string[] LiteralPath

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)
{
Expand All @@ -235,10 +224,6 @@ private void ProcessPaths(List<string> resolvedPaths)
Language = Language.CSharp;
break;

case ".VB":
Language = Language.VisualBasic;
break;

case ".DLL":
loadAssembly = true;
break;
Expand All @@ -253,7 +238,6 @@ private void ProcessPaths(List<string> resolvedPaths)
currentExtension);

ThrowTerminatingError(errorRecord);

break;
}

Expand All @@ -273,9 +257,9 @@ private void ProcessPaths(List<string> resolvedPaths)

ThrowTerminatingError(errorRecord);
}

_paths = resolvedPaths.ToArray();
}

_paths = resolvedPaths.ToArray();
}

private string[] _paths;
Expand Down Expand Up @@ -425,15 +409,12 @@ public string OutputAssembly
/// <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
Expand All @@ -444,7 +425,6 @@ public string OutputAssembly
/// 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)
Expand Down Expand Up @@ -497,18 +477,9 @@ private string GetMethodTemplate(Language language)
" {{\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
Expand All @@ -522,17 +493,9 @@ private string GetNamespaceTemplate(Language language)
"{{\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
Expand All @@ -546,17 +509,9 @@ private string GetUsingTemplate(Language language)
"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
Expand All @@ -572,15 +527,9 @@ private string GetUsingSet(Language language)
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();
Expand Down Expand Up @@ -910,14 +859,10 @@ private CommandLineArguments ParseCompilerOption(IEnumerable<string> args)
{
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;
throw PSTraceSource.NewNotSupportedException();
}

return null;
}

private SyntaxTree ParseSourceText(SourceText sourceText, ParseOptions parseOptions, string path = "")
Expand All @@ -927,15 +872,9 @@ private SyntaxTree ParseSourceText(SourceText sourceText, ParseOptions parseOpti
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()
Expand All @@ -945,15 +884,9 @@ private CompilationOptions GetDefaultCompilationOptions()
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)
Expand Down Expand Up @@ -1069,16 +1002,12 @@ private void CompileToAssembly(List<SyntaxTree> syntaxTrees, CompilationOptions
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)
Expand Down Expand Up @@ -1162,12 +1091,12 @@ private void CacheNewTypes(ConcurrentBag<String> newTypes)
}
}

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;

Expand All @@ -1189,7 +1118,7 @@ private void DoEmitAndLoadAssemply(Compilation compilation, EmitOptions emitOpti
Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(ms);

CacheNewTypes(newTypes);
CacheAssemply(assembly);
CacheAssembly(assembly);

if (PassThru)
{
Expand All @@ -1212,7 +1141,7 @@ private void DoEmitAndLoadAssemply(Compilation compilation, EmitOptions emitOpti
Assembly assembly = Assembly.LoadFrom(_outputAssembly);

CacheNewTypes(newTypes);
CacheAssemply(assembly);
CacheAssembly(assembly);

WriteTypes(assembly);
}
Expand Down
Loading