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 00000000..03cc7fe4
--- /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 f067728e..acc264b4 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.
+ /// 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, 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 a7f6b38b..07505f13 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 47b2357c..917d6ccc 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 e5ec667c..34e4805d 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 f6aae61e..518a603e 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 0580c4e6..ae0382e4 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 0cdd189e..7d706c68 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 f0faea40..03b53a7a 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 09f21616..c2a14d04 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,19 @@ 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")]
+ [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";
+
+ 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 22b22e24..a67328fc 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);
}
}