Files
openidea/generators/dotnet.toml
T
LU XIANGRUIandwildcatDownstairs b1db1c6c3d feat(new-project): .NET solution name and SDK version (#5385)
The .NET entry of File › New Project had no way to create a solution or pick the SDK. It now has:
- **Create solution** (on by default) and **Solution name** (defaults to the project name). As in Visual Studio, the folder gets `<Solution>.sln`/`.slnx` at the top and the project in its own subfolder (`dotnet new sln`, then `dotnet sln add`). Unchecked, the layout is the same as before: the project at the top, no solution.
- **SDK version** lists the SDKs from `dotnet --list-sdks`, 8.0 and later, newest first. Older ones are left out because Blazor Web App and the templates' current options need SDK 8. The chosen one is pinned with `dotnet new globaljson --sdk-version … --roll-forward latestFeature`, which runs before `dotnet new <template>`. Without a roll-forward policy, `global.json` defaults to `latestPatch`: the project would then fail to build on a machine with only another feature band of the same .NET version, such as a teammate's or this one after an SDK update. `dotnet new` resolves the SDK through `global.json`, so the template also targets the pinned SDK's framework (10.0.401 gives `net10.0` on a machine whose default SDK is 11). **Automatic** writes no `global.json`.
How it is built: a new `dotnet` SDK kind in `ide-sdk` (detection runs in `sdk_detect`'s `spawn_blocking`), SDK field options that submit the version instead of the home (`version = true`) and leave out SDKs below a `min`, and `if-set` on generator steps (it already existed for arguments), so the pinning step runs only when a version is chosen. The New Project dialog now detects SDKs for any SDK field kind, not only `jdk`.
Edge cases: an empty or invalid solution name marks the field and disables Create. A solution name equal to the project name gives `App/App.slnx` + `App/App/App.csproj`. The generator now requires SDK 8.0 or later through `dotnet --version`, and a version command that exits with an error no longer counts as a version. So a missing `dotnet`, or a runtime-only install (no SDK), shows "dotnet is not installed" with the install hint and disables Create. An SDK 7 default shows that 8.0 or later is needed.

Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
2026-11-24 05:30:14 +08:00

105 lines
2.8 KiB
TOML

id = "dotnet"
name = ".NET"
group = ".NET"
icon = "project-dotnet"
description = "A .NET project from dotnet new."
# `dotnet --version` fails without an SDK (a runtime-only install). Blazor Web App and the
# other templates' current options need SDK 8.
[[requires]]
program = "dotnet"
min = "8.0"
hint = "Install the .NET SDK: https://dotnet.microsoft.com/download"
[[fields]]
id = "template"
label = "Template:"
type = "choice"
options = [
{ value = "console", label = "Console App" },
{ value = "classlib", label = "Class Library" },
{ value = "webapi", label = "ASP.NET Core Web API" },
{ value = "mvc", label = "ASP.NET Core MVC" },
{ value = "blazor", label = "Blazor Web App" },
{ value = "xunit", label = "xUnit Test Project" },
]
default = "console"
[[fields]]
id = "lang"
label = "Language:"
type = "choice"
segmented = true
options = [{ value = "C#", label = "C#" }, { value = "F#", label = "F#" }]
default = "C#"
visible = { template = ["console", "classlib", "webapi", "mvc", "xunit"] }
[[fields]]
id = "sdk"
label = "SDK version:"
type = "sdk"
sdk = "dotnet"
version = true
min = "8.0"
hint = "Pinned in global.json; later patches and feature bands of the same .NET version also qualify. Automatic lets dotnet choose the SDK."
[[fields]]
id = "solution"
label = "Create solution"
type = "checkbox"
default = "true"
hint = "As in Visual Studio: the solution file at the top, the project in its own folder."
[[fields]]
id = "solution-name"
label = "Solution name:"
type = "text"
pattern = '^[^/\\:*?"<>|.\s][^/\\:*?"<>|]*$'
default = "{{name}}"
visible = { solution = "true" }
# Write the pin before the SDK resolver runs, even under an incompatible parent global.json.
[[files]]
path = "global.json"
if-set = "sdk"
content = '''
{
"sdk": {
"version": "{{sdk|json}}",
"rollForward": "latestFeature"
}
}
'''
[[after]]
label = "Creating the solution"
program = "dotnet"
args = ["new", "sln", "--name", "{{solution-name}}"]
when = { solution = "true" }
env = { DOTNET_CLI_TELEMETRY_OPTOUT = "1", DOTNET_NOLOGO = "1" }
[[after]]
label = "Running dotnet new"
program = "dotnet"
args = [
"new", "{{template}}", "--name", "{{name}}",
{ when = { solution = "true" }, args = ["--output", "{{name}}"] },
{ when = { solution = "false" }, args = ["--output", "."] },
{ when = { template = ["console", "classlib", "webapi", "mvc", "xunit"] }, args = ["--language", "{{lang}}"] },
]
env = { DOTNET_CLI_TELEMETRY_OPTOUT = "1", DOTNET_NOLOGO = "1" }
[[after]]
label = "Adding the project to the solution"
program = "dotnet"
args = ["sln", "add", "{{name}}"]
when = { solution = "true" }
env = { DOTNET_CLI_TELEMETRY_OPTOUT = "1", DOTNET_NOLOGO = "1" }
[[after]]
label = "Adding a .gitignore"
program = "dotnet"
args = ["new", "gitignore"]
env = { DOTNET_CLI_TELEMETRY_OPTOUT = "1", DOTNET_NOLOGO = "1" }
optional = true