Skip to content

Commit cb98714

Browse files
committed
PR comments
1 parent 9245d43 commit cb98714

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

.autover/changes/8a5204e4-83ec-45ac-8c75-27512ae9cdf8.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "Amazon.Lambda.Tools",
55
"Type": "Patch",
66
"ChangelogMessages": [
7-
"Use MSBuild to find target framework"
7+
"Update logic for finding project properties to use MSBuild instead of parsing the project file directly. See https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2022 for more details."
88
]
99
}
1010
]

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public static string DeterminePublishLocation(string workingDirectory, string pr
208208

209209

210210
// <summary>
211-
/// Looks up specified properties from a project file.
211+
/// Looks up specified properties from a project.
212212
/// </summary>
213213
/// <param name="projectLocation">The location of the project file.</param>
214214
/// <param name="propertyNames">The names of the properties to look up.</param>
@@ -240,41 +240,49 @@ public static Dictionary<string, string> LookupProjectProperties(string projectL
240240
try
241241
{
242242
process.Start();
243-
string output = process.StandardOutput.ReadToEnd();
243+
string output = process.StandardOutput.ReadToEnd().Trim();
244244
string error = process.StandardError.ReadToEnd();
245245
process.WaitForExit(5000);
246246

247247
if (process.ExitCode == 0)
248248
{
249-
using JsonDocument doc = JsonDocument.Parse(output);
250-
JsonElement root = doc.RootElement;
251-
JsonElement propertiesElement = root.GetProperty("Properties");
252-
253-
foreach (var property in propertyNames)
249+
if (propertyNames.Length == 1)
250+
{
251+
// If only one property was requested, the output is the direct value
252+
properties[propertyNames[0]] = output;
253+
}
254+
else
254255
{
255-
if (propertiesElement.TryGetProperty(property, out JsonElement propertyValue))
256+
// Multiple properties were requested, so we expect JSON output
257+
using JsonDocument doc = JsonDocument.Parse(output);
258+
JsonElement root = doc.RootElement;
259+
JsonElement propertiesElement = root.GetProperty("Properties");
260+
261+
foreach (var property in propertyNames)
256262
{
257-
properties[property] = propertyValue.GetString();
263+
if (propertiesElement.TryGetProperty(property, out JsonElement propertyValue))
264+
{
265+
properties[property] = propertyValue.GetString();
266+
}
258267
}
259268
}
260269
}
261270
else
262271
{
263-
Console.WriteLine($"MSBuild process exited with code {process.ExitCode}. Error: {error}");
264272
// Fallback to XML parsing
265273
properties = LookupProjectPropertiesFromXml(projectFile, propertyNames);
266274
}
267275
}
268-
catch (Exception ex)
276+
catch (Exception)
269277
{
270-
Console.WriteLine($"Error executing MSBuild or parsing output: {ex.Message}");
271278
// Fallback to XML parsing
272279
properties = LookupProjectPropertiesFromXml(projectFile, propertyNames);
273280
}
274281

275282
return properties;
276283
}
277284

285+
278286
private static Dictionary<string, string> LookupProjectPropertiesFromXml(string projectFile, string[] propertyNames)
279287
{
280288
var properties = new Dictionary<string, string>();
@@ -290,9 +298,8 @@ private static Dictionary<string, string> LookupProjectPropertiesFromXml(string
290298
}
291299
}
292300
}
293-
catch (Exception ex)
301+
catch (Exception)
294302
{
295-
Console.WriteLine($"Error parsing project file XML: {ex.Message}");
296303
}
297304
return properties;
298305
}

0 commit comments

Comments
 (0)