Skip to content

Commit

Permalink
cleanup code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Staijen committed Nov 9, 2016
1 parent bcf0a8f commit 8b49592
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 139 deletions.
20 changes: 20 additions & 0 deletions src/Cake.Sonar/ArgumentAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Cake.Sonar
{
[AttributeUsage(AttributeTargets.Property)]
public class ArgumentAttribute : Attribute
{
public string Name { get; set; }

public ArgumentAttribute(string name)
{
Name = name;
}

}
}

6 changes: 5 additions & 1 deletion src/Cake.Sonar/Cake.Sonar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="ArgumentAttribute.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SonarBeginSettings.cs" />
<Compile Include="SonarCake.cs" />
<Compile Include="SonarCakeAliases.cs" />
<Compile Include="SonarEndSettings.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
138 changes: 0 additions & 138 deletions src/Cake.Sonar/Class1.cs

This file was deleted.

34 changes: 34 additions & 0 deletions src/Cake.Sonar/SonarBeginSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Cake.Sonar
{
public class SonarBeginSettings
{
[Argument("/d:sonar.host.url=")]
public string Url { get; set; }

[Argument("/d:sonar.login=")]
public string Login { get; set; }

[Argument("/d:sonar.password=")]
public string Password { get; set; }

[Argument("/k:")]
public string Key { get; set; }

[Argument("/n:")]
public string Name { get; set; }

[Argument("/d:sonar.branch=")]
public string Branch { get; set; }

[Argument("/v:")]
public string Version{ get; set; }

[Argument("/d:sonar.cs.nunit.reportsPaths=")]
public string NUnitReportsPath { get; set; }

[Argument("/d:sonar.cs.dotcover.reportsPaths=")]
public string DotCoverReportsPath { get; set; }

public bool Verbose { get; set; }
}
}
79 changes: 79 additions & 0 deletions src/Cake.Sonar/SonarCake.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Linq;
using System.Reflection;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;

namespace Cake.Sonar
{
public class SonarCake
{
private readonly ICakeContext _context;
private readonly ICakeLog _log;
private readonly IProcessRunner _runner;

public SonarCake(ICakeContext context)
{
_context = context;
_log = _context.Log;
_runner = _context.ProcessRunner;
}

public static string ToolPath = "Tools/MSBuild.SonarQube.Runner.Tool/tools/MSBuild.SonarQube.Runner.exe";

public void Begin(SonarBeginSettings settings)
{
var arguments = new ProcessArgumentBuilder();
arguments.Append("begin");

AppendArguments(settings, arguments);

_log.Information(arguments.Render());

var proces = _runner.Start(ToolPath, new ProcessSettings()
{
Arguments = arguments
});

if (settings.Verbose)
arguments.Append("/d:sonar.verbuse=true");

proces.WaitForExit();
var exitCode = proces.GetExitCode();
_log.Information($"Exitcode {exitCode}");
}

public void End(SonarEndSettings settings)
{
var arguments = new ProcessArgumentBuilder();
arguments.Append("end");

AppendArguments(settings, arguments);

_log.Information(arguments.Render());

var proces = _runner.Start(ToolPath, new ProcessSettings()
{
Arguments = arguments
});

proces.WaitForExit();
var exitCode = proces.GetExitCode();
_log.Information($"Exitcode {exitCode}");
}

public void AppendArguments(object s, ProcessArgumentBuilder builder)
{
foreach (var pi in s.GetType().GetProperties())
{
var attr = pi.GetCustomAttributes<ArgumentAttribute>().FirstOrDefault();
if (attr != null)
{
var value = pi.GetValue(s);
if( value != null )
builder.Append($"{attr.Name}{pi.GetValue(s)}");
}
}
}
}
}
35 changes: 35 additions & 0 deletions src/Cake.Sonar/SonarCakeAliases.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Cake.Core;
using Cake.Core.Annotations;

namespace Cake.Sonar
{
[CakeAliasCategory("Sonar")]
public static class SonarCakeAliases
{
[CakeMethodAlias]
public static void SonarBegin(this ICakeContext context, SonarBeginSettings settings)
{
new SonarCake(context).Begin(settings);
}

[CakeMethodAlias]
public static void SonarEnd(this ICakeContext context, SonarEndSettings settings)
{
new SonarCake(context).End(settings);
}

[CakeMethodAlias]
public static void Sonar(this ICakeContext context, Action<ICakeContext> action, SonarBeginSettings settings)
{
var cake = new SonarCake(context);
cake.Begin(settings);
action(context);
cake.End(new SonarEndSettings()
{
Login = settings.Login,
Password = settings.Password
});
}
}
}
12 changes: 12 additions & 0 deletions src/Cake.Sonar/SonarEndSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Cake.Sonar
{
public class SonarEndSettings
{

[Argument("/d:sonar.login=")]
public string Login { get; set; }

[Argument("/d:sonar.password=")]
public string Password { get; set; }
}
}

0 comments on commit 8b49592

Please sign in to comment.