-
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
Open
jviau
wants to merge
1
commit into
feature/msbuild-sdk
Choose a base branch
from
jviau/msbuild-sdk/collect-packages
base: feature/msbuild-sdk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,9 @@ | ||
| // 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; | ||
|
|
||
| internal static class Constants | ||
| { | ||
| 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,48 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace System; | ||
|
|
||
| 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,72 @@ | ||
| // 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 | ||
| 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); | ||
|
|
||
| // We have two 'IsImplicitlyDefined' attributes, one for the package and one for the extension. | ||
| // IsImplicitlyDefined on the ITaskItem indicates the reference is from an extension package. | ||
| // IsImplicitlyDefined on the attribute indicates if the package can be trimmed or not. | ||
| if (!IsImplicitlyDefined(customAttribute)) | ||
| { | ||
| // We can trim this reference if it is NOT implicitly defined. | ||
| // Trimming means we will exclude it if it is not used. | ||
| extensionReference.SetCanTrim(true); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| extensionReference = default; | ||
| return false; | ||
| } | ||
|
|
||
| private static bool IsImplicitlyDefined(CustomAttribute customAttribute) | ||
| { | ||
| return customAttribute.ConstructorArguments.Count >= 3 | ||
| && bool.TryParse( | ||
| customAttribute.ConstructorArguments[2].Value?.ToString(), | ||
| out bool isImplicitlyDefined) | ||
| && isImplicitlyDefined; | ||
| } | ||
| } | ||
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,28 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using Mono.Cecil; | ||
|
|
||
| namespace Azure.Functions.Sdk; | ||
|
|
||
| public class FunctionsAssemblyResolver : DefaultAssemblyResolver | ||
| { | ||
| private static readonly ImmutableHashSet<string> TrustedPlatformAssemblies | ||
| = GetTrustedPlatformAssemblies(); | ||
|
|
||
| public override AssemblyDefinition? Resolve(AssemblyNameReference name) | ||
| { | ||
| // As soon as we get to a TPA we can stop. This helps prevent the odd circular reference | ||
| // with type forwarders as well. | ||
| AssemblyDefinition assemblyDef = base.Resolve(name); | ||
| return TrustedPlatformAssemblies.Contains(assemblyDef.MainModule.FileName) ? null : assemblyDef; | ||
| } | ||
|
|
||
| private static ImmutableHashSet<string> GetTrustedPlatformAssemblies() | ||
| { | ||
| object data = AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES"); | ||
| return data is null ? [] : data.ToString().Split(Path.PathSeparator) | ||
| .ToImmutableHashSet(StringComparer.OrdinalIgnoreCase); | ||
| } | ||
| } |
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,69 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.IO.Abstractions; | ||
| using System.Text.RegularExpressions; | ||
| using Microsoft.Build.Framework; | ||
| using Mono.Cecil; | ||
| using ILogger = NuGet.Common.ILogger; | ||
|
|
||
| namespace Azure.Functions.Sdk; | ||
|
|
||
| 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); | ||
|
|
||
| private readonly IFileSystem _fileSystem; | ||
| private readonly ReaderParameters _readerParameters; | ||
| private readonly FunctionsAssemblyResolver _resolver; | ||
|
|
||
| public FunctionsAssemblyScanner( | ||
| IFileSystem? fileSystem = null, | ||
| IEnumerable<string>? searchDirectories = null) | ||
| { | ||
| _resolver = new(); | ||
| _readerParameters = new ReaderParameters | ||
| { | ||
| AssemblyResolver = _resolver, | ||
| }; | ||
|
|
||
| _fileSystem = fileSystem ?? new FileSystem(); | ||
| if (searchDirectories != null) | ||
| { | ||
| foreach (string directory in searchDirectories) | ||
| { | ||
| _resolver.AddSearchDirectory(directory); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static FunctionsAssemblyScanner FromTaskItems( | ||
| IEnumerable<ITaskItem> items, IFileSystem? fileSystem = null) | ||
| { | ||
| IEnumerable<string> searchDirectories = items | ||
| .Select(item => Path.GetDirectoryName(item.ItemSpec)) | ||
| .Where(dir => !string.IsNullOrEmpty(dir)) | ||
| .Distinct(); | ||
|
|
||
| return new(fileSystem, searchDirectories); | ||
| } | ||
|
|
||
| public static bool IsExcludedPackage(string name) | ||
| { | ||
| return string.IsNullOrEmpty(name) || ExcludedPackagesRegex.IsMatch(name); | ||
| } | ||
|
|
||
| public IEnumerable<WebJobsReference> GetWebJobsReferences(string assembly, ILogger? logger = null) | ||
| { | ||
| AssemblyDefinition definition = ReadAssembly(assembly); | ||
| return WebJobsReference.FromModule(definition, logger); | ||
| } | ||
|
|
||
| private AssemblyDefinition ReadAssembly(string assemblyPath) | ||
| { | ||
| Throw.IfNullOrEmpty(assemblyPath); | ||
| return AssemblyDefinition.ReadAssembly(assemblyPath, _readerParameters); | ||
| } | ||
| } |
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,17 @@ | ||
| // 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; | ||
|
|
||
| public static class LockFileExtensions | ||
| { | ||
| 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)); | ||
| } | ||
| } |
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.