Пример #1
0
 public static string GenerateContract(ApiContractGenerator generator, string sourceCode, Microsoft.CodeAnalysis.VisualBasic.LanguageVersion languageVersion, IAssemblyReferenceResolver assemblyResolver = null)
 {
     using var stream = new MemoryStream();
     EmitCompilation(sourceCode, stream, languageVersion);
     return(GenerateContract(generator, stream, assemblyResolver));
 }
Пример #2
0
        private static VisualBasicCompilationOptions CreateVisualBasicCompilationOptions(
            string assemblyFileName,
            CompilationOptionsReader optionsReader
            )
        {
            var pdbOptions = optionsReader.GetMetadataCompilationOptions();

            var langVersionString = pdbOptions.GetUniqueOption(
                CompilationOptionNames.LanguageVersion
                );

            pdbOptions.TryGetUniqueOption(
                CompilationOptionNames.Optimization,
                out var optimization
                );
            pdbOptions.TryGetUniqueOption(CompilationOptionNames.Platform, out var platform);
            pdbOptions.TryGetUniqueOption(
                CompilationOptionNames.GlobalNamespaces,
                out var globalNamespacesString
                );

            IEnumerable <GlobalImport>?globalImports = null;

            if (!string.IsNullOrEmpty(globalNamespacesString))
            {
                globalImports = GlobalImport.Parse(globalNamespacesString.Split(';'));
            }

            VB.LanguageVersion langVersion = default;
            VB.LanguageVersionFacts.TryParse(langVersionString, ref langVersion);

            IReadOnlyDictionary <string, object>?preprocessorSymbols = null;

            if (pdbOptions.OptionToString(CompilationOptionNames.Define) is string defineString)
            {
                preprocessorSymbols =
                    VisualBasicCommandLineParser.ParseConditionalCompilationSymbols(
                        defineString,
                        out var diagnostics
                        );
                var diagnostic = diagnostics?.FirstOrDefault(x => x.IsUnsuppressedError);
                if (diagnostic is object)
                {
                    throw new Exception($"Cannot create compilation options: {diagnostic}");
                }
            }

            var parseOptions = VisualBasicParseOptions.Default
                               .WithLanguageVersion(langVersion)
                               .WithPreprocessorSymbols(preprocessorSymbols.ToImmutableArrayOrEmpty());

            var(optimizationLevel, plus) = GetOptimizationLevel(optimization);
            var isChecked      = pdbOptions.OptionToBool(CompilationOptionNames.Checked) ?? true;
            var embedVBRuntime =
                pdbOptions.OptionToBool(CompilationOptionNames.EmbedRuntime) ?? false;
            var rootNamespace = pdbOptions.OptionToString(CompilationOptionNames.RootNamespace);

            var compilationOptions = new VisualBasicCompilationOptions(
                pdbOptions.OptionToEnum <OutputKind>(CompilationOptionNames.OutputKind)
                ?? OutputKind.DynamicallyLinkedLibrary,
                moduleName: assemblyFileName,
                mainTypeName: optionsReader.GetMainTypeName(),
                scriptClassName: "Script",
                globalImports: globalImports,
                rootNamespace: rootNamespace,
                optionStrict: pdbOptions.OptionToEnum <OptionStrict>(
                    CompilationOptionNames.OptionStrict
                    ) ?? OptionStrict.Off,
                optionInfer: pdbOptions.OptionToBool(CompilationOptionNames.OptionInfer) ?? false,
                optionExplicit: pdbOptions.OptionToBool(CompilationOptionNames.OptionExplicit)
                ?? false,
                optionCompareText: pdbOptions.OptionToBool(CompilationOptionNames.OptionCompareText)
                ?? false,
                parseOptions: parseOptions,
                embedVbCoreRuntime: embedVBRuntime,
                optimizationLevel: optimizationLevel,
                checkOverflow: isChecked,
                cryptoKeyContainer: null,
                cryptoKeyFile: null,
                cryptoPublicKey: optionsReader.GetPublicKey()?.ToImmutableArray() ?? default,
                delaySign: null,
                platform: GetPlatform(platform),
                generalDiagnosticOption: ReportDiagnostic.Default,
                specificDiagnosticOptions: null,
                concurrentBuild: true,
                deterministic: true,
                xmlReferenceResolver: null,
                sourceReferenceResolver: null,
                metadataReferenceResolver: null,
                assemblyIdentityComparer: null,
                strongNameProvider: null,
                publicSign: false,
                reportSuppressedDiagnostics: false,
                metadataImportOptions: MetadataImportOptions.Public
                );

            compilationOptions.DebugPlusMode = plus;

            return(compilationOptions);
        }
Пример #3
0
 public static void EmitCompilation(string sourceCode, Stream target, Microsoft.CodeAnalysis.VisualBasic.LanguageVersion languageVersion)
 {
     EmitCompilation(
         BaseVisualBasicCompilation.Value.AddSyntaxTrees(VisualBasicSyntaxTree.ParseText(sourceCode, new VisualBasicParseOptions(languageVersion))),
         target);
 }