Skip to content

Commit

Permalink
feat: Update to System.CommandLine (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminMichaelis authored Nov 30, 2023
1 parent ced82dc commit ca3b3e1
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Polly" Version="8.2.0" />
<PackageVersion Include="System.CommandLine.DragonFruit" Version="0.4.0-alpha.22272.1" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.23407.1" />
<PackageVersion Include="xunit" Version="2.6.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions EssentialCSharp.ListingManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
NuGet.config = NuGet.config
README.md = README.md
EndProjectSection
EndProject
Expand Down
34 changes: 17 additions & 17 deletions ListingManager/EssentialCSharp.ListingManager.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<PackAsTool>true</PackAsTool>
<ToolCommandName>ListingManager</ToolCommandName>
<RepositoryType>git</RepositoryType>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<PackAsTool>true</PackAsTool>
<ToolCommandName>ListingManager</ToolCommandName>
<RepositoryType>git</RepositoryType>
<Deterministic>true</Deterministic>
<PackageTags>IntelliTect, EssentialCSharp, ListingManager</PackageTags>
<Authors>Benjamin Michaelis, IntelliTect</Authors>
<Version>0.1.5</Version>
<PackageId>IntelliTect.EssentialCSharp.ListingManager</PackageId>
<Description>Tool used to expose useful functionality to IntelliTect/EssentialCSharp collaborators</Description>
<NoWarn>CS1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LibGit2Sharp" />
<PackageReference Include="System.CommandLine.DragonFruit" />
</ItemGroup>
<Authors>Benjamin Michaelis, IntelliTect</Authors>
<Version>0.1.5</Version>
<PackageId>IntelliTect.EssentialCSharp.ListingManager</PackageId>
<Description>Tool used to expose useful functionality to IntelliTect/EssentialCSharp collaborators</Description>
<NoWarn>CS1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LibGit2Sharp" />
<PackageReference Include="System.CommandLine" />
</ItemGroup>

</Project>
141 changes: 76 additions & 65 deletions ListingManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,70 @@

namespace EssentialCSharp.ListingManager;

public class Program
public sealed class Program
{
private const string IntelliTect =
@" _____ _ _ _ _______ _
|_ _| | | | | ( )|__ __| | |
| | _ __ | |_ ___| | |_ | | ___ ___| |_
| | | '_ \| __/ _ \ | | | | |/ _ \/ __| __|
_| |_| | | | || __/ | | | | | __/ (__| |_
|_____|_| |_|\__\___|_|_|_| |_|\___|\___|\__|";
private static int Main(string[] args)
private static Task<int> Main(string[] args)
{
var directoryIn = new Option<DirectoryInfo>(
name: "--path",
description: "The directory of the chapter to update listings on.")
{ IsRequired = true };

// With proper logging implemented, this option will hopefully be removed
var verboseOption = new Option<bool>(
name: "--verbose",
description: "Displays more detailed messages in the log");

var previewOption = new Option<bool>(
name: "--preview",
description: "Writes all logs to console as if changes will be made without actually making changes.");

var allChaptersOption = new Option<bool>(
name: "--allChapters",
description: "The passed in path is the parent directory to many chapter directories rather than a single chapter directory.");
CliConfiguration configuration = GetConfiguration();
return configuration.InvokeAsync(args);
}

// TODO: Add better descriptions when their functionality becomes clearer
var byFolderOption = new Option<bool>(
name: "--byfolder",
description: "");
public static CliConfiguration GetConfiguration()
{
// Use the ExistingOnly method to only parse the arguments that are defined in the configuration

var singleDirOption = new Option<bool>(
name: "--singleDir",
description: "All listings are in a single directory and not separated into chapter and chapter test directories");
CliArgument<DirectoryInfo> directoryInArgument = new("directoryIn")
{
Description = "The directory of the chapter to update listings on.",
};
directoryInArgument.AcceptExistingOnly();

var listingUpdating = new Command("update", "Updates namespaces and filenames for all listings and accompanying tests within a chapter")
// With proper logging implemented, this option will hopefully be removed
CliOption<bool> verboseOption = new("--verbose")
{
directoryIn,
verboseOption,
previewOption,
byFolderOption,
singleDirOption,
allChaptersOption
Description = "Displays more detailed messages in the log.",
};

// Give better description when intent and functionality becomes more flushed out
var scan = new Command("scan", "Scans for various things")
CliOption<bool> previewOption = new("--preview")
{
directoryIn
Description = "Displays the changes that will be made without actually making them.",
};

var scanForMismatchedListings = new Command("listings", "Scans for mismatched listings")
CliOption<bool> allChaptersOption = new("--all-chapters")
{
directoryIn
Description = "The passed in path is the parent directory to many chapter directories rather than a single chapter directory.",
};

var scanForMissingTests = new Command("tests", "Scans for missing tests")
// TODO: Add better descriptions when their functionality becomes clearer
CliOption<bool> byFolderOption = new("--by-folder")
{
directoryIn,
allChaptersOption,
singleDirOption
Description = "Updates namespaces and filenames for all listings and accompanying tests within a folder.",
};

scan.AddCommand(scanForMismatchedListings);
scan.AddCommand(scanForMissingTests);
CliOption<bool> singleDirOption = new("--single-dir")
{
Description = "All listings are in a single directory and not separated into chapter and chapter test directories.",
};

var rootCommand = new RootCommand()
CliCommand listingUpdating = new("update", "Updates namespaces and filenames for all listings and accompanying tests within a chapter")
{
listingUpdating,
scan
directoryInArgument,
verboseOption,
previewOption,
byFolderOption,
singleDirOption,
allChaptersOption
};

listingUpdating.SetHandler((directoryIn, verbose, preview, byFolder, singleDir, allChapters) =>
listingUpdating.SetAction((ParseResult parseResult) =>
{
Console.WriteLine(IntelliTect);
DirectoryInfo directoryIn = parseResult.CommandResult.GetValue(directoryInArgument)!;
bool verbose = parseResult.CommandResult.GetValue(verboseOption);
bool preview = parseResult.CommandResult.GetValue(previewOption);
bool byFolder = parseResult.CommandResult.GetValue(byFolderOption);
bool singleDir = parseResult.CommandResult.GetValue(singleDirOption);
bool allChapters = parseResult.CommandResult.GetValue(allChaptersOption);
Console.WriteLine($"Updating listings within: {directoryIn}");
ListingManager listingManager = new(directoryIn);
if (allChapters)
Expand All @@ -90,23 +76,40 @@ private static int Main(string[] args)
{
listingManager.UpdateChapterListingNumbers(directoryIn, verbose, preview, byFolder, singleDir);
}
}, directoryIn, verboseOption, previewOption, byFolderOption, singleDirOption, allChaptersOption);
});

scanForMismatchedListings.SetHandler((directoryIn) =>
CliCommand scan = new("scan", "Scans for various things");

CliCommand listings = new("listings", "Scans for mismatched listings")
{
Console.WriteLine(IntelliTect);
var extraListings = ListingManager.GetAllExtraListings(directoryIn!.FullName).OrderBy(x => x);
directoryInArgument
};

listings.SetAction((ParseResult parseResult) =>
{
DirectoryInfo directoryIn = parseResult.CommandResult.GetValue(directoryInArgument)!;
var extraListings = ListingManager.GetAllExtraListings(directoryIn.FullName).OrderBy(x => x);
Console.WriteLine("---Extra Listings---");
foreach (string extraListing in extraListings)
{
Console.WriteLine(extraListing);
}
}, directoryIn);
});
scan.Subcommands.Add(listings);

scanForMissingTests.SetHandler((directoryIn, allChapters, singleDir) =>
CliCommand tests = new("tests", "Scans for mismatched tests")
{
Console.WriteLine(IntelliTect);
directoryInArgument,
allChaptersOption,
singleDirOption
};

tests.SetAction((ParseResult parseResult) =>
{
DirectoryInfo directoryIn = parseResult.CommandResult.GetValue(directoryInArgument)!;
bool allChapters = parseResult.CommandResult.GetValue(allChaptersOption);
bool singleDir = parseResult.CommandResult.GetValue(singleDirOption);
Console.WriteLine("---Missing Tests---");
if (allChapters)
Expand All @@ -117,8 +120,16 @@ private static int Main(string[] args)
{
ScanManager.ScanForMissingTests(directoryIn, singleDir);
}
}, directoryIn, allChaptersOption, singleDirOption);
});

scan.Subcommands.Add(tests);

CliRootCommand rootCommand = new("The EssentialCSharp.ListingManager helps to organize and manage the EssentialCSharp source code")
{
listingUpdating,
scan
};

return rootCommand.Invoke(args);
return new CliConfiguration(rootCommand);
}
}
18 changes: 18 additions & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="daily" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-libraries/nuget/v3/index.json" />
</packageSources>

<packageSourceMapping>
<packageSource key="daily">
<package pattern="System.CommandLine" />
<package pattern="System.CommandLine.*" />
</packageSource>
<packageSource key="nuget.org">
<package pattern="*" />
</packageSource>
</packageSourceMapping>
</configuration>

0 comments on commit ca3b3e1

Please sign in to comment.