Skip to content

Commit

Permalink
Improve logging (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmus authored Dec 1, 2021
1 parent 125f533 commit 4d79e4e
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cSpell.words": [
"apikey",
"Mikkelsen",
"Rasmus"
]
}
54 changes: 45 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Here are some examples of typical arguments passed to Bake.
Here is the simple use case for using Bake on e.g. pull requests

```
bake run --build-version 1.0
bake run
```

### Basic release build
Expand All @@ -46,35 +46,71 @@ created during the release to the GitHub package store for the owner of the
current repository.

```
bake run --convention=Release --build-version 1.0 --destination="nuget>github"
bake run --convention=Release --destination="nuget>github,container>rasmus"
```

### Examples of common used arguments
### Recognized repository content

Here are some examples of common used argumnets to Bake
When Bake analyzes a repository, it first gathers gathers basic information
regarding the environment its executed within.

* **Git** - Repository information like origin, branch name and commit hash are
stored and used when creating e.g. release artifacts
* **GitHub** - If the repository origin originates from GitHub, the information
is gathered and used to further enrich any release artifacts
* **GitHub Action** - When Bake is executed from within a GitHub action, it
automatically recognizes the token and uses that when publishing artifacts
and releases
* **Release notes** - If the repository contains a `RELEASE_NOTES.md` file,
the content as well as the version information is used to further enrich
any release artifacts

After the initial environment information gathering is completed, Bake starts
to scan the repository for files and structures it knows how to process.

* **.NET** - Directories that contain [.NET](https://dot.net/) projects are
analyzed and the application/service is built, tested and optionally
containerized
* **Docker** - Directories that contain a
[`Dockerfile`](https://docs.docker.com/engine/reference/builder/) will get
the file built
* **Go** - Directories that contain [Go](https://go.dev/) projects are analyzed
and the application/service is built, tested and optionally containerized

Based on the selected convention (by providing e.g. `--convention=Release`)
and the destinations for artifacts, Bake pushes/uploads/creates the built
artifacts to their configured destinations.

### Examples of sending artifacts to their destinations

Here are some examples of common used arguments to Bake

* `--destination=`
* **Container**
* `container>{username}`, e.g. simply `container>rasmus` - Will mark the
destination as [Docker Hub](https://hub.docker.com/) with that username
* `container>github` - Send condcontainers to the GitHub package repository
* `container>github` - Send containers to the GitHub package repository
for at owner/organization of the git repository
* `container>localhost:5000` - Send containers to a specific container
registry
* **NuGet**
* `nuget` - An unamed destination will send NuGet packages to the central
* `nuget` - An unnamed destination will send NuGet packages to the central
NuGet repository at [nuget.org](https://www.nuget.org/). Bake will look for
an API in an environment variabled named `NUGET_APIKEY`
an API in an environment variables named `NUGET_APIKEY`
* `nuget>github` - Send NuGet packages to the specific need with is owned
by the owner of the repositry of the current repository. Bake will
by the owner of the repository of the current repository. Bake will
automatically setup the API key for the current build using the
`GITHUB_TOKEN` (automatically provided in GitHub actions), thus no
additional configuration is required
* `nuget>http://localhost:5555/v3/index.json` - Send NuGet packages to the feed
specified by the URL. Bake will look for the API key in an environment
variable named `bake_credentials_nuget_{hostname}_apikey`, in which
`{hostname}` is the hostname of the URL with invalid characters removed

* **Release**
* `release>github` - Creates release on GitHub within the current GitHub
repository with the release notes and any important artifacts neatly
bundled together in ZIP files with any `README.md`, `LICENSE` and
`RELEASE_NOTES.md` in ZIP files found in the root of the repository

## License

Expand Down
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 0.4-beta

* New: Run .NET in an Alpine Linux image instead of a full blow Ubuntu
* New: Provide descriptions of what each sub-command actually does
* Fixed: Not specifying a version should merely default to 1.0 instead
of throwing an exception
* Fixed: Release files containing Bake should be named `bake` instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public async Task Run()
// Act
var returnCode = await ExecuteAsync(TestState.New(
"run",
"--print-plan=true",
"--convention=Release",
"--destination=container>localhost:5000",
"--build-version", SemVer.Random.ToString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public async Task Run()
// Act
var returnCode = await ExecuteAsync(TestState.New(
"run",
"--print-plan=true",
"--build-version", version));

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public async Task Run()
// Arrange
var testState = TestState.New(
"run",
"--print-plan=true",
"--convention=Release",
"--destination=release>github,nuget>http://localhost:5555/v3/index.json",
"--build-version", SemVer.Random.ToString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public async Task Run()
// Act
var returnCode = await ExecuteAsync(
"run",
"--print-plan=true",
"--build-version", version);

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void SetUp()
Inject<IDestinationParser>(new DestinationParser(new Defaults()));
}

[CommandVerb("A")]
[Command("A", "B")]
public class CommandA : ICommand
{
// ReSharper disable once UnusedMember.Global
Expand Down
4 changes: 3 additions & 1 deletion Source/Bake/Commands/Apply/ApplyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

namespace Bake.Commands.Apply
{
[CommandVerb("apply")]
[Command(
"apply",
"Takes an existing plan and executes it")]
public class ApplyCommand : ICommand
{
private readonly ILogger<ApplyCommand> _logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,27 @@

namespace Bake.Commands
{
public class CommandVerbAttribute : Attribute
public class CommandAttribute : Attribute
{
public string Name { get; }
public string Description { get; }

public CommandVerbAttribute(
string name)
public CommandAttribute(
string name,
string description)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}

if (string.IsNullOrEmpty(description))
{
throw new ArgumentNullException(nameof(description));
}

Name = name;
Description = description;
}
}
}
16 changes: 10 additions & 6 deletions Source/Bake/Commands/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ public CommandLineApplication Create(IEnumerable<Type> types)
.ToArray();
}));

app.Description =
app.Description =
@"Bake is a convention based build tool that focuses on minimal to none
effort to configure and setup. Ideally you should be able to run bake in
any repository with minimal arguments and get the ""expected"" output or
better. This however comes at the cost of conventions and how well Bake
works on a project all depends on how many of the conventions that
project follows.";
project follows.
https://github.com/rasmus/Bake";
app.OnExecute(() =>
{
Console.WriteLine(app.GetHelpText());
Expand All @@ -143,10 +145,10 @@ works on a project all depends on how many of the conventions that
$"Type '{type.PrettyPrint()}' is not of type '{commandType.PrettyPrint()}'");
}

var verb = type.GetCustomAttribute<CommandVerbAttribute>()?.Name;
if (string.IsNullOrEmpty(verb))
var commandAttribute = type.GetCustomAttribute<CommandAttribute>();
if (commandAttribute == null)
{
throw new ArgumentException($"Type '{type.PrettyPrint()}' does not have a verb");
throw new ArgumentException($"Type '{type.PrettyPrint()}' does not have a command attribute");
}

var methodInfos = type
Expand All @@ -162,7 +164,7 @@ works on a project all depends on how many of the conventions that

var methodInfo = methodInfos.Single();

app.Command(verb, cmd =>
var command = app.Command(commandAttribute.Name, cmd =>
{
var options = new List<(CommandOption, Type)>();
foreach (var parameterInfo in methodInfo.GetParameters())
Expand Down Expand Up @@ -225,6 +227,8 @@ works on a project all depends on how many of the conventions that
}
});
});

command.Description = commandAttribute.Description;
}

return app;
Expand Down
4 changes: 3 additions & 1 deletion Source/Bake/Commands/Plan/PlanCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

namespace Bake.Commands.Plan
{
[CommandVerb("plan")]
[Command(
"plan",
"Analyzes the current directory and builds a plan")]
public class PlanCommand : ICommand
{
private readonly ILogger<PlanCommand> _logger;
Expand Down
6 changes: 4 additions & 2 deletions Source/Bake/Commands/Run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

namespace Bake.Commands.Run
{
[CommandVerb("run")]
[Command(
"run",
"Analyzes the current directory, builds a plan and then executes the plan")]
public class RunCommand : ICommand
{
private readonly IEditor _editor;
Expand All @@ -61,7 +63,7 @@ public async Task<int> ExecuteAsync(
Convention convention = Convention.Default,
Destination[] destination = null,
LogEventLevel logLevel = LogEventLevel.Information,
bool printPlan = false)
bool printPlan = true)
{
_logCollector.LogLevel = logLevel;

Expand Down
21 changes: 18 additions & 3 deletions Source/Bake/Cooking/Composers/DockerComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using Bake.ValueObjects.Destinations;
using Bake.ValueObjects.Recipes;
using Bake.ValueObjects.Recipes.Docker;
using Microsoft.Extensions.Logging;

namespace Bake.Cooking.Composers
{
Expand All @@ -46,15 +47,18 @@ public class DockerComposer : Composer
ArtifactType.Dockerfile,
};

private readonly ILogger<DockerComposer> _logger;
private readonly IFileSystem _fileSystem;
private readonly IContainerTagParser _containerTagParser;
private readonly IConventionInterpreter _conventionInterpreter;

public DockerComposer(
ILogger<DockerComposer> logger,
IFileSystem fileSystem,
IContainerTagParser containerTagParser,
IConventionInterpreter conventionInterpreter)
{
_logger = logger;
_fileSystem = fileSystem;
_containerTagParser = containerTagParser;
_conventionInterpreter = conventionInterpreter;
Expand All @@ -79,15 +83,26 @@ public override async Task<IReadOnlyCollection<Recipe>> ComposeAsync(

foreach (var dockerFilePath in dockerFilePaths)
{
_logger.LogInformation(
"Located Dockerfile at these locations, scheduling build: {DockerFiles}",
dockerFilePaths);
var directoryName = Path.GetFileName(Path.GetDirectoryName(dockerFilePath));
recipes.AddRange(CreateRecipes(dockerFilePath, directoryName, ingredients.Version, urls));
}

foreach (var fileArtifact in context
var dockerfileArtifacts = context
.GetArtifacts<FileArtifact>()
.Where(a => a.Key.Type == ArtifactType.Dockerfile))
.Where(a => a.Key.Type == ArtifactType.Dockerfile)
.ToArray();
if (dockerfileArtifacts.Any())
{
recipes.AddRange(CreateRecipes(fileArtifact.Path, fileArtifact.Key.Name, ingredients.Version, urls));
_logger.LogInformation(
"Other build steps will produce Dockerfile at these locations, scheduling build: {DockerFiles}",
dockerfileArtifacts.Select(a => a.Path).ToList());
foreach (var dockerfileArtifact in dockerfileArtifacts)
{
recipes.AddRange(CreateRecipes(dockerfileArtifact.Path, dockerfileArtifact.Key.Name, ingredients.Version, urls));
}
}

if (_conventionInterpreter.ShouldArtifactsBePublished(ingredients.Convention))
Expand Down
6 changes: 6 additions & 0 deletions Source/Bake/Cooking/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public async Task<Book> ComposeAsync(
Context context,
CancellationToken cancellationToken)
{
_logger.LogInformation(
"Starting compose for {Convention} run in {WorkingDirectory} with initial version as {Version}",
context.Ingredients.Convention,
context.Ingredients.WorkingDirectory,
context.Ingredients.Version);

await Task.WhenAll(_gathers.Select(g => g.GatherAsync(context.Ingredients, cancellationToken)));

var recipes = new List<Recipe>();
Expand Down

0 comments on commit 4d79e4e

Please sign in to comment.