Skip to content

Commit

Permalink
.NET C# no way to enable different .NET sdk's on .netcore projects! (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
4 people authored Jan 10, 2025
1 parent 1420eae commit baa0e37
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 6 deletions.
27 changes: 27 additions & 0 deletions modules/vstudio/_preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,33 @@
tokens = "true",
}

p.api.register {
name = "dotnetsdk",
scope = "project",
kind = "string",
allowed = {
"Default",
"Web",
"Razor",
"Worker",
"Blazor",
"WindowsDesktop",
"MSTest",
function (value)
-- value is expected to be in the format <sdk>/<version>
local parts = value:explode("/", true, 1)

if parts and #parts == 2 then
if p.api.checkValue(p.field.get("dotnetsdk"), parts[1], "string") then
return value
end
end

return nil
end
}
}

p.api.register {
name = "mfc",
scope = "config",
Expand Down
1 change: 1 addition & 0 deletions modules/vstudio/tests/_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ return {
"cs2005/test_debug_props.lua",
"cs2005/test_debug_props_2019.lua",
"cs2005/test_documentation_file.lua",
"cs2005/test_dotnetsdk.lua",
"cs2005/test_files.lua",
"cs2005/test_icon.lua",
"cs2005/test_netcore.lua",
Expand Down
182 changes: 182 additions & 0 deletions modules/vstudio/tests/cs2005/test_dotnetsdk.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
--
-- tests/actions/vstudio/cs2005/test_dotnetsdk.lua
-- Test DotnetSDK feature Visual Studio 2005+ C# project.
-- Copyright (c) 2012-2025 Jason Perkins and the Premake project
--
local p = premake
local suite = test.declare("vstudio_cs2005_dotnetsdk")
local dn2005 = p.vstudio.dotnetbase
--
-- Setup
--

local wks, prj

--
-- Setup and teardown
--
function suite.setup()
p.action.set("vs2010")
wks = test.createWorkspace()
configurations { "Debug", "Release" }
language "C#"
dotnetframework "net8.0"
end

local function setConfig()
local cfg = test.getconfig(prj, "Debug")
dn2005.projectElement(cfg);
end

local function prepare()
prj = test.getproject(wks, 1)
end

function suite.testNone()
prepare()
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk">
]]
end

function suite.testDefault()
prepare()
setConfig()
dotnetsdk "Default"
test.capture [[
<Project Sdk="Microsoft.NET.Sdk">
]]
end

function suite.testWeb()
prepare()
dotnetsdk "Web"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.Web">
]]
end

function suite.testRazor()
prepare()
dotnetsdk "Razor"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.Razor">
]]
end

function suite.testWorker()
prepare()
dotnetsdk "Worker"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.Worker">
]]
end

function suite.testBlazor()
prepare()
dotnetsdk "Blazor"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
]]
end

function suite.testWindowsDesktop()
prepare()
dotnetsdk "WindowsDesktop"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
]]
end

function suite.testMSTest()
prepare()
dotnetsdk "MSTest/3.4.0"
setConfig()

test.capture [[
<Project Sdk="MSTest.Sdk/3.4.0">
]]
end

function suite.testWPFFlag()
prepare()
flags { "WPF" }
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
]]
end

function suite.testWebVersion()
prepare()
dotnetsdk "Web/3.4.0"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.Web/3.4.0">
]]
end

function suite.testRazorVersion()
prepare()
dotnetsdk "Razor/3.4.0"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.Razor/3.4.0">
]]
end

function suite.testWorkerVersion()
prepare()
dotnetsdk "Worker/3.4.0"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.Worker/3.4.0">
]]
end

function suite.testBlazorVersion()
prepare()
dotnetsdk "Blazor/3.4.0"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly/3.4.0">
]]
end

function suite.testWindowsDesktopVersion()
prepare()
dotnetsdk "WindowsDesktop/3.4.0"
setConfig()

test.capture [[
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop/3.4.0">
]]
end

function suite.testCustomSDKVersion()
prepare()
premake.api.addAllowed("dotnetsdk", "CustomSdk")
dotnetsdk "CustomSdk/3.4.0"
setConfig()

test.capture [[
<Project Sdk="CustomSdk/3.4.0">
]]
end
1 change: 1 addition & 0 deletions modules/vstudio/vs2005_csproj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
dotnetbase.csversion,
dotnetbase.projectConfigurations,
dotnetbase.netcore.enableDefaultCompileItems,
dotnetbase.netcore.dotnetsdk
}
else
return {
Expand Down
33 changes: 28 additions & 5 deletions modules/vstudio/vs2005_dotnetbase.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@

function dotnetbase.projectElement(prj)
if dotnetbase.isNewFormatProject(prj) then
if prj.flags.WPF then
_p('<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">')
else
_p('<Project Sdk="Microsoft.NET.Sdk">')
end
_p('<Project Sdk="%s">', dotnetbase.netcore.getsdk(prj))
else
local ver = ''
local action = p.action.current()
Expand Down Expand Up @@ -827,6 +823,33 @@
end
end

function dotnetbase.netcore.getsdk(cfg)
local map = {
["Default"] = "Microsoft.NET.Sdk",
["Web"] = "Microsoft.NET.Sdk.Web",
["Razor"] = "Microsoft.NET.Sdk.Razor",
["Worker"] = "Microsoft.NET.Sdk.Worker",
["Blazor"] = "Microsoft.NET.Sdk.BlazorWebAssembly",
["WindowsDesktop"] = "Microsoft.NET.Sdk.WindowsDesktop",
["MSTest"] = "MSTest.Sdk",
}

local parts = nil

if cfg.dotnetsdk then
parts = cfg.dotnetsdk:explode("/", true, 1)
end

local sdk = (parts and #parts > 0 and parts[1]) or cfg.dotnetsdk
if not parts or #parts < 2 then
return map[sdk or "Default"]
elseif parts and #parts == 2 then
return string.format("%s/%s", map[parts[1]] or parts[1], parts[2])
end

return map["Default"]
end

function dotnetbase.allowUnsafeBlocks(cfg)
if cfg.clr == "Unsafe" then
_p(2,'<AllowUnsafeBlocks>true</AllowUnsafeBlocks>')
Expand Down
9 changes: 9 additions & 0 deletions src/_premake_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@
}
}

--27 November 2024
api.deprecateValue("flags", "LinkTimeOptimization", "Use `linktimeoptimization` instead.",
function(value)
linktimeoptimization("On")
Expand All @@ -1128,6 +1129,14 @@
linktimeoptimization("Default")
end)

--25 November 2024
api.deprecateValue("flags", "WPF", 'Use `dotnetsdk "WindowsDesktop"` instead.',
function(value)
dotnetsdk "WindowsDesktop"
end,
function(value)
dotnetsdk "Default"
end)
api.deprecateValue("flags", "FatalWarnings", "Use `fatalwarnings { \"All\" }` instead.",
function(value)
fatalwarnings({ "All" })
Expand Down
4 changes: 3 additions & 1 deletion src/base/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@

if type(field.allowed) == "table" then
for i, item in ipairs(field.allowed) do
field.allowed[item:lower()] = item
if type(item) == "string" then
field.allowed[item:lower()] = item
end
end
end

Expand Down
45 changes: 45 additions & 0 deletions website/docs/dotnetsdk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Selects a .NET SDK

```lua
dotnetsdk "SDK"
```

For more information see the MSDN documentation [here](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview)

## Parameters ##
`SDK` is one of:

* [Default](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props)
* [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)
* [Razor](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/sdk?toc=%2Fdotnet%2Fnavigate%2Ftools-diagnostics%2Ftoc.json&bc=%2Fdotnet%2Fbreadcrumb%2Ftoc.json)
* [Worker](https://learn.microsoft.com/en-us/dotnet/core/extensions/workers)
* [Blazor](https://learn.microsoft.com/en-us/aspnet/core/blazor/)
* [WindowsDesktop](https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props-desktop?view=aspnetcore-8.0)
* [MSTest](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-sdk): Requires a version be specified.

### Applies To ###

Project configurations.

### Availability ###

Premake 5.0 beta5 or later.

Visual studio is the only toolset currently supported.

### Examples ###
```lua
dotnetsdk "Web"
```

```lua
dotnetsdk "Web/3.4.0"
```

A custom SDK can be specified using the following:
```lua
premake.api.addAllowed("dotnetsdk", "CustomSDK") -- add the custom SDK to allowed values for dotnetsdk
dotnetsdk "CustomSDK"

dotnetsdk "CustomSDK/3.4.0" -- Specifying a version with a custom SDK is also supported
```
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ module.exports = {
'display',
'documentationfile',
'dotnetframework',
'dotnetsdk',
'dpiawareness',
'editandcontinue',
'editorintegration',
Expand Down

0 comments on commit baa0e37

Please sign in to comment.