diff --git a/modules/vstudio/_preload.lua b/modules/vstudio/_preload.lua index 9ca891b81..cfe8b2603 100644 --- a/modules/vstudio/_preload.lua +++ b/modules/vstudio/_preload.lua @@ -294,6 +294,21 @@ tokens = "true", } + p.api.register { + name = "dotnetsdk", + scope = "project", + kind = "string", + allowed = { + "Default", + "Web", + "Razor", + "Worker", + "Blazor", + "WindowsDesktop", + "MSTest" + } + } + -- -- Decide when the full module should be loaded. -- diff --git a/modules/vstudio/tests/_tests.lua b/modules/vstudio/tests/_tests.lua index 82e00e14d..f649b751f 100644 --- a/modules/vstudio/tests/_tests.lua +++ b/modules/vstudio/tests/_tests.lua @@ -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", diff --git a/modules/vstudio/tests/cs2005/test_dotnetsdk.lua b/modules/vstudio/tests/cs2005/test_dotnetsdk.lua new file mode 100644 index 000000000..51aa8642f --- /dev/null +++ b/modules/vstudio/tests/cs2005/test_dotnetsdk.lua @@ -0,0 +1,145 @@ +-- +-- tests/actions/vstudio/cs2005/test_dotnetsdk.lua +-- Test DotnetSDK feature Visual Studio 2005+ C# project. +-- Copyright (c) 2012-2024 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, cwd + +-- +-- Setup and teardown +-- + function suite.setup() + cwd = os.getcwd() + os.chdir(_TESTS_DIR) + + p.action.set("vs2010") + wks = test.createWorkspace() + configurations { "Debug", "Release" } + language "C#" + dotnetframework "net8.0" + end + + function suite.teardown() + os.chdir(cwd) + 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 [[ + + ]] + end + + function suite.testDefault() + prepare() + setConfig() + dotnetsdk "Web" + test.capture [[ + + ]] + end + + function suite.testWeb() + prepare() + dotnetsdk "Web" + setConfig() + + test.capture [[ + + ]] + end + + function suite.testRazor() + prepare() + dotnetsdk "Razor" + setConfig() + + test.capture [[ + + ]] + end + + function suite.testWorker() + prepare() + dotnetsdk "Worker" + setConfig() + + test.capture [[ + + ]] + end + + function suite.testBlazor() + prepare() + dotnetsdk "Blazor" + setConfig() + + test.capture [[ + + ]] + end + + function suite.testWindowsDesktop() + prepare() + dotnetsdk "WindowsDesktop" + setConfig() + + test.capture [[ + + ]] + end + + function suite.testMSTest() + prepare() + dotnetsdk "MSTest" + setConfig() + + test.capture [[ + + ]] + end + + function suite.testWPFFlag() + prepare() + dotnetsdk "Web" + flags { "WPF" } + setConfig() + + test.capture [[ + + ]] + end + + function suite.testMSTestGlobalJSON() + prepare() + local cfg = test.getconfig(prj, "Debug") + prj.dotnetsdk = "MSTest" + dn2005.output_global_json(prj) + test.capture[[{"msbuild-sdks":{"MSTest.Sdk": "3.6.1"}}]] + end + + function suite.testMSTestGlobalJSONExists() + prepare() + local cfg = test.getconfig(prj, "Debug") + prj.dotnetsdk = "MSTest" + p.generate(prj, path.join(prj.workspace.location, "global.json"), function() p.outln('{"test":"global"') end) + dn2005.output_global_json(prj) + test.capture[[{"test":"global","msbuild-sdks":{"MSTest.Sdk": "3.6.1"}}]] + end diff --git a/modules/vstudio/vs2005_csproj.lua b/modules/vstudio/vs2005_csproj.lua index 38820e0ac..04945357a 100644 --- a/modules/vstudio/vs2005_csproj.lua +++ b/modules/vstudio/vs2005_csproj.lua @@ -53,6 +53,7 @@ dotnetbase.netcore.useWpf, dotnetbase.csversion, dotnetbase.netcore.enableDefaultCompileItems, + dotnetbase.netcore.dotnetsdk } else return { diff --git a/modules/vstudio/vs2005_dotnetbase.lua b/modules/vstudio/vs2005_dotnetbase.lua index 32cdd4beb..fc2bf043b 100644 --- a/modules/vstudio/vs2005_dotnetbase.lua +++ b/modules/vstudio/vs2005_dotnetbase.lua @@ -57,11 +57,7 @@ function dotnetbase.projectElement(prj) if dotnetbase.isNewFormatProject(prj) then - if prj.flags.WPF then - _p('') - else - _p('') - end + _p('', dotnetbase.netcore.getsdk(prj)) else local ver = '' local action = p.action.current() @@ -820,8 +816,46 @@ 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", + } + + if cfg.flags.WPF then + return map["WindowsDesktop"] + end + + return map[cfg.dotnetsdk or "Default"] + end + + function dotnetbase.netcore.dotnetsdk(cfg) + dotnetbase.generate_global_json(cfg) + end + function dotnetbase.allowUnsafeBlocks(cfg) if cfg.clr == "Unsafe" then _p(2,'true') end end + + function dotnetbase.output_global_json(prj) + if prj.dotnetsdk == "MSTest" then + local globaljson = json.decode(io.readfile(path.join(prj.workspace.location, "global.json"))) or {} + globaljson["msbuild-sdks"] = globaljson["msbuild-sdks"] or {} + globaljson["msbuild-sdks"]["MSTest.Sdk"] = "3.6.1" + + _p(json.encode(globaljson)) + end + end + function dotnetbase.generate_global_json(prj) + local content = p.capture(function() dotnetbase.output_global_json(prj) end) + if content ~= nil and #content > 0 then + p.generate(prj, path.join(prj.workspace.location, "global.json"), function() p.outln(content) end) + end + end diff --git a/website/docs/dotnetsdk.md b/website/docs/dotnetsdk.md new file mode 100644 index 000000000..5dbc57330 --- /dev/null +++ b/website/docs/dotnetsdk.md @@ -0,0 +1,36 @@ +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) + + +## mstest ## +SDK used for MSTest is `"3.6.1"`, to use another version create or update global.json near the solution. +### Applies To ### + +Project configurations. + +### Availability ### + +Premake 5.0 beta3 or later. + +Visual studio is the only toolset currently supported. + +### Examples ### +```lua +dotnetsdk "Web" +``` diff --git a/website/sidebars.js b/website/sidebars.js index 20c9c8b4b..ca76a3d66 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -125,6 +125,7 @@ module.exports = { 'display', 'documentationfile', 'dotnetframework', + 'dotnetsdk', 'dpiawareness', 'editandcontinue', 'editorintegration',