Skip to content

Commit bb8c53e

Browse files
Add .NET 7 Support (#3088)
* Update templates for in-proc and isolated * Added helper methods for .NET 7 project deployment * Update isolated templates version * Alter template package source and modify dotnet project deployment * Add the target-framework parameter * Corrected typo * Updated .nupkg versions
1 parent c3eae69 commit bb8c53e

File tree

6 files changed

+69
-13
lines changed

6 files changed

+69
-13
lines changed

build/BuildSteps.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public static void AddDistLib()
240240
public static void AddTemplatesNupkgs()
241241
{
242242
var templatesPath = Path.Combine(Settings.OutputDir, "nupkg-templates");
243-
var isolatedTemplatesPath = Path.Combine(templatesPath, "net6-isolated");
243+
var isolatedTemplatesPath = Path.Combine(templatesPath, "net-isolated");
244244

245245
Directory.CreateDirectory(templatesPath);
246246
Directory.CreateDirectory(isolatedTemplatesPath);

build/Settings.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ private static string config(string @default = null, [CallerMemberName] string k
1919
: value;
2020
}
2121

22-
public const string DotnetIsolatedItemTemplatesVersion = "4.0.2057";
23-
public const string DotnetIsolatedProjectTemplatesVersion = "4.0.2057";
24-
public const string DotnetItemTemplatesVersion = "4.0.2057";
25-
public const string DotnetProjectTemplatesVersion = "4.0.2057";
22+
public const string DotnetIsolatedItemTemplatesVersion = "4.0.2226";
23+
public const string DotnetIsolatedProjectTemplatesVersion = "4.0.2226";
24+
public const string DotnetItemTemplatesVersion = "4.0.2185";
25+
public const string DotnetProjectTemplatesVersion = "4.0.2185";
2626
public const string TemplateJsonVersion = "3.1.1648";
2727

2828
public static readonly string SBOMManifestToolPath = Path.GetFullPath("../ManifestTool/Microsoft.ManifestTool.dll");
@@ -108,7 +108,7 @@ private static string config(string @default = null, [CallerMemberName] string k
108108
public static readonly string PreSignTestDir = "PreSignTest";
109109

110110
public static readonly string SignTestDir = "SignTest";
111-
111+
112112
public static readonly string DotnetIsolatedItemTemplates = $"https://www.nuget.org/api/v2/package/Microsoft.Azure.Functions.Worker.ItemTemplates/{DotnetIsolatedItemTemplatesVersion}";
113113

114114
public static readonly string DotnetIsolatedProjectTemplates = $"https://www.nuget.org/api/v2/package/Microsoft.Azure.Functions.Worker.ProjectTemplates/{DotnetIsolatedProjectTemplatesVersion}";

src/Azure.Functions.Cli/Actions/LocalActions/InitAction.cs

+31-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ internal class InitAction : BaseAction
4242

4343
public string Language { get; set; }
4444

45+
public string TargetFramework { get; set; }
46+
4547
public bool? ManagedDependencies { get; set; }
4648

4749
public WorkerRuntime ResolvedWorkerRuntime { get; set; }
@@ -107,6 +109,11 @@ public override ICommandLineParserResult ParseArgs(string[] args)
107109
.WithDescription("Initialize a language specific project. Currently supported when --worker-runtime set to node. Options are - \"typescript\" and \"javascript\"")
108110
.Callback(l => Language = l);
109111

112+
Parser
113+
.Setup<string>("target-framework")
114+
.WithDescription("Initialize a project with the given target framework moniker. Currently supported only when --worker-runtime set to dotnet-isolated. Options are - \"net48\", \"net6.0\", and \"net7.0\"")
115+
.Callback(tf => TargetFramework = tf);
116+
110117
Parser
111118
.Setup<bool>("managed-dependencies")
112119
.WithDescription("Installs managed dependencies. Currently, only the PowerShell worker runtime supports this functionality.")
@@ -170,10 +177,11 @@ private async Task InitFunctionAppProject()
170177
}
171178

172179
TelemetryHelpers.AddCommandEventToDictionary(TelemetryCommandEvents, "WorkerRuntime", ResolvedWorkerRuntime.ToString());
173-
180+
181+
ValidateTargetFramework();
174182
if (WorkerRuntimeLanguageHelper.IsDotnet(ResolvedWorkerRuntime) && !Csx)
175183
{
176-
await DotnetHelpers.DeployDotnetProject(Utilities.SanitizeLiteral(Path.GetFileName(Environment.CurrentDirectory), allowed: "-"), Force, ResolvedWorkerRuntime);
184+
await DotnetHelpers.DeployDotnetProject(Utilities.SanitizeLiteral(Path.GetFileName(Environment.CurrentDirectory), allowed: "-"), Force, ResolvedWorkerRuntime, TargetFramework);
177185
}
178186
else
179187
{
@@ -298,6 +306,27 @@ private static async Task InitLanguageSpecificArtifacts(WorkerRuntime workerRunt
298306
}
299307
}
300308

309+
private void ValidateTargetFramework()
310+
{
311+
if (ResolvedWorkerRuntime == Helpers.WorkerRuntime.dotnetIsolated)
312+
{
313+
if (string.IsNullOrEmpty(TargetFramework))
314+
{
315+
// Default to .NET 6 if the target framework is not specified
316+
// NOTE: we must have TargetFramework be non-empty for a dotnet-isolated project, even if it is not specified by the user, due to the structure of the new templates
317+
TargetFramework = Common.TargetFramework.net6;
318+
}
319+
if (!TargetFrameworkHelper.GetSupportedTargetFrameworks().Contains(TargetFramework, StringComparer.InvariantCultureIgnoreCase))
320+
{
321+
throw new CliArgumentsException($"Unable to parse target framework {TargetFramework}. Valid options are \"net48\", \"net6.0\", and \"net7.0\"");
322+
}
323+
}
324+
else if (!string.IsNullOrEmpty(TargetFramework))
325+
{
326+
throw new CliArgumentsException("The --target-framework option is supported only when --worker-runtime is set to dotnet-isolated");
327+
}
328+
}
329+
301330
private static async Task WriteLocalSettingsJson(WorkerRuntime workerRuntime)
302331
{
303332
var localSettingsJsonContent = await StaticResources.LocalSettingsJson;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Azure.Functions.Cli.Common
2+
{
3+
public static class TargetFramework
4+
{
5+
public const string net48 = "net48";
6+
public const string net6 = "net6.0";
7+
public const string net7 = "net7.0";
8+
}
9+
}

src/Azure.Functions.Cli/Helpers/DotnetHelpers.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ public static void EnsureDotnet()
2525
}
2626
}
2727

28-
public async static Task DeployDotnetProject(string Name, bool force, WorkerRuntime workerRuntime)
28+
public async static Task DeployDotnetProject(string Name, bool force, WorkerRuntime workerRuntime, string targetFramework = "")
2929
{
3030
await TemplateOperation(async () =>
3131
{
32+
var frameworkString = string.IsNullOrEmpty(targetFramework)
33+
? string.Empty
34+
: $"--Framework \"{targetFramework}\"";
3235
var connectionString = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
3336
? $"--StorageConnectionStringValue \"{Constants.StorageEmulatorConnectionString}\""
3437
: string.Empty;
35-
var exe = new Executable("dotnet", $"new func --AzureFunctionsVersion v4 --name {Name} {connectionString} {(force ? "--force" : string.Empty)}");
38+
var exe = new Executable("dotnet", $"new func {frameworkString} --AzureFunctionsVersion v4 --name {Name} {connectionString} {(force ? "--force" : string.Empty)}");
3639
var exitCode = await exe.RunAsync(o => { }, e => ColoredConsole.Error.WriteLine(ErrorColor(e)));
3740
if (exitCode != 0)
3841
{
@@ -206,7 +209,7 @@ private static Task TemplateOperation(Func<Task> action, WorkerRuntime workerRun
206209
}
207210
else
208211
{
209-
return WebJobsTemplateOpetation(action);
212+
return WebJobsTemplateOperation(action);
210213
}
211214
}
212215

@@ -224,7 +227,7 @@ private static async Task IsolatedTemplateOperation(Func<Task> action)
224227
}
225228
}
226229

227-
private static async Task WebJobsTemplateOpetation(Func<Task> action)
230+
private static async Task WebJobsTemplateOperation(Func<Task> action)
228231
{
229232
try
230233
{
@@ -264,7 +267,7 @@ private static async Task UninstallWebJobsTemplates()
264267

265268
private static Task InstallWebJobsTemplates() => DotnetTemplatesAction("install", "templates");
266269

267-
private static Task InstallIsolatedTemplates() => DotnetTemplatesAction("install", Path.Combine("templates", "net6-isolated"));
270+
private static Task InstallIsolatedTemplates() => DotnetTemplatesAction("install", Path.Combine("templates", $"net-isolated"));
268271

269272
private static async Task DotnetTemplatesAction(string action, string templateDirectory)
270273
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using Azure.Functions.Cli.Common;
3+
4+
namespace Azure.Functions.Cli.Helpers
5+
{
6+
public static class TargetFrameworkHelper
7+
{
8+
private static IEnumerable<string> supportedTargetFrameworks = new string[] { TargetFramework.net48, TargetFramework.net6, TargetFramework.net7 };
9+
10+
public static IEnumerable<string> GetSupportedTargetFrameworks()
11+
{
12+
return supportedTargetFrameworks;
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)