Skip to content

Commit

Permalink
Fix defaults (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmus authored Jan 19, 2022
1 parent 00600a4 commit 64ba9c4
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 24 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
drop:
- all
```
* New: Now possible to change Bake internal defaults via environment
variables. More of these will be exposed in upcomming releases
* Fixed: Docker Hub push URL should just be the username
# 0.6-beta
Expand Down
1 change: 1 addition & 0 deletions Source/Bake.Tests/ExplicitTests/GitHubReleaseCookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected override IServiceCollection Configure(IServiceCollection serviceCollec
.AddSingleton<GitHubReleaseCook>()
.AddSingleton<IDefaults, Defaults>()
.AddSingleton<IFileSystem, FileSystem>()
.AddSingleton(TestEnvironmentVariables.None)
.AddSingleton<IEnvironmentVariables>(new TestEnvironmentVariables(new Dictionary<string, string>
{
["github_personal_token"] = GetToken(),
Expand Down
2 changes: 2 additions & 0 deletions Source/Bake.Tests/Helpers/TestEnvironmentVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace Bake.Tests.Helpers
{
public class TestEnvironmentVariables : IEnvironmentVariables
{
public static IEnvironmentVariables None { get; } = new TestEnvironmentVariables(new Dictionary<string, string>());

private readonly IReadOnlyDictionary<string, string> _environmentVariables;

public TestEnvironmentVariables(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected override IServiceCollection Configure(IServiceCollection serviceCollec
.AddTransient<IFileSystem, FileSystem>()
.AddTransient<ICredentials, Credentials>()
.AddTransient<IDefaults, Defaults>()
.AddSingleton(TestEnvironmentVariables.None)
.AddTransient<IConventionInterpreter, ConventionInterpreter>()
.AddTransient<IDotNetTfmParser, DotNetTfmParser>()
.AddTransient<IDockerLabels, DockerLabels>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class CommandFactoryTests : TestFor<CommandFactory>
[SetUp]
public void SetUp()
{
Inject<IDestinationParser>(new DestinationParser(new Defaults()));
Inject<IDestinationParser>(new DestinationParser(new Defaults(TestEnvironmentVariables.None)));
}

[Command("A", "B")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class GitHubGatherTests : TestFor<GitHubGather>
[SetUp]
public void SetUp()
{
Inject<IDefaults>(new Defaults());
Inject<IDefaults>(new Defaults(TestEnvironmentVariables.None));
}

[TestCase("https://github.com/rasmus/Bake.git", "rasmus", "Bake", "https://api.github.com/")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DestinationParserTests : TestFor<DestinationParser>
[SetUp]
public void SetUp()
{
Inject<IDefaults>(new Defaults());
Inject<IDefaults>(new Defaults(TestEnvironmentVariables.None));
}

[TestCase(
Expand All @@ -58,7 +58,7 @@ public void NuGetRegistry(

[TestCase(
"container>rasmus",
"registry.hub.docker.com/rasmus/")]
"rasmus/")]
[TestCase(
"container>localhost:5000",
"localhost:5000/")]
Expand Down
2 changes: 1 addition & 1 deletion Source/Bake/Cooking/Composers/DotNetComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private Recipe CreateRestoreRecipe(
var gitHub = ingredients.GitHub;
if (gitHub != null)
{
sources.Add(_defaults.GitHubNuGetRegistry.AbsoluteUri.Replace("/OWNER/", $"/{gitHub.Owner}/"));
sources.Add(_defaults.GitHubNuGetRegistry.Replace("/OWNER/", $"/{gitHub.Owner}/"));
}

return new DotNetRestoreSolutionRecipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private async Task ExtractNuGetDestinationAsync(
{
case Names.DynamicDestinations.GitHub:
var gitHubInformation = await ingredients.GitHubTask;
var url = new Uri(_defaults.GitHubNuGetRegistry.AbsoluteUri.Replace("/OWNER/", $"/{gitHubInformation.Owner}/"), UriKind.Absolute);
var url = new Uri(_defaults.GitHubNuGetRegistry.Replace("/OWNER/", $"/{gitHubInformation.Owner}/"), UriKind.Absolute);

var nugetRegistryDestination = new NuGetRegistryDestination(url);
ingredients.Destinations.Add(nugetRegistryDestination);
Expand Down
5 changes: 3 additions & 2 deletions Source/Bake/Cooking/Ingredients/Gathers/GitHubGather.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public async Task GatherAsync(
}

Uri apiUrl;
if (string.Equals(gitInformation.OriginUrl.Host, _defaults.GitHubUrl.Host, StringComparison.OrdinalIgnoreCase))
var gitHubUrl = new Uri(_defaults.GitHubUrl);
if (string.Equals(gitInformation.OriginUrl.Host, gitHubUrl.Host, StringComparison.OrdinalIgnoreCase))
{
apiUrl = GitHubApiUrl;
_logger.LogInformation(
Expand Down Expand Up @@ -115,7 +116,7 @@ public async Task GatherAsync(
repo = repo[..lastIndex];
}

var url = new Uri($"{_defaults.GitHubUrl.Scheme}://{_defaults.GitHubUrl.Host}/{match.Groups["owner"].Value}/{repo}");
var url = new Uri($"{gitHubUrl.Scheme}://{gitHubUrl.Host}/{match.Groups["owner"].Value}/{repo}");
ingredients.GitHub = new GitHubInformation(
match.Groups["owner"].Value,
repo,
Expand Down
6 changes: 4 additions & 2 deletions Source/Bake/Core/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ public async Task<string> TryGetNuGetApiKeyAsync(
$"bake_credentials_nuget_{hostname}_apikey"
};

if (string.Equals(url.Host, _defaults.GitHubNuGetRegistry.Host, StringComparison.OrdinalIgnoreCase))
var gitHubNuGetRegistry = new Uri(_defaults.GitHubNuGetRegistry, UriKind.Absolute);
if (string.Equals(url.Host, gitHubNuGetRegistry.Host, StringComparison.OrdinalIgnoreCase))
{
possibilities.AddRange(GitHubTokenPossibilities);
}

if (string.Equals(url.Host, _defaults.NuGetRegistry.Host, StringComparison.OrdinalIgnoreCase))
var nuGetRegistry = new Uri(_defaults.NuGetRegistry, UriKind.Absolute);
if (string.Equals(url.Host, nuGetRegistry.Host, StringComparison.OrdinalIgnoreCase))
{
possibilities.Add("nuget_apikey");
}
Expand Down
46 changes: 39 additions & 7 deletions Source/Bake/Core/Defaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,50 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;

// ReSharper disable StringLiteralTypo

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Bake.Core
{
public class Defaults : IDefaults
{
public Uri GitHubUrl { get; } = new("https://github.com/", UriKind.Absolute);
public Uri GitHubNuGetRegistry { get; } = new("https://nuget.pkg.github.com/OWNER/index.json");
public Uri NuGetRegistry { get; } = new("https://api.nuget.org/v3/index.json");
public string DockerHubUserRegistry { get; } = new("registry.hub.docker.com/{USER}/");
public string GitHubUserRegistry { get; } = new("ghcr.io/{USER}/");
private readonly IEnvironmentVariables _environmentVariables;

public string GitHubUrl { get; private set; } = "https://github.com/";
public string GitHubNuGetRegistry { get; private set; } = "https://nuget.pkg.github.com/OWNER/index.json";
public string NuGetRegistry { get; private set; } = "https://api.nuget.org/v3/index.json";
public string DockerHubUserRegistry { get; private set; } = "{USER}/";
public string GitHubUserRegistry { get; private set; } = "ghcr.io/{USER}/";

public Defaults(
IEnvironmentVariables environmentVariables)
{
_environmentVariables = environmentVariables;
}

public async Task InitializeAsync(
CancellationToken cancellationToken)
{
var e = await _environmentVariables.GetAsync(cancellationToken);

GitHubUrl = Get(e, "github_url", GitHubUrl);
GitHubNuGetRegistry = Get(e, "github_packages_nuget_url", GitHubNuGetRegistry);
GitHubUserRegistry = Get(e, "github_packages_container_url", GitHubUserRegistry);
NuGetRegistry = Get(e, "nuget_url", NuGetRegistry);
DockerHubUserRegistry = Get(e, "dockerhub_user_url", DockerHubUserRegistry);
}

private static string Get(
IReadOnlyDictionary<string, string> environmentVariables,
string name,
string defaultValue)
{
return environmentVariables.TryGetValue($"bake_defaults_{name}", out var v) && !string.IsNullOrEmpty(v)
? v
: defaultValue;
}
}
}
2 changes: 1 addition & 1 deletion Source/Bake/Core/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Bake.Core
{
public class EnvironmentVariables : IEnvironmentVariables
{
private readonly Lazy<Task<IReadOnlyDictionary<string, string>>> _environmentVariables = new Lazy<Task<IReadOnlyDictionary<string, string>>>(
private readonly Lazy<Task<IReadOnlyDictionary<string, string>>> _environmentVariables = new(
GetEnvironmentVariablesAsync,
LazyThreadSafetyMode.ExecutionAndPublication);

Expand Down
12 changes: 8 additions & 4 deletions Source/Bake/Core/IDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Bake.Core
{
public interface IDefaults
{
Uri GitHubUrl { get; }
Uri GitHubNuGetRegistry { get; }
Uri NuGetRegistry { get; }
string GitHubUrl { get; }
string GitHubNuGetRegistry { get; }
string NuGetRegistry { get; }
string DockerHubUserRegistry { get; }
string GitHubUserRegistry { get; }

Task InitializeAsync(
CancellationToken cancellationToken);
}
}
8 changes: 7 additions & 1 deletion Source/Bake/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,30 @@
using System.Threading;
using System.Threading.Tasks;
using Bake.Commands;
using Bake.Core;

namespace Bake
{
public class Executor : IExecutor
{
private readonly ICommandFactory _commandFactory;
private readonly IDefaults _defaults;

public Executor(
ICommandFactory commandFactory)
ICommandFactory commandFactory,
IDefaults defaults)
{
_commandFactory = commandFactory;
_defaults = defaults;
}

public async Task<int> ExecuteAsync(
string[] args,
IReadOnlyCollection<Type> commandTypes,
CancellationToken cancellationToken)
{
await _defaults.InitializeAsync(cancellationToken);

var app = _commandFactory.Create(commandTypes);

return await app.ExecuteAsync(args, cancellationToken);
Expand Down
2 changes: 1 addition & 1 deletion Source/Bake/Services/DestinationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public bool TryParse(string str, out Destination destination)
case Names.ArtifactTypes.NuGet:
if (parts.Length == 1)
{
destination = new NuGetRegistryDestination(_defaults.NuGetRegistry);
destination = new NuGetRegistryDestination(new Uri(_defaults.NuGetRegistry, UriKind.Absolute));
return true;
}

Expand Down

0 comments on commit 64ba9c4

Please sign in to comment.