Skip to content

Commit 5c1e258

Browse files
authored
Include user specified MSBuild parameters when evaluating MSBuild properties (#352)
1 parent 8b8b49d commit 5c1e258

File tree

11 files changed

+57
-24
lines changed

11 files changed

+57
-24
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.Tools",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Include user specified MSBuild parameters when evaluating MSBuild properties"
8+
]
9+
}
10+
]
11+
}

src/Amazon.Common.DotNetCli.Tools/Utilities.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,10 @@ public static string DeterminePublishLocation(string workingDirectory, string pr
211211
/// Looks up specified properties from a project.
212212
/// </summary>
213213
/// <param name="projectLocation">The location of the project file.</param>
214+
/// <param name="msBuildParameters">Additional MSBuild parameters passed by the user from the commandline</param>
214215
/// <param name="propertyNames">The names of the properties to look up.</param>
215216
/// <returns>A dictionary of property names and their values.</returns>
216-
public static Dictionary<string, string> LookupProjectProperties(string projectLocation, params string[] propertyNames)
217+
public static Dictionary<string, string> LookupProjectProperties(string projectLocation, string msBuildParameters, params string[] propertyNames)
217218
{
218219
var projectFile = FindProjectFileInDirectory(projectLocation);
219220
var properties = new Dictionary<string, string>();
@@ -225,6 +226,11 @@ public static Dictionary<string, string> LookupProjectProperties(string projectL
225226
$"--getProperty:{string.Join(',', propertyNames)}"
226227
};
227228

229+
if (!string.IsNullOrEmpty(msBuildParameters))
230+
{
231+
arguments.Add(msBuildParameters);
232+
}
233+
228234
var process = new Process
229235
{
230236
StartInfo = new ProcessStartInfo
@@ -302,16 +308,17 @@ private static Dictionary<string, string> LookupProjectPropertiesFromXml(string
302308
{
303309
}
304310
return properties;
305-
}
311+
}
306312

307313
/// <summary>
308314
/// Looks up the target framework from a project file.
309315
/// </summary>
310316
/// <param name="projectLocation">The location of the project file.</param>
317+
/// <param name="msBuildParameters">Additonal MSBuild paramteres passed by the user from the commandline</param>
311318
/// <returns>The target framework of the project.</returns>
312-
public static string LookupTargetFrameworkFromProjectFile(string projectLocation)
319+
public static string LookupTargetFrameworkFromProjectFile(string projectLocation, string msBuildParameters)
313320
{
314-
var properties = LookupProjectProperties(projectLocation, "TargetFramework", "TargetFrameworks");
321+
var properties = LookupProjectProperties(projectLocation, msBuildParameters, "TargetFramework", "TargetFrameworks");
315322
if (properties.TryGetValue("TargetFramework", out var targetFramework) && !string.IsNullOrEmpty(targetFramework))
316323
{
317324
return targetFramework;
@@ -331,10 +338,11 @@ public static string LookupTargetFrameworkFromProjectFile(string projectLocation
331338
/// Retrieve the `OutputType` property of a given project
332339
/// </summary>
333340
/// <param name="projectLocation">Path of the project</param>
341+
/// <param name="msBuildParameters">Additonal MSBuild paramteres passed by the user from the commandline</param>
334342
/// <returns>The value of the `OutputType` property</returns>
335-
public static string LookupOutputTypeFromProjectFile(string projectLocation)
343+
public static string LookupOutputTypeFromProjectFile(string projectLocation, string msBuildParameters)
336344
{
337-
var properties = LookupProjectProperties(projectLocation, "OutputType");
345+
var properties = LookupProjectProperties(projectLocation, msBuildParameters, "OutputType");
338346
return properties.TryGetValue("OutputType", out var outputType) ? outputType.Trim() : null;
339347
}
340348

@@ -353,7 +361,7 @@ public static bool LookPublishAotFlag(string projectLocation, string msBuildPara
353361
}
354362
}
355363

356-
var properties = LookupProjectProperties(projectLocation, "PublishAot");
364+
var properties = LookupProjectProperties(projectLocation, msBuildParameters, "PublishAot");
357365
if (properties.TryGetValue("PublishAot", out var publishAot))
358366
{
359367
return bool.TryParse(publishAot, out var result) && result;
@@ -369,7 +377,7 @@ public static bool HasExplicitSelfContainedFlag(string projectLocation, string m
369377
return true;
370378
}
371379

372-
var properties = LookupProjectProperties(projectLocation, "SelfContained");
380+
var properties = LookupProjectProperties(projectLocation, msBuildParameters, "SelfContained");
373381
if (properties.TryGetValue("SelfContained", out var selfContained))
374382
{
375383
return bool.TryParse(selfContained, out var isSelfContained) && isSelfContained;

src/Amazon.ElasticBeanstalk.Tools/Commands/DeployEnvironmentCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected override async Task<bool> PerformActionAsync()
138138

139139
if (string.IsNullOrEmpty(targetFramework))
140140
{
141-
targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation);
141+
targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, null);
142142
if (string.IsNullOrEmpty(targetFramework))
143143
{
144144
targetFramework = this.GetStringValueOrDefault(this.DeployEnvironmentOptions.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, true);

src/Amazon.ElasticBeanstalk.Tools/Commands/PackageCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected override Task<bool> PerformActionAsync()
6464

6565
if (string.IsNullOrEmpty(targetFramework))
6666
{
67-
targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation);
67+
targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, null);
6868
if (string.IsNullOrEmpty(targetFramework))
6969
{
7070
targetFramework = this.GetStringValueOrDefault(this.DeployEnvironmentOptions.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, true);

src/Amazon.Lambda.Tools/Commands/DeployFunctionCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,11 @@ protected override async Task<bool> PerformActionAsync()
227227
// Release will be the default configuration if nothing set.
228228
string configuration = this.GetStringValueOrDefault(this.Configuration, CommonDefinedCommandOptions.ARGUMENT_CONFIGURATION, false);
229229

230+
string msbuildParameters = this.GetStringValueOrDefault(this.MSBuildParameters, CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS, false);
230231
var targetFramework = this.GetStringValueOrDefault(this.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, false);
231232
if (string.IsNullOrEmpty(targetFramework))
232233
{
233-
targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation);
234+
targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, msbuildParameters);
234235

235236
// If we still don't know what the target framework is ask the user what targetframework to use.
236237
// This is common when a project is using multi targeting.
@@ -239,7 +240,6 @@ protected override async Task<bool> PerformActionAsync()
239240
targetFramework = this.GetStringValueOrDefault(this.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, true);
240241
}
241242
}
242-
string msbuildParameters = this.GetStringValueOrDefault(this.MSBuildParameters, CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS, false);
243243

244244
bool isNativeAot = Utilities.LookPublishAotFlag(projectLocation, this.MSBuildParameters);
245245

src/Amazon.Lambda.Tools/Commands/PackageCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,11 @@ protected override async Task<bool> PerformActionAsync()
206206
// Release will be the default configuration if nothing set.
207207
var configuration = this.GetStringValueOrDefault(this.Configuration, CommonDefinedCommandOptions.ARGUMENT_CONFIGURATION, false);
208208

209+
var msbuildParameters = this.GetStringValueOrDefault(this.MSBuildParameters, CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS, false);
209210
var targetFramework = this.GetStringValueOrDefault(this.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, false);
210211
if (string.IsNullOrEmpty(targetFramework))
211212
{
212-
targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation);
213+
targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, msbuildParameters);
213214

214215
// If we still don't know what the target framework is ask the user what targetframework to use.
215216
// This is common when a project is using multi targeting.
@@ -221,7 +222,6 @@ protected override async Task<bool> PerformActionAsync()
221222

222223
bool isNativeAot = Utilities.LookPublishAotFlag(projectLocation, this.MSBuildParameters);
223224

224-
var msbuildParameters = this.GetStringValueOrDefault(this.MSBuildParameters, CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS, false);
225225
var architecture = this.GetStringValueOrDefault(this.Architecture, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_ARCHITECTURE, false);
226226
var disableVersionCheck = this.GetBoolValueOrDefault(this.DisableVersionCheck, LambdaDefinedCommandOptions.ARGUMENT_DISABLE_VERSION_CHECK, false).GetValueOrDefault();
227227

src/Amazon.Lambda.Tools/LambdaPackager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static bool CreateApplicationBundle(LambdaToolsDefaults defaults, IToolLo
114114
bool? useContainerForBuild, string containerImageForBuild, string codeMountDirectory,
115115
out string publishLocation, ref string zipArchivePath)
116116
{
117-
LambdaUtilities.ValidateTargetFramework(projectLocation, targetFramework, isNativeAot);
117+
LambdaUtilities.ValidateTargetFramework(projectLocation, msbuildParameters, targetFramework, isNativeAot);
118118

119119
LambdaUtilities.ValidateNativeAotArchitecture(architecture, isNativeAot);
120120

src/Amazon.Lambda.Tools/LambdaUtilities.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ public static class LambdaUtilities
6464
{Amazon.Lambda.Runtime.Dotnetcore10.Value, TargetFrameworkMonikers.netcoreapp10}
6565
};
6666

67-
public static string DetermineTargetFrameworkFromLambdaRuntime(string lambdaRuntime, string projectLocation)
67+
public static string DetermineTargetFrameworkFromLambdaRuntime(string lambdaRuntime, string projectLocation, string msbuildParameters)
6868
{
6969
string framework;
7070
if (_lambdaRuntimeToDotnetFramework.TryGetValue(lambdaRuntime, out framework))
7171
return framework;
7272

73-
framework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation);
73+
framework = Utilities.LookupTargetFrameworkFromProjectFile(projectLocation, msbuildParameters);
7474
return framework;
7575
}
7676

@@ -83,9 +83,9 @@ public static string DetermineLambdaRuntimeFromTargetFramework(string targetFram
8383
return kvp.Key;
8484
}
8585

86-
public static void ValidateTargetFramework(string projectLocation, string targetFramework, bool isNativeAot)
86+
public static void ValidateTargetFramework(string projectLocation, string msbuildParameters, string targetFramework, bool isNativeAot)
8787
{
88-
var outputType = Utilities.LookupOutputTypeFromProjectFile(projectLocation);
88+
var outputType = Utilities.LookupOutputTypeFromProjectFile(projectLocation, msbuildParameters);
8989
var ouputTypeIsExe = outputType != null && outputType.ToLower().Equals("exe");
9090

9191
if (isNativeAot && !ouputTypeIsExe)

src/Amazon.Lambda.Tools/TemplateProcessor/TemplateProcessorManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private async Task<UpdateResourceResults> PackageDotnetProjectAsync(IUpdateResou
251251
var outputPackage = GenerateOutputZipFilename(field);
252252
command.OutputPackageFileName = outputPackage;
253253
command.TargetFramework =
254-
LambdaUtilities.DetermineTargetFrameworkFromLambdaRuntime(field.Resource.LambdaRuntime, location);
254+
LambdaUtilities.DetermineTargetFrameworkFromLambdaRuntime(field.Resource.LambdaRuntime, location, null);
255255

256256
command.Architecture = field.Resource.LambdaArchitecture;
257257
command.LayerVersionArns = field.Resource.LambdaLayers;

test/Amazon.Common.DotNetCli.Tools.Test/UtilitiesTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void CheckFramework(string projectPath, string expectedFramework)
1818
{
1919
var assembly = this.GetType().GetTypeInfo().Assembly;
2020
var fullPath = Path.GetFullPath(Path.GetDirectoryName(assembly.Location) + projectPath);
21-
var determinedFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectPath);
21+
var determinedFramework = Utilities.LookupTargetFrameworkFromProjectFile(projectPath, null);
2222
Assert.Equal(expectedFramework, determinedFramework);
2323
}
2424

@@ -80,7 +80,7 @@ public void TestLookForPublishAotFlag(string projectLocation, string msBuildPara
8080
[InlineData("../../../../../testapps/TestNativeAotSingleProject", "Exe")]
8181
public void TestLookupOutputTypeFromProjectFile(string projectLocation, string expected)
8282
{
83-
var result = Utilities.LookupOutputTypeFromProjectFile(projectLocation);
83+
var result = Utilities.LookupOutputTypeFromProjectFile(projectLocation, null);
8484

8585
Assert.Equal(expected, result);
8686
}
@@ -100,5 +100,19 @@ public void TestHasExplicitSelfContainedFlag(string projectLocation, string msBu
100100

101101
Assert.Equal(expected, result);
102102
}
103+
104+
[Theory]
105+
[InlineData("TargetFramework", "", "net6.0")]
106+
[InlineData("TargetFramework", "/p:NonExistence=net20.0", "net6.0")]
107+
[InlineData("TargetFramework", "/p:TargetFramework=net20.0", "net20.0")]
108+
[InlineData("TargetFramework", "/p:TargetFramework=net20.0 /p:OutputType=FutureDevice", "net20.0")]
109+
[InlineData("OutputType", "/p:TargetFramework=net20.0 /p:OutputType=FutureDevice", "FutureDevice")]
110+
public void TestPropertyEvaluationWithMSBuildParameters(string property, string msbuildparameters, string expectedValue)
111+
{
112+
var projectLocation = "../../../../../testapps/TestFunction";
113+
114+
var value = Utilities.LookupProjectProperties(projectLocation, msbuildparameters, property)[property];
115+
Assert.Equal(expectedValue, value);
116+
}
103117
}
104118
}

0 commit comments

Comments
 (0)