Skip to content

Commit

Permalink
Merge pull request #79 from nekresh/generator-diagnostic
Browse files Browse the repository at this point in the history
Add simple diagnostic output to InterfaceStubGenerator.
  • Loading branch information
anaisbetts committed Dec 8, 2014
2 parents 9219181 + d02831b commit 6d78458
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 1 deletion.
99 changes: 99 additions & 0 deletions InterfaceStubGenerator/Diagnostics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;

namespace Refit.Generator
{
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)
{
Type = type;
Code = code;
}

protected void setLocation(Location location)
{
var line = location.GetMappedLineSpan().StartLinePosition;

File = location.FilePath;
Line = line.Line + 1;
Character = line.Character + 1;
}

public override string ToString()
{
var builder = new StringBuilder();

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

return builder.ToString();
}
}

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")
{
setLocation(method.GetLocation());

InterfaceName = @interface.Identifier.Text;
MethodName = method.Identifier.Text;

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.",
InterfaceName, MethodName);
}
}

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

public MultipleRefitMethodSameNameWarning(InterfaceDeclarationSyntax @interface, MethodDeclarationSyntax method)
: base("RF002")
{
setLocation(method.GetLocation());

InterfaceName = @interface.Identifier.Text;
MethodName = method.Identifier.Text;

Message = string.Format(
"Method {0}.{1} has been declared multiple times. Refit doesn't support overloading.",
InterfaceName, MethodName);
}
}
}
23 changes: 23 additions & 0 deletions InterfaceStubGenerator/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public string GenerateInterfaceStubs(string[] paths)

var templateInfo = GenerateTemplateInfoForInterfaceList(interfacesToGenerate);

GenerateWarnings(interfacesToGenerate);

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

public void GenerateWarnings(List<InterfaceDeclarationSyntax> interfacesToGenerate)
{
var missingAttributeWarnings = 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));

var overloadWarnings = interfacesToGenerate
.SelectMany(i => i.Members.OfType<MethodDeclarationSyntax>().Select(m => new {Interface = i, Method = m}))
.Where(x => HasRefitHttpMethodAttribute(x.Method))
.GroupBy(x => new {Interface = x.Interface, MethodName = x.Method.Identifier.Text})
.Where(g => g.Count() > 1)
.SelectMany(g => g.Select(x => new MultipleRefitMethodSameNameWarning(x.Interface, x.Method)));

var diagnostics = missingAttributeWarnings.Concat<Diagnostic>(overloadWarnings);

foreach (var diagnostic in diagnostics) {
Console.Error.WriteLine(diagnostic);
}
}

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>
16 changes: 16 additions & 0 deletions Refit-Tests/MethodOverloads.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Threading.Tasks;
using Refit;

namespace Refit.Tests
{
public interface IUseOverloadedMethods
{
[Get("/")]
Task Get();

[Get("/{id}")]
Task Get(int id);
}
}

1 change: 1 addition & 0 deletions Refit-Tests/Refit-Tests-Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="GitHubApi.cs" />
<Compile Include="IntegrationTestHelper.cs" />
<Compile Include="RefitStubs.cs" />
<Compile Include="MethodOverloads.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
Expand Down
1 change: 1 addition & 0 deletions Refit-Tests/Refit-Tests-Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<Compile Include="RefitStubs.cs" />
<Compile Include="RequestBuilder.cs" />
<Compile Include="RestService.cs" />
<Compile Include="MethodOverloads.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\InterfaceStubGenerator\InterfaceStubGenerator.csproj">
Expand Down
1 change: 1 addition & 0 deletions Refit-Tests/Refit-Tests-iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<Compile Include="RestService.cs" />
<Compile Include="RefitStubs.cs" />
<Compile Include="DeliminatorSeparatedPropertyNamesContractResolver.cs" />
<Compile Include="MethodOverloads.cs" />
</ItemGroup>

<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
Expand Down

0 comments on commit 6d78458

Please sign in to comment.