Skip to content

Commit baa0e37

Browse files
lolrobbe2robbe beernaertBeernaert RobbeJarod42
authored
.NET C# no way to enable different .NET sdk's on .netcore projects! (#2289)
* added C# documentationFile feature * wrote unit tests for C# documentationFile * removed null opt to not enable documentationFile * add documentationFile documentation * add documentationFile documentation * added some extra info for custom dir * implemented comments * fixed title * fixed spelling + addd test * fixed spelling + addd test * improved grammar / capitalization * changed styling of the documentation * changed docs to fit api * added MSFT links to See Also * fixed capitalization * changed wording/ capitalization of documentation * removed the slashes! * added documenationfile to the sidebar * Rename documentationFile.md to documentationfile.md * added support for newer <GenerateDocumentationFile> tag (functional result stays the same) * changed tests * changed default from documentationfile "" to documentationfile(true) * changed documentation to match! * changed availability in docs for documentationfile.md * added support to select .NET sdk in visual studio. * updated sidebar.js * added comma! * use map instead of ugly if else statement. Co-authored-by: Joris Dauphin <[email protected]> * tried to cleanup projectElement function to extract the wpf sdk selection to new dedecated function! (global.json file writing not workin) * put the api.register next to dotnetframework register function * figured out the file logic also made it so it does not overwrite global.json when it already exists potentially overwriting variables * implemented feedback from samsinsane * added a test for WPF flag * updated the dotnetsk options in the docs * fixed WindowsDesktop type and added Default to docs * implemented feedback from Jarod42 * added test but test is not fully correct because file is not written in test case but is written in normal ussage * at this point when running tests file creation is blocked i think because the file is created fine when using in non test environment! also tried changing dir to _TESTS_DIR to no avail. * removed the global.json test and put a warning comment in the appropriate place as file writing is working * premake.generate is kinda working as intended but the test part not so much! * fixed the testMSTestGlobalJSON (thanks jarod for the help!) * fixed the indentation * forgot to fix the indentation * made global.json look a bit better for endusers. * the desired flow for global.json is working but the test is not working due to using p.generate twice! * implemented feedback from jarod * fixed thr broken global.json test * updated dotnetsdk to now accept version localy and removed global.json file generation * updated docs * fixed code formating! * added dynamicvallidator type to api.lua * the dynamicvallidator function is working now * updated documentation to show how to add custom sdk's * deprecated WPF flag * removed cwd Co-authored-by: Joris Dauphin <[email protected]> * this made it work and supports multiple functions. maby obfuscate the key a bit more. * things suddenly seem to work * removed unused code --------- Co-authored-by: robbe beernaert <[email protected]> Co-authored-by: Beernaert Robbe <[email protected]> Co-authored-by: Joris Dauphin <[email protected]>
1 parent 1420eae commit baa0e37

File tree

9 files changed

+297
-6
lines changed

9 files changed

+297
-6
lines changed

modules/vstudio/_preload.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,33 @@
608608
tokens = "true",
609609
}
610610

611+
p.api.register {
612+
name = "dotnetsdk",
613+
scope = "project",
614+
kind = "string",
615+
allowed = {
616+
"Default",
617+
"Web",
618+
"Razor",
619+
"Worker",
620+
"Blazor",
621+
"WindowsDesktop",
622+
"MSTest",
623+
function (value)
624+
-- value is expected to be in the format <sdk>/<version>
625+
local parts = value:explode("/", true, 1)
626+
627+
if parts and #parts == 2 then
628+
if p.api.checkValue(p.field.get("dotnetsdk"), parts[1], "string") then
629+
return value
630+
end
631+
end
632+
633+
return nil
634+
end
635+
}
636+
}
637+
611638
p.api.register {
612639
name = "mfc",
613640
scope = "config",

modules/vstudio/tests/_tests.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ return {
1515
"cs2005/test_debug_props.lua",
1616
"cs2005/test_debug_props_2019.lua",
1717
"cs2005/test_documentation_file.lua",
18+
"cs2005/test_dotnetsdk.lua",
1819
"cs2005/test_files.lua",
1920
"cs2005/test_icon.lua",
2021
"cs2005/test_netcore.lua",
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
--
2+
-- tests/actions/vstudio/cs2005/test_dotnetsdk.lua
3+
-- Test DotnetSDK feature Visual Studio 2005+ C# project.
4+
-- Copyright (c) 2012-2025 Jason Perkins and the Premake project
5+
--
6+
local p = premake
7+
local suite = test.declare("vstudio_cs2005_dotnetsdk")
8+
local dn2005 = p.vstudio.dotnetbase
9+
--
10+
-- Setup
11+
--
12+
13+
local wks, prj
14+
15+
--
16+
-- Setup and teardown
17+
--
18+
function suite.setup()
19+
p.action.set("vs2010")
20+
wks = test.createWorkspace()
21+
configurations { "Debug", "Release" }
22+
language "C#"
23+
dotnetframework "net8.0"
24+
end
25+
26+
local function setConfig()
27+
local cfg = test.getconfig(prj, "Debug")
28+
dn2005.projectElement(cfg);
29+
end
30+
31+
local function prepare()
32+
prj = test.getproject(wks, 1)
33+
end
34+
35+
function suite.testNone()
36+
prepare()
37+
setConfig()
38+
39+
test.capture [[
40+
<Project Sdk="Microsoft.NET.Sdk">
41+
]]
42+
end
43+
44+
function suite.testDefault()
45+
prepare()
46+
setConfig()
47+
dotnetsdk "Default"
48+
test.capture [[
49+
<Project Sdk="Microsoft.NET.Sdk">
50+
]]
51+
end
52+
53+
function suite.testWeb()
54+
prepare()
55+
dotnetsdk "Web"
56+
setConfig()
57+
58+
test.capture [[
59+
<Project Sdk="Microsoft.NET.Sdk.Web">
60+
]]
61+
end
62+
63+
function suite.testRazor()
64+
prepare()
65+
dotnetsdk "Razor"
66+
setConfig()
67+
68+
test.capture [[
69+
<Project Sdk="Microsoft.NET.Sdk.Razor">
70+
]]
71+
end
72+
73+
function suite.testWorker()
74+
prepare()
75+
dotnetsdk "Worker"
76+
setConfig()
77+
78+
test.capture [[
79+
<Project Sdk="Microsoft.NET.Sdk.Worker">
80+
]]
81+
end
82+
83+
function suite.testBlazor()
84+
prepare()
85+
dotnetsdk "Blazor"
86+
setConfig()
87+
88+
test.capture [[
89+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
90+
]]
91+
end
92+
93+
function suite.testWindowsDesktop()
94+
prepare()
95+
dotnetsdk "WindowsDesktop"
96+
setConfig()
97+
98+
test.capture [[
99+
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
100+
]]
101+
end
102+
103+
function suite.testMSTest()
104+
prepare()
105+
dotnetsdk "MSTest/3.4.0"
106+
setConfig()
107+
108+
test.capture [[
109+
<Project Sdk="MSTest.Sdk/3.4.0">
110+
]]
111+
end
112+
113+
function suite.testWPFFlag()
114+
prepare()
115+
flags { "WPF" }
116+
setConfig()
117+
118+
test.capture [[
119+
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
120+
]]
121+
end
122+
123+
function suite.testWebVersion()
124+
prepare()
125+
dotnetsdk "Web/3.4.0"
126+
setConfig()
127+
128+
test.capture [[
129+
<Project Sdk="Microsoft.NET.Sdk.Web/3.4.0">
130+
]]
131+
end
132+
133+
function suite.testRazorVersion()
134+
prepare()
135+
dotnetsdk "Razor/3.4.0"
136+
setConfig()
137+
138+
test.capture [[
139+
<Project Sdk="Microsoft.NET.Sdk.Razor/3.4.0">
140+
]]
141+
end
142+
143+
function suite.testWorkerVersion()
144+
prepare()
145+
dotnetsdk "Worker/3.4.0"
146+
setConfig()
147+
148+
test.capture [[
149+
<Project Sdk="Microsoft.NET.Sdk.Worker/3.4.0">
150+
]]
151+
end
152+
153+
function suite.testBlazorVersion()
154+
prepare()
155+
dotnetsdk "Blazor/3.4.0"
156+
setConfig()
157+
158+
test.capture [[
159+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly/3.4.0">
160+
]]
161+
end
162+
163+
function suite.testWindowsDesktopVersion()
164+
prepare()
165+
dotnetsdk "WindowsDesktop/3.4.0"
166+
setConfig()
167+
168+
test.capture [[
169+
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop/3.4.0">
170+
]]
171+
end
172+
173+
function suite.testCustomSDKVersion()
174+
prepare()
175+
premake.api.addAllowed("dotnetsdk", "CustomSdk")
176+
dotnetsdk "CustomSdk/3.4.0"
177+
setConfig()
178+
179+
test.capture [[
180+
<Project Sdk="CustomSdk/3.4.0">
181+
]]
182+
end

modules/vstudio/vs2005_csproj.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
dotnetbase.csversion,
5555
dotnetbase.projectConfigurations,
5656
dotnetbase.netcore.enableDefaultCompileItems,
57+
dotnetbase.netcore.dotnetsdk
5758
}
5859
else
5960
return {

modules/vstudio/vs2005_dotnetbase.lua

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@
5757

5858
function dotnetbase.projectElement(prj)
5959
if dotnetbase.isNewFormatProject(prj) then
60-
if prj.flags.WPF then
61-
_p('<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">')
62-
else
63-
_p('<Project Sdk="Microsoft.NET.Sdk">')
64-
end
60+
_p('<Project Sdk="%s">', dotnetbase.netcore.getsdk(prj))
6561
else
6662
local ver = ''
6763
local action = p.action.current()
@@ -827,6 +823,33 @@
827823
end
828824
end
829825

826+
function dotnetbase.netcore.getsdk(cfg)
827+
local map = {
828+
["Default"] = "Microsoft.NET.Sdk",
829+
["Web"] = "Microsoft.NET.Sdk.Web",
830+
["Razor"] = "Microsoft.NET.Sdk.Razor",
831+
["Worker"] = "Microsoft.NET.Sdk.Worker",
832+
["Blazor"] = "Microsoft.NET.Sdk.BlazorWebAssembly",
833+
["WindowsDesktop"] = "Microsoft.NET.Sdk.WindowsDesktop",
834+
["MSTest"] = "MSTest.Sdk",
835+
}
836+
837+
local parts = nil
838+
839+
if cfg.dotnetsdk then
840+
parts = cfg.dotnetsdk:explode("/", true, 1)
841+
end
842+
843+
local sdk = (parts and #parts > 0 and parts[1]) or cfg.dotnetsdk
844+
if not parts or #parts < 2 then
845+
return map[sdk or "Default"]
846+
elseif parts and #parts == 2 then
847+
return string.format("%s/%s", map[parts[1]] or parts[1], parts[2])
848+
end
849+
850+
return map["Default"]
851+
end
852+
830853
function dotnetbase.allowUnsafeBlocks(cfg)
831854
if cfg.clr == "Unsafe" then
832855
_p(2,'<AllowUnsafeBlocks>true</AllowUnsafeBlocks>')

src/_premake_init.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,7 @@
11201120
}
11211121
}
11221122

1123+
--27 November 2024
11231124
api.deprecateValue("flags", "LinkTimeOptimization", "Use `linktimeoptimization` instead.",
11241125
function(value)
11251126
linktimeoptimization("On")
@@ -1128,6 +1129,14 @@
11281129
linktimeoptimization("Default")
11291130
end)
11301131

1132+
--25 November 2024
1133+
api.deprecateValue("flags", "WPF", 'Use `dotnetsdk "WindowsDesktop"` instead.',
1134+
function(value)
1135+
dotnetsdk "WindowsDesktop"
1136+
end,
1137+
function(value)
1138+
dotnetsdk "Default"
1139+
end)
11311140
api.deprecateValue("flags", "FatalWarnings", "Use `fatalwarnings { \"All\" }` instead.",
11321141
function(value)
11331142
fatalwarnings({ "All" })

src/base/api.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,9 @@
265265

266266
if type(field.allowed) == "table" then
267267
for i, item in ipairs(field.allowed) do
268-
field.allowed[item:lower()] = item
268+
if type(item) == "string" then
269+
field.allowed[item:lower()] = item
270+
end
269271
end
270272
end
271273

website/docs/dotnetsdk.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Selects a .NET SDK
2+
3+
```lua
4+
dotnetsdk "SDK"
5+
```
6+
7+
For more information see the MSDN documentation [here](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview)
8+
9+
## Parameters ##
10+
`SDK` is one of:
11+
12+
* [Default](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props)
13+
* [Web](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/web-sdk?toc=%2Fdotnet%2Fnavigate%2Ftools-diagnostics%2Ftoc.json&bc=%2Fdotnet%2Fbreadcrumb%2Ftoc.json)
14+
* [Razor](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/sdk?toc=%2Fdotnet%2Fnavigate%2Ftools-diagnostics%2Ftoc.json&bc=%2Fdotnet%2Fbreadcrumb%2Ftoc.json)
15+
* [Worker](https://learn.microsoft.com/en-us/dotnet/core/extensions/workers)
16+
* [Blazor](https://learn.microsoft.com/en-us/aspnet/core/blazor/)
17+
* [WindowsDesktop](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props-desktop?view=aspnetcore-8.0)
18+
* [MSTest](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-sdk): Requires a version be specified.
19+
20+
### Applies To ###
21+
22+
Project configurations.
23+
24+
### Availability ###
25+
26+
Premake 5.0 beta5 or later.
27+
28+
Visual studio is the only toolset currently supported.
29+
30+
### Examples ###
31+
```lua
32+
dotnetsdk "Web"
33+
```
34+
35+
```lua
36+
dotnetsdk "Web/3.4.0"
37+
```
38+
39+
A custom SDK can be specified using the following:
40+
```lua
41+
premake.api.addAllowed("dotnetsdk", "CustomSDK") -- add the custom SDK to allowed values for dotnetsdk
42+
dotnetsdk "CustomSDK"
43+
44+
dotnetsdk "CustomSDK/3.4.0" -- Specifying a version with a custom SDK is also supported
45+
```

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ module.exports = {
122122
'display',
123123
'documentationfile',
124124
'dotnetframework',
125+
'dotnetsdk',
125126
'dpiawareness',
126127
'editandcontinue',
127128
'editorintegration',

0 commit comments

Comments
 (0)