Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a named global namespace #1525

Merged
merged 2 commits into from
Apr 7, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class ExtractMetadataInputModel

public bool UseCompatibilityFileName { get; set; }

public string GlobalNamespaceId { get; set; }

public override string ToString()
{
using(StringWriter writer = new StringWriter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public bool IsMatch(ISymbol symbol)
{
throw new ArgumentNullException("symbol");
}
return (_uidRegex == null || _uidRegex.IsMatch(VisitorHelper.GetId(symbol))) &&
var id = VisitorHelper.GetId(symbol);

return (_uidRegex == null || (id != null && _uidRegex.IsMatch(id))) &&
(Kind == null || Kind.Value.Contains(symbol)) &&
(Attribute == null || Attribute.ContainedIn(symbol));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static bool CanVisitCore(ISymbol symbol, Func<ISymbol, bool, IFilterVisi
return true;
}

if (symbol.IsImplicitlyDeclared)
if (!(symbol is INamespaceSymbol) && symbol.IsImplicitlyDeclared)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ public override void VisitNamespace(INamespaceSymbol symbol)
{
if (symbol.IsGlobalNamespace)
{
Append(VisitorHelper.GlobalNamespaceId);
return;
}

if (!symbol.ContainingNamespace.IsGlobalNamespace)
{
symbol.ContainingNamespace.Accept(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,17 @@ public override MetadataItem VisitAssembly(IAssemblySymbol symbol)
item.Type = MemberType.Assembly;
_references = new Dictionary<string, ReferenceItem>();

var typeMembers = symbol.GlobalNamespace.GetTypeMembers().Where(member => member.DeclaredAccessibility == Accessibility.Public);
if (typeMembers.Any())
IEnumerable<INamespaceSymbol> namespaces;
if (!string.IsNullOrEmpty(VisitorHelper.GlobalNamespaceId))
{
Logger.LogWarning($"DocFX currently only supports generating metadata with namespace defined. The following types in assembly \"{symbol.MetadataName}\" will have no metadata generated: {string.Join(", ", typeMembers.Select(m => m.MetadataName))}. ");
namespaces = Enumerable.Repeat(symbol.GlobalNamespace, 1);
}
else
{
namespaces = symbol.GlobalNamespace.GetNamespaceMembers();
}


var namespaces = symbol.GlobalNamespace.GetNamespaceMembers().ToList();
item.Items = VisitDescendants(
namespaces,
ns => ns.GetMembers().OfType<INamespaceSymbol>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Microsoft.DocAsCode.Metadata.ManagedReference

public static class VisitorHelper
{
public static string GlobalNamespaceId { get; set; }
private static readonly Regex GenericMethodPostFix = new Regex(@"``\d+$", RegexOptions.Compiled);

public static void FeedComments(MetadataItem item, ITripleSlashCommentParserContext context)
Expand All @@ -44,6 +45,12 @@ public static string GetId(ISymbol symbol)
return null;
}

var namespaceSymbol = symbol as INamespaceSymbol;
if (namespaceSymbol != null && namespaceSymbol.IsGlobalNamespace)
{
return GlobalNamespaceId;
}

var assemblySymbol = symbol as IAssemblySymbol;
if (assemblySymbol != null)
{
Expand All @@ -54,13 +61,32 @@ public static string GetId(ISymbol symbol)
{
return typeof(object).FullName;
}

return GetDocumentationCommentId(symbol)?.Substring(2);
}

private static string GetDocumentationCommentId(ISymbol symbol)
{
string str = symbol.GetDocumentationCommentId();
if (string.IsNullOrEmpty(str))
{
return null;
}

return str.Substring(2);
bool inGlobalNamespace =
symbol.ContainingNamespace == null ||
symbol.ContainingNamespace.IsGlobalNamespace;

if (inGlobalNamespace && !string.IsNullOrEmpty(GlobalNamespaceId))
{
bool isNamespace = (symbol is INamespaceSymbol);
bool isTypeParameter = (symbol is ITypeParameterSymbol);
if (!isNamespace && !isTypeParameter)
{
str = str.Insert(2, GlobalNamespaceId + ".");
}
}
return str;
}

public static string GetCommentId(ISymbol symbol)
Expand All @@ -74,7 +100,8 @@ public static string GetCommentId(ISymbol symbol)
{
return "T:" + typeof(object).FullName;
}
return symbol.GetDocumentationCommentId();

return GetDocumentationCommentId(symbol);
}

public static string GetOverloadId(ISymbol symbol)
Expand Down
3 changes: 3 additions & 0 deletions src/docfx/Models/MetadataCommandOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ internal class MetadataCommandOptions : ICanPrintHelpMessage, ILoggable

[Option("filter", HelpText = "Specify the filter config file")]
public string FilterConfigFile { get; set; }

[Option("globalNamespaceId", HelpText = "Specify the name to use for the global namespace")]
public string GlobalNamespaceId { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/docfx/Models/MetadataJsonItemConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class MetadataJsonItemConfig
[JsonProperty("filter")]
public string FilterConfigFile { get; set; }

[JsonProperty("globalNamespaceId")]
public string GlobalNamespaceId { get; set; }

[JsonProperty("useCompatibilityFileName")]
public bool? UseCompatibilityFileName { get; set; }
}
Expand Down
49 changes: 31 additions & 18 deletions src/docfx/SubCommands/MetadataCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ public MetadataCommand(MetadataCommandOptions options)

public void Exec(SubCommandRunningContext context)
{
string originalGlobalNamespaceId = VisitorHelper.GlobalNamespaceId;
EnvironmentContext.SetBaseDirectory(Path.GetFullPath(string.IsNullOrEmpty(Config.BaseDirectory) ? Directory.GetCurrentDirectory() : Config.BaseDirectory));
foreach (var inputModel in InputModels)
{
VisitorHelper.GlobalNamespaceId = inputModel.GlobalNamespaceId;

// TODO: Use plugin to generate metadata for files with different extension?
using (var worker = new ExtractMetadataWorker(inputModel, inputModel.ForceRebuild, inputModel.UseCompatibilityFileName))
{
Expand All @@ -43,40 +46,49 @@ public void Exec(SubCommandRunningContext context)
}
}
EnvironmentContext.Clean();
VisitorHelper.GlobalNamespaceId = originalGlobalNamespaceId;
}

private MetadataJsonConfig ParseOptions(MetadataCommandOptions options)
{
MetadataJsonConfig config;

string configFile;
if (TryGetJsonConfig(options.Projects, out configFile))
{
var config = CommandUtility.GetConfig<MetadataConfig>(configFile).Item;
config = CommandUtility.GetConfig<MetadataConfig>(configFile).Item;
if (config == null) throw new DocumentException($"Unable to find metadata subcommand config in file '{configFile}'.");
config.BaseDirectory = Path.GetDirectoryName(configFile);
config.OutputFolder = options.OutputFolder;
foreach (var item in config)
{
item.Raw |= options.PreserveRawInlineComments;
item.Force |= options.ForceRebuild;
item.ShouldSkipMarkup |= options.ShouldSkipMarkup;
item.FilterConfigFile = string.IsNullOrEmpty(options.FilterConfigFile) ? item.FilterConfigFile : Path.GetFullPath(options.FilterConfigFile);
}
return config;
}
else
{
var config = new MetadataJsonConfig();
config = new MetadataJsonConfig();
config.Add(new MetadataJsonItemConfig
{
Force = options.ForceRebuild,
ShouldSkipMarkup = options.ShouldSkipMarkup,
Destination = options.OutputFolder,
Raw = options.PreserveRawInlineComments,
Source = new FileMapping(new FileMappingItem(options.Projects.ToArray())) { Expanded = true },
FilterConfigFile = string.IsNullOrEmpty(options.FilterConfigFile) ? null : Path.GetFullPath(options.FilterConfigFile)
Source = new FileMapping(new FileMappingItem(options.Projects.ToArray())) { Expanded = true }
});
return config;
}

foreach (var item in config)
{
item.Force |= options.ForceRebuild;
item.Raw |= options.PreserveRawInlineComments;
item.ShouldSkipMarkup |= options.ShouldSkipMarkup;
if (!string.IsNullOrEmpty(options.FilterConfigFile))
{
item.FilterConfigFile = Path.GetFullPath(options.FilterConfigFile);
}

if (!string.IsNullOrEmpty(options.GlobalNamespaceId))
{
item.GlobalNamespaceId = options.GlobalNamespaceId;
}
}

config.OutputFolder = options.OutputFolder;

return config;
}

private IEnumerable<ExtractMetadataInputModel> GetInputModels(MetadataJsonConfig configs)
Expand All @@ -102,7 +114,8 @@ private ExtractMetadataInputModel ConvertToInputModel(MetadataJsonItemConfig con
ShouldSkipMarkup = configModel?.ShouldSkipMarkup ?? false,
ApiFolderName = string.Empty,
FilterConfigFile = configModel?.FilterConfigFile,
UseCompatibilityFileName = configModel?.UseCompatibilityFileName ?? false,
GlobalNamespaceId = configModel?.GlobalNamespaceId,
UseCompatibilityFileName = configModel?.UseCompatibilityFileName ?? false
};

var expandedFileMapping = GlobUtility.ExpandFileMapping(Config.BaseDirectory, projects);
Expand Down