Skip to content

Commit

Permalink
Adding System.CommandLine template (#18)
Browse files Browse the repository at this point in the history
* WIP command line app

* Updating to latest System.CommandLine

* Version 1.3.0

* Updating READMEs
  • Loading branch information
Keboo authored Apr 20, 2023
1 parent 17d5694 commit 735e8f7
Show file tree
Hide file tree
Showing 19 changed files with 767 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ updates:
schedule:
interval: "weekly"

- package-ecosystem: "nuget"
directory: "/templates/console/ConsoleApp"
schedule:
interval: "weekly"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ If you have previously installed the templates and want to install the latest ve
```

## Included Templates
- [Full WPF Project](./templates/WPF/WpfApp/README.md)
- [WPF Solution](./templates/WPF/WpfApp/README.md)
- [NuGet Package Solution](./templates/Library/NuGet/README.md)
- [System.CommandLine Solution](./templates/Console/ConsoleApp/README.md)


# Local testing
Expand Down
2 changes: 1 addition & 1 deletion Templates.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageType>Template</PackageType>
<PackageVersion>1.2.1</PackageVersion>
<PackageVersion>1.3.0</PackageVersion>
<PackageId>Keboo.Dotnet.Templates</PackageId>
<Title>Keboo's .NET Templates</Title>
<Authors>Keboo</Authors>
Expand Down
3 changes: 2 additions & 1 deletion TestLocal.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dotnet new uninstall Keboo.Dotnet.Templates
Remove-Item -Path "Keboo.Dotnet.Templates.*.nupkg"

dotnet pack -o .

dotnet new install Keboo.Dotnet.Templates.1.0.0.nupkg
dotnet new install $(Get-ChildItem -Path "Keboo.Dotnet.Templates.*.nupkg").Name
378 changes: 378 additions & 0 deletions templates/Console/ConsoleApp/.editorconfig

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions templates/Console/ConsoleApp/.template.config/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Keboo",
"classifications": [
"Common",
"CommandLine"
],
"identity": "Keboo.Console.ConsoleApp",
"name": "Keboo CLI Application",
"shortName": "keboo.console",
"tags": {
"language": "C#",
"type": "solution"
},
"preferNameDirectory":true,
"sourceName": "ConsoleApp",
"symbols":{
"createdDate": {
"type": "generated",
"generator": "now",
"parameters": {
"format": "yyyy"
},
"replaces":"1970"
},
"user_secrets_id":{
"type": "generated",
"generator": "guid",
"replaces": "12345678-9abc-0123-4567-890abcdef123",
"parameters": {
"defaultFormat":"d"
}
},
"no-sln": {
"type": "parameter",
"dataType":"bool",
"defaultValue": "false"
},
"no-tests": {
"type": "parameter",
"dataType":"bool",
"defaultValue": "false"
}
},
"sources": [
{
"modifiers": [
{
"condition": "(no-sln)",
"exclude": [
"ConsoleApp.sln"
]
},
{
"condition": "(no-tests)",
"exclude": [
"ConsoleApp.Tests/*"
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="Moq.AutoMock" />
<PackageReference Include="System.CommandLine" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ConsoleApp\ConsoleApp.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
<Using Include="Moq.AutoMock" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions templates/Console/ConsoleApp/ConsoleApp.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.CommandLine;

namespace ConsoleApp.Tests;

public class ProgramTests
{
[Fact]
public async Task Invoke_WithHelpOption_DisplaysHelp()
{
using StringWriter stdOut = new();
int exitCode = await Invoke("--help", stdOut);

Assert.Equal(0, exitCode);
Assert.Contains("--help", stdOut.ToString());
}

[Fact]
public async Task Invoke_AddWithTwoNumbers_DisplaysResult()
{
using StringWriter stdOut = new();
int exitCode = await Invoke("add 4 2", stdOut);

Assert.Equal(0, exitCode);
Assert.Contains("The result is 6", stdOut.ToString());
}

private static Task<int> Invoke(string commandLine, StringWriter console)
{
CliConfiguration configuration = Program.GetConfiguration();
configuration.Output = console;
return configuration.InvokeAsync(commandLine);
}
}
40 changes: 40 additions & 0 deletions templates/Console/ConsoleApp/ConsoleApp.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{55D459AD-9D92-4A4A-ADAB-D0143BA39451}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp.Tests", "ConsoleApp.Tests\ConsoleApp.Tests.csproj", "{E0A57BAE-FFD5-46A6-9A93-33DF9195362B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D88D7F20-19C5-42B4-8561-874CC78B134A}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
Directory.Packages.props = Directory.Packages.props
NuGet.config = NuGet.config
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{55D459AD-9D92-4A4A-ADAB-D0143BA39451}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55D459AD-9D92-4A4A-ADAB-D0143BA39451}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55D459AD-9D92-4A4A-ADAB-D0143BA39451}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55D459AD-9D92-4A4A-ADAB-D0143BA39451}.Release|Any CPU.Build.0 = Release|Any CPU
{E0A57BAE-FFD5-46A6-9A93-33DF9195362B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0A57BAE-FFD5-46A6-9A93-33DF9195362B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0A57BAE-FFD5-46A6-9A93-33DF9195362B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0A57BAE-FFD5-46A6-9A93-33DF9195362B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DF353750-2089-4072-9508-65E9DC66DF2D}
EndGlobalSection
EndGlobal
12 changes: 12 additions & 0 deletions templates/Console/ConsoleApp/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine" />
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions templates/Console/ConsoleApp/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.CommandLine;

namespace ConsoleApp;

public sealed class Program
{
private static Task<int> Main(string[] args)
{
CliConfiguration configuration = GetConfiguration();
return configuration.InvokeAsync(args);
}

public static CliConfiguration GetConfiguration()
{
CliArgument<int> number1 = new("number1")
{
Description = "The first number to add"
};
CliArgument<int> number2 = new("number2")
{
Description = "The second number to add"
};
CliCommand addCommand = new("add", "Add two numbers together")
{
number1,
number2
};
addCommand.SetAction((ParseResult parseResult) =>
{
int value1 = parseResult.CommandResult.GetValue(number1);
int value2 = parseResult.CommandResult.GetValue(number2);
int result = value1 + value2;
parseResult.Configuration.Output.WriteLine($"The result is {result}");
});

CliRootCommand rootCommand = new("A starter console app by Keboo")
{
addCommand
};
return new CliConfiguration(rootCommand);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"ConsoleApp": {
"commandName": "Project"
},
"Display Help": {
"commandName": "Project",
"commandLineArgs": "--help"
}
}
}
26 changes: 26 additions & 0 deletions templates/Console/ConsoleApp/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
This file allow for customizing your build process.
See: https://learn.microsoft.com/visualstudio/msbuild/customize-your-build
-->
<Project>
<!--
Uncomment if you need to enable inclusion of another Directory.Build.props file from a parent directory
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
-->

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>11</LangVersion>
</PropertyGroup>

<!--
This allows all projects to share the same user secrets file.
If you want project to have their own, set it to a different GUID on each project.
See: https://learn.microsoft.com/dotnet/architecture/microservices/secure-net-microservices-web-applications/developer-app-secrets-storage
-->
<PropertyGroup Label="User Secrets">
<UserSecretsId>12345678-9abc-0123-4567-890abcdef123</UserSecretsId>
</PropertyGroup>

</Project>
10 changes: 10 additions & 0 deletions templates/Console/ConsoleApp/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--
This file allow for customizing your build process.
See: https://learn.microsoft.com/visualstudio/msbuild/customize-your-build
-->
<Project>
<!--
Uncomment if you need to enable inclusion of another Directory.Build.targets file from a parent directory
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" />
-->
</Project>
36 changes: 36 additions & 0 deletions templates/Console/ConsoleApp/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--
This enabled central package management.
This allows for controling all NuGet packages within the Directory.Packages.props file
See https://learn.microsoft.com/nuget/consume-packages/Central-Package-Management
-->
<Project>
<!--
Uncomment if you need to enable inclusion of another Directory.Packages.props file from a parent directory
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Packages.props, $(MSBuildThisFileDirectory)..))" />
-->
<!-- This property enables the Central Package Management feature -->
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<!--
This defines the set of centrally managed packages.
This would typically list all NuGet packages used within this solution.
-->
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageVersion>
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="Moq.AutoMock" Version="3.5.0" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.23219.2" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageVersion>
</ItemGroup>
</Project>
Loading

0 comments on commit 735e8f7

Please sign in to comment.