Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added attributes to supress build and target banners. #887

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ else {
$env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe"
}

Write-Output "Microsoft (R) .NET Core SDK version $(& $env:DOTNET_EXE --version)"
Write-Output "Microsoft (R) .NET SDK version $(& $env:DOTNET_EXE --version)"

ExecSafe { & $env:DOTNET_EXE build $BuildProjectFile /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary }
ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile --no-build -- $BuildArguments }
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ else
export DOTNET_EXE="$DOTNET_DIRECTORY/dotnet"
fi

echo "Microsoft (R) .NET Core SDK version $("$DOTNET_EXE" --version)"
echo "Microsoft (R) .NET SDK version $("$DOTNET_EXE" --version)"

"$DOTNET_EXE" build "$BUILD_PROJECT_FILE" /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary
"$DOTNET_EXE" run --project "$BUILD_PROJECT_FILE" --no-build -- "$@"
68 changes: 39 additions & 29 deletions source/Nuke.Common/Execution/BuildExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static IReadOnlyCollection<string> UpdateInvocationHash(NukeBuild build)
{
var continueParameterName = ParameterService.GetParameterMemberName(() => build.Continue);
var invocation = EnvironmentInfo.CommandLineArguments
.Where(x => !x.StartsWith("-") || x.TrimStart("-").EqualsOrdinalIgnoreCase(continueParameterName))
.Where(x => !x.StartsWith("-", StringComparison.Ordinal) || x.TrimStart("-").EqualsOrdinalIgnoreCase(continueParameterName))
.JoinSpace();
var invocationHash = invocation.GetMD5Hash();

Expand Down Expand Up @@ -100,38 +100,48 @@ private static void Execute(
return;
}

using (Logging.SetTarget(target.Name))
using (NukeBuild.Host.WriteBlock(target.Name))
{
target.Stopwatch.Start();
target.Status = ExecutionStatus.Running;
build.ExecuteExtension<IOnTargetRunning>(x => x.OnTargetRunning(build, target));
try
{
target.Actions.ForEach(x => x());
target.Stopwatch.Stop();
target.Status = ExecutionStatus.Succeeded;
build.ExecuteExtension<IOnTargetSucceeded>(x => x.OnTargetSucceeded(build, target));

AppendToBuildAttemptFile(target.Name);
}
catch (Exception exception)
{
build.ReportSummary(_ =>
target.SummaryInformation.Any()
? target.SummaryInformation
: _.AddPair(exception.GetType().Name, exception.Message.SplitLineBreaks().First()));
using var loggingTarget = Logging.SetTarget(target.Name);
using var targetBlock = NoTargetBannerAttribute.IsDeclaredOn(target)
? default
: NukeBuild.Host.WriteBlock(target.Name);

Log.Error(exception, "Target {TargetName} failed", target.Name);
target.Stopwatch.Start();
target.Status = ExecutionStatus.Running;
build.ExecuteExtension<IOnTargetRunning>(x => x.OnTargetRunning(build, target));

target.Stopwatch.Stop();
target.Status = ExecutionStatus.Failed;
build.ExecuteExtension<IOnTargetFailed>(x => x.OnTargetFailed(build, target));
try
{
target.Actions.ForEach(x => x());
target.Stopwatch.Stop();
target.Status = ExecutionStatus.Succeeded;
build.ExecuteExtension<IOnTargetSucceeded>(x => x.OnTargetSucceeded(build, target));

if (!target.ProceedAfterFailure && !failureMode)
throw new TargetExecutionException(target.Name, exception);
}
AppendToBuildAttemptFile(target.Name);
}
catch (Exception exception)
{
build.ReportSummary(
_ => target.SummaryInformation.Any()
? target.SummaryInformation
: _.AddPair(
exception.GetType()
.Name,
exception.Message.SplitLineBreaks()
.First()
)
);

Log.Error(exception, "Target {TargetName} failed", target.Name);

target.Stopwatch.Stop();
target.Status = ExecutionStatus.Failed;
build.ExecuteExtension<IOnTargetFailed>(x => x.OnTargetFailed(build, target));

if (!target.ProceedAfterFailure
&& !failureMode)
throw new TargetExecutionException(target.Name, exception);
}

}

private static void MarkSkippedTargets(NukeBuild build, IReadOnlyCollection<string> skippedTargets)
Expand Down
36 changes: 36 additions & 0 deletions source/Nuke.Common/Execution/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
// ReSharper disable CheckNamespace

namespace Nuke.Common
{
Expand All @@ -34,6 +35,11 @@ protected Host()

protected internal void WriteLogo()
{
if (NoBuildBannerAttribute.IsDeclared)
{
return;
}

Debug();
new[]
{
Expand All @@ -49,6 +55,11 @@ protected internal void WriteLogo()

protected internal virtual IDisposable WriteBlock(string text)
{
if (NoBuildBannerAttribute.IsDeclared)
{
return new DelegateDisposable(static () => {});
}

return DelegateDisposable.CreateBracket(
() =>
{
Expand Down Expand Up @@ -79,6 +90,11 @@ protected internal virtual bool FilterMessage(string message)

internal virtual void WriteSummary(NukeBuild build)
{
if (NoBuildBannerAttribute.IsDeclared)
{
return;
}

WriteSevereLogEvents(Logging.InMemorySink.Instance.LogEvents);
WriteSummaryTable(build);

Expand All @@ -90,6 +106,11 @@ internal virtual void WriteSummary(NukeBuild build)

protected virtual void WriteSevereLogEvents(IReadOnlyCollection<LogEvent> instanceLogEvents)
{
if (NoBuildBannerAttribute.IsDeclared)
{
return;
}

if (instanceLogEvents.Count == 0)
return;

Expand All @@ -110,6 +131,11 @@ protected virtual void WriteSevereLogEvents(IReadOnlyCollection<LogEvent> instan

protected virtual void WriteSummaryTable(NukeBuild build)
{
if (NoBuildBannerAttribute.IsDeclared)
{
return;
}

var firstColumn = Math.Max(build.ExecutionPlan.Max(x => x.Name.Length) + 4, val2: 19);
var secondColumn = 10;
var thirdColumn = 10;
Expand Down Expand Up @@ -175,11 +201,21 @@ static string GetInformation(ExecutableTarget target)

protected virtual void WriteSuccessfulBuild(NukeBuild build)
{
if (NoBuildBannerAttribute.IsDeclared)
{
return;
}

Success($"Build succeeded on {DateTime.Now.ToString(CultureInfo.CurrentCulture)}. \(^ᴗ^)/");
}

protected virtual void WriteFailedBuild()
{
if (NoBuildBannerAttribute.IsDeclared)
{
return;
}

Error($"Build failed on {DateTime.Now.ToString(CultureInfo.CurrentCulture)}. (╯°□°)╯︵ ┻━┻");
}

Expand Down
64 changes: 64 additions & 0 deletions source/Nuke.Common/Execution/NoBuildBannerAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

// ReSharper disable TemplateIsNotCompileTimeConstantProblem
// ReSharper disable LoopCanBeConvertedToQuery
// ReSharper disable PossibleMultipleEnumeration
// ReSharper disable AssignNullToNotNullAttribute
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable MemberCanBeInternal

namespace Nuke.Common.Execution;

using System;
using System.Linq;

using Serilog;

[ AttributeUsage(AttributeTargets.Class | AttributeTargets.Module, AllowMultiple = false, Inherited = true) ]
public sealed class NoBuildBannerAttribute : Attribute
{
private static bool? _isDeclared;
public NoBuildBannerAttribute() { }

public static bool IsDeclared => _isDeclared ??= GetIsDeclared();

private static bool GetIsDeclared()
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var result = IsDefined(assembly, typeof(NoBuildBannerAttribute));

if (result)
{
Log.Verbose($"NoBuildBannerAttribute: Found on assembly {assembly.FullName}");

return true;
}

foreach (var type in assembly.GetTypes())
{
if (!type.IsSubclassOf(typeof(NukeBuild)))
{
continue;
}

result = IsDefined(type, typeof(NoBuildBannerAttribute));

if (result)
{
Log.Verbose($"NoBuildBannerAttribute: Found on type {type.FullName}");

return true;
}

Log.Verbose($"NoBuildBannerAttribute: Not found on type {type.FullName}");
}

Log.Verbose($"NoBuildBannerAttribute: Not found on assembly {assembly.FullName}");
}

return false;
}
}
33 changes: 33 additions & 0 deletions source/Nuke.Common/Execution/NoTargetBannerAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE
// ReSharper disable MemberCanBeInternal

namespace Nuke.Common.Execution
{
using System;
using System.Linq;

using JetBrains.Annotations;

using Serilog;

[ AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true) ]
public sealed class NoTargetBannerAttribute : Attribute
{
public NoTargetBannerAttribute() {
}

public static bool IsDeclaredOn([ NotNull ] ExecutableTarget target)
{
var attributes = target.Member.GetCustomAttributes(typeof(NoTargetBannerAttribute), true);

Log.Verbose($"NoTargetBannerAttribute: rtype: {attributes?.Length}");

return attributes is
{
Length: > 0,
};
}
}
}
2 changes: 1 addition & 1 deletion source/Nuke.GlobalTool/templates/build.netcore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ else {
$env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe"
}

Write-Output "Microsoft (R) .NET Core SDK version $(& $env:DOTNET_EXE --version)"
Write-Output "Microsoft (R) .NET SDK version $(& $env:DOTNET_EXE --version)"

ExecSafe { & $env:DOTNET_EXE build $BuildProjectFile /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet }
ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile --no-build -- $BuildArguments }
2 changes: 1 addition & 1 deletion source/Nuke.GlobalTool/templates/build.netcore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ else
export DOTNET_EXE="$DOTNET_DIRECTORY/dotnet"
fi

echo "Microsoft (R) .NET Core SDK version $("$DOTNET_EXE" --version)"
echo "Microsoft (R) .NET SDK version $("$DOTNET_EXE" --version)"

"$DOTNET_EXE" build "$BUILD_PROJECT_FILE" /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet
"$DOTNET_EXE" run --project "$BUILD_PROJECT_FILE" --no-build -- "$@"