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

Add simple diagnostic output to InterfaceStubGenerator. #79

Merged
merged 7 commits into from
Dec 8, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
98 changes: 98 additions & 0 deletions InterfaceStubGenerator/Diagnostics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Refit.Generator
{
public class DiagnosticsLogger
{
List<Diagnostic> diagnostics = new List<Diagnostic>();

public IEnumerable<Diagnostic> Diagnostics { get { return diagnostics.AsReadOnly(); } }

public void Add(Diagnostic diagnostic)
{
this.diagnostics.Add(diagnostic);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.

}

public void AddRange(IEnumerable<Diagnostic> collection)
{
this.diagnostics.AddRange(collection);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.

}

public void Dump()
{
var builder = new StringBuilder();

foreach (var diagnostic in diagnostics)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⬆️

builder.Clear();

if (!string.IsNullOrWhiteSpace(diagnostic.File))
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⬆️

builder.Append(diagnostic.File);
if (diagnostic.Line.HasValue)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⬆️

builder.AppendFormat("({0}", diagnostic.Line);
if (diagnostic.Character.HasValue)
builder.AppendFormat(",{0}", diagnostic.Character);
builder.Append(")");
}
builder.Append(": ");
}
builder.AppendFormat("{0} {1}", diagnostic.Type, diagnostic.Code);
if (!string.IsNullOrWhiteSpace(diagnostic.Message))
builder.AppendFormat(": {0}", diagnostic.Message);

Console.Error.WriteLine(builder.ToString());
}
}
}

public class Diagnostic
{
public string Type { get; private set; }
public string Code { get; private set; }
public string File { get; protected set; }
public int? Line { get; protected set; }
public int? Character { get; protected set; }
public string Message { get; protected set; }

public Diagnostic(string type, string code)
{
this.Type = type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this. (etc)

this.Code = code;
}
}

public class Warning : Diagnostic
{
public Warning(string code) : base("warning", code) { }
}

public class MissingRefitAttributeWarning : Warning
{
public string InterfaceName { get; private set; }
public string MethodName { get; private set; }

public MissingRefitAttributeWarning(InterfaceDeclarationSyntax @interface, MethodDeclarationSyntax method)
: base("RF001")
{
var location = method.GetLocation();
var line = location.GetMappedLineSpan().StartLinePosition;

this.File = location.FilePath;
this.Line = line.Line + 1;
this.Character = line.Character + 1;
this.InterfaceName = @interface.Identifier.Text;
this.MethodName = method.Identifier.Text;

this.Message = string.Format(
"Method {0}.{1} either has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument.",
this.InterfaceName, this.MethodName);
}
}
}
12 changes: 12 additions & 0 deletions InterfaceStubGenerator/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public string GenerateInterfaceStubs(string[] paths)

var templateInfo = GenerateTemplateInfoForInterfaceList(interfacesToGenerate);

var logger = new DiagnosticsLogger();
GenerateWarnings(interfacesToGenerate, logger);
logger.Dump();

Encoders.HtmlEncode = (s) => s;
var text = Render.StringToString(ExtractTemplateSource(), templateInfo);
return text;
Expand Down Expand Up @@ -126,6 +130,14 @@ public ClassTemplateInfo GenerateClassInfoForInterface(InterfaceDeclarationSynta
return ret;
}

public void GenerateWarnings(List<InterfaceDeclarationSyntax> interfacesToGenerate, DiagnosticsLogger logger)
{
logger.AddRange(interfacesToGenerate
.SelectMany(i => i.Members.OfType<MethodDeclarationSyntax>().Select(m => new { Interface = i, Method = m }))
.Where(x => !HasRefitHttpMethodAttribute(x.Method))
.Select(x => new MissingRefitAttributeWarning(x.Interface, x.Method)));
}

public static string ExtractTemplateSource()
{
var ourPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Expand Down
3 changes: 2 additions & 1 deletion InterfaceStubGenerator/InterfaceStubGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Diagnostics.cs" />
<Compile Include="InterfaceStubGenerator.cs" />
<Compile Include="Mono.Options\Options.cs" />
<Compile Include="Program.cs" />
Expand All @@ -95,4 +96,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
18 changes: 18 additions & 0 deletions Refit-Tests/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ public void RetainsAliasesInUsings()
CollectionAssert.Contains(usingList, "SomeType = CollisionA.SomeType");
CollectionAssert.Contains(usingList, "CollisionB");
}

[Test]
public void CheckGeneratedWarning()
{
var fixture = new InterfaceStubGenerator();
var file = CSharpSyntaxTree.ParseFile(IntegrationTestHelper.GetPath("InterfaceStubGenerator.cs"));

var input = file.GetRoot().DescendantNodes()
.OfType<InterfaceDeclarationSyntax>()
.First(x => x.Identifier.ValueText == "IAmARefitInterfaceButNobodyUsesMe");
var logger = new DiagnosticsLogger();

fixture.GenerateWarnings(new List<InterfaceDeclarationSyntax> { input }, logger);
var result = logger.Diagnostics.OfType<MissingRefitAttributeWarning>().Single();

Assert.AreEqual("IAmARefitInterfaceButNobodyUsesMe", result.InterfaceName);
Assert.AreEqual("NoConstantsAllowed", result.MethodName);
}
}

public static class ThisIsDumbButMightHappen
Expand Down