Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH2904: Frosting cake.config&tool config support #3059

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Cake.Core/Configuration/CakeConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Cake.Core.Configuration.Parser;
using Cake.Core.IO;

Expand Down Expand Up @@ -43,18 +45,40 @@ public CakeConfigurationProvider(IFileSystem fileSystem, ICakeEnvironment enviro
/// <param name="arguments">The arguments.</param>
/// <returns>The created configuration.</returns>
public ICakeConfiguration CreateConfiguration(DirectoryPath path, IDictionary<string, string> arguments)
=> CreateConfiguration(path, Enumerable.Empty<KeyValuePair<string, string>>(), arguments);

/// <summary>
/// Creates a configuration from the provided arguments.
/// </summary>
/// <param name="path">The directory to look for the configuration file.</param>
/// <param name="baseConfiguration">The initial base configuration.</param>
/// <param name="arguments">The arguments.</param>
/// <returns>The created configuration.</returns>
public ICakeConfiguration CreateConfiguration(DirectoryPath path, IEnumerable<KeyValuePair<string, string>> baseConfiguration, IDictionary<string, string> arguments)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}

if (baseConfiguration == null)
{
throw new ArgumentNullException(nameof(baseConfiguration));
}

if (arguments == null)
{
throw new ArgumentNullException(nameof(arguments));
}

var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

// Add base configuration.
foreach (var kv in baseConfiguration)
{
result[KeyNormalizer.Normalize(kv.Key)] = kv.Value;
}

// Get all environment variables.
foreach (var variable in _environment.GetEnvironmentVariables())
{
Expand Down
11 changes: 11 additions & 0 deletions src/Cake.Frosting/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,16 @@ public static IServiceCollection UseCakeSetting(this IServiceCollection services
services.AddSingleton(new FrostingConfigurationValue(key, value));
return services;
}

/// <summary>
/// Sets the tool path configuration to the specified value.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="toolPath">The tool path value.</param>
/// <returns>The same <see cref="IServiceCollection"/> instance so that multiple calls can be chained.</returns>
public static IServiceCollection UseToolPath(this IServiceCollection services, DirectoryPath toolPath)
{
return services.UseCakeSetting("paths_tools", toolPath.FullPath);
}
}
}
1 change: 1 addition & 0 deletions src/Cake.Frosting/Internal/Commands/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public override int Execute(CommandContext context, DefaultCommandSettings setti
// Register arguments
var arguments = new CakeArguments(context.Remaining.Parsed);
_services.AddSingleton<ICakeArguments>(arguments);
_services.AddSingleton(context.Remaining);

var provider = _services.BuildServiceProvider();

Expand Down
33 changes: 24 additions & 9 deletions src/Cake.Frosting/Internal/FrostingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,47 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Cake.Core;
using Cake.Core.Configuration;
using Cake.Core.IO;
using Spectre.Console.Cli;

namespace Cake.Frosting.Internal
{
internal sealed class FrostingConfiguration : ICakeConfiguration
{
private readonly Dictionary<string, string> _lookup;
private readonly ICakeConfiguration _cakeConfiguration;

public FrostingConfiguration(IEnumerable<FrostingConfigurationValue> values)
public FrostingConfiguration(IEnumerable<FrostingConfigurationValue> values, IFileSystem fileSystem, ICakeEnvironment environment, IRemainingArguments remainingArguments)
{
if (values is null)
{
throw new ArgumentNullException(nameof(values));
}

_lookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
if (fileSystem is null)
{
throw new ArgumentNullException(nameof(fileSystem));
}

if (environment is null)
{
throw new ArgumentNullException(nameof(environment));
}

var baseConfiguration = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var value in values)
{
_lookup[value.Key] = value.Value;
baseConfiguration[value.Key] = value.Value;
}
}

public string GetValue(string key)
{
_lookup.TryGetValue(key, out var value);
return value;
var provider = new CakeConfigurationProvider(fileSystem, environment);
var args = remainingArguments.Parsed.ToDictionary(x => x.Key, x => x.FirstOrDefault() ?? string.Empty);

_cakeConfiguration = provider.CreateConfiguration(environment.WorkingDirectory, baseConfiguration, args);
}

public string GetValue(string key) => _cakeConfiguration.GetValue(key);
}
}
2 changes: 1 addition & 1 deletion tests/integration/Cake.Frosting/build/Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackAsTool>true</PackAsTool>

<!-- Make sure start same folder .NET Core CLI and Visual Studio -->
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
<RunWorkingDirectory>$(MSBuildProjectDirectory)\..</RunWorkingDirectory>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tests/integration/Cake.Frosting/build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void Configure(IServiceCollection services)
{
services.UseContext<Context>();
services.UseLifetime<Lifetime>();
services.UseTool(new System.Uri("nuget:?package=xunit.runner.console"));
services.UseWorkingDirectory("..");
}
}
2 changes: 2 additions & 0 deletions tests/integration/Cake.Frosting/cake.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Paths]
Tools=../tools/frostingtools