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
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public async Task ExecuteAsync(IComponentProviderContext context)
{
var components = context.ComponentReferences.Where(x => x.Provider == _name && x.IsEnabled);

var refs = await FetchRefsAsync(context.CancellationToken);
var refs = await FetchRefsAsync(context);

var tasks = components
.Select(x => ProcessComponentAsync(x, refs, context.Logger, context.CancellationToken))
.Select(x => ProcessComponentAsync(x, refs, context))
.ToArray();

List<string>? errors = null;
Expand All @@ -82,18 +82,20 @@ public async Task ExecuteAsync(IComponentProviderContext context)
}

private async Task<IReadOnlyDictionary<string, string>> FetchRefsAsync(
CancellationToken cancellationToken)
IComponentProviderContext context)
{
var directory =
new DirectoryInfo(Path.Combine(_cloneDirectory.FullName, "__refs"));
var refs = new Dictionary<string, string>();

string gitUrl = GitUrl.Create(_repositoryUrl, context.Parameter);

var sparseConfig =
new GitSparseCheckoutConfiguration(_repositoryUrl, directory.FullName, _arguments);
await _git.SparseCheckoutAsync(sparseConfig, cancellationToken);
new GitSparseCheckoutConfiguration(gitUrl, directory.FullName, _arguments);
await _git.SparseCheckoutAsync(sparseConfig, context.CancellationToken);

var showRefConfig = new GitShowRefsConfiguration(directory.FullName, _arguments);
var refsOutput = await _git.ShowRefsAsync(showRefConfig, cancellationToken);
var refsOutput = await _git.ShowRefsAsync(showRefConfig, context.CancellationToken);

foreach (var line in refsOutput.Split('\n'))
{
Expand All @@ -115,8 +117,7 @@ private async Task<IReadOnlyDictionary<string, string>> FetchRefsAsync(
private async Task<ComponentOrError?> ProcessComponentAsync(
ComponentReferenceDefinition definition,
IReadOnlyDictionary<string, string> refs,
IConsoleLogger logger,
CancellationToken cancellationToken)
IComponentProviderContext context)
{
var version = definition.Version ?? "latest";
var componentName = definition.ComponentName;
Expand All @@ -129,13 +130,15 @@ private async Task<IReadOnlyDictionary<string, string>> FetchRefsAsync(
directory.EnsureFolder();

var cloneArgument = _arguments.ToList();

string gitUrl = GitUrl.Create(_repositoryUrl, context.Parameter);

var cloneConfiguration = new GitCloneConfiguration(
_repositoryUrl,
gitUrl,
directory.FullName,
cloneArgument.ToArray());

await _git.CloneAsync(cloneConfiguration, cancellationToken);
await _git.CloneAsync(cloneConfiguration, context.CancellationToken);

if (version is not "latest")
{
Expand All @@ -147,7 +150,7 @@ private async Task<IReadOnlyDictionary<string, string>> FetchRefsAsync(

await _git.CheckoutAsync(
new GitCheckoutConfiguration(directory.FullName, hash, cloneArgument.ToArray()),
cancellationToken);
context.CancellationToken);
}

var pathToComponent = Path
Expand All @@ -159,7 +162,7 @@ await _git.CheckoutAsync(
$"Could not find component {componentName} ({version}) in git repository");
}

logger.FoundComponent(componentName, version);
context.Logger.FoundComponent(componentName, version);

var json = JsonSchema.FromFile(pathToComponent);
var component =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Confix.Tool.Common.Pipelines;

namespace Confix.Tool.Entities.Components.Git;

public static class GitUrl
{
public static string Create(string repositoryUrl, IParameterCollection parameters)
{
parameters.TryGet(GitUsernameOptions.Instance, out string? username);
parameters.TryGet(GitTokenOptions.Instance, out string? token);

if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(token))
{
if (repositoryUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
var uri = new Uri(repositoryUrl);
var builder = new UriBuilder(uri)
{
UserName = username,
Password = token
};
return builder.Uri.ToString();
}
}

return repositoryUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public sealed class BuildProjectMiddleware : IMiddleware
/// <inheritdoc />
public async Task InvokeAsync(IMiddlewareContext context, MiddlewareDelegate next)
{
var variableContext = new VariableProviderContext(
context.Parameter,
context.CancellationToken);

context.SetStatus("Building the project files");

var cancellationToken = context.CancellationToken;
Expand All @@ -32,7 +36,7 @@ public async Task InvokeAsync(IMiddlewareContext context, MiddlewareDelegate nex
context.Logger.ReplaceVariablesOfConfigurationFile(file);

file.Content = await variableReplacer
.RewriteOrThrowAsync(content, context.CancellationToken);
.RewriteOrThrowAsync(content, variableContext);
}

await next(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Confix.Tool.Entities.Components.DotNet;
using Confix.Tool.Middlewares.JsonSchemas;
using Confix.Tool.Schema;
using Confix.Variables;

namespace Confix.Tool.Middlewares.Project;

Expand All @@ -30,6 +31,10 @@ public async Task InvokeAsync(IMiddlewareContext context, MiddlewareDelegate nex
var jsonSchemas = context.Features.Get<JsonSchemaFeature>();
var configuration = context.Features.Get<ConfigurationFeature>();
var files = context.Features.Get<ConfigurationFileFeature>().Files;

var variableContext = new VariableProviderContext(
context.Parameter,
context.CancellationToken);

configuration.EnsureProjectScope();

Expand All @@ -42,7 +47,7 @@ public async Task InvokeAsync(IMiddlewareContext context, MiddlewareDelegate nex

context.SetStatus("Loading variables...");
var variableResolver = context.Features.Get<VariablesFeature>().Resolver;
var variables = await variableResolver.ListVariables(cancellationToken);
var variables = await variableResolver.ListVariables(variableContext);

context.SetStatus("Composing the schema...");
var jsonSchema = _projectComposer.Compose(components, variables);
Expand Down
15 changes: 15 additions & 0 deletions src/Confix.Tool/src/Confix.Library/Options/GitTokenOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.CommandLine;

namespace Confix.Tool;

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

private GitTokenOptions()
: base("--git-token")
{
Arity = ArgumentArity.ZeroOrOne;
Description = "The token used for git authentication.";
}
}
15 changes: 15 additions & 0 deletions src/Confix.Tool/src/Confix.Library/Options/GitUsernameOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.CommandLine;

namespace Confix.Tool;

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

private GitUsernameOptions()
: base("--git-username")
{
Arity = ArgumentArity.ZeroOrOne;
Description = "The username used for git authentication.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ protected override void Configure(IPipelineDescriptor builder)
.AddOption(ActiveEnvironmentOption.Instance)
.AddOption(OutputFileOption.Instance)
.AddOption(EncryptionOption.Instance)
.AddOption(GitUsernameOptions.Instance)
.AddOption(GitTokenOptions.Instance)
.UseHandler(InvokeAsync);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ protected override void Configure(IPipelineDescriptor builder)
{
builder
.AddOption(NoRestoreOptions.Instance)
.AddOption(GitUsernameOptions.Instance)
.AddOption(GitTokenOptions.Instance)
.Use<LoadConfigurationMiddleware>()
.UseReadConfigurationFiles()
.UseEnvironment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Confix.Tool.Middlewares.Reporting;
using Confix.Tool.Schema;
using Confix.Utilities;
using Confix.Variables;
using Microsoft.Extensions.DependencyInjection;

namespace Confix.Tool.Reporting;
Expand Down Expand Up @@ -215,11 +216,18 @@ private static async Task<List<VariableReport>> GetVariablesAsync(
ConfigurationFile file,
CancellationToken ct)
{
var variableContext = new VariableProviderContext(
context.Parameter,
context.CancellationToken);

var variablesFeature = context.Features.Get<VariablesFeature>();
var variables = new List<VariableReport>();

var content = await file.TryLoadContentAsync(ct);
foreach (var variable in await variablesFeature.Extractor.ExtractAsync(content, ct))
var variableInfos = await variablesFeature.Extractor
.ExtractAsync(content, variableContext);

foreach (var variable in variableInfos)
{
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(variable.VariableValue));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ protected override void Configure(IPipelineDescriptor builder)
{
builder
.AddOption(DotnetConfigurationOptions.Instance)
.AddOption(GitUsernameOptions.Instance)
.AddOption(GitTokenOptions.Instance)
.Use<LoadConfigurationMiddleware>()
.UseEnvironment()
.UseHandler(InvokeAsync);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ protected override void Configure(IPipelineDescriptor builder)

private static async Task InvokeAsync(IMiddlewareContext context)
{
var cancellationToken = context.CancellationToken;
var variableContext = new VariableProviderContext(
context.Parameter,
context.CancellationToken);

var variableFeature = context.Features.Get<VariablesFeature>();

Expand All @@ -48,9 +50,9 @@ private static async Task InvokeAsync(IMiddlewareContext context)
context.Status.Message =
$"Copy variable {fromVariablePath.ToString().AsHighlighted()} to {toVariablePath.ToString().AsHighlighted()}...";

var fromValue = await fromResolver.ResolveOrThrowAsync(fromVariablePath, cancellationToken);
var fromValue = await fromResolver.ResolveOrThrowAsync(fromVariablePath, variableContext);

var result = await toResolver.SetVariable(toVariablePath, fromValue, cancellationToken);
var result = await toResolver.SetVariable(toVariablePath, fromValue, variableContext);

context.Logger.VariableSet(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ protected override void Configure(IPipelineDescriptor builder)

private static async Task InvokeAsync(IMiddlewareContext context)
{
var variableContext = new VariableProviderContext(
context.Parameter,
context.CancellationToken);

var resolver = context.Features.Get<VariablesFeature>().Resolver;

if (!context.Parameter.TryGet(VariableNameOption.Instance, out string variableName))
Expand All @@ -32,7 +36,7 @@ private static async Task InvokeAsync(IMiddlewareContext context)
context.Status.Message = $"Resolving variable {variablePath.ToString().AsHighlighted()}...";

var result = await resolver
.ResolveOrThrowAsync(variablePath, context.CancellationToken);
.ResolveOrThrowAsync(variablePath, variableContext);

context.Logger.PrintVariableResolved(variablePath, result.ToJsonString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ private static async Task InvokeAsync(IMiddlewareContext context)
IEnumerable<VariablePath> variables;

context.Status.Message = "Fetching Variables...";

var variableContext = new VariableProviderContext(
context.Parameter,
context.CancellationToken);

if (context.Parameter
.TryGet(VariableProviderNameOption.Instance, out string? variableProviderName))
{
variables =
await resolver.ListVariables(variableProviderName, context.CancellationToken);
await resolver.ListVariables(variableProviderName, variableContext);
}
else
{
variables = await resolver.ListVariables(context.CancellationToken);
variables = await resolver.ListVariables(variableContext);
}

context.Logger.PrintVariables(variables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ private static async Task InvokeAsync(IMiddlewareContext context)
{
context.Features.Get<ConfigurationFeature>().EnsureProjectScope();

var variableContext = new VariableProviderContext(
context.Parameter,
context.CancellationToken);

var resolver = context.Features.Get<VariablesFeature>().Resolver;
if (!context.Parameter.TryGet(VariableNameOption.Instance, out string variableName))
{
Expand All @@ -44,7 +48,7 @@ private static async Task InvokeAsync(IMiddlewareContext context)
$"Setting variable {parsed.Value.ToString().AsHighlighted()}...";

var result = await resolver
.SetVariable(parsed.Value, value, context.CancellationToken);
.SetVariable(parsed.Value, value, variableContext);

context.Logger.VariableSet(result);
}
Expand Down
Loading
Loading