private static ICciWriter GetWriter(TextWriter output, IStyleSyntaxWriter syntaxWriter) { ICciFilter filter = GetFilter(); switch (s_writer) { case WriterType.DocIds: return(new DocumentIdWriter(output, filter, s_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, s_apiOnly); writer.IncludeSpaceBetweenMemberGroups = writer.IncludeMemberGroupHeadings = s_memberHeadings; writer.HighlightBaseMembers = s_hightlightBaseMembers; writer.HighlightInterfaceMembers = s_hightlightInterfaceMembers; writer.PutBraceOnNewLine = true; writer.PlatformNotSupportedExceptionMessage = s_exceptionMessage; writer.IncludeGlobalPrefixForCompilation = s_global; writer.AlwaysIncludeBase = s_alwaysIncludeBase; return(writer); } } }
private static ICciWriter GetWriter(TextWriter output, IStyleSyntaxWriter syntaxWriter) { ICciFilter filter = GetFilter(); switch (s_writer) { case WriterType.DocIds: return(new DocumentIdWriter(output, filter)); case WriterType.TypeForwards: return(new TypeForwardWriter(output, filter)); case WriterType.TypeList: return(new TypeListWriter(syntaxWriter, filter)); default: case WriterType.CSDecl: { CSharpWriter writer = new CSharpWriter(syntaxWriter, filter, s_apiOnly); writer.IncludeSpaceBetweenMemberGroups = writer.IncludeMemberGroupHeadings = s_memberHeadings; writer.HighlightBaseMembers = s_hightlightBaseMembers; writer.HighlightInterfaceMembers = s_hightlightInterfaceMembers; writer.PutBraceOnNewLine = true; writer.ThrowPlatformNotSupportedForCompilation = s_throw; return(writer); } } }
private static ICciWriter GetWriter(TextWriter output, IStyleSyntaxWriter syntaxWriter) { ICciFilter filter = GetFilter(); switch (s_writer) { case WriterType.DocIds: return new DocumentIdWriter(output, filter); case WriterType.TypeForwards: return new TypeForwardWriter(output, filter); case WriterType.TypeList: return new TypeListWriter(syntaxWriter, filter); default: case WriterType.CSDecl: { CSharpWriter writer = new CSharpWriter(syntaxWriter, filter, s_apiOnly); writer.IncludeSpaceBetweenMemberGroups = writer.IncludeMemberGroupHeadings = s_memberHeadings; writer.HighlightBaseMembers = s_hightlightBaseMembers; writer.HighlightInterfaceMembers = s_hightlightInterfaceMembers; writer.PutBraceOnNewLine = true; writer.ThrowPlatformNotSupportedForCompilation = s_throw; return writer; } } }
public CSharpWriter(ISyntaxWriter writer, ICciFilter filter, bool apiOnly, bool writeAssemblyAttributes = false) : base(filter) { _syntaxWriter = writer; _styleWriter = writer as IStyleSyntaxWriter; _declarationWriter = new CSDeclarationWriter(_syntaxWriter, filter, !apiOnly); _writeAssemblyAttributes = writeAssemblyAttributes; }
private static int Main(string[] args) { ParseCommandLine(args); HostEnvironment host = new HostEnvironment(); host.UnableToResolve += host_UnableToResolve; host.UnifyToLibPath = true; if (!string.IsNullOrWhiteSpace(s_libPath)) { host.AddLibPaths(HostEnvironment.SplitPaths(s_libPath)); } IEnumerable <IAssembly> assemblies = host.LoadAssemblies(HostEnvironment.SplitPaths(s_assembly)); if (!assemblies.Any()) { Console.WriteLine("ERROR: Failed to load any assemblies from '{0}'", s_assembly); return(1); } string headerText = GetHeaderText(); bool loopPerAssembly = Directory.Exists(s_out); if (loopPerAssembly) { foreach (var assembly in assemblies) { using (TextWriter output = GetOutput(GetFilename(assembly))) using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output)) { if (headerText != null) { output.Write(headerText); } ICciWriter writer = GetWriter(output, syntaxWriter); writer.WriteAssemblies(new IAssembly[] { assembly }); } } } else { using (TextWriter output = GetOutput()) using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output)) { if (headerText != null) { output.Write(headerText); } ICciWriter writer = GetWriter(output, syntaxWriter); writer.WriteAssemblies(assemblies); } } return(0); }
public DiffCSharpWriter(IStyleSyntaxWriter writer, MappingSettings settings, IEnumerable <DiffComment> diffComments, bool includePseudoCustomAttributes) : base(settings) { _syntaxWriter = writer; _settings = InitializeSettings(settings); _formatter = new CSDeclarationWriter(_syntaxWriter, _settings.Filter, forCompilation: false, includePseudoCustomAttributes: includePseudoCustomAttributes) { LangVersion = CSDeclarationWriter.LangVersionPreview }; _declHelper = new CSDeclarationHelper(_settings.Filter, forCompilation: false, includePseudoCustomAttributes: includePseudoCustomAttributes); _diffComments = diffComments ?? Enumerable.Empty <DiffComment>(); }
private static int Main(string[] args) { ParseCommandLine(args); HostEnvironment host = new HostEnvironment(); host.UnableToResolve += host_UnableToResolve; host.UnifyToLibPath = true; if (!string.IsNullOrWhiteSpace(s_libPath)) { host.AddLibPaths(HostEnvironment.SplitPaths(s_libPath)); } IEnumerable <IAssembly> assemblies = host.LoadAssemblies(HostEnvironment.SplitPaths(s_assembly)); if (!assemblies.Any()) { Console.WriteLine("ERROR: Failed to load any assemblies from '{0}'", s_assembly); return(1); } using (TextWriter output = GetOutput()) using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output)) { if (!String.IsNullOrEmpty(s_headerFile)) { if (!File.Exists(s_headerFile)) { Console.WriteLine("ERROR: header file '{0}' does not exist", s_headerFile); return(1); } using (TextReader headerText = File.OpenText(s_headerFile)) { output.Write(headerText.ReadToEnd()); } } ICciWriter writer = GetWriter(output, syntaxWriter); writer.WriteAssemblies(assemblies); } return(0); }
private static ICciDifferenceWriter CreateExportWriter(DiffFormat format, TextWriter textWriter, DiffConfiguration configuration, IStyleSyntaxWriter writer, IEnumerable <DiffComment> diffComments) { var mappingSettings = GetMappingSettings(configuration); var includeAttributes = configuration.IsOptionSet(DiffConfigurationOptions.DiffAttributes); switch (format) { case DiffFormat.Csv: var diffColumns = DiffCsvColumn.CreateStandardColumns(configuration).Where(c => c.IsVisible).ToArray(); var csvTextWriter = new CsvTextWriter(textWriter); csvTextWriter.WriteLine(diffColumns.Select(c => c.Name)); return(new DiffCsvWriter(csvTextWriter, mappingSettings, diffColumns, CancellationToken.None)); case DiffFormat.Html: return(new DiffCSharpWriter(writer, mappingSettings, diffComments, includeAttributes) { IncludeAssemblyProperties = configuration.IsOptionSet(DiffConfigurationOptions.DiffAssemblyInfo), HighlightBaseMembers = configuration.IsOptionSet(DiffConfigurationOptions.HighlightBaseMembers) }); case DiffFormat.WordXml: case DiffFormat.Text: case DiffFormat.UnifiedDiff: return(new DiffCSharpWriter(writer, mappingSettings, diffComments, includeAttributes) { IncludeAssemblyProperties = configuration.IsOptionSet(DiffConfigurationOptions.DiffAssemblyInfo), HighlightBaseMembers = configuration.IsOptionSet(DiffConfigurationOptions.HighlightBaseMembers) }); default: throw new ArgumentOutOfRangeException("format"); } }
public override bool Execute() { HostEnvironment host = new(); host.UnableToResolve += (sender, e) => Log.LogWarning("Unable to resolve assembly '{0}' referenced by '{1}'.", e.Unresolved.ToString(), e.Referrer.ToString()); host.UnifyToLibPath = true; if (!string.IsNullOrWhiteSpace(LibPath)) { host.AddLibPaths(HostEnvironment.SplitPaths(LibPath)); } IEnumerable <IAssembly> assemblies = host.LoadAssemblies(HostEnvironment.SplitPaths(Assembly)); if (!assemblies.Any()) { Log.LogError("ERROR: Failed to load any assemblies from '{0}'", Assembly); return(false); } string headerText = GetHeaderText(HeaderFile, WriterType, SyntaxWriterType); bool loopPerAssembly = Directory.Exists(OutputPath); if (loopPerAssembly) { foreach (IAssembly assembly in assemblies) { using (TextWriter output = GetOutput(GetFilename(assembly, WriterType, SyntaxWriterType))) using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, WriterType, SyntaxWriterType)) { ICciWriter writer = null; try { if (headerText != null) { output.Write(headerText); } bool includeInternals = RespectInternals && assembly.Attributes.HasAttributeOfType(InternalsVisibleTypeName); writer = GetWriter(output, syntaxWriter, includeInternals); writer.WriteAssemblies(new IAssembly[] { assembly }); } finally { if (writer is CSharpWriter csWriter) { csWriter.Dispose(); } } } } } else { using (TextWriter output = GetOutput(OutputPath)) using (IStyleSyntaxWriter syntaxWriter = GetSyntaxWriter(output, WriterType, SyntaxWriterType)) { ICciWriter writer = null; try { if (headerText != null) { output.Write(headerText); } bool includeInternals = RespectInternals && assemblies.Any(assembly => assembly.Attributes.HasAttributeOfType(InternalsVisibleTypeName)); writer = GetWriter(output, syntaxWriter, includeInternals); writer.WriteAssemblies(assemblies); } finally { if (writer is CSharpWriter csWriter) { csWriter.Dispose(); } } } } return(!Log.HasLoggedErrors); }
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)); }
public static IDisposable StartStyle(this IStyleSyntaxWriter writer, SyntaxStyle style) { return(writer.StartStyle(style, null)); }
public DiffCSharpWriter(IStyleSyntaxWriter writer, MappingSettings settings) : this(writer, settings, null, false) { }
public DiffCSharpWriter(IStyleSyntaxWriter writer, MappingSettings settings, IEnumerable <DiffComment> diffComments) : this(writer, settings, diffComments, includePseudoCustomAttributes : false) { }