-
Notifications
You must be signed in to change notification settings - Fork 201
[MSBUILD SDK] Add target to resolve extension packages #3224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| namespace Azure.Functions.Sdk; | ||
|
|
||
| /// <summary> | ||
| /// Commonly used constants. | ||
| /// </summary> | ||
| internal static class Constants | ||
| { | ||
| /// <summary> | ||
| /// The folder name where Azure Functions extensions are output. | ||
| /// </summary> | ||
| public const string ExtensionsOutputFolder = ".azurefunctions"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| namespace System; | ||
|
|
||
| /// <summary> | ||
| /// Extensions for exceptions. | ||
| /// </summary> | ||
| internal static class ExceptionExtensions | ||
| { | ||
| /// <summary> | ||
| /// Determines whether the exception is considered fatal and should not be caught. | ||
| /// </summary> | ||
| /// <param name="exception">The exception to check.</param> | ||
| /// <returns></returns> | ||
| public static bool IsFatal(this Exception? exception) | ||
| { | ||
| while (exception is not null) | ||
| { | ||
| if (exception | ||
| is (OutOfMemoryException and not InsufficientMemoryException) | ||
| or AppDomainUnloadedException | ||
| or BadImageFormatException | ||
| or CannotUnloadAppDomainException | ||
| or InvalidProgramException | ||
| or AccessViolationException) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (exception is AggregateException aggregate) | ||
| { | ||
| foreach (Exception inner in aggregate.InnerExceptions) | ||
| { | ||
| if (inner.IsFatal()) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| exception = exception.InnerException; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
| using Mono.Cecil; | ||
|
|
||
| namespace Azure.Functions.Sdk; | ||
|
|
||
| /// <summary> | ||
| /// Represents an Azure Functions extension reference. | ||
| /// </summary> | ||
| public static class ExtensionReference | ||
| { | ||
| private const string ExtensionsInformationType = "Microsoft.Azure.Functions.Worker.Extensions.Abstractions.ExtensionInformationAttribute"; | ||
|
|
||
| /// <summary> | ||
| /// Gets the root path of the Azure Functions SDK module. | ||
| /// </summary> | ||
| /// <param name="path">The path to the assembly.</param> | ||
| /// <param name="sourcePackageId">The source package ID.</param> | ||
| /// <param name="extensionReference">The resulting extension reference, if found.</param> | ||
| public static bool TryGetFromModule( | ||
| string path, | ||
| string sourcePackageId, | ||
| [NotNullWhen(true)] out ITaskItem? extensionReference) | ||
| { | ||
| // Read the assembly from the specified path | ||
| using AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(path); | ||
|
|
||
| // Find the extension attribute | ||
| foreach (CustomAttribute customAttribute in assembly.CustomAttributes) | ||
| { | ||
| if (string.Equals( | ||
| customAttribute.AttributeType.FullName, | ||
| ExtensionsInformationType, | ||
| StringComparison.Ordinal)) | ||
| { | ||
| customAttribute.GetArguments(out string name, out string version); | ||
| extensionReference = new TaskItem(name); | ||
| extensionReference.SetVersion(version); | ||
| extensionReference.SetIsImplicitlyDefined(true); | ||
| extensionReference.SetSourcePackageId(sourcePackageId); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| extensionReference = default; | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Azure.Functions.Sdk; | ||
|
|
||
| /// <summary> | ||
| /// Class to help with scanning functions related assemblies. | ||
| /// </summary> | ||
| public sealed partial class FunctionsAssemblyScanner | ||
| { | ||
| private static readonly Regex ExcludedPackagesRegex = new( | ||
| @"^(System|Azure\.Core|Azure\.Identity|Microsoft\.Bcl|Microsoft\.Extensions|Microsoft\.Identity|Microsoft\.NETCore|Microsoft\.NETStandard|Microsoft\.Win32|Grpc)(\..*|$)", | ||
| RegexOptions.Compiled); | ||
|
|
||
| /// <summary> | ||
| /// Checks if the given package name should be scanned or not. | ||
| /// </summary> | ||
| /// <param name="name">The name of the package.</param> | ||
| /// <returns><c>true</c> if package should be scanned, <c>false</c> otherwise.</returns> | ||
| public static bool ShouldScanPackage(string name) | ||
| { | ||
| return !string.IsNullOrEmpty(name) && !ExcludedPackagesRegex.IsMatch(name); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using NuGet.Packaging; | ||
| using NuGet.ProjectModel; | ||
|
|
||
| namespace Azure.Functions.Sdk; | ||
|
|
||
| /// <summary> | ||
| /// Extensions for NuGet lock files. | ||
| /// </summary> | ||
| public static class LockFileExtensions | ||
| { | ||
| /// <summary> | ||
| /// Gets a package path resolver for the given lock file. | ||
| /// </summary> | ||
| /// <param name="lockFile">The lock file.</param> | ||
| /// <returns></returns> | ||
| public static FallbackPackagePathResolver GetPathResolver(this LockFile lockFile) | ||
| { | ||
| Throw.IfNull(lockFile); | ||
| string? userPackageFolder = lockFile.PackageFolders.FirstOrDefault()?.Path; | ||
| return new(userPackageFolder, lockFile.PackageFolders.Skip(1).Select(folder => folder.Path)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
| using NuGet.Common; | ||
|
|
||
| namespace Azure.Functions.Sdk; | ||
|
|
||
| /// <summary> | ||
| /// Provides an implementation of the NuGet logger that routes log messages to MSBuild's logging infrastructure. | ||
| /// </summary> | ||
| /// <param name="logger">The MSBuild task logging helper used to record log messages. Cannot be null.</param> | ||
| internal sealed class MSBuildNugetLogger(TaskLoggingHelper logger) | ||
| : NuGet.Common.ILogger | ||
| { | ||
| private readonly TaskLoggingHelper _logger = Throw.IfNull(logger); | ||
|
|
||
| private delegate void LogMessageWithDetails(string subcategory, | ||
| string code, | ||
| string helpKeyword, | ||
| string? file, | ||
| int lineNumber, | ||
| int columnNumber, | ||
| int endLineNumber, | ||
| int endColumnNumber, | ||
| MessageImportance importance, | ||
| string message, | ||
| params object[] messageArgs); | ||
|
|
||
| private delegate void LogErrorWithDetails(string subcategory, | ||
| string code, | ||
| string helpKeyword, | ||
| string? file, | ||
| int lineNumber, | ||
| int columnNumber, | ||
| int endLineNumber, | ||
| int endColumnNumber, | ||
| string message, | ||
| params object[] messageArgs); | ||
|
|
||
| private delegate void LogMessageAsString(MessageImportance importance, | ||
| string message, | ||
| params object[] messageArgs); | ||
|
|
||
| private delegate void LogErrorAsString(string message, | ||
| params object[] messageArgs); | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Log(LogLevel level, string data) | ||
| { | ||
| switch (level) | ||
| { | ||
| case LogLevel.Warning: | ||
| _logger.LogWarning(data); | ||
| break; | ||
| case LogLevel.Error: | ||
| _logger.LogError(data); | ||
| break; | ||
| default: | ||
| MessageImportance importance = Convert(level); | ||
| _logger.LogMessage(importance, data); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Log(ILogMessage message) | ||
| { | ||
| INuGetLogMessage nugetMessage = message switch | ||
| { | ||
| INuGetLogMessage nugetLogMessage => nugetLogMessage, | ||
| _ => new RestoreLogMessage(message.Level, message.Message) | ||
| { | ||
| Code = message.Code, | ||
| FilePath = message.ProjectPath, | ||
| ProjectPath = message.ProjectPath, | ||
| }, | ||
| }; | ||
|
|
||
| if (nugetMessage.Code == NuGetLogCode.Undefined) | ||
| { | ||
| Log(nugetMessage.Level, nugetMessage.Message); | ||
| } | ||
| else | ||
| { | ||
| string enumName = Enum.GetName(typeof(NuGetLogCode), nugetMessage.Code); | ||
| switch (nugetMessage.Level) | ||
| { | ||
| case LogLevel.Warning: | ||
| _logger.LogWarning(string.Empty, | ||
| enumName, | ||
| enumName, | ||
| nugetMessage.FilePath, | ||
| nugetMessage.StartLineNumber, | ||
| nugetMessage.StartColumnNumber, | ||
| nugetMessage.EndLineNumber, | ||
| nugetMessage.EndColumnNumber, | ||
| nugetMessage.Message); | ||
| break; | ||
| case LogLevel.Error: | ||
| _logger.LogError(string.Empty, | ||
| enumName, | ||
| enumName, | ||
| nugetMessage.FilePath, | ||
| nugetMessage.StartLineNumber, | ||
| nugetMessage.StartColumnNumber, | ||
| nugetMessage.EndLineNumber, | ||
| nugetMessage.EndColumnNumber, | ||
| nugetMessage.Message); | ||
| break; | ||
| default: | ||
| _logger.LogMessage(string.Empty, | ||
| enumName, | ||
| enumName, | ||
| nugetMessage.FilePath, | ||
| nugetMessage.StartLineNumber, | ||
| nugetMessage.StartColumnNumber, | ||
| nugetMessage.EndLineNumber, | ||
| nugetMessage.EndColumnNumber, | ||
| Convert(nugetMessage.Level), | ||
| nugetMessage.Message); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public System.Threading.Tasks.Task LogAsync(LogLevel level, string data) | ||
| { | ||
| Log(level, data); | ||
| return System.Threading.Tasks.Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public System.Threading.Tasks.Task LogAsync(ILogMessage message) | ||
| { | ||
| Log(message); | ||
| return System.Threading.Tasks.Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LogDebug(string data) | ||
| { | ||
| Log(LogLevel.Debug, data); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LogError(string data) | ||
| { | ||
| Log(LogLevel.Error, data); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LogInformation(string data) | ||
| { | ||
| Log(LogLevel.Information, data); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LogInformationSummary(string data) | ||
| { | ||
| LogInformation(data); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LogMinimal(string data) | ||
| { | ||
| Log(LogLevel.Minimal, data); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LogVerbose(string data) | ||
| { | ||
| Log(LogLevel.Verbose, data); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void LogWarning(string data) | ||
| { | ||
| Log(LogLevel.Warning, data); | ||
| } | ||
|
|
||
| private static MessageImportance Convert(LogLevel level) | ||
| { | ||
| return level switch | ||
| { | ||
| LogLevel.Debug => MessageImportance.Low, | ||
| LogLevel.Verbose => MessageImportance.Low, | ||
| LogLevel.Information => MessageImportance.Normal, | ||
| LogLevel.Minimal => MessageImportance.High, | ||
| _ => MessageImportance.Low, | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.