Skip to content

Commit 79c3207

Browse files
committed
Improvements
1 parent b89f7ea commit 79c3207

File tree

5 files changed

+66
-40
lines changed

5 files changed

+66
-40
lines changed

Directory.Packages.props

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<Project>
2-
32
<PropertyGroup>
43
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
54
</PropertyGroup>
6-
75
<ItemGroup>
86
</ItemGroup>
9-
</Project>
7+
<ItemGroup>
8+
<PackageVersion Include="System.CommandLine" Version="2.0.1" />
9+
</ItemGroup>
10+
</Project>

src/DotNetProjectStarter/DotNetProjectStarter.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
<OutputType>Exe</OutputType>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="System.CommandLine" />
12+
</ItemGroup>
13+
1014
</Project>

src/DotNetProjectStarter/LibraryTemplateConstants.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,6 @@
143143
if: |
144144
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) ||
145145
(github.event_name == 'workflow_dispatch' && inputs.publish-to-nuget == true)
146-
environment:
147-
name: nuget.org
148-
url: https://www.nuget.org/packages/{0}
149146
150147
steps:
151148
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@@ -167,7 +164,7 @@
167164
uses: NuGet/login@d22cc5f58ff5b88bf9bd452535b4335137e24544 # v1.1.0
168165
id: nugetlogin
169166
with:
170-
user: {1}
167+
user: {0}
171168
172169
- name: Push to NuGet.org
173170
run: dotnet nuget push "./packages/**/*.nupkg" --api-key "${{ steps.nugetlogin.outputs.NUGET_API_KEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate
@@ -210,6 +207,18 @@
210207
211208
""";
212209

210+
public const string ProjectFileWithExplicitPackageId = """
211+
<Project Sdk="Microsoft.NET.Sdk">
212+
213+
<PropertyGroup>
214+
<TargetFrameworks>net462;net8.0</TargetFrameworks>
215+
<PackageId>{0}</PackageId>
216+
</PropertyGroup>
217+
218+
</Project>
219+
220+
""";
221+
213222
public const string TestDirectoryBuildProps = """
214223
<Project>
215224

src/DotNetProjectStarter/LibraryTemplateGenerator.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@ public bool Generate(TemplateGenerationOptions options)
2222
var githubWorkflowsPath = Path.Combine(outputDirectory, ".github", "workflows");
2323
Directory.CreateDirectory(githubWorkflowsPath);
2424
File.WriteAllText(Path.Combine(githubWorkflowsPath, "ci.yml"), LibraryTemplateConstants.LibraryTemplateCIYML);
25-
File.WriteAllText(Path.Combine(githubWorkflowsPath, "release.yml"), string.Format(CultureInfo.InvariantCulture, LibraryTemplateConstants.ReleaseYML, packageName ?? "PACKAGE_NAME_GOES_HERE", nugetUsername ?? "NUGET_USERNAME_GOES_HERE"));
25+
File.WriteAllText(Path.Combine(githubWorkflowsPath, "release.yml"), string.Format(CultureInfo.InvariantCulture, LibraryTemplateConstants.ReleaseYML, nugetUsername ?? "NUGET_USERNAME_GOES_HERE"));
2626

2727
var srcProjectDirectory = Path.Combine(outputDirectory, "src", projectName);
2828
Directory.CreateDirectory(srcProjectDirectory);
29-
File.WriteAllText(Path.Combine(srcProjectDirectory, $"{projectName}.csproj"), LibraryTemplateConstants.ProjectFile);
29+
if (packageName is not null && packageName != projectName)
30+
{
31+
File.WriteAllText(Path.Combine(srcProjectDirectory, $"{projectName}.csproj"), string.Format(CultureInfo.InvariantCulture, LibraryTemplateConstants.ProjectFileWithExplicitPackageId, packageName));
32+
}
33+
else
34+
{
35+
File.WriteAllText(Path.Combine(srcProjectDirectory, $"{projectName}.csproj"), LibraryTemplateConstants.ProjectFile);
36+
}
3037

3138
var testsDirectory = Path.Combine(outputDirectory, "tests");
3239
var testsProjectDirectory = Path.Combine(testsDirectory, $"{projectName}.Tests");
@@ -55,7 +62,6 @@ public bool Generate(TemplateGenerationOptions options)
5562
Console.WriteLine("- Update Directory.Build.props with package author.");
5663
Console.WriteLine("- Update .github/workflows/release.yml with NuGet username.");
5764
}
58-
Console.WriteLine("- Update .github/workflows/release.yml with NuGet package name.");
5965
Console.WriteLine("- Update LICENSE with license copyright holder.");
6066
Console.WriteLine("- Login to nuget.org with your account and set up trusted publishing via <https://www.nuget.org/account/trustedpublishing>. Set the Workflow File to 'release.yml'.");
6167
return true;
Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
using System;
1+
using System.CommandLine;
22

3-
string? name = null;
4-
string? outputDirectory = null;
5-
string? nugetPackageName = null;
6-
string? nugetUsername = null;
3+
args = ["--name"];
74

8-
// TODO: Use System.CommandLine, or at least write a better parser if adding a dependency is problematic.
9-
for (int i = 0; i < args.Length; i++)
5+
var nameOption = new Option<string>("--name")
106
{
11-
if (args[i] == "--name")
12-
{
13-
name = GetArgumentValue(i, args);
14-
}
15-
else if (args[i] == "--output-directory")
16-
{
17-
outputDirectory = GetArgumentValue(i, args);
18-
}
19-
else if (args[i] == "--nuget-username")
20-
{
21-
nugetUsername = GetArgumentValue(i, args);
22-
}
23-
}
7+
Description = "The name of the project to create",
8+
Arity = ArgumentArity.ExactlyOne,
9+
};
2410

25-
var options = new TemplateGenerationOptions(name, outputDirectory, nugetPackageName, nugetUsername);
11+
var outputDirectoryOption = new Option<string>("--output-directory")
12+
{
13+
Description = "The directory where the project will be created",
14+
Arity = ArgumentArity.ExactlyOne,
15+
};
2616

27-
var generator = new LibraryTemplateGenerator();
28-
generator.Generate(options);
17+
var nugetPackageNameOption = new Option<string>("--nuget-package-name")
18+
{
19+
Description = "The name of the NuGet package for package publishing",
20+
Arity = ArgumentArity.ExactlyOne,
21+
};
2922

30-
static string GetArgumentValue(int i, string[] args)
23+
var nugetUsernameOption = new Option<string>("--nuget-username")
3124
{
32-
if (args.Length < i + 2 || args[i + 1].StartsWith("--", StringComparison.Ordinal))
33-
{
34-
throw new InvalidOperationException($"A value for '{args[i]}' was not provided.");
35-
}
25+
Description = "The NuGet username for package publishing",
26+
Arity = ArgumentArity.ExactlyOne,
27+
};
28+
29+
var rootCommand = new RootCommand("Creates a new .NET project template");
30+
rootCommand.Options.Add(nameOption);
31+
rootCommand.Options.Add(outputDirectoryOption);
32+
rootCommand.Options.Add(nugetPackageNameOption);
33+
rootCommand.Options.Add(nugetUsernameOption);
3634

37-
return args[i + 1];
38-
}
35+
var parseResult = rootCommand.Parse(args);
36+
var name = parseResult.GetValue(nameOption);
37+
var outputDirectory = parseResult.GetValue(outputDirectoryOption);
38+
var nugetPackageName = parseResult.GetValue(nugetPackageNameOption);
39+
var nugetUsername = parseResult.GetValue(nugetUsernameOption);
40+
41+
var options = new TemplateGenerationOptions(name, outputDirectory, nugetPackageName, nugetUsername);
42+
43+
var generator = new LibraryTemplateGenerator();
44+
generator.Generate(options);

0 commit comments

Comments
 (0)