in src/Aspire.Hosting.AWS/Utils/ProjectUtilities.cs [21:78]
public static void UpdateLaunchSettingsWithLambdaTester(
string resourceName,
string functionHandler,
string assemblyName,
string projectPath,
string runtimeSupportAssemblyPath,
string targetFramework,
ILogger? logger = null)
{
try
{
// Retrieve the current launch settings JSON from wherever it's stored.
string launchSettingsJson = GetLaunchSettings(projectPath);
// Parse the JSON into a mutable JsonNode (root is expected to be an object)
JsonNode? rootNode = JsonNode.Parse(launchSettingsJson);
if (rootNode is not JsonObject root)
{
// If the parsed JSON isn’t an object, initialize a new one.
root = new JsonObject();
}
// Get or create the "profiles" JSON object
JsonObject profiles = root["profiles"]?.AsObject() ?? new JsonObject();
root["profiles"] = profiles; // Ensure it's added to the root
var launchSettingsNodeKey = $"{Constants.LaunchSettingsNodePrefix}{resourceName}";
// Get or create the specific profile for Amazon.Lambda.TestTool
JsonObject? lambdaTester = profiles[launchSettingsNodeKey]?.AsObject();
if (lambdaTester == null)
{
lambdaTester = new JsonObject
{
["commandName"] = "Executable",
["executablePath"] = "dotnet"
};
profiles[launchSettingsNodeKey] = lambdaTester;
}
// Update properties that contain a path that is environment-specific
lambdaTester["commandLineArgs"] =
$"exec --depsfile ./{assemblyName}.deps.json --runtimeconfig ./{assemblyName}.runtimeconfig.json {SubstituteHomePath(runtimeSupportAssemblyPath)} {functionHandler}";
lambdaTester["workingDirectory"] = Path.Combine(".", "bin", "$(Configuration)", targetFramework);
// Serialize the updated JSON with indentation
var options = new JsonSerializerOptions { WriteIndented = true };
string updatedJson = root.ToJsonString(options);
// Save the updated JSON back to the launch settings file.
SaveLaunchSettings(projectPath, updatedJson);
}
catch (JsonException ex)
{
logger?.LogError(ex, "Failed to parse the launchSettings.json file for the project '{ProjectPath}'.", projectPath);
}
}