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

Added capability to show obsolete members in diff with message #171

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
51 changes: 41 additions & 10 deletions Data/ApiDiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class NamespaceDiffInfo : DiffInfo
{
public string Namespace;
public List<TypeDiffInfo> Types = new List<TypeDiffInfo> ();
public int NumAdditions => Types.Sum(x => x.NumAdditions);
public int NumRemovals => Types.Sum(x => x.NumRemovals);
public int NumAdditions => Types.Sum (x => x.NumAdditions);
public int NumRemovals => Types.Sum (x => x.NumRemovals);
public int NumObsolete => Types.Sum (x => x.NumObsolete);
}

public class TypeDiffInfo : DiffInfo
Expand All @@ -43,18 +44,37 @@ public class TypeDiffInfo : DiffInfo
public int NumAdditions {
get {
switch (Action) {
case ListDiffActionType.Remove: return 0;
case ListDiffActionType.Update: return Members.Sum(x => x.NumAdditions);
default: return 1 + Members.Sum(x => x.NumAdditions);
case ListDiffActionType.Remove:
return 0;
case ListDiffActionType.Update:
return Members.Sum (x => x.NumAdditions);
default:
return 1 + Members.Sum (x => x.NumAdditions);
}
}
}
public int NumRemovals {
get {
switch (Action) {
case ListDiffActionType.Remove: return 1;
case ListDiffActionType.Update: return Members.Sum(x => x.NumRemovals);
default: return 0;
case ListDiffActionType.Remove:
return 1;
case ListDiffActionType.Update:
return Members.Sum (x => x.NumRemovals);
default:
return 0;
}
}
}

public int NumObsolete {
get {
switch (Action) {
case ListDiffActionType.Remove:
return 0;
case ListDiffActionType.Update:
return Members.Sum (x => x.NumObsolete);
default:
return 0;
}
}
}
Expand All @@ -65,6 +85,8 @@ public class MemberDiffInfo : DiffInfo
public IMemberDefinition Member;
public int NumAdditions => Action == ListDiffActionType.Add ? 1 : 0;
public int NumRemovals => Action == ListDiffActionType.Remove ? 1 : 0;
public int NumObsolete;
public string ObsoleteMessage;
}

public ApiDiff (PackageData package, PackageTargetFramework framework, PackageData otherPackage, PackageTargetFramework otherFramework)
Expand Down Expand Up @@ -128,6 +150,8 @@ public ApiDiff (PackageData package, PackageTargetFramework framework, PackageDa
break;
}



if (ta.ActionType == ListDiffActionType.Remove) {
types.Add (ti);
continue;
Expand All @@ -146,6 +170,13 @@ public ApiDiff (PackageData package, PackageTargetFramework framework, PackageDa
break;
default:
mi.Member = ma.DestinationItem;

var (isObsolete, obsoleteMessage) = mi.Member.GetObsoleteMessage ();
if (isObsolete) {
mi.NumObsolete++;
mi.ObsoleteMessage = obsoleteMessage;
ti.Members.Add (mi);
}
break;
}
}
Expand Down Expand Up @@ -175,14 +206,14 @@ public static async Task<ApiDiff> GetAsync (
var otherVersion = versions.GetVersion (inputOtherVersion);
var framework = (inputFramework ?? "").ToString ().ToLowerInvariant ().Trim ();

return await cache.GetAsync(
return await cache.GetAsync (
Tuple.Create (versions.LowerId, version.ShortVersionString, framework),
otherVersion.ShortVersionString,
httpClient,
token)
.ConfigureAwait (false);
}

class ApiDiffCache : DataCache<Tuple<string, string, string>, string, ApiDiff>
{
public ApiDiffCache () : base (TimeSpan.FromDays (1)) { }
Expand Down
22 changes: 22 additions & 0 deletions Data/AttributeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Linq;
using Mono.Cecil;

namespace FuGetGallery
{
internal static class AttributeExtensions
{
internal static (bool, string) GetObsoleteMessage (this ICustomAttributeProvider attributeProvider)
{
if (!attributeProvider.HasCustomAttributes)
return (false, null);

var obsoleteAttrribute =
attributeProvider.CustomAttributes.FirstOrDefault (ca =>
ca.AttributeType.FullName == "System.ObsoleteAttribute");

return obsoleteAttrribute != null
? (true, obsoleteAttrribute.ConstructorArguments.FirstOrDefault ().Value?.ToString ())
: (false, null);
}
}
}
2 changes: 1 addition & 1 deletion Data/CecilExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static string GetXmlName (this IMemberDefinition member)
return Mono.Cecil.Rocks.DocCommentId.GetDocCommentId (member);
}

static void WriteEncoded (string s, TextWriter w)
public static void WriteEncoded (string s, TextWriter w)
{
for (var i = 0; i < s.Length; i++) {
switch (s[i]) {
Expand Down
12 changes: 10 additions & 2 deletions Data/TypeDocumentation.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
Expand All @@ -13,7 +12,6 @@
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.CSharp.OutputVisitor;
using System.Xml.Linq;
using System.Text;
using ICSharpCode.Decompiler.TypeSystem.Implementation;
Expand Down Expand Up @@ -86,10 +84,20 @@ void WriteDocumentation (TextWriter w)
var xmlName = m.GetXmlName ();
MemberXmlDocs mdocs = null;
xmlDocs?.GetLanguage(languageCode).MemberDocs.TryGetValue (xmlName, out mdocs);
var (isObsolete, obsoleteMessage) = m.GetObsoleteMessage ();
if (isObsolete && obsoleteMessage == null) {
obsoleteMessage = m.Name + " is obsolete";
}

w.WriteLine ("<div class='member-code'>");
m.WritePrototypeHtml (w, framework: framework, mdocs, linkToCode: true, isExtensionClass);
w.WriteLine ("</div>");

if (isObsolete) {
w.Write ("<div class='member-obsolete'><b>Obsolete:</b> ");
CecilExtensions.WriteEncoded (obsoleteMessage, w);
w.Write ("</div>");
}

w.WriteLine ("<p>");
if (mdocs != null) {
Expand Down
15 changes: 12 additions & 3 deletions Pages/packages/details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,15 @@
<p>@diff.Error</p>
}
<p>
<span class="diff-Add">
<span class="diff-Add" style="margin-right:0.5em;">
<strong>@diff.Namespaces.Sum(ni => ni.NumAdditions)</strong> Additions
</span>
<span class="diff-Remove" style="margin:0.5em;">
<span class="diff-Remove"style="margin-right:0.5em;">
<strong>@diff.Namespaces.Sum(ni => ni.NumRemovals)</strong> Removals
</span>
<span class="diff-Obsolete">
<strong>@diff.Namespaces.Sum(ni => ni.NumObsolete)</strong> Obsolete
</span>
</p>
@foreach (var ni in diff.Namespaces)
{
Expand All @@ -362,7 +365,13 @@
<ul class="diff-Members">
@foreach (var mi in ti.Members)
{
<li class="[email protected] diff-Member"><span class="inline-code">@Html.Raw(mi.Member.GetPrototypeHtml(framework: ti.Framework, null, linkToCode: false, isExtensionClass))</span></li>
<li class="[email protected] diff-Member @(mi.NumObsolete > 0 ? "diff-Obsolete" : "")">
<span class="inline-code">@Html.Raw(mi.Member.GetPrototypeHtml(framework: ti.Framework, null, linkToCode: false, isExtensionClass))</span>
@if (!string.IsNullOrWhiteSpace(mi.ObsoleteMessage))
{
<p style="margin-top: 1em; margin-bottom: 0em;">@mi.ObsoleteMessage</p>
}
</li>
}
</ul>
</li>
Expand Down
10 changes: 5 additions & 5 deletions Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
try {
new Database ().MigrateAsync ().Wait ();
services.AddScoped<Database, Database> ();
try {
new Database ().MigrateAsync ().Wait ();
services.AddScoped<Database, Database> ();
}
catch (Exception dbex) {
Console.WriteLine ($"Failed to open the database: {dbex}");
catch (Exception dbex) {
Console.WriteLine ($"Failed to open the database: {dbex}");
}
services.AddControllers();
services.AddRazorPages(options =>
Expand Down
Loading