Skip to content

Commit

Permalink
praeclarum#167 - Added capability to show obsolete members with message
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmcroft committed Jun 5, 2022
1 parent 1fc7615 commit d1e7641
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
61 changes: 52 additions & 9 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, message) = ValidateObsolete (mi.Member);
if (isObsolete) {
mi.NumObsolete++;
mi.ObsoleteMessage = message;
ti.Members.Add (mi);
}
break;
}
}
Expand Down Expand Up @@ -175,14 +206,26 @@ 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);
}

private static (bool, string) ValidateObsolete (ICustomAttributeProvider member)
{
if (!member.HasCustomAttributes)
return (false, null);

foreach (var ca in member.CustomAttributes.Where (ca => ca.AttributeType.FullName == "System.ObsoleteAttribute")) {
return (true, ca.ConstructorArguments.First ().Value.ToString ());
}

return (false, null);
}

class ApiDiffCache : DataCache<Tuple<string, string, string>, string, ApiDiff>
{
public ApiDiffCache () : base (TimeSpan.FromDays (1)) { }
Expand Down
11 changes: 10 additions & 1 deletion Pages/packages/details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@
<span class="diff-Remove" style="margin:0.5em;">
<strong>@diff.Namespaces.Sum(ni => ni.NumRemovals)</strong> Removals
</span>
<span class="diff-Update" style="margin:0.5em;">
<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">
<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

0 comments on commit d1e7641

Please sign in to comment.