Skip to content
Merged
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
4 changes: 3 additions & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
global-json-file: ./global.json
dotnet-version: |
8.0.x
9.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Confix.Tool.Abstractions;
using Confix.Tool.Commands.Logging;
using Confix.Tool.Common.Pipelines;

namespace Confix.Tool.Entities.Components;

Expand All @@ -11,12 +12,14 @@ public ComponentProviderContext(
CancellationToken cancellationToken,
ProjectDefinition project,
SolutionDefinition solution,
IParameterCollection parameter,
IReadOnlyList<ComponentReferenceDefinition> componentReferences)
{
Logger = logger;
CancellationToken = cancellationToken;
Project = project;
Solution = solution;
Parameter = parameter;
ComponentReferences = componentReferences;
}

Expand All @@ -36,4 +39,6 @@ public ComponentProviderContext(

/// <inheritdoc />
public IList<Component> Components { get; } = new List<Component>();

public IParameterCollection Parameter { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Confix.Tool.Abstractions;
using Confix.Tool.Commands.Logging;
using Confix.Tool.Commands.Temp;
using Confix.Tool.Common.Pipelines;
using Confix.Tool.Schema;
using Json.Schema;
using Spectre.Console;
Expand Down Expand Up @@ -34,21 +35,38 @@ public async Task ExecuteAsync(IComponentProviderContext context)

context.Logger.FoundDotnetProject(csproj);

var buildResult = await DotnetHelpers.BuildProjectAsync(csproj, context.CancellationToken);
var buildResult = await DotnetHelpers.BuildProjectAsync(
csproj,
GetConfiguration(context),
context.CancellationToken);

if (!buildResult.Succeeded)
{
var output = buildResult.Output.EscapeMarkup();
throw new ExitException($"Failed to build project:\n{output}");
}

var projectAssembly = DotnetHelpers.GetAssemblyNameFromCsproj(csproj);
var components = await DiscoverResources(context.Logger, projectAssembly, projectDirectory);
var components = await DiscoverResources(
context.Logger,
projectAssembly,
projectDirectory);

foreach (var component in components)
{
context.Components.Add(component);
}
}

private string GetConfiguration(IComponentProviderContext context)
{
context.Parameter.TryGet(DotnetConfigurationOptions.Instance, out string? configuration);

return string.IsNullOrEmpty(configuration)
? DotnetConfigurationOptions.Default
: configuration;
}

private static async Task<IReadOnlyList<Component>> DiscoverResources(
IConsoleLogger logger,
string rootAssemblyName,
Expand Down Expand Up @@ -94,8 +112,10 @@ private static async Task<IReadOnlyList<Component>> DiscoverResources(
var referencedAssemblies = assembly
.GetReferencedAssemblies()
.Where(x => !string.IsNullOrWhiteSpace(x.Name) &&
!x.Name.StartsWith("System", StringComparison.InvariantCulture) &&
!x.Name.StartsWith("Microsoft", StringComparison.InvariantCulture))
!x.Name.StartsWith("System",
StringComparison.InvariantCulture) &&
!x.Name.StartsWith("Microsoft",
StringComparison.InvariantCulture))
.ToArray();

referencedAssemblies.ForEach(x => assembliesToScan.Enqueue(x.Name!));
Expand Down Expand Up @@ -203,8 +223,8 @@ private static async ValueTask<ComponentConfiguration> LoadComponentConfiguratio
private record DiscoveredResource(Assembly Assembly, string ResourceName)
{
public Stream GetStream() => Assembly.GetManifestResourceStream(ResourceName) ??
throw new ExitException(
$"Could not find resource: {ResourceName}");
throw new ExitException(
$"Could not find resource: {ResourceName}");
}
}

Expand All @@ -215,9 +235,9 @@ file static class Extensions
try
{
return context
.GetAssemblies()
.FirstOrDefault(x => x.FullName == assemblyName) ??
context.LoadFromAssemblyName(assemblyName);
.GetAssemblies()
.FirstOrDefault(x => x.FullName == assemblyName) ??
context.LoadFromAssemblyName(assemblyName);
}
catch
{
Expand All @@ -236,11 +256,11 @@ public static bool IsComponentRoot(this Assembly assembly)
// even though both are assembly metadata attributes, they are not of the equal
// type, so we need to compare the full name
return x.AttributeType.FullName ==
typeof(AssemblyMetadataAttribute).FullName &&
x.ConstructorArguments is
[
{ Value: "IsConfixComponentRoot" }, { Value: "true" }
];
typeof(AssemblyMetadataAttribute).FullName &&
x.ConstructorArguments is
[
{ Value: "IsConfixComponentRoot" }, { Value: "true" }
];
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface IComponentProviderContext
IList<Component> Components { get; }

IReadOnlyList<ComponentReferenceDefinition> ComponentReferences { get; }

IParameterCollection Parameter { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public Task InvokeAsync(IMiddlewareContext context, MiddlewareDelegate next)
var definitions = configuration.Project?.ComponentProviders ??
Array.Empty<ComponentProviderDefinition>();

var executor = ComponentProviderExecutor.FromDefinitions(_factory, definitions);
var executor = ComponentProviderExecutor.FromDefinitions(
_factory,
definitions,
context.Parameter);

context.Features.Set(new ComponentProviderExecutorFeature(executor));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ public sealed class ComponentProviderExecutor
: IComponentProviderExecutor, IAsyncDisposable
{
private readonly IReadOnlyList<IComponentProvider> _providers;
private readonly IParameterCollection _parameters;

public ComponentProviderExecutor(IReadOnlyList<IComponentProvider> providers)
public ComponentProviderExecutor(
IReadOnlyList<IComponentProvider> providers,
IParameterCollection parameters)
{
_providers = providers;
_parameters = parameters;
}

public async Task ExecuteAsync(IComponentProviderContext context)
Expand Down Expand Up @@ -42,6 +46,7 @@ public async Task<IList<Component>> LoadComponents(
cancellationToken,
project,
solution,
_parameters,
project.Components);

await ExecuteAsync(providerContext);
Expand All @@ -55,7 +60,8 @@ public async Task<IList<Component>> LoadComponents(

public static IComponentProviderExecutor FromDefinitions(
IComponentProviderFactory componentProviders,
IEnumerable<ComponentProviderDefinition> configurations)
IEnumerable<ComponentProviderDefinition> configurations,
IParameterCollection parameters)
{
var providers = new List<IComponentProvider>();

Expand All @@ -68,7 +74,8 @@ public static IComponentProviderExecutor FromDefinitions(
providers.Add(new LocalComponentProvider());
providers.Add(new MergeComponentProvider());

return new ComponentProviderExecutor(providers);

return new ComponentProviderExecutor(providers, parameters);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.CommandLine;

namespace Confix.Tool;

internal sealed class DotnetConfigurationOptions : Option<string>
{
public static DotnetConfigurationOptions Instance { get; } = new();

public static string Default => "Debug";

private DotnetConfigurationOptions()
: base("--dotnet-configuration")
{
Arity = ArgumentArity.ExactlyOne;
SetDefaultValue(Default);
Description = "The configuration passed to dotnet commands. Defaults to 'Debug'.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private static async Task<bool> ValidateComponentAsync(
ct,
project,
solution,
context.Parameter,
new[] { reference });

var executor = context.Features.Get<ComponentProviderExecutorFeature>().Executor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public sealed class RestoreCommandPipeline : Pipeline
protected override void Configure(IPipelineDescriptor builder)
{
builder
.AddOption(DotnetConfigurationOptions.Instance)
.Use<LoadConfigurationMiddleware>()
.UseEnvironment()
.UseHandler(InvokeAsync);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ public static class DotnetHelpers
{
public static async Task<ProcessExecutionResult> BuildProjectAsync(
FileInfo path,
string configuration,
CancellationToken cancellationToken)
{
var startInfo = new ProcessStartInfo
{
FileName = "dotnet", // The dotnet CLI
Arguments = $"build {path}", // The command to build your project
Arguments = $"build {path} --configuration {configuration}", // The command to build your project
RedirectStandardOutput = true, // Redirect output so we can read it
UseShellExecute = false // Don't use the shell to execute the command
};
Expand Down
16 changes: 13 additions & 3 deletions src/Confix.Tool/src/Confix.Nuke/Confix.Tool.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ public partial class ConfixTasks : ToolTasks
/// <remarks><p>This is a <a href="http://www.nuke.build/docs/authoring-builds/cli-tools.html#fluent-apis">CLI wrapper with fluent API</a> that allows to modify the following arguments:</p><ul><li><c>--encrypt</c> via <see cref="ConfixBuildSettings.Encrypt"/></li><li><c>--environment</c> via <see cref="ConfixBuildSettings.Environment"/></li><li><c>--output-file</c> via <see cref="ConfixBuildSettings.OutputFile"/></li><li><c>--verbosity</c> via <see cref="ConfixBuildSettings.Verbosity"/></li></ul></remarks>
public static IEnumerable<(ConfixBuildSettings Settings, IReadOnlyCollection<Output> Output)> ConfixBuild(CombinatorialConfigure<ConfixBuildSettings> configurator, int degreeOfParallelism = 1, bool completeOnFailure = false) => configurator.Invoke(ConfixBuild, degreeOfParallelism, completeOnFailure);
/// <summary><p>For more details, visit the <a href="https://swisslife-oss.github.io/Confix/">official website</a>.</p></summary>
/// <remarks><p>This is a <a href="http://www.nuke.build/docs/authoring-builds/cli-tools.html#fluent-apis">CLI wrapper with fluent API</a> that allows to modify the following arguments:</p><ul><li><c>--environment</c> via <see cref="ConfixRestoreSettings.Environment"/></li><li><c>--verbosity</c> via <see cref="ConfixRestoreSettings.Verbosity"/></li></ul></remarks>
/// <remarks><p>This is a <a href="http://www.nuke.build/docs/authoring-builds/cli-tools.html#fluent-apis">CLI wrapper with fluent API</a> that allows to modify the following arguments:</p><ul><li><c>--dotnet-configuration</c> via <see cref="ConfixRestoreSettings.DotnetConfiguration"/></li><li><c>--environment</c> via <see cref="ConfixRestoreSettings.Environment"/></li><li><c>--verbosity</c> via <see cref="ConfixRestoreSettings.Verbosity"/></li></ul></remarks>
public static IReadOnlyCollection<Output> ConfixRestore(ConfixRestoreSettings options = null) => new ConfixTasks().Run(options);
/// <summary><p>For more details, visit the <a href="https://swisslife-oss.github.io/Confix/">official website</a>.</p></summary>
/// <remarks><p>This is a <a href="http://www.nuke.build/docs/authoring-builds/cli-tools.html#fluent-apis">CLI wrapper with fluent API</a> that allows to modify the following arguments:</p><ul><li><c>--environment</c> via <see cref="ConfixRestoreSettings.Environment"/></li><li><c>--verbosity</c> via <see cref="ConfixRestoreSettings.Verbosity"/></li></ul></remarks>
/// <remarks><p>This is a <a href="http://www.nuke.build/docs/authoring-builds/cli-tools.html#fluent-apis">CLI wrapper with fluent API</a> that allows to modify the following arguments:</p><ul><li><c>--dotnet-configuration</c> via <see cref="ConfixRestoreSettings.DotnetConfiguration"/></li><li><c>--environment</c> via <see cref="ConfixRestoreSettings.Environment"/></li><li><c>--verbosity</c> via <see cref="ConfixRestoreSettings.Verbosity"/></li></ul></remarks>
public static IReadOnlyCollection<Output> ConfixRestore(Configure<ConfixRestoreSettings> configurator) => new ConfixTasks().Run(configurator.Invoke(new ConfixRestoreSettings()));
/// <summary><p>For more details, visit the <a href="https://swisslife-oss.github.io/Confix/">official website</a>.</p></summary>
/// <remarks><p>This is a <a href="http://www.nuke.build/docs/authoring-builds/cli-tools.html#fluent-apis">CLI wrapper with fluent API</a> that allows to modify the following arguments:</p><ul><li><c>--environment</c> via <see cref="ConfixRestoreSettings.Environment"/></li><li><c>--verbosity</c> via <see cref="ConfixRestoreSettings.Verbosity"/></li></ul></remarks>
/// <remarks><p>This is a <a href="http://www.nuke.build/docs/authoring-builds/cli-tools.html#fluent-apis">CLI wrapper with fluent API</a> that allows to modify the following arguments:</p><ul><li><c>--dotnet-configuration</c> via <see cref="ConfixRestoreSettings.DotnetConfiguration"/></li><li><c>--environment</c> via <see cref="ConfixRestoreSettings.Environment"/></li><li><c>--verbosity</c> via <see cref="ConfixRestoreSettings.Verbosity"/></li></ul></remarks>
public static IEnumerable<(ConfixRestoreSettings Settings, IReadOnlyCollection<Output> Output)> ConfixRestore(CombinatorialConfigure<ConfixRestoreSettings> configurator, int degreeOfParallelism = 1, bool completeOnFailure = false) => configurator.Invoke(ConfixRestore, degreeOfParallelism, completeOnFailure);
/// <summary><p>Validates the schema of all the projects</p><p>For more details, visit the <a href="https://swisslife-oss.github.io/Confix/">official website</a>.</p></summary>
/// <remarks><p>This is a <a href="http://www.nuke.build/docs/authoring-builds/cli-tools.html#fluent-apis">CLI wrapper with fluent API</a> that allows to modify the following arguments:</p><ul><li><c>--environment</c> via <see cref="ConfixValidateSettings.Environment"/></li><li><c>--verbosity</c> via <see cref="ConfixValidateSettings.Verbosity"/></li></ul></remarks>
Expand Down Expand Up @@ -570,6 +570,8 @@ public partial class ConfixBuildSettings : ToolOptions
[Command(Type = typeof(ConfixTasks), Command = nameof(ConfixTasks.ConfixRestore), Arguments = "restore")]
public partial class ConfixRestoreSettings : ToolOptions
{
/// <summary>The configuration passed to dotnet commands. Defaults to 'Debug'.</summary>
[Argument(Format = "--dotnet-configuration {value}")] public string DotnetConfiguration => Get<string>(() => DotnetConfiguration);
/// <summary>The name of the environment to run the command in. Overrules the active environment set in .confixrc</summary>
[Argument(Format = "--environment {value}")] public string Environment => Get<string>(() => Environment);
/// <summary>Sets the verbosity level</summary>
Expand Down Expand Up @@ -1428,6 +1430,14 @@ [Pure] [Builder(Type = typeof(ConfixBuildSettings), Property = nameof(ConfixBuil
[ExcludeFromCodeCoverage]
public static partial class ConfixRestoreSettingsExtensions
{
#region DotnetConfiguration
/// <inheritdoc cref="ConfixRestoreSettings.DotnetConfiguration"/>
[Pure] [Builder(Type = typeof(ConfixRestoreSettings), Property = nameof(ConfixRestoreSettings.DotnetConfiguration))]
public static T SetDotnetConfiguration<T>(this T o, string v) where T : ConfixRestoreSettings => o.Modify(b => b.Set(() => o.DotnetConfiguration, v));
/// <inheritdoc cref="ConfixRestoreSettings.DotnetConfiguration"/>
[Pure] [Builder(Type = typeof(ConfixRestoreSettings), Property = nameof(ConfixRestoreSettings.DotnetConfiguration))]
public static T ResetDotnetConfiguration<T>(this T o) where T : ConfixRestoreSettings => o.Modify(b => b.Remove(() => o.DotnetConfiguration));
#endregion
#region Environment
/// <inheritdoc cref="ConfixRestoreSettings.Environment"/>
[Pure] [Builder(Type = typeof(ConfixRestoreSettings), Property = nameof(ConfixRestoreSettings.Environment))]
Expand Down
6 changes: 6 additions & 0 deletions src/Confix.Tool/src/Confix.Nuke/Confix.Tool.json
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@
"definiteArgument": "restore",
"settingsClass": {
"properties": [
{
"name": "DotnetConfiguration",
"type": "string",
"format": "--dotnet-configuration {value}",
"help": "The configuration passed to dotnet commands. Defaults to 'Debug'."
},
{
"name": "Environment",
"type": "string",
Expand Down
Loading