Skip to content

Commit

Permalink
move
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoch committed Mar 23, 2018
1 parent deede14 commit 539523e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
// 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.Execution;
using Xunit;

namespace Nuke.Core.Tests
{
public class TemporaryFileArgumentsProviderTest
public class EnvironmentInfoTest
{
[Theory]
[InlineData("arg0 arg1 arg2", new[] { "arg0", "arg1", "arg2" })]
Expand All @@ -19,9 +22,9 @@ public class TemporaryFileArgumentsProviderTest
[InlineData("\"arg0 \\\"arg1\\\"\" arg2", new[] { "arg0 \"arg1\"", "arg2" })]
[InlineData("'arg0 \\'arg1\\'' arg2", new[] { "arg0 'arg1'", "arg2" })]
[InlineData("\\\\ \\ \\\\", new[] { "\\\\", "\\", "\\\\" })]
public void TestSplitArguments(string commandLine, string[] expected)
public void TestParseCommandLineArguments(string commandLine, string[] expected)
{
var arguments = TemporaryFileArgumentsProvider.SplitArguments(commandLine);
var arguments = EnvironmentInfo.ParseCommandLineArguments(commandLine);

arguments.Should().BeEquivalentTo(expected);
}
Expand Down
59 changes: 56 additions & 3 deletions source/Nuke.Core/EnvironmentInfo.Others.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Nuke.Core.Execution;
using Nuke.Core.Utilities;
using Nuke.Core.Utilities.Collections;

#if NETCORE
Expand All @@ -26,9 +30,58 @@ public static string WorkingDirectory
=> Environment.CurrentDirectory;
#endif

public static IReadOnlyDictionary<string, string> Variables => Environment.GetEnvironmentVariables().ToGeneric<string, string>(StringComparer.OrdinalIgnoreCase).AsReadOnly();
public static IReadOnlyDictionary<string, string> Variables
=> Environment.GetEnvironmentVariables()
.ToGeneric<string, string>(StringComparer.OrdinalIgnoreCase)
.AsReadOnly();

private static Lazy<string[]> s_commandLineArguments = new Lazy<string[]>(() => GetSurrogateArguments() ?? Environment.GetCommandLineArgs());

public static string[] CommandLineArguments => s_commandLineArguments.Value;

private const string c_nukeTmpFileName = "nuke.tmp";

[CanBeNull]
private static string[] GetSurrogateArguments()
{
if (BuildDirectory == null)
return null;

var argumentsFile = BuildDirectory / c_nukeTmpFileName;
if (!File.Exists(argumentsFile))
return null;

var argumentLines = File.ReadAllLines(argumentsFile);
ControlFlow.Assert(argumentLines.Length == 1, $"{c_nukeTmpFileName} must have only one single line");

File.Delete(argumentsFile);
if (File.GetLastWriteTime(argumentsFile).AddMinutes(value: 1) < DateTime.Now)
return null;

var splittedArguments = ParseCommandLineArguments(argumentLines.Single());
return new[] { Assembly.GetEntryAssembly().Location }.Concat(splittedArguments).ToArray();
}

public static string[] ParseCommandLineArguments(string commandLine)
{
var inSingleQuotes = false;
var inDoubleQuotes = false;
var escaped = false;
return commandLine.Split(x =>
{
if (x == '\"' && !inSingleQuotes && !escaped)
inDoubleQuotes = !inDoubleQuotes;

if (x == '\'' && !inDoubleQuotes && !escaped)
inSingleQuotes = !inSingleQuotes;

escaped = x == '\\' && !escaped;

public static Lazy<string[]> CommandLineArguments = new Lazy<string[]>(() =>
TemporaryFileArgumentsProvider.GetArguments() ?? Environment.GetCommandLineArgs());
return x == ' ' && !(inDoubleQuotes || inSingleQuotes);
})
.Select(x => x.TrimMatchingDoubleQuotes().TrimMatchingQuotes().Replace("\\\"", "\"").Replace("\\\'", "'"))
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
}
}
}
7 changes: 2 additions & 5 deletions source/Nuke.Core/Execution/ParameterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ public ParameterService(
[CanBeNull] string[] commandLineArguments = null,
[CanBeNull] IReadOnlyDictionary<string, string> environmentVariables = null)
{
if (environmentVariables == null)
_environmentVariablesProvider = () => EnvironmentInfo.Variables;
else
_environmentVariablesProvider = () => environmentVariables;
_commandLineArguments = commandLineArguments ?? EnvironmentInfo.CommandLineArguments.Value;
_environmentVariablesProvider = () => environmentVariables ?? EnvironmentInfo.Variables;
_commandLineArguments = commandLineArguments ?? EnvironmentInfo.CommandLineArguments;
}

public static ParameterService Instance => s_instance ?? (s_instance = new ParameterService());
Expand Down
83 changes: 0 additions & 83 deletions source/Nuke.Core/Execution/TemporaryFileArgumentsProvider.cs

This file was deleted.

0 comments on commit 539523e

Please sign in to comment.