Skip to content

Commit 3e1fbd8

Browse files
Add advanced log methods
1 parent e6c1349 commit 3e1fbd8

File tree

75 files changed

+477
-186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+477
-186
lines changed

Build/Program.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
GetNextNuGetVersion(new NuGetRestoreSettings(packageId), defaultVersion)
3838
}.Max()!;
3939

40-
Info(packageVersion.ToString());
41-
4240
const int minSdk = 6;
4341
var maxSdk = minSdk;
4442
new DotNet().WithVersion(true)
@@ -49,8 +47,8 @@
4947
var frameworks = string.Join(";", allFrameworks);
5048
var framework = $"net{maxSdk}.0";
5149

52-
Info($"frameworks: {frameworks}");
53-
Info($"framework: {framework}");
50+
Summary("Frameworks: ".WithColor(Color.Header), frameworks.WithColor(Color.Details));
51+
Summary("Framework: ".WithColor(Color.Header), framework.WithColor(Color.Details));
5452

5553
var packages = new[]
5654
{
@@ -267,9 +265,8 @@
267265
Info("Pushing NuGet packages was skipped.");
268266
}
269267

270-
Info($"Tool and package version: {packageVersion}");
271-
Info($"Template version: {packageVersion}");
272-
Info($"The coverage percentage: {coveragePercentage}");
268+
Summary("Package version: ".WithColor(Color.Header), packageVersion.WithColor(Color.Details));
269+
Summary("Coverage percentage: ".WithColor(Color.Header), coveragePercentage.WithColor(Color.Details));
273270

274271
return 0;
275272

CSharpInteractive.HostApi/Color.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace HostApi;
88
/// </code>
99
/// </example>
1010
/// </summary>
11-
/// <seealso cref="IHost.WriteLine"/>
11+
/// <seealso cref="IHost.WriteLine()"/>
1212
public enum Color
1313
{
1414
/// <summary>

CSharpInteractive.HostApi/IHost.cs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ public interface IHost
6464
/// <typeparam name="T">The type of the value to be converted to a line.</typeparam>
6565
void WriteLine<T>(T line, Color color = Color.Default);
6666

67+
/// <summary>
68+
/// Writes a line to stdOut.
69+
/// <example>
70+
/// <code>
71+
/// WriteLine("Hello !".WithColor(Color.Highlighted));
72+
/// </code>
73+
/// </example>
74+
/// </summary>
75+
/// <param name="line">Any value that will be converted to a line.</param>
76+
void WriteLine(params Text[] line);
77+
6778
/// <summary>
6879
/// Writes an error to stdErr. This error will affect the summary run statistics.
6980
/// <example>
@@ -77,6 +88,29 @@ public interface IHost
7788
/// <param name="errorId">Unique error identifier, optional.</param>
7889
void Error(string? error, string? errorId = null);
7990

91+
/// <summary>
92+
/// Writes an error to stdErr. This error will affect the summary run statistics.
93+
/// <example>
94+
/// <code>
95+
/// Error(new Text("Some "), new Text("error", Color.Error));
96+
/// </code>
97+
/// </example>
98+
/// </summary>
99+
/// <param name="error">Error message.</param>
100+
void Error(params Text[] error);
101+
102+
/// <summary>
103+
/// Writes an error to stdErr. This error will affect the summary run statistics.
104+
/// <example>
105+
/// <code>
106+
/// Error("ERR327", new Text("Some "), new Text("error", Color.Error));
107+
/// </code>
108+
/// </example>
109+
/// </summary>
110+
/// <param name="errorId">Unique error identifier, optional.</param>
111+
/// <param name="error">Error message.</param>
112+
void Error(string errorId, params Text[] error);
113+
80114
/// <summary>
81115
/// Writes a warning to stdOut. This warning will affect the summary run statistics.
82116
/// <example>
@@ -88,17 +122,39 @@ public interface IHost
88122
/// <param name="warning">Warning message.</param>
89123
void Warning(string? warning);
90124

125+
/// <summary>
126+
/// Writes a warning to stdOut. This warning will affect the summary run statistics.
127+
/// <example>
128+
/// <code>
129+
/// Warning(new Text("Some "), new Text("warning", Color.Warning));
130+
/// </code>
131+
/// </example>
132+
/// </summary>
133+
/// <param name="warning">Warning message.</param>
134+
void Warning(params Text[] warning);
135+
91136
/// <summary>
92137
/// Writes a summary message to stdOut.
93138
/// <example>
94139
/// <code>
95-
/// Info("Some info");
140+
/// Info("Some summary");
96141
/// </code>
97142
/// </example>
98143
/// </summary>
99144
/// <param name="summary">Summary message.</param>
100145
void Summary(string? summary);
101146

147+
/// <summary>
148+
/// Writes a summary message to stdOut.
149+
/// <example>
150+
/// <code>
151+
/// Summary(new Text("Some "), new Text("summary", Color.Highlighted));
152+
/// </code>
153+
/// </example>
154+
/// </summary>
155+
/// <param name="summary">Summary message.</param>
156+
void Summary(params Text[] summary);
157+
102158
/// <summary>
103159
/// Writes an information message to stdOut.
104160
/// <example>
@@ -110,6 +166,17 @@ public interface IHost
110166
/// <param name="text">Information message.</param>
111167
void Info(string? text);
112168

169+
/// <summary>
170+
/// Writes an information message to stdOut.
171+
/// <example>
172+
/// <code>
173+
/// Ingo(new Text("Some "), new Text("info", Color.Highlighted));
174+
/// </code>
175+
/// </example>
176+
/// </summary>
177+
/// <param name="text">Information message.</param>
178+
void Info(params Text[] text);
179+
113180
/// <summary>
114181
/// Writes a trace message to stdOut for the appropriate logging level.
115182
/// <example>
@@ -129,6 +196,24 @@ public interface IHost
129196
/// <param name="origin">Source of the trace message, optional.</param>
130197
void Trace(string? trace, string? origin = null);
131198

199+
/// <summary>
200+
/// Writes a trace message to stdOut for the appropriate logging level.
201+
/// <example>
202+
/// <code>
203+
/// Trace(new Text("Trace message", Color.Details));
204+
/// </code>
205+
/// </example>
206+
/// <example>
207+
/// When run as a script:
208+
/// <code>
209+
/// #l Diagnostic
210+
/// Trace("Tracing ", "details".WithColor(Color.Details));
211+
/// </code>
212+
/// </example>
213+
/// </summary>
214+
/// <param name="trace">Trace message.</param>
215+
void Trace(params Text[] trace);
216+
132217
/// <summary>
133218
/// Provides an instance of a service by its type.
134219
/// <example>

CSharpInteractive/Core/Text.cs renamed to CSharpInteractive.HostApi/Text.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1-
namespace CSharpInteractive.Core;
1+
namespace HostApi;
22

33
using System.Text;
4-
using HostApi;
54

5+
/// <summary>
6+
/// Represents text with a color.
7+
/// </summary>
8+
/// <param name="Value">Text.</param>
9+
/// <param name="Color">Color of text.</param>
610
[ExcludeFromCodeCoverage]
7-
internal readonly record struct Text(string Value, Color Color)
11+
[Target]
12+
public readonly record struct Text(string Value = "", Color Color = Color.Default)
813
{
914
// ReSharper disable once UnusedMember.Global
1015
public static readonly Text Empty = new(string.Empty);
11-
public static readonly Text NewLine = new(System.Environment.NewLine);
16+
public static readonly Text NewLine = new(Environment.NewLine);
1217
public static readonly Text Space = new(" ");
1318
public static readonly Text Tab = new(" ");
1419

20+
/// <summary>
21+
/// Creates text with the default color.
22+
/// </summary>
23+
/// <param name="value">Text.</param>
1524
public Text(string value)
1625
// ReSharper disable once IntroduceOptionalParameters.Global
1726
: this(value, Color.Default)
1827
{ }
1928

2029
public static implicit operator Text[](Text text) => [text];
2130

31+
public static implicit operator Text (string text) => new(text);
32+
2233
public override string ToString()
2334
{
2435
var sb = new StringBuilder();
@@ -57,4 +68,8 @@ public override string ToString()
5768
Array.Copy(text, 0, newText, 1, text.Length);
5869
return newText;
5970
}
71+
72+
public bool Equals(Text other) => Value == other.Value && Color == other.Color;
73+
74+
public override int GetHashCode() => Value.GetHashCode() ^ 33 + (int)Color;
6075
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace HostApi;
2+
3+
/// <summary>
4+
/// Text extensions.
5+
/// </summary>
6+
public static partial class TextExtensions
7+
{
8+
/// <summary>
9+
/// Converts an object to text with color.
10+
/// </summary>
11+
/// <param name="it">The object to convert.</param>
12+
/// <param name="color">Text color.</param>
13+
/// <returns></returns>
14+
public static Text WithColor<T>(this T it, Color color = Color.Default) => new(it?.ToString() ?? "", color);
15+
}

CSharpInteractive.Tests/HostServiceTests.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void ShouldSendError()
7171
host.Error("Err", "Id");
7272

7373
// Then
74-
_log.Verify(i => i.Error(new ErrorId("Id"), It.Is<Text[]>(text => text.Length == 1 && text[0].Value == "Err" && text[0].Color == Color.Error)));
74+
_log.Verify(i => i.Error(new ErrorId("Id"), "Err"));
7575
}
7676

7777
[Fact]
@@ -84,7 +84,20 @@ public void ShouldSendWarning()
8484
host.Warning("Warn");
8585

8686
// Then
87-
_log.Verify(i => i.Warning(It.Is<Text[]>(text => text.Length == 1 && text[0].Value == "Warn" && text[0].Color == Color.Warning)));
87+
_log.Verify(i => i.Warning("Warn"));
88+
}
89+
90+
[Fact]
91+
public void ShouldSendSummary()
92+
{
93+
// Given
94+
var host = CreateInstance();
95+
96+
// When
97+
host.Summary("Summary message");
98+
99+
// Then
100+
_log.Verify(i => i.Summary("Summary message"));
88101
}
89102

90103
[Fact]
@@ -97,7 +110,7 @@ public void ShouldSendInfo()
97110
host.Info("Info");
98111

99112
// Then
100-
_log.Verify(i => i.Info(It.Is<Text[]>(text => text.Length == 1 && text[0].Value == "Info" && text[0].Color == Color.Default)));
113+
_log.Verify(i => i.Info("Info"));
101114
}
102115

103116
[Fact]

CSharpInteractive.Tests/ProcessOutputWriterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void ShouldWriteStdErr()
3131
writer.Write(new Output(Mock.Of<IStartInfo>(), true, "Err", 11));
3232

3333
// Then
34-
_console.Verify(i => i.WriteToErr("Err", Environment.NewLine));
34+
_console.Verify(i => i.WriteToErr(new ValueTuple<ConsoleColor?, string>(null, "Err"), new ValueTuple<ConsoleColor?, string>(null, Environment.NewLine)));
3535
}
3636

3737
private ProcessOutputWriter CreateInstance() =>

CSharpInteractive.Tests/ProgramTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public ProgramTests()
3030
_active = new Mock<IActive>();
3131
_active.Setup(i => i.Activate()).Returns(_activationToken.Object);
3232
_statistics = new Mock<IStatistics>();
33-
_statistics.SetupGet(i => i.Errors).Returns(Array.Empty<Text[]>());
33+
_statistics.SetupGet(i => i.Items).Returns(Array.Empty<StatisticsItem>());
3434
}
3535

3636
[Fact]
@@ -107,7 +107,7 @@ public void ShouldFailedWhenHasErrors()
107107
var program = CreateInstance();
108108

109109
// When
110-
_statistics.SetupGet(i => i.Errors).Returns([new Text("some error")]);
110+
_statistics.SetupGet(i => i.Items).Returns([new StatisticsItem(StatisticsType.Error, new Text("some error"))]);
111111
var actualResult = program.Run();
112112

113113
// Then

CSharpInteractive.Tests/README_TEMPLATE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ WriteLine("Hello");
117117

118118
``` CSharp
119119
WriteLine("Hello", Header);
120+
WriteLine("Hello ".WithColor(Header), "world!");
120121
```
121122

122123
### Writing an empty line to a build log
@@ -128,31 +129,37 @@ WriteLine();
128129
### Registering errors in the build log
129130

130131
``` CSharp
132+
Error("Error info");
131133
Error("Error info", "Error identifier");
134+
Error("Error: ".WithColor(), "datails".WithColor(Color.Details));
132135
```
133136

134137
### Registering warnings in the build log
135138

136139
``` CSharp
137140
Warning("Warning info");
141+
Warning("Warning ", "info".WithColor(Color.Details));
138142
```
139143

140144
### Registering a summary in the build log
141145

142146
``` CSharp
143147
Summary("Summary message");
148+
Summary("Summary ", "message".WithColor(Color.Details));
144149
```
145150

146151
### Registering information in the build log
147152

148153
``` CSharp
149154
Info("Some info");
155+
Info("Some ", "info".WithColor(Color.Details));
150156
```
151157

152158
### Registering trace information in the build log
153159

154160
``` CSharp
155161
Trace("Some trace info");
162+
Trace("Some trace ", "info".WithColor(Color.Details));
156163
```
157164

158165
## Arguments and parameters

CSharpInteractive.Tests/ScriptRunnerTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ internal void ShouldRun(CommandResult[] results, string[] errors, string[] warni
3030
// Given
3131
var runner = CreateInstance();
3232
_commandsRunner.Setup(i => i.Run(It.IsAny<IEnumerable<ICommand>>())).Returns(results);
33-
_statistics.SetupGet(i => i.Errors).Returns(errors.Select(i => new Text[] {new(i)}).ToArray);
34-
_statistics.SetupGet(i => i.Warnings).Returns(warnings.Select(i => new Text[] {new(i)}).ToArray);
33+
_statistics.SetupGet(i => i.Items).Returns(errors.Select(i => new StatisticsItem(StatisticsType.Error, [new Text(i)])).Concat(warnings.Select(i => new StatisticsItem(StatisticsType.Warning, [new Text(i)]))).ToArray);
3534

3635
// When
3736
var actualExitCode = runner.Run();
@@ -120,7 +119,7 @@ public void ShouldShowErrorWhenScriptIsUncompleted()
120119
var runner = CreateInstance();
121120
//_log.Setup(i => i.Error(ErrorId.UncompletedScript, It.IsAny<string>()));
122121
_commandSource.Setup(i => i.GetCommands()).Returns([new ScriptCommand(string.Empty, string.Empty), new CodeCommand()]);
123-
_statistics.Setup(i => i.Errors).Returns(new List<Text[]>());
122+
_statistics.Setup(i => i.Items).Returns(new List<StatisticsItem>());
124123
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
125124
_commandsRunner.Setup(i => i.Run(It.IsAny<IEnumerable<ICommand>>())).Callback<IEnumerable<ICommand>>(i => i.Count()).Returns([new CommandResult(new ScriptCommand(string.Empty, string.Empty), true)]);
126125

0 commit comments

Comments
 (0)