Skip to content

Commit

Permalink
case-insensitivity for task names
Browse files Browse the repository at this point in the history
  • Loading branch information
refractalize committed Mar 25, 2013
1 parent 4784ce5 commit 68d4f60
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 403 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -15,3 +15,5 @@ Downloads/
/TestResult.xml
_ReShaper.*/
*.nupkg
_TeamCity.*/
packages/
10 changes: 6 additions & 4 deletions Bounce.Console.Tests/Bounce.Console.Tests.csproj
Expand Up @@ -45,6 +45,9 @@
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="FS">
<HintPath>..\packages\FS.0.0.3\lib\net40\FS.dll</HintPath>
</Reference>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\ZipSharp\ICSharpCode.SharpZipLib.dll</HintPath>
Expand Down Expand Up @@ -92,10 +95,9 @@
<Project>{E7E05DAC-9A59-41F5-9FED-47FDF809A699}</Project>
<Name>Bounce.TestHelpers</Name>
</ProjectReference>
<ProjectReference Include="..\LegacyBounce.Framework\LegacyBounce.Framework.csproj">
<Project>{198788B4-CB73-4C5A-8E83-191ED52C4BD9}</Project>
<Name>LegacyBounce.Framework</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Expand Up @@ -2,7 +2,6 @@
using System.IO;
using Bounce.Framework;
using Bounce.TestHelpers;
using LegacyBounce.Framework;
using NUnit.Framework;
using ProcessOutput = Bounce.Framework.ProcessOutput;

Expand All @@ -11,7 +10,7 @@ namespace Bounce.Console.Tests {
public class RunsBatchFileBeforeRunningTargetsFeature {
private void UnzipSolution() {
FileSystemTestHelper.RecreateDirectory(@"BeforeBounceFeature");
new DirectoryUtils().CopyDirectory(@"..\..\BeforeBounceFeature", @"BeforeBounceFeature", new string [0], new string [0]);
new FS.FileSystem().Copy(@"..\..\BeforeBounceFeature", @"BeforeBounceFeature");
}

[Test]
Expand Down
4 changes: 4 additions & 0 deletions Bounce.Console.Tests/packages.config
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FS" version="0.0.3" targetFramework="net40" />
</packages>
2 changes: 2 additions & 0 deletions Bounce.Framework.Tests/Bounce.Framework.Tests.csproj
Expand Up @@ -82,11 +82,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MockTask.cs" />
<Compile Include="SimpleDependencyResolver.cs" />
<Compile Include="TaskMethodTest.cs" />
<Compile Include="ParameterParserTest.cs" />
<Compile Include="ParametersTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TaskRunnerTests.cs" />
<Compile Include="TasksForTaskNames.cs" />
<Compile Include="VisualStudio\ProjectFilePropertyExpressionParserTest.cs" />
<Compile Include="Web\WebSiteBindingParserTest.cs" />
Expand Down
16 changes: 16 additions & 0 deletions Bounce.Framework.Tests/MockTask.cs
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;

namespace Bounce.Framework.Tests {
public class MockTask : ITask {
public string FullName { get; set; }
public IEnumerable<ITaskParameter> Parameters { get; set; }
public TaskParameters WasInvokedWithTaskParameters { get; private set; }
public bool WasInvoked { get; private set; }

public void Invoke(TaskParameters taskParameters) {
WasInvoked = true;
WasInvokedWithTaskParameters = taskParameters;
}
}
}
11 changes: 0 additions & 11 deletions Bounce.Framework.Tests/TaskMethodTest.cs
Expand Up @@ -144,16 +144,5 @@ public class BadException : Exception {}
public class CustomType
{
}

[Test]
public void EnumeratesTaskNamesForMethodInfo() {
var taskNames = new TaskMethod(typeof(TasksForTaskNames).GetMethod("DoStuff"), Resolver).AllNames.ToArray();

Assert.That(taskNames, Has.Member("DoStuff"));
Assert.That(taskNames, Has.Member("TasksForTaskNames.DoStuff"));
Assert.That(taskNames, Has.Member("Tests.TasksForTaskNames.DoStuff"));
Assert.That(taskNames, Has.Member("Framework.Tests.TasksForTaskNames.DoStuff"));
Assert.That(taskNames, Has.Member("Bounce.Framework.Tests.TasksForTaskNames.DoStuff"));
}
}
}
61 changes: 61 additions & 0 deletions Bounce.Framework.Tests/TaskRunnerTests.cs
@@ -0,0 +1,61 @@
using System.Collections.Generic;
using NUnit.Framework;

namespace Bounce.Framework.Tests {
[TestFixture]
public class TaskRunnerTests {
[Test]
public void InvokesTaskByWholeName() {
var task = new MockTask {FullName = "asdf"};
var runner = new TaskRunner();
var taskParameters = new TaskParameters(new Dictionary<string, string>());
runner.RunTask("asdf", taskParameters, new [] {task});

Assert.That(task.WasInvoked, Is.True);
Assert.That(task.WasInvokedWithTaskParameters, Is.SameAs(taskParameters));
}

[Test]
public void CanInvokeTaskByPartialNames() {
var task = new MockTask {FullName = "The.Full.Path.To.Task"};
var runner = new TaskRunner();

AssertTaskIsInvokedWithName("The.Full.Path.To.Task", runner, task);
AssertTaskIsInvokedWithName("Full.Path.To.Task", runner, task);
AssertTaskIsInvokedWithName("Path.To.Task", runner, task);
AssertTaskIsInvokedWithName("To.Task", runner, task);
AssertTaskIsInvokedWithName("Task", runner, task);
}

[Test]
public void TaskCanBeInvokedWithNameInsensitiveOfCase() {
var task = new MockTask {FullName = "The.Full.Path.To.Task"};
var runner = new TaskRunner();

AssertTaskIsInvokedWithName("the.full.path.to.task", runner, task);
AssertTaskIsInvokedWithName("task", runner, task);
}

[Test]
public void CannotBeInvokedTaskByPartialNameWithoutSpecificName() {
var task = new MockTask {FullName = "The.Full.Path.To.Task"};
var runner = new TaskRunner();

AssertTaskIsNotInvokedWithName("The.Full.Path.To", runner, task);
AssertTaskIsNotInvokedWithName("Full.Path.To", runner, task);
AssertTaskIsNotInvokedWithName("Path.To", runner, task);
AssertTaskIsNotInvokedWithName("To", runner, task);
}

private static void AssertTaskIsInvokedWithName(string taskName, TaskRunner runner, MockTask task) {
runner.RunTask(taskName, new TaskParameters(new Dictionary<string, string>()), new[] {task});
Assert.That(task.WasInvoked, Is.True);
}

private static void AssertTaskIsNotInvokedWithName(string taskName, TaskRunner runner, MockTask task) {
Assert.That(
() => runner.RunTask(taskName, new TaskParameters(new Dictionary<string, string>()), new[] {task}),
Throws.InstanceOf<NoMatchingTaskException>());
}
}
}
2 changes: 1 addition & 1 deletion Bounce.Framework.nuspec
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Bounce</id>
<version>0.8.2</version>
<version>0.9.0</version>
<authors>Tim MacFarlane and contributors</authors>
<language>en-GB</language>
<owners>Tim MacFarlane and contributors</owners>
Expand Down
2 changes: 2 additions & 0 deletions Bounce.Framework/Bounce.Framework.csproj
Expand Up @@ -93,6 +93,7 @@
<Compile Include="CommandExecutionException.cs" />
<Compile Include="ConsoleShellLogger.cs" />
<Compile Include="IShellLogger.cs" />
<Compile Include="ITaskRunner.cs" />
<Compile Include="MultiShellLogger.cs" />
<Compile Include="StringShellLogger.cs" />
<Compile Include="DefaultLogFormatter.cs" />
Expand All @@ -102,6 +103,7 @@
<Compile Include="IDependencyResolver.cs" />
<Compile Include="Parameters.cs" />
<Compile Include="TaskException.cs" />
<Compile Include="TaskRunner.cs" />
<Compile Include="TeamCity\MsBuildLogger.cs" />
<Compile Include="TeamCity\TeamCityArtifactPublisher.cs" />
<Compile Include="TeamCity\TeamCityFormatter.cs" />
Expand Down
25 changes: 10 additions & 15 deletions Bounce.Framework/BounceRunner.cs
Expand Up @@ -7,6 +7,15 @@

namespace Bounce.Framework {
public class BounceRunner {
private readonly ITaskRunner TaskRunner;

public BounceRunner() : this(new TaskRunner()) {
}

public BounceRunner(ITaskRunner taskRunner) {
TaskRunner = taskRunner;
}

public int Run(string bounceDirectory, string [] rawArguments) {
try {
Directory.SetCurrentDirectory(Path.GetDirectoryName(bounceDirectory));
Expand All @@ -17,7 +26,7 @@ public class BounceRunner {
var tasks = Tasks(bounceDirectory, dependencyResolver);

if (rawArguments.Length > 0) {
RunTask(TaskName(rawArguments), parameters, tasks);
TaskRunner.RunTask(TaskName(rawArguments), parameters, tasks);
} else {
UsageHelp.WriteUsage(Console.Out, tasks);
}
Expand Down Expand Up @@ -46,20 +55,6 @@ public class BounceRunner {
return new TaskParameters(new ArgumentsParser().ParseParameters(arguments.Skip(1)));
}

private void RunTask(string taskName, TaskParameters taskParameters, IEnumerable<ITask> tasks) {
var matchingTasks = tasks.Where(t => t.AllNames.Contains(taskName));

if (matchingTasks.Count() > 1) {
throw new AmbiguousTaskNameException(taskName, matchingTasks.OfType<ITask>());
}
else if (!matchingTasks.Any())
{
throw new NoMatchingTaskException(taskName, tasks);
} else {
matchingTasks.Single().Invoke(taskParameters);
}
}

private IEnumerable<ITask> Tasks(string bounceDirectory, AttributedDependencyResolvers dependencyResolvers) {
IEnumerable<Type> allTypes = Directory.GetFiles(bounceDirectory).Where(IsBounceExecutable).SelectMany(file => {
var assembly = Assembly.LoadFrom(file);
Expand Down
2 changes: 0 additions & 2 deletions Bounce.Framework/ITask.cs
Expand Up @@ -3,9 +3,7 @@

namespace Bounce.Framework {
public interface ITask {
string Name { get; }
string FullName { get; }
IEnumerable<string> AllNames { get; }
IEnumerable<ITaskParameter> Parameters { get; }
void Invoke(TaskParameters taskParameters);
}
Expand Down
7 changes: 7 additions & 0 deletions Bounce.Framework/ITaskRunner.cs
@@ -0,0 +1,7 @@
using System.Collections.Generic;

namespace Bounce.Framework {
public interface ITaskRunner {
void RunTask(string taskName, TaskParameters taskParameters, IEnumerable<ITask> tasks);
}
}
15 changes: 0 additions & 15 deletions Bounce.Framework/TaskMethod.cs
Expand Up @@ -49,21 +49,6 @@ private object ParseParameter(TaskParameters taskParameters, ITaskParameter p)
}
}

public IEnumerable<string> AllNames {
get {
var fullName = FullName;

yield return fullName;
int index = fullName.IndexOf(".");
while (index > 0)
{
fullName = fullName.Substring(index + 1);
yield return fullName;
index = fullName.IndexOf(".");
}
}
}

public IEnumerable<ITaskParameter> Parameters {
get { return Method.GetParameters().Select(p => (ITaskParameter) new TaskMethodParameter(p)); }
}
Expand Down
32 changes: 32 additions & 0 deletions Bounce.Framework/TaskRunner.cs
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;

namespace Bounce.Framework {
public class TaskRunner : ITaskRunner {
public void RunTask(string taskName, TaskParameters taskParameters, IEnumerable<ITask> tasks) {
var matchingTasks = tasks.Where(t => AllTaskNames(t).Contains(taskName.ToLower())).ToArray();

if (matchingTasks.Count() > 1) {
throw new AmbiguousTaskNameException(taskName, matchingTasks);
} else if (!matchingTasks.Any()) {
throw new NoMatchingTaskException(taskName, tasks);
} else {
matchingTasks.Single().Invoke(taskParameters);
}
}

public IEnumerable<string> AllTaskNames(ITask task)
{
var fullName = task.FullName.ToLower();

yield return fullName;
int index = fullName.IndexOf(".");
while (index > 0)
{
fullName = fullName.Substring(index + 1);
yield return fullName;
index = fullName.IndexOf(".");
}
}
}
}
58 changes: 0 additions & 58 deletions Build/Build.cs

This file was deleted.

5 changes: 0 additions & 5 deletions Build/Build.csproj
Expand Up @@ -49,7 +49,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Build.cs" />
<Compile Include="NewBuild.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand All @@ -58,10 +57,6 @@
<Project>{05E5A4A4-E497-4FDF-B843-8EE583F5D71E}</Project>
<Name>Bounce.Framework</Name>
</ProjectReference>
<ProjectReference Include="..\LegacyBounce.Framework\LegacyBounce.Framework.csproj">
<Project>{198788B4-CB73-4C5A-8E83-191ED52C4BD9}</Project>
<Name>LegacyBounce.Framework</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down

0 comments on commit 68d4f60

Please sign in to comment.