-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nuke temp file command line argument provider.
Change ParameterService to singleton. Add get positional argument to parameter service. Add IEnumerable.SingleOrDefaultOrError. Update FlluentAssertions.
- Loading branch information
Showing
9 changed files
with
208 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright Matthias Koch, Sebastian Karasek 2018. | ||
// Distributed under the MIT License. | ||
// https://github.com/nuke-build/nuke/blob/master/LICENSE | ||
|
||
using System; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using Nuke.Core.Utilities.Collections; | ||
using Xunit; | ||
|
||
namespace Nuke.Core.Tests | ||
{ | ||
public class EnumerableExtensionsTest | ||
{ | ||
[Fact] | ||
public void SingleOrDefaultOrError_ThrowsExceptionWithMessage() | ||
{ | ||
var x = new[] { "a", "a" }; | ||
Action a = () => x.SingleOrDefaultOrError("error"); | ||
a.Should().Throw<InvalidOperationException>().WithMessage("error"); | ||
} | ||
|
||
[Theory] | ||
[InlineData(new[] { "a", "b", "c" }, "a", "a")] | ||
[InlineData(new[] { "a", "b", "c" }, "d", null)] | ||
public void SingleOrDefaultOrError(string[] enumerable, string equalsWith, string expectedValue) | ||
{ | ||
enumerable.SingleOrDefaultOrError(x => x.Equals(equalsWith), "error").Should().Be(expectedValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
source/Nuke.Core/Execution/NukeTempfileArgumentProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using JetBrains.Annotations; | ||
using Nuke.Core.Utilities.Collections; | ||
|
||
namespace Nuke.Core.Execution | ||
{ | ||
public static class NukeTempfileArgumentProvider | ||
{ | ||
[CanBeNull] private static string[] s_arguments; | ||
[CanBeNull] private static readonly FileInfo s_tempConfigFile; | ||
|
||
static NukeTempfileArgumentProvider() | ||
{ | ||
try | ||
{ | ||
s_tempConfigFile = new FileInfo(EnvironmentInfo.BuildDirectory / "nuke.tmp"); | ||
} | ||
catch (Exception) | ||
{ | ||
s_tempConfigFile = null; | ||
} | ||
} | ||
|
||
public static bool IsAvailable | ||
{ | ||
get | ||
{ | ||
if (s_tempConfigFile == null || !s_tempConfigFile.Exists) return false; | ||
if (s_tempConfigFile.LastWriteTime.AddMinutes(value: 1) >= DateTime.Now) return true; | ||
|
||
s_tempConfigFile.Delete(); | ||
return false; | ||
} | ||
} | ||
|
||
public static string[] Execute() | ||
{ | ||
if (s_arguments != null) return s_arguments; | ||
|
||
var args = new List<string> { nameof(NukeTempfileArgumentProvider) }; | ||
args.AddRange(ReadAndDeleteTempFile().Split(' ')); | ||
return s_arguments = args.ToArray(); | ||
} | ||
|
||
private static string ReadAndDeleteTempFile() | ||
{ | ||
ControlFlow.Assert(IsAvailable, "nuke.tmp file was deleted"); | ||
|
||
var lines = File.ReadAllLines(s_tempConfigFile.NotNull().FullName); | ||
|
||
s_tempConfigFile.Delete(); | ||
|
||
var configLine = lines.ToList() | ||
.SingleOrDefaultOrError(x => !string.IsNullOrWhiteSpace(x), "nuke.tmp must not contain more than one line."); | ||
ControlFlow.Assert(configLine != null, "No configuration values found"); | ||
return configLine; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
source/Nuke.Core/Utilities/Collections/Enumerable.SingleOrDefaultOrError.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace Nuke.Core.Utilities.Collections | ||
{ | ||
|
||
public static partial class EnumerableExtensions | ||
{ | ||
public static T SingleOrDefaultOrError<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate, string message) | ||
{ | ||
if (enumerable == null) throw new ArgumentNullException(nameof(enumerable)); | ||
if (predicate == null) throw new ArgumentNullException(nameof(predicate)); | ||
if (string.IsNullOrEmpty(message)) throw new ArgumentException("message must not be null or empty", nameof(message)); | ||
|
||
|
||
try | ||
{ | ||
return enumerable.SingleOrDefault(predicate); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
throw new InvalidOperationException(message, ex); | ||
} | ||
} | ||
|
||
public static T SingleOrDefaultOrError<T>(this IEnumerable<T> enumerable, string message) | ||
{ | ||
if (enumerable == null) throw new ArgumentNullException(nameof(enumerable)); | ||
if (string.IsNullOrEmpty(message)) throw new ArgumentException("message must not be null or empty", nameof(message)); | ||
|
||
try | ||
{ | ||
return enumerable.SingleOrDefault(); | ||
} | ||
catch (InvalidOperationException ex) | ||
{ | ||
throw new InvalidOperationException(message, ex); | ||
} | ||
|
||
} | ||
} | ||
} |