Skip to content

Commit 234578d

Browse files
Refactor codebase to improve readability, remove unused variables, and optimize for modern features such as list and array shorthand syntax.
1 parent 7c059fc commit 234578d

File tree

72 files changed

+473
-387
lines changed

Some content is hidden

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

72 files changed

+473
-387
lines changed

Build/Tools.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Xml;
21
using HostApi;
32
using NuGet.Versioning;
43
// ReSharper disable CheckNamespace

CSharpInteractive.HostApi/CommandLine.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,31 @@ public partial record CommandLine(
3535
string ShortName = "")
3636
: IStartInfo
3737
{
38-
private readonly string _shortName = ShortName;
3938

4039
/// <summary>
4140
/// Creates a new command line.
4241
/// </summary>
4342
/// <param name="executablePath">Path to the executable file.</param>
4443
/// <param name="args">Command line arguments.</param>
4544
public CommandLine(string executablePath, params string[] args)
46-
: this(executablePath, string.Empty, args, Array.Empty<(string name, string value)>())
45+
: this(executablePath, string.Empty, args, [])
4746
{ }
4847

4948
internal CommandLine(IStartInfo startInfo)
5049
: this(startInfo.ExecutablePath, startInfo.WorkingDirectory, startInfo.Args, startInfo.Vars, startInfo.ShortName)
5150
{ }
5251

5352
/// <inheritdoc/>
54-
public string ShortName =>
55-
!string.IsNullOrWhiteSpace(_shortName)
56-
? _shortName
53+
public string ShortName
54+
{
55+
get => !string.IsNullOrWhiteSpace(field)
56+
? field
5757
: Path.GetFileNameWithoutExtension(ExecutablePath);
58+
} = ShortName;
5859

5960
/// <inheritdoc/>
60-
public IStartInfo GetStartInfo(IHost host)
61-
{
62-
if (host == null) throw new ArgumentNullException(nameof(host));
63-
return this;
64-
}
61+
public IStartInfo GetStartInfo(IHost host) =>
62+
host != null ? this : throw new ArgumentNullException(nameof(host));
6563

6664
/// <inheritdoc/>
6765
public override string ToString()

CSharpInteractive.HostApi/CommandLineTools.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ReSharper disable UnusedMember.Global
12
namespace HostApi;
23

34
/// <summary>
@@ -46,10 +47,7 @@ public static ICommandLine Customize(this ICommandLine baseCommandLine, Func<Com
4647

4748
private class CustomCommandLine(ICommandLine baseCommandLine, Func<CommandLine, ICommandLine> customizer) : ICommandLine
4849
{
49-
public IStartInfo GetStartInfo(IHost host)
50-
{
51-
if (host == null) throw new ArgumentNullException(nameof(host));
52-
return customizer(new CommandLine(baseCommandLine.GetStartInfo(host))).GetStartInfo(host);
53-
}
50+
public IStartInfo GetStartInfo(IHost host) =>
51+
host != null ? customizer(new CommandLine(baseCommandLine.GetStartInfo(host))).GetStartInfo(host) : throw new ArgumentNullException(nameof(host));
5452
}
5553
}

CSharpInteractive.HostApi/DotNetBlameDumpType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace HostApi;
1+
// ReSharper disable UnusedMember.Global
2+
namespace HostApi;
23

34
/// <summary>
45
/// The type of dump.

CSharpInteractive.HostApi/DotNetBuildServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public enum DotNetBuildServer
2525
Razor
2626
}
2727

28+
// ReSharper disable once UnusedType.Global
2829
internal static class DotNetBuildServerExtensions
2930
{
3031
[SuppressMessage("ReSharper", "UnusedParameter.Global")]

CSharpInteractive.HostApi/DotNetCommands.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,7 +3643,7 @@ public IStartInfo GetStartInfo(IHost host)
36433643
///<code>
36443644
/// using HostApi;
36453645
///
3646-
/// string? repositoryPath = default;
3646+
/// string? repositoryPath = null;
36473647
/// new DotNetNuGetConfigGet()
36483648
/// .WithConfigKey("repositoryPath")
36493649
/// .Run(output =&gt; repositoryPath = output.Line).EnsureSuccess();
@@ -4749,6 +4749,7 @@ public IStartInfo GetStartInfo(IHost host)
47494749
/// .WithTemplateName("sln")
47504750
/// .WithName("MySolution")
47514751
/// .WithForce(true)
4752+
/// .AddArgs("--format", "sln")
47524753
/// .Run().EnsureSuccess();
47534754
///
47544755
/// new DotNetSlnAdd()
@@ -5110,9 +5111,7 @@ public DotNetTest()
51105111
public IStartInfo GetStartInfo(IHost host)
51115112
{
51125113
if (host == null) throw new ArgumentNullException(nameof(host));
5113-
var components = host.GetService<HostComponents>();
5114-
var virtualContext = components.VirtualContext;
5115-
var settings = components.DotNetSettings;
5114+
var (_, settings, _, virtualContext) = host.GetService<HostComponents>();
51165115
return host.CreateCommandLine(ExecutablePath)
51175116
.WithShortName(ToString())
51185117
.WithWorkingDirectory(WorkingDirectory)

CSharpInteractive.HostApi/DotNetCommands.tt

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ using Internal;
357357
includeGeneratedArg,
358358
verbosityArg,
359359
binaryLogArg,
360-
reportArg,
360+
reportArg
361361
]
362362
),
363363
new(
@@ -382,7 +382,7 @@ using Internal;
382382
includeGeneratedArg,
383383
verbosityArg,
384384
binaryLogArg,
385-
reportArg,
385+
reportArg
386386
]
387387
),
388388
new(
@@ -406,7 +406,7 @@ using Internal;
406406
includeGeneratedArg,
407407
verbosityArg,
408408
binaryLogArg,
409-
reportArg,
409+
reportArg
410410
]
411411
),
412412
new(
@@ -1616,16 +1616,15 @@ using Internal;
16161616
)
16171617
};
16181618

1619-
foreach (var command in commands)
1619+
foreach (var (name, description, comments, strings, args, commandTypes, additionalArgs) in commands)
16201620
{
1621-
var name = command.Name;
16221621
var commandArgs = new List<string>();
1623-
foreach (var arg in command.CommandArgs)
1622+
foreach (var arg in strings)
16241623
{
16251624
if (arg.StartsWith("$"))
16261625
{
16271626
var curArg = arg.Substring(1);
1628-
var notEmpty = command.Args.FirstOrDefault(i => i.PropertyName == curArg && i.IsCollection);
1627+
var notEmpty = args.FirstOrDefault(i => i.PropertyName == curArg && i.IsCollection);
16291628
if (notEmpty is not null && !string.IsNullOrWhiteSpace(notEmpty.PropertyName))
16301629
{
16311630
var compositeArg = $"new [] {{{string.Join(", ", commandArgs)}}}.Concat({curArg}).ToArray()";
@@ -1645,16 +1644,16 @@ using Internal;
16451644
#>
16461645

16471646
/// <summary>
1648-
/// <#= command.Description #>
1647+
/// <#= description #>
16491648
<#
1650-
foreach (var comment in command.Comments)
1649+
foreach (var comment in comments)
16511650
{
16521651
#>
16531652
/// <#= comment #>
16541653
<#
16551654
}
16561655

1657-
var exampleFile = Path.Combine("..", "CSharpInteractive.Tests", "UsageScenarios", "Comments", $"{command.Name}Scenario.txt");
1656+
var exampleFile = Path.Combine("..", "CSharpInteractive.Tests", "UsageScenarios", "Comments", $"{name}Scenario.txt");
16581657
if (File.Exists(exampleFile))
16591658
{
16601659
var exampleLines = File.ReadAllLines(exampleFile);
@@ -1685,7 +1684,7 @@ using Internal;
16851684
/// <param name="Args">Specifies the set of command line arguments to use when starting the tool.</param>
16861685
/// <param name="Vars">Specifies the set of environment variables that apply to this process and its child processes.</param>
16871686
<#
1688-
foreach (var arg in command.Args.Where(i => i.IsCollection))
1687+
foreach (var arg in args.Where(i => i.IsCollection))
16891688
{
16901689
#>
16911690
/// <param name="<#= arg.PropertyName #>"><#= arg.Comments #></param>
@@ -1695,7 +1694,7 @@ using Internal;
16951694
/// <param name="ExecutablePath">Overrides the tool executable path.</param>
16961695
/// <param name="WorkingDirectory">Specifies the working directory for the tool to be started.</param>
16971696
<#
1698-
foreach (var arg in command.Args.Where(i => !i.IsCollection))
1697+
foreach (var arg in args.Where(i => !i.IsCollection))
16991698
{
17001699
#>
17011700
/// <param name="<#= arg.PropertyName #>"><#= arg.Comments #></param>
@@ -1704,13 +1703,13 @@ using Internal;
17041703
#>
17051704
/// <param name="ShortName">Specifies a short name for this operation.</param>
17061705
[Target]
1707-
public partial record <#= command.Name #>(
1706+
public partial record <#= name #>(
17081707
IEnumerable<string> Args,
17091708
IEnumerable<(string name, string value)> Vars,
17101709
<#
1711-
var initializer = string.Join(", ", new[] {"args", "[]"}.Concat(Enumerable.Repeat("[]", command.Args.Count(i => i.IsCollection))));
1712-
var fullInitializer = string.Join(", ", new[] {"[]", "[]"}.Concat(Enumerable.Repeat("[]", command.Args.Count(i => i.IsCollection))));
1713-
foreach (var arg in command.Args)
1710+
var initializer = string.Join(", ", new[] {"args", "[]"}.Concat(Enumerable.Repeat("[]", args.Count(i => i.IsCollection))));
1711+
var fullInitializer = string.Join(", ", new[] {"[]", "[]"}.Concat(Enumerable.Repeat("[]", args.Count(i => i.IsCollection))));
1712+
foreach (var arg in args)
17141713
{
17151714
var type = arg.Type;
17161715
var defaultValue = "";
@@ -1728,7 +1727,7 @@ public partial record <#= command.Name #>(
17281727
{
17291728
if (!arg.IsCollection)
17301729
{
1731-
defaultValue = " = default";
1730+
defaultValue = " = null";
17321731
}
17331732
}
17341733
}
@@ -1763,7 +1762,7 @@ public partial record <#= command.Name #>(
17631762
{
17641763
if (host == null) throw new ArgumentNullException(nameof(host));
17651764
<#
1766-
if ((command.CommandTypes & CommandTypes.RequiresSettings) == CommandTypes.RequiresSettings)
1765+
if ((commandTypes & CommandTypes.RequiresSettings) == CommandTypes.RequiresSettings)
17671766
{
17681767
#>
17691768
var components = host.GetService<HostComponents>();
@@ -1777,12 +1776,12 @@ public partial record <#= command.Name #>(
17771776
.WithWorkingDirectory(WorkingDirectory)
17781777
.WithVars(Vars.ToArray())
17791778
<#
1780-
foreach (var arg in command.CommandArgs)
1779+
foreach (var arg in strings)
17811780
{
17821781
if (arg.StartsWith("$"))
17831782
{
17841783
var notEmptyArg = arg.Substring(1);
1785-
var notEmpty = command.Args.FirstOrDefault(i => i.PropertyName == notEmptyArg && i.IsCollection);
1784+
var notEmpty = args.FirstOrDefault(i => i.PropertyName == notEmptyArg && i.IsCollection);
17861785
if (notEmpty is not null && !string.IsNullOrWhiteSpace(notEmpty.PropertyName))
17871786
{
17881787
notEmptyArg = $"{notEmptyArg}.ToArray()";
@@ -1804,35 +1803,35 @@ public partial record <#= command.Name #>(
18041803
}
18051804
#>
18061805
<#
1807-
if ((command.CommandTypes & CommandTypes.Build) == CommandTypes.Build)
1806+
if ((commandTypes & CommandTypes.Build) == CommandTypes.Build)
18081807
{
18091808
#>
18101809
.AddMSBuildLoggers(host, Verbosity)
18111810
<#
18121811
}
18131812

1814-
if ((command.CommandTypes & CommandTypes.Test) == CommandTypes.Test)
1813+
if ((commandTypes & CommandTypes.Test) == CommandTypes.Test)
18151814
{
18161815
#>
18171816
.AddTestLoggers(host, Loggers)
18181817
<#
18191818
}
18201819

1821-
foreach (var additionalArg in command.AdditionalArgs)
1820+
foreach (var additionalArg in additionalArgs)
18221821
{
18231822
#>
18241823
.AddArgs("<#= additionalArg #>")
18251824
<#
18261825
}
18271826

1828-
foreach (var arg in command.Args.Where(i => !i.IsProject).Where(i => i.IsCollection && i.ArgName != "--property"))
1827+
foreach (var arg in args.Where(i => !i.IsProject).Where(i => i.IsCollection && i.ArgName != "--property"))
18291828
{
18301829
#>
18311830
.AddArgs(<#= arg.PropertyName #>.ToArgs("<#= arg.ArgName #>", "<#= arg.Separator #>"))
18321831
<#
18331832
}
18341833

1835-
foreach (var arg in command.Args.Where(i => !i.IsProject).Where(i => !string.IsNullOrWhiteSpace(i.ArgName) && !i.Type.StartsWith("bool") && !i.IsCollection))
1834+
foreach (var arg in args.Where(i => !i.IsProject).Where(i => !string.IsNullOrWhiteSpace(i.ArgName) && !i.Type.StartsWith("bool") && !i.IsCollection))
18361835
{
18371836
if (string.IsNullOrWhiteSpace(arg.AddArgOverride))
18381837
{
@@ -1848,7 +1847,7 @@ public partial record <#= command.Name #>(
18481847
}
18491848
}
18501849

1851-
var boolArgs = command.Args.Where(i => !string.IsNullOrWhiteSpace(i.ArgName) && i.Type.StartsWith("bool")).ToArray();
1850+
var boolArgs = args.Where(i => !string.IsNullOrWhiteSpace(i.ArgName) && i.Type.StartsWith("bool")).ToArray();
18521851
#><#= boolArgs.Length > 0 ? " .AddBooleanArgs(\n": ""#><#
18531852
for (var i = 0; i < boolArgs.Length; i++)
18541853
{
@@ -1858,7 +1857,7 @@ public partial record <#= command.Name #>(
18581857
<#
18591858
}
18601859
#><#= boolArgs.Length > 0 ? " )\n": ""#><#
1861-
foreach (var arg in command.Args.Where(i => !string.IsNullOrWhiteSpace(i.ArgName) && i.IsCollection && i.ArgName == "--property").ToArray())
1860+
foreach (var arg in args.Where(i => !string.IsNullOrWhiteSpace(i.ArgName) && i.IsCollection && i.ArgName == "--property").ToArray())
18621861
{
18631862
#>
18641863
.AddProps("--property", <#= arg.PropertyName #>.ToArray())
@@ -1869,7 +1868,7 @@ public partial record <#= command.Name #>(
18691868
}
18701869

18711870
/// <inheritdoc/>
1872-
public override string ToString() => (ExecutablePath == string.Empty ? "dotnet" : Path.GetFileNameWithoutExtension(ExecutablePath)).GetShortName("<#= command.Description #>", ShortName, <#= string.Join(", ", commandArgs) #>);
1871+
public override string ToString() => (ExecutablePath == string.Empty ? "dotnet" : Path.GetFileNameWithoutExtension(ExecutablePath)).GetShortName("<#= description #>", ShortName, <#= string.Join(", ", commandArgs) #>);
18731872
}
18741873
<#
18751874
}

CSharpInteractive.HostApi/DotNetFormatSeverity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ public enum DotNetFormatSeverity
2121
Error
2222
}
2323

24+
// ReSharper disable once UnusedType.Global
2425
internal static class DotNetFormatSeverityExtensions
2526
{
2627
// ReSharper disable once UnusedParameter.Global
28+
// ReSharper disable once UnusedMember.Global
2729
public static string[] ToArgs(this DotNetFormatSeverity? severity, string name, string collectionSeparator) =>
2830
severity switch
2931
{

CSharpInteractive.HostApi/DotNetLanguage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ public enum DotNetLanguage
3636
TypeScript
3737
}
3838

39+
// ReSharper disable once UnusedType.Global
3940
internal static class DotNetLanguageExtensions
4041
{
4142
// ReSharper disable once UnusedParameter.Global
43+
// ReSharper disable once UnusedMember.Global
4244
public static string[] ToArgs(this DotNetLanguage? language, string name, string collectionSeparator) =>
4345
language switch
4446
{

CSharpInteractive.HostApi/DotNetNewListColumn.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public enum DotNetNewListColumn
2727
Type
2828
}
2929

30+
// ReSharper disable once UnusedType.Global
3031
internal static class DotNetNewListColumnExtensions
3132
{
3233
// ReSharper disable once UnusedParameter.Global

0 commit comments

Comments
 (0)