Skip to content

Commit 0793f40

Browse files
committed
Migrate SDK to C#14 extension syntax
1 parent 1a90683 commit 0793f40

File tree

8 files changed

+292
-258
lines changed

8 files changed

+292
-258
lines changed

src/Azure.Functions.Sdk/ExceptionExtensions.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,45 @@ namespace System;
88
/// </summary>
99
internal static class ExceptionExtensions
1010
{
11-
/// <summary>
12-
/// Determines whether the exception is considered fatal and should not be caught.
13-
/// </summary>
14-
/// <param name="exception">The exception to check.</param>
15-
/// <returns></returns>
16-
public static bool IsFatal(this Exception? exception)
11+
extension(Exception? exception)
1712
{
18-
while (exception is not null)
13+
/// <summary>
14+
/// Determines whether the exception is considered fatal and should not be caught.
15+
/// </summary>
16+
/// <param name="exception">The exception to check.</param>
17+
/// <returns></returns>
18+
public bool IsFatal()
1919
{
20-
if (exception
21-
is (OutOfMemoryException and not InsufficientMemoryException)
22-
or AppDomainUnloadedException
23-
or BadImageFormatException
24-
or CannotUnloadAppDomainException
25-
or InvalidProgramException
26-
or AccessViolationException)
20+
while (exception is not null)
2721
{
28-
return true;
29-
}
22+
if (exception
23+
is (OutOfMemoryException and not InsufficientMemoryException)
24+
or AppDomainUnloadedException
25+
or BadImageFormatException
26+
or CannotUnloadAppDomainException
27+
or InvalidProgramException
28+
or AccessViolationException)
29+
{
30+
return true;
31+
}
3032

31-
if (exception is AggregateException aggregate)
32-
{
33-
foreach (Exception inner in aggregate.InnerExceptions)
33+
if (exception is AggregateException aggregate)
3434
{
35-
if (inner.IsFatal())
35+
foreach (Exception inner in aggregate.InnerExceptions)
3636
{
37-
return true;
37+
if (inner.IsFatal())
38+
{
39+
return true;
40+
}
3841
}
3942
}
43+
else
44+
{
45+
exception = exception.InnerException;
46+
}
4047
}
41-
else
42-
{
43-
exception = exception.InnerException;
44-
}
45-
}
4648

47-
return false;
49+
return false;
50+
}
4851
}
4952
}
Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using NuGet.Common;
45
using NuGet.Packaging;
56
using NuGet.ProjectModel;
67

@@ -11,15 +12,47 @@ namespace Azure.Functions.Sdk;
1112
/// </summary>
1213
public static class LockFileExtensions
1314
{
14-
/// <summary>
15-
/// Gets a package path resolver for the given lock file.
16-
/// </summary>
17-
/// <param name="lockFile">The lock file.</param>
18-
/// <returns></returns>
19-
public static FallbackPackagePathResolver GetPathResolver(this LockFile lockFile)
15+
extension(LockFile lockFile)
2016
{
21-
Throw.IfNull(lockFile);
22-
string? userPackageFolder = lockFile.PackageFolders.FirstOrDefault()?.Path;
23-
return new(userPackageFolder, lockFile.PackageFolders.Skip(1).Select(folder => folder.Path));
17+
/// <summary>
18+
/// Reads a lock file from the given path.
19+
/// </summary>
20+
/// <param name="path">The path to read.</param>
21+
/// <param name="logger">The optional logger.</param>
22+
/// <returns>The read <see cref="LockFile"/>.</returns>
23+
public static LockFile Read(string path, ILogger? logger = null)
24+
{
25+
Throw.IfNullOrEmpty(path);
26+
LockFileFormat format = new();
27+
return logger is null ? format.Read(path) : format.Read(path, logger);
28+
}
29+
30+
/// <summary>
31+
/// Reads a lock file from the given stream.
32+
/// </summary>
33+
/// <param name="path">The path for <see cref="LockFile.Path"/>.</param>
34+
/// <param name="stream">The stream to read.</param>
35+
/// <param name="logger">The optional logger.</param>
36+
/// <returns>The read <see cref="LockFile"/>.</returns>
37+
public static LockFile Read(string path, Stream stream, ILogger? logger = null)
38+
{
39+
Throw.IfNullOrEmpty(path);
40+
Throw.IfNull(stream);
41+
42+
LockFileFormat format = new();
43+
return logger is null ? format.Read(stream, path) : format.Read(stream, logger, path);
44+
}
45+
46+
/// <summary>
47+
/// Gets a package path resolver for the given lock file.
48+
/// </summary>
49+
/// <param name="lockFile">The lock file.</param>
50+
/// <returns></returns>
51+
public FallbackPackagePathResolver GetPathResolver()
52+
{
53+
Throw.IfNull(lockFile);
54+
string? userPackageFolder = lockFile.PackageFolders.FirstOrDefault()?.Path;
55+
return new(userPackageFolder, lockFile.PackageFolders.Skip(1).Select(folder => folder.Path));
56+
}
2457
}
2558
}

src/Azure.Functions.Sdk/TaskItemExtensions.cs

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,69 @@ namespace Azure.Functions.Sdk;
1111
/// </summary>
1212
public static class TaskItemExtensions
1313
{
14-
/// <summary>
15-
/// Gets the "Version" metadata from the task item.
16-
/// </summary>
17-
/// <param name="taskItem">The task item.</param>
18-
/// <returns>The version metadata, or empty string if not present.</returns>
19-
public static string GetVersion(this ITaskItem taskItem)
14+
extension(ITaskItem taskItem)
2015
{
21-
return taskItem.GetMetadata("Version") ?? string.Empty;
22-
}
16+
/// <summary>
17+
/// Gets the "Version" metadata from the task item.
18+
/// </summary>
19+
/// <param name="taskItem">The task item.</param>
20+
/// <returns>The version metadata, or empty string if not present.</returns>
21+
public string GetVersion()
22+
{
23+
return taskItem.GetMetadata("Version") ?? string.Empty;
24+
}
2325

24-
/// <summary>
25-
/// Sets the "Version" metadata on the task item.
26-
/// </summary>
27-
/// <param name="taskItem">The task item.</param>
28-
/// <param name="version">The version to set.</param>
29-
public static void SetVersion(this ITaskItem taskItem, string version)
30-
{
31-
taskItem.SetMetadata("Version", version);
32-
}
26+
/// <summary>
27+
/// Sets the "Version" metadata on the task item.
28+
/// </summary>
29+
/// <param name="taskItem">The task item.</param>
30+
/// <param name="version">The version to set.</param>
31+
public void SetVersion(string version)
32+
{
33+
taskItem.SetMetadata("Version", version);
34+
}
3335

34-
/// <summary>
35-
/// Gets the "IsImplicitlyDefined" metadata from the task item.
36-
/// </summary>
37-
/// <param name="taskItem">The task item.</param>
38-
/// <returns><c>true</c> if implicitly defined, <c>false</c> otherwise.</returns>
39-
public static bool GetIsImplicitlyDefined(this ITaskItem taskItem)
40-
{
41-
return bool.TryParse(taskItem.GetMetadata("IsImplicitlyDefined"), out bool isImplicitlyDefined)
42-
&& isImplicitlyDefined;
43-
}
36+
/// <summary>
37+
/// Gets the "IsImplicitlyDefined" metadata from the task item.
38+
/// </summary>
39+
/// <param name="taskItem">The task item.</param>
40+
/// <returns><c>true</c> if implicitly defined, <c>false</c> otherwise.</returns>
41+
public bool GetIsImplicitlyDefined()
42+
{
43+
return bool.TryParse(taskItem.GetMetadata("IsImplicitlyDefined"), out bool isImplicitlyDefined)
44+
&& isImplicitlyDefined;
45+
}
4446

45-
/// <summary>
46-
/// Sets the "IsImplicitlyDefined" metadata on the task item.
47-
/// </summary>
48-
/// <param name="taskItem">The task item.</param>
49-
/// <param name="isImplicitlyDefined">The value to set.</param>
50-
public static void SetIsImplicitlyDefined(this ITaskItem taskItem, bool isImplicitlyDefined)
51-
{
52-
taskItem.SetMetadata("IsImplicitlyDefined", isImplicitlyDefined.ToString().ToLowerInvariant());
53-
}
47+
/// <summary>
48+
/// Sets the "IsImplicitlyDefined" metadata on the task item.
49+
/// </summary>
50+
/// <param name="taskItem">The task item.</param>
51+
/// <param name="isImplicitlyDefined">The value to set.</param>
52+
public void SetIsImplicitlyDefined(bool isImplicitlyDefined)
53+
{
54+
taskItem.SetMetadata("IsImplicitlyDefined", isImplicitlyDefined.ToString().ToLowerInvariant());
55+
}
5456

55-
/// <summary>
56-
/// Gets the "SourcePackageId" metadata from the task item.
57-
/// </summary>
58-
/// <param name="taskItem">The task item.</param>
59-
/// <param name="packageId">The package ID, if found.</param>
60-
/// <returns><c>true</c> if "SourcePackageId" is found, <c>false</c> if not found.</returns>
61-
public static bool TryGetSourcePackageId(
62-
this ITaskItem taskItem, [NotNullWhen(true)] out string? packageId)
63-
{
64-
packageId = taskItem.GetMetadata("SourcePackageId");
65-
return !string.IsNullOrEmpty(packageId);
66-
}
57+
/// <summary>
58+
/// Gets the "SourcePackageId" metadata from the task item.
59+
/// </summary>
60+
/// <param name="taskItem">The task item.</param>
61+
/// <param name="packageId">The package ID, if found.</param>
62+
/// <returns><c>true</c> if "SourcePackageId" is found, <c>false</c> if not found.</returns>
63+
public bool TryGetSourcePackageId([NotNullWhen(true)] out string? packageId)
64+
{
65+
packageId = taskItem.GetMetadata("SourcePackageId");
66+
return !string.IsNullOrEmpty(packageId);
67+
}
6768

68-
/// <summary>
69-
/// Sets the "SourcePackageId" metadata on the task item.
70-
/// </summary>
71-
/// <param name="taskItem">The task item.</param>
72-
/// <param name="sourcePackageId">The source package ID to set.</param>
73-
public static void SetSourcePackageId(this ITaskItem taskItem, string sourcePackageId)
74-
{
75-
taskItem.SetMetadata("SourcePackageId", sourcePackageId);
69+
/// <summary>
70+
/// Sets the "SourcePackageId" metadata on the task item.
71+
/// </summary>
72+
/// <param name="taskItem">The task item.</param>
73+
/// <param name="sourcePackageId">The source package ID to set.</param>
74+
public void SetSourcePackageId(string sourcePackageId)
75+
{
76+
taskItem.SetMetadata("SourcePackageId", sourcePackageId);
77+
}
7678
}
7779
}

src/Azure.Functions.Sdk/Tasks/Extensions/ResolveExtensionPackages.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ private bool TryGetLockFile([NotNullWhen(true)] out LockFile? lockFile)
9696
return false;
9797
}
9898

99-
IFileInfo info = _fileSystem.FileInfo.New(ProjectAssetsFile);
100-
using FileSystemStream stream = info.OpenRead();
101-
LockFileFormat format = new();
102-
lockFile = format.Read(stream, new MSBuildNugetLogger(Log), ProjectAssetsFile);
99+
using FileSystemStream stream = _fileSystem.File.OpenRead(ProjectAssetsFile);
100+
lockFile = LockFile.Read(ProjectAssetsFile, stream, new MSBuildNugetLogger(Log));
103101
return true;
104102
}
105103

0 commit comments

Comments
 (0)