public DocumentIdWriter(TextWriter writer, ICciFilter filter, DocIdKinds kinds)
     : base(filter)
 {
     _writer = writer;
     _kinds  = kinds;
 }
示例#2
0
文件: Program.cs 项目: wfurt/arcade
        private static int Main(string[] args)
        {
            var app = new CommandLineApplication
            {
                Name                 = "GenAPI",
                FullName             = "A command line tool to generate code for the API surface of an assembly.",
                ResponseFileHandling = ResponseFileHandling.ParseArgsAsSpaceSeparated
            };

            app.HelpOption("-?|-h|--help");
            app.VersionOption("-v|--version", GetAssemblyVersion());

            CommandArgument assemblyArg = app.Argument("assembly", "Path for an specific assembly or a directory to get all assemblies.");

            assemblyArg.IsRequired();
            CommandOption libPath     = app.Option("-l|--lib-path", "Delimited (',' or ';') set of paths to use for resolving assembly references", CommandOptionType.SingleValue);
            CommandOption apiList     = app.Option("-a|--api-list", "Specify a api list in the DocId format of which APIs to include.", CommandOptionType.SingleValue);
            CommandOption outFilePath = app.Option("-o|--out", "Output path. Default is the console. Can specify an existing directory as well and then a file will be created for each assembly with the matching name of the assembly.", CommandOptionType.SingleValue);
            CommandOption headerFile  = app.Option("-h|--header-file", "Specify a file with header content to prepend to output.", CommandOptionType.SingleValue);
            CommandOption <WriterType>       writerType       = app.Option <WriterType>("-w|--writer", "Specify the writer type to use. Legal values: CSDecl, DocIds, TypeForwards, TypeList. Default is CSDecl.", CommandOptionType.SingleValue);
            CommandOption <SyntaxWriterType> syntaxWriterType = app.Option <SyntaxWriterType>("-s|--syntax", "Specific the syntax writer type. Only used if the writer is CSDecl. Legal values: Text, Html, Xml. Default is Text.", CommandOptionType.SingleValue);
            CommandOption <DocIdKinds>       docIdKinds       = app.Option <DocIdKinds>("-d|--doc-id-kinds", "Only include API of the specified kinds. Legal values: A, Assembly, Namespace, N, T, Type, Field, F, P, Property, Method, M, Event, E, All. Default is All.", CommandOptionType.SingleValue);
            CommandOption exceptionMessage      = app.Option("-t|--throw", "Method bodies should throw PlatformNotSupportedException.", CommandOptionType.SingleValue);
            CommandOption globalPrefix          = app.Option("-g|--global", "Include global prefix for compilation.", CommandOptionType.NoValue);
            CommandOption excludeApiList        = app.Option("--exclude-api-list", "Specify a api list in the DocId format of which APIs to exclude.", CommandOptionType.SingleValue);
            CommandOption excludeAttributesList = app.Option("--exclude-attributes-list", "Specify a list in the DocId format of which attributes should be excluded from being applied on apis.", CommandOptionType.SingleValue);
            CommandOption apiOnly                    = app.Option("--api-only", "[CSDecl] Include only API's not CS code that compiles.", CommandOptionType.NoValue);
            CommandOption all                        = app.Option("--all", "Include all API's not just public APIs. Default is public only.", CommandOptionType.NoValue);
            CommandOption memberHeadings             = app.Option("--member-headings", "[CSDecl] Include member headings for each type of member.", CommandOptionType.NoValue);
            CommandOption hightlightBaseMembers      = app.Option("--hightlight-base-members", "[CSDecl] Highlight overridden base members.", CommandOptionType.NoValue);
            CommandOption hightlightInterfaceMembers = app.Option("--hightlight-interface-members", "[CSDecl] Highlight interface implementation members.", CommandOptionType.NoValue);
            CommandOption alwaysIncludeBase          = app.Option("--always-include-base", "[CSDecl] Include base types, interfaces, and attributes, even when those types are filtered.", CommandOptionType.NoValue);
            CommandOption excludeMembers             = app.Option("--exclude-members", "Exclude members when return value or parameter types are excluded.", CommandOptionType.NoValue);
            CommandOption langVersion                = app.Option("--lang-version", "Language Version to target", CommandOptionType.SingleValue);

            app.OnExecute(() =>
            {
                HostEnvironment host  = new HostEnvironment();
                host.UnableToResolve += (sender, e) =>
                                        Console.WriteLine("Unable to resolve assembly '{0}' referenced by '{1}'.", e.Unresolved.ToString(), e.Referrer.ToString());;

                host.UnifyToLibPath = true;
                if (!string.IsNullOrWhiteSpace(libPath.Value()))
                {
                    host.AddLibPaths(HostEnvironment.SplitPaths(libPath.Value()));
                }

                IEnumerable <IAssembly> assemblies = host.LoadAssemblies(HostEnvironment.SplitPaths(assemblyArg.Value));

                if (!assemblies.Any())
                {
                    Console.WriteLine("ERROR: Failed to load any assemblies from '{0}'", assemblyArg.Value);
                    return(1);
                }

                string headerText    = GetHeaderText(headerFile.Value());
                bool loopPerAssembly = Directory.Exists(outFilePath.Value());

                if (loopPerAssembly)
                {
                    foreach (var assembly in assemblies)
                    {
                        using (TextWriter output = GetOutput(GetFilename(assembly, writerType.ParsedValue, syntaxWriterType.ParsedValue)))
                            using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, writerType.ParsedValue, syntaxWriterType.ParsedValue))
                            {
                                if (headerText != null)
                                {
                                    output.Write(headerText);
                                }
                                ICciWriter writer = GetWriter(output, syntaxWriter);
                                writer.WriteAssemblies(new IAssembly[] { assembly });
                            }
                    }
                }
                else
                {
                    using (TextWriter output = GetOutput(outFilePath.Value()))
                        using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, writerType.ParsedValue, syntaxWriterType.ParsedValue))
                        {
                            if (headerText != null)
                            {
                                output.Write(headerText);
                            }
                            ICciWriter writer = GetWriter(output, syntaxWriter);
                            writer.WriteAssemblies(assemblies);
                        }
                }

                return(0);
            });

            ICciWriter GetWriter(TextWriter output, ISyntaxWriter syntaxWriter)
            {
                ICciFilter filter = GetFilter(apiList.Value(), all.HasValue(), apiOnly.HasValue(),
                                              excludeApiList.Value(), excludeMembers.HasValue(), excludeAttributesList.Value());

                switch (writerType.ParsedValue)
                {
                case WriterType.DocIds:
                    DocIdKinds docIdKind = docIdKinds.HasValue() ? docIdKinds.ParsedValue : DocIdKinds.All;
                    return(new DocumentIdWriter(output, filter, docIdKind));

                case WriterType.TypeForwards:
                    return(new TypeForwardWriter(output, filter)
                    {
                        IncludeForwardedTypes = true
                    });

                case WriterType.TypeList:
                    return(new TypeListWriter(syntaxWriter, filter));

                default:
                case WriterType.CSDecl:
                {
                    CSharpWriter writer = new CSharpWriter(syntaxWriter, filter, apiOnly.HasValue());
                    writer.IncludeSpaceBetweenMemberGroups = writer.IncludeMemberGroupHeadings = memberHeadings.HasValue();
                    writer.HighlightBaseMembers            = hightlightBaseMembers.HasValue();
                    writer.HighlightInterfaceMembers       = hightlightInterfaceMembers.HasValue();
                    writer.PutBraceOnNewLine = true;
                    writer.PlatformNotSupportedExceptionMessage = exceptionMessage.Value();
                    writer.IncludeGlobalPrefixForCompilation    = globalPrefix.HasValue();
                    writer.AlwaysIncludeBase = alwaysIncludeBase.HasValue();
                    writer.LangVersion       = GetLangVersion();
                    return(writer);
                }
                }
            }

            Version GetLangVersion()
            {
                if (langVersion.HasValue())
                {
                    var langVersionValue = langVersion.Value();

                    if (langVersionValue.Equals("default", StringComparison.OrdinalIgnoreCase))
                    {
                        return(CSDeclarationWriter.LangVersionDefault);
                    }
                    else if (langVersionValue.Equals("latest", StringComparison.OrdinalIgnoreCase))
                    {
                        return(CSDeclarationWriter.LangVersionLatest);
                    }
                    else if (langVersionValue.Equals("preview", StringComparison.OrdinalIgnoreCase))
                    {
                        return(CSDeclarationWriter.LangVersionPreview);
                    }
                    else if (Version.TryParse(langVersionValue, out var parsedVersion))
                    {
                        return(parsedVersion);
                    }
                }

                return(CSDeclarationWriter.LangVersionDefault);
            }

            return(app.Execute(args));
        }