Skip to content

Commit

Permalink
Merge pull request #14 from samsmithnz/CreateDotNetTool
Browse files Browse the repository at this point in the history
Creating dotnet census tool
  • Loading branch information
samsmithnz authored Aug 1, 2022
2 parents 5afb38e + 79758eb commit 6bda583
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 60 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ jobs:
dotnet-version: 6.0.x
- name: .NET test
run: dotnet test src/DotNetCensus.Tests/DotNetCensus.Tests.csproj -c Release
- name: .NET publish
run: dotnet publish src/DotNetCensus.Core/DotNetCensus.Core.csproj -c Release -p:Version='${{ steps.gitversion.outputs.SemVer }}'
- name: .NET pack NuGet package
run: dotnet pack src/DotNetCensus/DotNetCensus.csproj -c Release -p:Version='${{ steps.gitversion.outputs.SemVer }}'
- name: Upload package back to GitHub
uses: actions/upload-artifact@v3
with:
name: drop
path: src/DotNetCensus.Core/bin/Release
name: nugetPackage
path: src/DotNetCensus/nupkg

release:
name: Release job
Expand All @@ -52,8 +52,8 @@ jobs:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: drop
path: drop
name: nugetPackage
path: nugetPackage
- name: Display GitVersion outputs
run: |
echo "Version: ${{ needs.build.outputs.Version }}"
Expand All @@ -64,4 +64,8 @@ jobs:
with:
tag: ${{ needs.build.outputs.Version }}
name: Release ${{ needs.build.outputs.Version }}
token: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
token: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
- name: Publish nuget package to nuget.org
if: needs.build.outputs.CommitsSinceVersionSource > 0 #Only publish a NuGet package if there has been a commit/version change
run: dotnet nuget push nugetPackage\*.nupkg --api-key "${{ secrets.NUGETTOKEN }}" --source "https://api.nuget.org/v3/index.json"
shell: pwsh
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
next-version: 0.3.0
next-version: 0.4.0
97 changes: 69 additions & 28 deletions src/DotNetCensus.Tests/ConsoleAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,39 @@ namespace DotNetCensus.Tests;
[TestClass]
public class ConsoleAppTests : BaseTests
{
[TestMethod]
public void RunSamplesWithNoParametersTest()
{
//Arrange
if (SamplesPath != null)
{
string[] parameters = new string[] { };
StringWriter sw = new();
string expected = @"
Framework FrameworkFamily Count
---------------------------------
";

//Act
Console.SetOut(sw);
Program.Main(parameters);

//Asset
Assert.IsNotNull(expected);
Assert.AreEqual(expected, Environment.NewLine + sw.ToString());
}
}

[TestMethod]
public void RunSamplesTest()
public void RunSamplesWithPathTest()
{
//Arrange
StringWriter sw = new();
string expected = @"
if (SamplesPath != null)
{
string[] parameters = new string[] { "-d", SamplesPath };
StringWriter sw = new();
string expected = @"
Framework FrameworkFamily Count
--------------------------------------------
.NET 5.0 .NET 1
Expand All @@ -29,25 +55,25 @@ Visual Basic 6 Visual Basic 6 1
";

//Act
if (SamplesPath != null)
{
//Act
Console.SetOut(sw);
Program.Main(new string[] { "-d", SamplesPath });
}
Program.Main(parameters);

//Asset
Assert.IsNotNull(expected);
Assert.AreEqual(expected, Environment.NewLine + sw.ToString());
//Asset
Assert.IsNotNull(expected);
Assert.AreEqual(expected, Environment.NewLine + sw.ToString());
}
}

[TestMethod]
public void RunSamplesWithTotalsTest()
{
//Arrange
bool includeTotal = true;
StringWriter sw = new();
string expected = @"
if (SamplesPath != null)
{
string[] parameters = new string[] { "-d", SamplesPath, "-t" };
StringWriter sw = new();
string expected = @"
Framework FrameworkFamily Count
--------------------------------------------
.NET 5.0 .NET 1
Expand All @@ -66,23 +92,38 @@ Visual Basic 6 Visual Basic 6 1
total frameworks 16
";
// string expected = @"
//Framework FrameworkFamily Count
//----------------------------------------
//net6.0 .NET 1
//total frameworks 1

//";
//Act
if (SamplesPath != null)
{
//Act
Console.SetOut(sw);
Program.Main(new string[] { "-d", SamplesPath, "-i", includeTotal.ToString() });
}
Program.Main(parameters);

//Asset
Assert.IsNotNull(expected);
Assert.AreEqual(expected, Environment.NewLine + sw.ToString());
//Asset
Assert.IsNotNull(expected);
Assert.AreEqual(expected, Environment.NewLine + sw.ToString());
}
}

[TestMethod]
public void RunSamplesWithInvalidParametersTest()
{
if (SamplesPath != null)
{
try
{
//Arrange
string[] parameters = new string[] { "-d", SamplesPath, "-z" }; //z is an invalid parameter
StringWriter sw = new();

//Act
Console.SetOut(sw);
Program.Main(parameters);
}
catch (Exception ex)
{
//Asset
Assert.IsNotNull(ex);
Assert.AreEqual("One or more errors occurred. (CommandLine.UnknownOptionError)", ex.Message);
}
}
}
}
34 changes: 21 additions & 13 deletions src/DotNetCensus/DotNetCensus.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackAsTool>true</PackAsTool>
<ToolCommandName>census</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/samsmithnz/DotNetCensus</PackageProjectUrl>
<PackageDescription>A tool to find and count .NET versions</PackageDescription>
<PackageTags>.NET;dotnet;count;census;.NET Framework;.NET Standard;.NET Core;.NET;c#;VB.NET;VB6;</PackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="ConsoleTables" Version="2.4.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="ConsoleTables" Version="2.4.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DotNetCensus.Core\DotNetCensus.Core.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotNetCensus.Core\DotNetCensus.Core.csproj" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions src/DotNetCensus/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ namespace DotNetCensus
{
public class Options
{
[Option('d', "directory", Required = true, HelpText = "Root directory to search for projects")]
[Option('d', "directory", Required = false, HelpText = "Root directory to search for projects")]
public string? Directory { get; set; }
[Option('i', "includetotals", Required = false, Default = false, HelpText = "Include totals in results")]

[Option('t', "Total", Required = false, Default = false, HelpText = "Include totals in results")]
public bool IncludeTotals { get; set; }
//[Option('o', "output", Required = false, HelpText = "output file to create csv file")]
//public string OutputFile { get; set; }
Expand Down
26 changes: 17 additions & 9 deletions src/DotNetCensus/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ public static void Main(string[] args)
.WithParsed(RunOptions)
.WithNotParsed(HandleParseError);


//If there is a folder to scan, run the process against it
if (string.IsNullOrEmpty(_directory) == false)
{
//Run the calculations to get and aggregate the results
List<Project> projects = ProjectScanning.SearchDirectory(_directory);
List<FrameworkSummary> results = Census.AggregateFrameworks(projects, _includeTotals);

//foreach (FrameworkSummary item in results)
//{
// Console.WriteLine(item.Framework + ": " + item.Count);
//}

//Create and output the table
ConsoleTable
.From<FrameworkSummary>(results)
.Configure(o => o.NumberAlignment = Alignment.Right)
Expand All @@ -40,17 +36,29 @@ public static void Main(string[] args)
static void RunOptions(Options opts)
{
//handle options
_directory = opts.Directory;
if (string.IsNullOrEmpty(opts.Directory) == true)
{
_directory = Directory.GetCurrentDirectory();
}
else
{
_directory = opts.Directory;
}
_includeTotals = opts.IncludeTotals;
//_outputFile = opts.OutputFile;
}

static void HandleParseError(IEnumerable<Error> errs)
{
//handle errors
foreach (Error err in errs)
var excList = new List<Exception>();
foreach (var err in errs)
{
excList.Add(new ArgumentException(err.ToString()));
}
if (excList.Any())
{
Console.WriteLine(err.ToString());
throw new AggregateException(excList);
}
}
}
Expand Down

0 comments on commit 6bda583

Please sign in to comment.