Skip to content

Commit

Permalink
Cake Frosting should respect configuration
Browse files Browse the repository at this point in the history
Closes #2904
  • Loading branch information
patriksvensson authored and pascalberger committed Jan 24, 2021
1 parent 4ae42eb commit fc502c2
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 34 deletions.
19 changes: 3 additions & 16 deletions src/Cake.Core/Tooling/ToolLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,9 @@ public sealed class ToolLocator : IToolLocator
/// <param name="strategy">The tool resolution strategy.</param>
public ToolLocator(ICakeEnvironment environment, IToolRepository repository, IToolResolutionStrategy strategy)
{
if (environment == null)
{
throw new ArgumentNullException(nameof(environment));
}
if (repository == null)
{
throw new ArgumentNullException(nameof(repository));
}
if (strategy == null)
{
throw new ArgumentNullException(nameof(strategy));
}

_environment = environment;
_repository = repository;
_strategy = strategy;
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
_strategy = strategy ?? throw new ArgumentNullException(nameof(strategy));
}

/// <inheritdoc/>
Expand Down
12 changes: 6 additions & 6 deletions src/Cake.Frosting.Tests/Fixtures/CakeHostFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ public CakeHostFixture()

FileSystem = new FakeFileSystem(Environment);
FileSystem.CreateDirectory("/Working");

Host.ConfigureServices(services => services.AddSingleton<IFileSystem>(FileSystem));
Host.ConfigureServices(services => services.AddSingleton<ICakeEnvironment>(Environment));
Host.ConfigureServices(services => services.AddSingleton<IConsole>(Console));
Host.ConfigureServices(services => services.AddSingleton<ICakeLog>(Log));
Host.ConfigureServices(services => services.AddSingleton<IToolInstaller>(Installer));
}

public void RegisterTask<T>()
Expand All @@ -47,6 +41,12 @@ public void RegisterTask<T>()

public int Run(params string[] args)
{
Host.ConfigureServices(services => services.AddSingleton<IFileSystem>(FileSystem));
Host.ConfigureServices(services => services.AddSingleton<ICakeEnvironment>(Environment));
Host.ConfigureServices(services => services.AddSingleton<IConsole>(Console));
Host.ConfigureServices(services => services.AddSingleton(Log));
Host.ConfigureServices(services => services.AddSingleton(Installer));

if (Strategy != null)
{
Host.ConfigureServices(services => services.AddSingleton(Strategy));
Expand Down
7 changes: 7 additions & 0 deletions src/Cake.Frosting/Cake.Frosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

<Import Project="$(MSBuildThisFileDirectory)..\Shared.msbuild" />

<ItemGroup>
<Compile Include="..\Cake.Core\Constants.cs" Link="Internal\Imported\Constants.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
</ItemGroup>
Expand All @@ -18,6 +22,9 @@
<ProjectReference Include="..\Cake.Common\Cake.Common.csproj" />
<ProjectReference Include="..\Cake.NuGet\Cake.NuGet.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Internal\Imported\" />
</ItemGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
15 changes: 14 additions & 1 deletion src/Cake.Frosting/Extensions/CakeHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Cake.Core.Composition;
using Cake.Core.IO;
using Cake.Core.Packaging;
using CoreConstants = Cake.Core.Constants;

namespace Cake.Frosting
{
Expand Down Expand Up @@ -152,7 +153,7 @@ public static CakeHost UsePackageInstaller<TPackageInstaller>(this CakeHost host
/// <param name="host">The <see cref="CakeHost"/> to configure.</param>
/// <param name="uri">The tool URI.</param>
/// <returns>The same <see cref="CakeHost"/> instance so that multiple calls can be chained.</returns>
public static CakeHost UseTool(this CakeHost host, Uri uri)
public static CakeHost InstallTool(this CakeHost host, Uri uri)
{
return host.ConfigureServices(services => services.UseTool(uri));
}
Expand All @@ -179,5 +180,17 @@ public static CakeHost UseCakeSetting(this CakeHost host, string key, string val
{
return host.ConfigureServices(services => services.UseCakeSetting(key, value));
}

/// <summary>
/// Sets the specified path as the path where tools and addins will be installed.
/// </summary>
/// <param name="host">The <see cref="CakeHost"/> to configure.</param>
/// <param name="path">The tool path.</param>
/// <returns>The same <see cref="CakeHost"/> instance so that multiple calls can be chained.</returns>
public static CakeHost SetToolPath(this CakeHost host, DirectoryPath path)
{
host.UseCakeSetting(CoreConstants.Paths.Tools, path.FullPath);
return host;
}
}
}
10 changes: 7 additions & 3 deletions src/Cake.Frosting/Internal/Commands/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ public override int Execute(CommandContext context, DefaultCommandSettings setti
return 0;
}

// Install tools
InstallTools(provider);
// Set the log verbosity
var log = provider.GetRequiredService<ICakeLog>();
log.Verbosity = settings.Verbosity;

// Run
var runner = GetFrostingEngine(provider, settings);

// Install tools
InstallTools(provider);

// Set the working directory
SetWorkingDirectory(provider, settings);

Expand All @@ -62,7 +66,7 @@ public override int Execute(CommandContext context, DefaultCommandSettings setti
runner.Settings.UseExclusiveTarget();
}

runner.Run(settings.Target, settings.Verbosity);
runner.Run(settings.Target);
}
catch (Exception ex)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Cake.Frosting/Internal/FrostingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Cake.Frosting.Internal
internal interface IFrostingEngine
{
ExecutionSettings Settings { get; }
CakeReport Run(string target, Verbosity verbosity);
CakeReport Run(string target);
}

internal abstract class FrostingEngine<THost> : IFrostingEngine
Expand Down Expand Up @@ -51,10 +51,8 @@ protected FrostingEngine(
_tasks = new List<IFrostingTask>(tasks ?? Array.Empty<IFrostingTask>());
}

public CakeReport Run(string target, Verbosity verbosity)
public CakeReport Run(string target)
{
_log.Verbosity = verbosity;

ConfigureTasks();
ConfigureLifetime();
ConfigureTaskLifetime();
Expand Down
21 changes: 17 additions & 4 deletions src/Cake.Frosting/Internal/ToolInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// 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 System.Globalization;
using System.Linq;
using Cake.Core;
using Cake.Core.Configuration;
using Cake.Core.Diagnostics;
using Cake.Core.Packaging;
using Cake.Core.Tooling;

Expand All @@ -15,19 +18,28 @@ internal sealed class ToolInstaller : IToolInstaller
{
private readonly ICakeEnvironment _environment;
private readonly IToolLocator _locator;
private readonly ICakeConfiguration _configuration;
private readonly ICakeLog _log;
private readonly List<IPackageInstaller> _installers;

public ToolInstaller(ICakeEnvironment environment, IToolLocator locator, IEnumerable<IPackageInstaller> installers)
public ToolInstaller(
ICakeEnvironment environment,
IToolLocator locator,
ICakeConfiguration configuration,
ICakeLog log,
IEnumerable<IPackageInstaller> installers)
{
_environment = environment;
_locator = locator;
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
_locator = locator ?? throw new ArgumentNullException(nameof(locator));
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_log = log ?? throw new ArgumentNullException(nameof(log));
_installers = new List<IPackageInstaller>(installers ?? Enumerable.Empty<IPackageInstaller>());
}

public void Install(PackageReference tool)
{
// Get the tool path.
var root = _environment.WorkingDirectory.Combine("tools").MakeAbsolute(_environment);
var root = _configuration.GetToolPath(".", _environment);

// Get the installer.
var installer = _installers.FirstOrDefault(i => i.CanInstall(tool, PackageType.Tool));
Expand All @@ -39,6 +51,7 @@ public void Install(PackageReference tool)
}

// Install the tool.
_log.Debug("Installing tool '{0}'...", tool.Package);
var result = installer.Install(tool, PackageType.Tool, root);
if (result.Count == 0)
{
Expand Down

0 comments on commit fc502c2

Please sign in to comment.