Skip to content

Commit

Permalink
Merge pull request #1202 from patriksvensson/feature/GH1184
Browse files Browse the repository at this point in the history
GH1184: Added support for manually including/excluding files for tools
  • Loading branch information
gep13 authored Sep 15, 2016
2 parents 8418de6 + 9219198 commit 063491a
Show file tree
Hide file tree
Showing 22 changed files with 332 additions and 39 deletions.
15 changes: 15 additions & 0 deletions src/Cake.Core.Tests/Unit/IO/GlobberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,21 @@ public void Should_Parse_Glob_Expressions_With_Parenthesis_In_Them()
Assert.Equal(1, result.Length);
Assert.ContainsFilePath(result, "/Foo (Bar)/Baz.c");
}

[Fact]
public void Should_Parse_Glob_Expressions_With_Relative_Directory_Not_At_The_Beginning()
{
// Given
var fixture = new GlobberFixture();

// When
var result = fixture.Match("/Working/./*.Test.dll");

// Then
Assert.Equal(2, result.Length);
Assert.ContainsFilePath(result, "/Working/Foo.Bar.Test.dll");
Assert.ContainsFilePath(result, "/Working/Bar.Qux.Test.dll");
}
}
}
}
5 changes: 3 additions & 2 deletions src/Cake.Core.Tests/Unit/Packaging/PackageReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Linq;
using Cake.Core.Packaging;
using Xunit;

Expand Down Expand Up @@ -88,8 +89,8 @@ public void Should_Return_Correct_Address(string uri)

// Then
Assert.Equal(2, result.Count);
Assert.Equal("Cake.Foo", result["package"]);
Assert.Equal("1.2.3", result["version"]);
Assert.Equal("Cake.Foo", result["package"].First());
Assert.Equal("1.2.3", result["version"].First());
}
}

Expand Down
34 changes: 22 additions & 12 deletions src/Cake.Core/Extensions/UriExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,46 @@

using System;
using System.Collections.Generic;
using System.Linq;

// ReSharper disable once CheckNamespace
namespace Cake.Core
{
internal static class UriExtensions
{
public static IDictionary<string, string> GetQueryString(this Uri uri)
public static IReadOnlyDictionary<string, IReadOnlyList<string>> GetQueryString(this Uri uri)
{
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var result = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
if (uri != null)
{
var query = uri.Query.TrimStart('?');
var parts = query.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var part in parts)
{
var pair = part.Split(new[] { '=' }, StringSplitOptions.None);
switch (pair.Length)

if (pair.Length == 1)
{
if (!result.ContainsKey(pair[0]))
{
result[pair[0]] = new List<string>();
}
}
else if (pair.Length == 2)
{
if (!result.ContainsKey(pair[0]))
{
result[pair[0]] = new List<string>();
}
result[pair[0]].Add(pair[1]);
}
else
{
case 1:
result[pair[0]] = string.Empty;
break;
case 2:
result[pair[0]] = pair[1];
break;
default:
throw new CakeException("Could not parse query string.");
throw new CakeException("Could not parse query string.");
}
}
}
return result;
return result.ToDictionary(kv => kv.Key, kv => kv.Value as IReadOnlyList<string>);
}
}
}
5 changes: 5 additions & 0 deletions src/Cake.Core/IO/Globbing/GlobParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ private static GlobNode ParseSegment(GlobParserContext context)
context.Accept();
return new ParentSegment();
}
if (context.CurrentToken.Kind == GlobTokenKind.Current)
{
context.Accept();
return new CurrentSegment();
}

var items = new List<GlobToken>();
while (true)
Expand Down
1 change: 1 addition & 0 deletions src/Cake.Core/IO/Globbing/GlobTokenKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal enum GlobTokenKind
Identifier,
WindowsRoot,
Parent,
Current,
EndOfText
}
}
4 changes: 4 additions & 0 deletions src/Cake.Core/IO/Globbing/GlobTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ private GlobTokenKind ScanToken()
TakeCharacter();
return GlobTokenKind.Parent;
}
if (_currentCharacter == '/')
{
return GlobTokenKind.Current;
}
}
while (IsAlphaNumeric(_currentCharacter))
{
Expand Down
5 changes: 5 additions & 0 deletions src/Cake.Core/IO/Globbing/GlobVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ public void VisitParent(ParentSegment node, GlobVisitorContext context)
context.Push(last);
}

public void VisitCurrent(CurrentSegment node, GlobVisitorContext context)
{
node.Next.Accept(this, context);
}

private static IEnumerable<IFileSystemInfo> FindCandidates(
DirectoryPath path,
MatchableNode node,
Expand Down
18 changes: 18 additions & 0 deletions src/Cake.Core/IO/Globbing/Nodes/CurrentSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;

namespace Cake.Core.IO.Globbing.Nodes
{
[DebuggerDisplay(".")]
internal sealed class CurrentSegment : GlobNode
{
[DebuggerStepThrough]
public override void Accept(GlobVisitor visitor, GlobVisitorContext context)
{
visitor.VisitCurrent(this, context);
}
}
}
1 change: 1 addition & 0 deletions src/Cake.Core/IO/Globbing/Nodes/ParentSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Cake.Core.IO.Globbing.Nodes
{
[DebuggerDisplay("..")]
internal sealed class ParentSegment : GlobNode
{
[DebuggerStepThrough]
Expand Down
20 changes: 17 additions & 3 deletions src/Cake.Core/Packaging/PackageReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Cake.Core.Packaging
{
Expand Down Expand Up @@ -35,7 +36,7 @@ public sealed class PackageReference
/// Gets the parameters.
/// </summary>
/// <value>The parameters.</value>
public IReadOnlyDictionary<string, string> Parameters { get; }
public IReadOnlyDictionary<string, IReadOnlyList<string>> Parameters { get; }

/// <summary>
/// Gets the package.
Expand All @@ -61,9 +62,22 @@ internal PackageReference(Uri uri)
{
OriginalString = uri.OriginalString;
Scheme = uri.Scheme;
Parameters = new ReadOnlyDictionary<string, string>(uri.GetQueryString());
Parameters = uri.GetQueryString();

if (Parameters.ContainsKey("package"))
{
if (Parameters["package"].Count == 1)
{
Package = Parameters["package"].FirstOrDefault();
}
else if (Parameters["package"].Count > 1)
{
throw new ArgumentException(
"Query string contains more than one parameter 'package'.",
nameof(uri));
}
}

Package = Parameters.ContainsKey("package") ? Parameters["package"] : null;
if (Package == null)
{
throw new ArgumentException(
Expand Down
6 changes: 5 additions & 1 deletion src/Cake.NuGet.Tests/Fixtures/NuGetContentResolverFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@ internal abstract class NuGetContentResolverFixture
{
public FakeFileSystem FileSystem { get; set; }
public FakeEnvironment Environment { get; set; }
public Globber Globber { get; set; }
public ICakeLog Log { get; set; }

public DirectoryPath Path { get; set; }
public PackageType PackageType { get; set; }
public PackageReference Package { get; set; }

protected NuGetContentResolverFixture(string framework)
{
Environment = FakeEnvironment.CreateUnixEnvironment();
Environment.Runtime.TargetFramework = new FrameworkName(framework);

FileSystem = new FakeFileSystem(Environment);
Globber = new Globber(FileSystem, Environment);
Log = new FakeLog();

Path = "/Working";
PackageType = PackageType.Addin;
Package = new PackageReference("nuget:?package=Foo");
}

public IReadOnlyCollection<IFile> GetFiles()
{
var resolver = GetResolver();
return resolver.GetFiles(Path, PackageType);
return resolver.GetFiles(Path, Package, PackageType);
}

protected abstract INuGetContentResolver GetResolver();
Expand Down
43 changes: 43 additions & 0 deletions src/Cake.NuGet.Tests/Fixtures/NuGetToolContentResolverFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using Cake.Core;
using Cake.Core.IO;
using Cake.Core.Packaging;

namespace Cake.NuGet.Tests.Fixtures
{
internal sealed class NuGetToolContentResolverFixture : NuGetContentResolverFixture
{
// This is a resolver without the addin assemblies method
// which is not needed for resolving tools.
private sealed class NuGetToolContentResolver : NuGetContentResolver
{
public NuGetToolContentResolver(IFileSystem fileSystem, ICakeEnvironment environment, IGlobber globber)
: base(fileSystem, environment, globber)
{
}

protected override IReadOnlyCollection<IFile> GetAddinAssemblies(DirectoryPath path)
{
throw new NotSupportedException("Only tools can be resolved with this resolver.");
}
}

public NuGetToolContentResolverFixture(string uri)
: base(".NETFramework,Version=v4.5")
{
Package = new PackageReference(uri);
PackageType = PackageType.Tool;
Path = new DirectoryPath(string.Concat("/Working/tools/", Package.Package));
}

protected override INuGetContentResolver GetResolver()
{
return new NuGetToolContentResolver(FileSystem, Environment, Globber);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public NuGetV2ContentResolverFixture(string framework = ".NETFramework,Version=v

protected override INuGetContentResolver GetResolver()
{
return new NuGetV2ContentResolver(FileSystem, Environment, Log);
return new NuGetV2ContentResolver(FileSystem, Environment, Globber, Log);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public NuGetV3ContentResolverFixture(string framework)

protected override INuGetContentResolver GetResolver()
{
return new NuGetV3ContentResolver(FileSystem, Environment);
return new NuGetV3ContentResolver(FileSystem, Environment, Globber);
}
}
}
Expand Down
Loading

0 comments on commit 063491a

Please sign in to comment.