From cac10d230809ba50f209100462d735d1ff784462 Mon Sep 17 00:00:00 2001 From: Norm Johanson Date: Fri, 29 Nov 2024 16:04:21 -0800 Subject: [PATCH 1/2] When using "dotnet msbuild getproperty" command to evaluate MSBuild parameters include user specified msbuild parameters. --- .../79b678d7-6533-4e17-ae69-100730e43027.json | 11 +++++++++ .../Utilities.cs | 24 ++++++++++++------- .../Commands/DeployEnvironmentCommand.cs | 2 +- .../Commands/PackageCommand.cs | 2 +- .../Commands/DeployFunctionCommand.cs | 4 ++-- .../Commands/PackageCommand.cs | 4 ++-- src/Amazon.Lambda.Tools/LambdaPackager.cs | 2 +- src/Amazon.Lambda.Tools/LambdaUtilities.cs | 8 +++---- .../TemplateProcessorManager.cs | 2 +- .../UtilitiesTests.cs | 16 +++++++++++-- .../UtilitiesTests.cs | 4 ++-- 11 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 .autover/changes/79b678d7-6533-4e17-ae69-100730e43027.json diff --git a/.autover/changes/79b678d7-6533-4e17-ae69-100730e43027.json b/.autover/changes/79b678d7-6533-4e17-ae69-100730e43027.json new file mode 100644 index 0000000..03cc7fe --- /dev/null +++ b/.autover/changes/79b678d7-6533-4e17-ae69-100730e43027.json @@ -0,0 +1,11 @@ +{ + "Projects": [ + { + "Name": "Amazon.Lambda.Tools", + "Type": "Patch", + "ChangelogMessages": [ + "Include user specified MSBuild parameters when evaluating MSBuild properties" + ] + } + ] +} \ No newline at end of file diff --git a/src/Amazon.Common.DotNetCli.Tools/Utilities.cs b/src/Amazon.Common.DotNetCli.Tools/Utilities.cs index f067728..fbd36a7 100644 --- a/src/Amazon.Common.DotNetCli.Tools/Utilities.cs +++ b/src/Amazon.Common.DotNetCli.Tools/Utilities.cs @@ -211,9 +211,10 @@ public static string DeterminePublishLocation(string workingDirectory, string pr /// Looks up specified properties from a project. /// /// The location of the project file. + /// Additonal MSBuild paramteres passed by the user from the commandline /// The names of the properties to look up. /// A dictionary of property names and their values. - public static Dictionary LookupProjectProperties(string projectLocation, params string[] propertyNames) + public static Dictionary LookupProjectProperties(string projectLocation, string msBuildParameters, params string[] propertyNames) { var projectFile = FindProjectFileInDirectory(projectLocation); var properties = new Dictionary(); @@ -225,6 +226,11 @@ public static Dictionary LookupProjectProperties(string projectL $"--getProperty:{string.Join(',', propertyNames)}" }; + if (!string.IsNullOrEmpty(msBuildParameters)) + { + arguments.Add(msBuildParameters); + } + var process = new Process { StartInfo = new ProcessStartInfo @@ -302,16 +308,17 @@ private static Dictionary LookupProjectPropertiesFromXml(string { } return properties; - } + } /// /// Looks up the target framework from a project file. /// /// The location of the project file. + /// Additonal MSBuild paramteres passed by the user from the commandline /// The target framework of the project. - public static string LookupTargetFrameworkFromProjectFile(string projectLocation) + public static string LookupTargetFrameworkFromProjectFile(string projectLocation, string msBuildParameters) { - var properties = LookupProjectProperties(projectLocation, "TargetFramework", "TargetFrameworks"); + var properties = LookupProjectProperties(projectLocation, msBuildParameters, "TargetFramework", "TargetFrameworks"); if (properties.TryGetValue("TargetFramework", out var targetFramework) && !string.IsNullOrEmpty(targetFramework)) { return targetFramework; @@ -331,10 +338,11 @@ public static string LookupTargetFrameworkFromProjectFile(string projectLocation /// Retrieve the `OutputType` property of a given project /// /// Path of the project + /// Additonal MSBuild paramteres passed by the user from the commandline /// The value of the `OutputType` property - public static string LookupOutputTypeFromProjectFile(string projectLocation) + public static string LookupOutputTypeFromProjectFile(string projectLocation, string msBuildParameters) { - var properties = LookupProjectProperties(projectLocation, "OutputType"); + var properties = LookupProjectProperties(projectLocation, msBuildParameters, "OutputType"); return properties.TryGetValue("OutputType", out var outputType) ? outputType.Trim() : null; } @@ -353,7 +361,7 @@ public static bool LookPublishAotFlag(string projectLocation, string msBuildPara } } - var properties = LookupProjectProperties(projectLocation, "PublishAot"); + var properties = LookupProjectProperties(projectLocation, msBuildParameters, "PublishAot"); if (properties.TryGetValue("PublishAot", out var publishAot)) { return bool.TryParse(publishAot, out var result) && result; @@ -369,7 +377,7 @@ public static bool HasExplicitSelfContainedFlag(string projectLocation, string m return true; } - var properties = LookupProjectProperties(projectLocation, "SelfContained"); + var properties = LookupProjectProperties(projectLocation, msBuildParameters, "SelfContained"); if (properties.TryGetValue("SelfContained", out var selfContained)) { return bool.TryParse(selfContained, out var isSelfContained) && isSelfContained; diff --git a/src/Amazon.ElasticBeanstalk.Tools/Commands/DeployEnvironmentCommand.cs b/src/Amazon.ElasticBeanstalk.Tools/Commands/DeployEnvironmentCommand.cs index a7f6b38..07505f1 100644 --- a/src/Amazon.ElasticBeanstalk.Tools/Commands/DeployEnvironmentCommand.cs +++ b/src/Amazon.ElasticBeanstalk.Tools/Commands/DeployEnvironmentCommand.cs @@ -138,7 +138,7 @@ protected override async Task PerformActionAsync() if (string.IsNullOrEmpty(targetFramework)) { - targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation); + targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, null); if (string.IsNullOrEmpty(targetFramework)) { targetFramework = this.GetStringValueOrDefault(this.DeployEnvironmentOptions.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, true); diff --git a/src/Amazon.ElasticBeanstalk.Tools/Commands/PackageCommand.cs b/src/Amazon.ElasticBeanstalk.Tools/Commands/PackageCommand.cs index 47b2357..917d6cc 100644 --- a/src/Amazon.ElasticBeanstalk.Tools/Commands/PackageCommand.cs +++ b/src/Amazon.ElasticBeanstalk.Tools/Commands/PackageCommand.cs @@ -64,7 +64,7 @@ protected override Task PerformActionAsync() if (string.IsNullOrEmpty(targetFramework)) { - targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation); + targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, null); if (string.IsNullOrEmpty(targetFramework)) { targetFramework = this.GetStringValueOrDefault(this.DeployEnvironmentOptions.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, true); diff --git a/src/Amazon.Lambda.Tools/Commands/DeployFunctionCommand.cs b/src/Amazon.Lambda.Tools/Commands/DeployFunctionCommand.cs index e5ec667..34e4805 100644 --- a/src/Amazon.Lambda.Tools/Commands/DeployFunctionCommand.cs +++ b/src/Amazon.Lambda.Tools/Commands/DeployFunctionCommand.cs @@ -227,10 +227,11 @@ protected override async Task PerformActionAsync() // Release will be the default configuration if nothing set. string configuration = this.GetStringValueOrDefault(this.Configuration, CommonDefinedCommandOptions.ARGUMENT_CONFIGURATION, false); + string msbuildParameters = this.GetStringValueOrDefault(this.MSBuildParameters, CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS, false); var targetFramework = this.GetStringValueOrDefault(this.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, false); if (string.IsNullOrEmpty(targetFramework)) { - targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation); + targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, msbuildParameters); // If we still don't know what the target framework is ask the user what targetframework to use. // This is common when a project is using multi targeting. @@ -239,7 +240,6 @@ protected override async Task PerformActionAsync() targetFramework = this.GetStringValueOrDefault(this.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, true); } } - string msbuildParameters = this.GetStringValueOrDefault(this.MSBuildParameters, CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS, false); bool isNativeAot = Utilities.LookPublishAotFlag(projectLocation, this.MSBuildParameters); diff --git a/src/Amazon.Lambda.Tools/Commands/PackageCommand.cs b/src/Amazon.Lambda.Tools/Commands/PackageCommand.cs index f6aae61..518a603 100644 --- a/src/Amazon.Lambda.Tools/Commands/PackageCommand.cs +++ b/src/Amazon.Lambda.Tools/Commands/PackageCommand.cs @@ -206,10 +206,11 @@ protected override async Task PerformActionAsync() // Release will be the default configuration if nothing set. var configuration = this.GetStringValueOrDefault(this.Configuration, CommonDefinedCommandOptions.ARGUMENT_CONFIGURATION, false); + var msbuildParameters = this.GetStringValueOrDefault(this.MSBuildParameters, CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS, false); var targetFramework = this.GetStringValueOrDefault(this.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, false); if (string.IsNullOrEmpty(targetFramework)) { - targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation); + targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, msbuildParameters); // If we still don't know what the target framework is ask the user what targetframework to use. // This is common when a project is using multi targeting. @@ -221,7 +222,6 @@ protected override async Task PerformActionAsync() bool isNativeAot = Utilities.LookPublishAotFlag(projectLocation, this.MSBuildParameters); - var msbuildParameters = this.GetStringValueOrDefault(this.MSBuildParameters, CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS, false); var architecture = this.GetStringValueOrDefault(this.Architecture, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_ARCHITECTURE, false); var disableVersionCheck = this.GetBoolValueOrDefault(this.DisableVersionCheck, LambdaDefinedCommandOptions.ARGUMENT_DISABLE_VERSION_CHECK, false).GetValueOrDefault(); diff --git a/src/Amazon.Lambda.Tools/LambdaPackager.cs b/src/Amazon.Lambda.Tools/LambdaPackager.cs index 0580c4e..ae0382e 100644 --- a/src/Amazon.Lambda.Tools/LambdaPackager.cs +++ b/src/Amazon.Lambda.Tools/LambdaPackager.cs @@ -114,7 +114,7 @@ public static bool CreateApplicationBundle(LambdaToolsDefaults defaults, IToolLo bool? useContainerForBuild, string containerImageForBuild, string codeMountDirectory, out string publishLocation, ref string zipArchivePath) { - LambdaUtilities.ValidateTargetFramework(projectLocation, targetFramework, isNativeAot); + LambdaUtilities.ValidateTargetFramework(projectLocation, msbuildParameters, targetFramework, isNativeAot); LambdaUtilities.ValidateNativeAotArchitecture(architecture, isNativeAot); diff --git a/src/Amazon.Lambda.Tools/LambdaUtilities.cs b/src/Amazon.Lambda.Tools/LambdaUtilities.cs index 0cdd189..7d706c6 100644 --- a/src/Amazon.Lambda.Tools/LambdaUtilities.cs +++ b/src/Amazon.Lambda.Tools/LambdaUtilities.cs @@ -64,13 +64,13 @@ public static class LambdaUtilities {Amazon.Lambda.Runtime.Dotnetcore10.Value, TargetFrameworkMonikers.netcoreapp10} }; - public static string DetermineTargetFrameworkFromLambdaRuntime(string lambdaRuntime, string projectLocation) + public static string DetermineTargetFrameworkFromLambdaRuntime(string lambdaRuntime, string projectLocation, string msbuildParameters) { string framework; if (_lambdaRuntimeToDotnetFramework.TryGetValue(lambdaRuntime, out framework)) return framework; - framework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation); + framework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, msbuildParameters); return framework; } @@ -83,9 +83,9 @@ public static string DetermineLambdaRuntimeFromTargetFramework(string targetFram return kvp.Key; } - public static void ValidateTargetFramework(string projectLocation, string targetFramework, bool isNativeAot) + public static void ValidateTargetFramework(string projectLocation, string msbuildParameters, string targetFramework, bool isNativeAot) { - var outputType = Utilities.LookupOutputTypeFromProjectFile(projectLocation); + var outputType = Utilities.LookupOutputTypeFromProjectFile(projectLocation, msbuildParameters); var ouputTypeIsExe = outputType != null && outputType.ToLower().Equals("exe"); if (isNativeAot && !ouputTypeIsExe) diff --git a/src/Amazon.Lambda.Tools/TemplateProcessor/TemplateProcessorManager.cs b/src/Amazon.Lambda.Tools/TemplateProcessor/TemplateProcessorManager.cs index f0faea4..03b53a7 100644 --- a/src/Amazon.Lambda.Tools/TemplateProcessor/TemplateProcessorManager.cs +++ b/src/Amazon.Lambda.Tools/TemplateProcessor/TemplateProcessorManager.cs @@ -251,7 +251,7 @@ private async Task PackageDotnetProjectAsync(IUpdateResou var outputPackage = GenerateOutputZipFilename(field); command.OutputPackageFileName = outputPackage; command.TargetFramework = - LambdaUtilities.DetermineTargetFrameworkFromLambdaRuntime(field.Resource.LambdaRuntime, location); + LambdaUtilities.DetermineTargetFrameworkFromLambdaRuntime(field.Resource.LambdaRuntime, location, null); command.Architecture = field.Resource.LambdaArchitecture; command.LayerVersionArns = field.Resource.LambdaLayers; diff --git a/test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs b/test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs index 09f2161..7aec0bf 100644 --- a/test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs +++ b/test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs @@ -18,7 +18,7 @@ public void CheckFramework(string projectPath, string expectedFramework) { var assembly = this.GetType().GetTypeInfo().Assembly; var fullPath = Path.GetFullPath(Path.GetDirectoryName(assembly.Location) + projectPath); - var determinedFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectPath); + var determinedFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectPath, null); Assert.Equal(expectedFramework, determinedFramework); } @@ -80,7 +80,7 @@ public void TestLookForPublishAotFlag(string projectLocation, string msBuildPara [InlineData("../../../../../testapps/TestNativeAotSingleProject", "Exe")] public void TestLookupOutputTypeFromProjectFile(string projectLocation, string expected) { - var result = Utilities.LookupOutputTypeFromProjectFile(projectLocation); + var result = Utilities.LookupOutputTypeFromProjectFile(projectLocation, null); Assert.Equal(expected, result); } @@ -100,5 +100,17 @@ public void TestHasExplicitSelfContainedFlag(string projectLocation, string msBu Assert.Equal(expected, result); } + + [Theory] + [InlineData("TargetFramework", "", "net6.0")] + [InlineData("TargetFramework", "/p:NonExistence=net20.0", "net6.0")] + [InlineData("TargetFramework", "/p:TargetFramework=net20.0", "net20.0")] + public void TestPropertyEvaluationWithMSBuildParameters(string property, string msbuildparameters, string expectedValue) + { + var projectLocation = "../../../../../testapps/TestFunction"; + + var value = Utilities.LookupProjectProperties(projectLocation, msbuildparameters, property)[property]; + Assert.Equal(expectedValue, value); + } } } \ No newline at end of file diff --git a/test/Amazon.Lambda.Tools.Test/UtilitiesTests.cs b/test/Amazon.Lambda.Tools.Test/UtilitiesTests.cs index 22b22e2..a67328f 100644 --- a/test/Amazon.Lambda.Tools.Test/UtilitiesTests.cs +++ b/test/Amazon.Lambda.Tools.Test/UtilitiesTests.cs @@ -216,12 +216,12 @@ public void TestValidateTargetFramework(string projectLocation, string targetFra { if (shouldThrow) { - Assert.Throws(() => LambdaUtilities.ValidateTargetFramework(projectLocation, targetFramework, isNativeAot)); + Assert.Throws(() => LambdaUtilities.ValidateTargetFramework(projectLocation, null, targetFramework, isNativeAot)); } else { // If this throws an exception, the test will fail, hench no assert is necessary - LambdaUtilities.ValidateTargetFramework(projectLocation, targetFramework, isNativeAot); + LambdaUtilities.ValidateTargetFramework(projectLocation, null, targetFramework, isNativeAot); } } From 8a5608b41a806e2580508b9c0e45302d1ef747ea Mon Sep 17 00:00:00 2001 From: Norm Johanson Date: Mon, 2 Dec 2024 13:45:20 -0800 Subject: [PATCH 2/2] Address PR comments --- src/Amazon.Common.DotNetCli.Tools/Utilities.cs | 2 +- test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Amazon.Common.DotNetCli.Tools/Utilities.cs b/src/Amazon.Common.DotNetCli.Tools/Utilities.cs index fbd36a7..acc264b 100644 --- a/src/Amazon.Common.DotNetCli.Tools/Utilities.cs +++ b/src/Amazon.Common.DotNetCli.Tools/Utilities.cs @@ -211,7 +211,7 @@ public static string DeterminePublishLocation(string workingDirectory, string pr /// Looks up specified properties from a project. /// /// The location of the project file. - /// Additonal MSBuild paramteres passed by the user from the commandline + /// Additional MSBuild parameters passed by the user from the commandline /// The names of the properties to look up. /// A dictionary of property names and their values. public static Dictionary LookupProjectProperties(string projectLocation, string msBuildParameters, params string[] propertyNames) diff --git a/test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs b/test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs index 7aec0bf..c2a14d0 100644 --- a/test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs +++ b/test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs @@ -105,6 +105,8 @@ public void TestHasExplicitSelfContainedFlag(string projectLocation, string msBu [InlineData("TargetFramework", "", "net6.0")] [InlineData("TargetFramework", "/p:NonExistence=net20.0", "net6.0")] [InlineData("TargetFramework", "/p:TargetFramework=net20.0", "net20.0")] + [InlineData("TargetFramework", "/p:TargetFramework=net20.0 /p:OutputType=FutureDevice", "net20.0")] + [InlineData("OutputType", "/p:TargetFramework=net20.0 /p:OutputType=FutureDevice", "FutureDevice")] public void TestPropertyEvaluationWithMSBuildParameters(string property, string msbuildparameters, string expectedValue) { var projectLocation = "../../../../../testapps/TestFunction";