Skip to content

Commit 330fc04

Browse files
committed
Update Lambda Tools to pass in lambda runtime package store manifest into the dotnet publish command.
1 parent 8a40479 commit 330fc04

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

Libraries/src/Amazon.Lambda.Tools/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ internal enum TemplateFormat { Json, Yaml }
1111

1212
public static class Constants
1313
{
14+
public const string ENV_DOTNET_LAMBDA_CLI_LOCAL_MANIFEST_OVERRIDE = "DOTNET_LAMBDA_CLI_LOCAL_MANIFEST_OVERRIDE";
15+
1416
public const string IAM_ARN_PREFIX = "arn:aws:iam::";
1517
public const string AWS_MANAGED_POLICY_ARN_PREFIX = "arn:aws:iam::aws:policy";
1618

Libraries/src/Amazon.Lambda.Tools/DotNetCLIWrapper.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public DotNetCLIWrapper(IToolLogger logger, string workingDirectory)
3131
/// <param name="outputLocation"></param>
3232
/// <param name="targetFramework"></param>
3333
/// <param name="configuration"></param>
34-
public int Publish(LambdaToolsDefaults defaults, string projectLocation, string outputLocation, string targetFramework, string configuration)
34+
public int Publish(LambdaToolsDefaults defaults, string projectLocation, string outputLocation, string targetFramework, string configuration, string deploymentTargetPackageStoreManifestContent)
3535
{
3636
if (Directory.Exists(outputLocation))
3737
{
@@ -80,6 +80,7 @@ public int Publish(LambdaToolsDefaults defaults, string projectLocation, string
8080
arguments.Append($" --framework \"{targetFramework}\"");
8181
}
8282

83+
string manifestPath = null;
8384
if (!string.Equals("netcoreapp1.0", targetFramework, StringComparison.OrdinalIgnoreCase))
8485
{
8586
arguments.Append(" /p:GenerateRuntimeConfigurationFiles=true");
@@ -90,6 +91,16 @@ public int Publish(LambdaToolsDefaults defaults, string projectLocation, string
9091
{
9192
arguments.Append(" -r linux-x64 --self-contained false /p:PreserveCompilationContext=false");
9293
}
94+
95+
// If we have a manifest of packages already deploy in target deployment environment then write it to disk and add the
96+
// command line switch
97+
if(!string.IsNullOrEmpty(deploymentTargetPackageStoreManifestContent))
98+
{
99+
manifestPath = Path.GetTempFileName();
100+
File.WriteAllText(manifestPath, deploymentTargetPackageStoreManifestContent);
101+
102+
arguments.Append($" --manifest \"{manifestPath}\"");
103+
}
93104
}
94105

95106
var psi = new ProcessStartInfo
@@ -129,6 +140,12 @@ public int Publish(LambdaToolsDefaults defaults, string projectLocation, string
129140
exitCode = proc.ExitCode;
130141
}
131142

143+
// If we wrote a temporary manifest file then clean it up after dotnet publish has executed.
144+
if(manifestPath != null && File.Exists(manifestPath))
145+
{
146+
File.Delete(manifestPath);
147+
}
148+
132149
if (exitCode == 0)
133150
{
134151
ProcessAdditionalFiles(defaults, Utilities.DetermineProjectLocation(this._workingDirectory, projectLocation), outputLocation);

Libraries/src/Amazon.Lambda.Tools/LambdaPackager.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ public static bool CreateApplicationBundle(LambdaToolsDefaults defaults, IToolLo
4646
string projectLocation, string configuration, string targetFramework, bool disableVersionCheck,
4747
out string publishLocation, ref string zipArchivePath)
4848
{
49-
50-
Utilities.ValidateMicrosoftAspNetCoreAllReference(logger, Utilities.DetermineProjectLocation(workingDirectory, projectLocation));
49+
string lambdaRuntimePackageStoreManifestContent = null;
50+
if (!disableVersionCheck)
51+
{
52+
Utilities.ValidateMicrosoftAspNetCoreAllReference(logger, Utilities.DetermineProjectLocation(workingDirectory, projectLocation), out lambdaRuntimePackageStoreManifestContent);
53+
}
5154

5255
var cli = new DotNetCLIWrapper(logger, workingDirectory);
5356

5457
publishLocation = Utilities.DeterminePublishLocation(workingDirectory, projectLocation, configuration, targetFramework);
5558
logger?.WriteLine("Executing publish command");
56-
if (cli.Publish(defaults, projectLocation, publishLocation, targetFramework, configuration) != 0)
59+
if (cli.Publish(defaults, projectLocation, publishLocation, targetFramework, configuration, lambdaRuntimePackageStoreManifestContent) != 0)
5760
return false;
5861

5962
var buildLocation = Utilities.DetermineBuildLocation(workingDirectory, projectLocation, configuration, targetFramework);

Libraries/src/Amazon.Lambda.Tools/Utilities.cs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,32 @@ public static string DeterminePublishLocation(string workingDirectory, string pr
231231
return path;
232232
}
233233

234-
public static void ValidateMicrosoftAspNetCoreAllReference(IToolLogger logger, string csprofPath)
234+
public static void ValidateMicrosoftAspNetCoreAllReference(IToolLogger logger, string csprofPath, out string manifestContent)
235235
{
236-
if(Directory.Exists(csprofPath))
236+
if(!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(Constants.ENV_DOTNET_LAMBDA_CLI_LOCAL_MANIFEST_OVERRIDE)))
237+
{
238+
var filePath = Environment.GetEnvironmentVariable(Constants.ENV_DOTNET_LAMBDA_CLI_LOCAL_MANIFEST_OVERRIDE);
239+
if(File.Exists(filePath))
240+
{
241+
logger?.WriteLine($"Using local manifest override: {filePath}");
242+
manifestContent = File.ReadAllText(filePath);
243+
}
244+
else
245+
{
246+
logger?.WriteLine("Using local manifest override");
247+
manifestContent = null;
248+
}
249+
}
250+
else
251+
{
252+
manifestContent = ToolkitConfigFileFetcher.Instance.GetFileContentAsync(logger, "LambdaPackageStoreManifest.xml").Result;
253+
}
254+
if (string.IsNullOrEmpty(manifestContent))
255+
{
256+
return;
257+
}
258+
259+
if (Directory.Exists(csprofPath))
237260
{
238261
var projectFiles = Directory.GetFiles(csprofPath, "*.csproj", SearchOption.TopDirectoryOnly);
239262
if(projectFiles.Length != 1)
@@ -251,15 +274,8 @@ public static void ValidateMicrosoftAspNetCoreAllReference(IToolLogger logger, s
251274

252275
var projectContent = File.ReadAllText(csprofPath);
253276

254-
var manifestContent = ToolkitConfigFileFetcher.Instance.GetFileContentAsync(logger, "LambdaPackageStoreManifest.xml").Result;
255-
if (!string.IsNullOrEmpty(manifestContent))
256-
{
257-
ValidateMicrosoftAspNetCoreAllReference(logger, manifestContent, projectContent);
258-
}
259-
else
260-
{
261-
logger.WriteLine("Skipping Microsoft.AspNetCore.All validation because error while downloading Lambda runtime store manifest.");
262-
}
277+
278+
ValidateMicrosoftAspNetCoreAllReference(logger, manifestContent, projectContent);
263279
}
264280

265281
/// <summary>
@@ -326,11 +342,11 @@ public static void ValidateMicrosoftAspNetCoreAllReference(IToolLogger logger, s
326342
}
327343
}
328344

329-
throw new LambdaToolsException($"Project is referencing version {projectAspNetCoreVersion} of {ASPNET_CORE_ALL} which is newer " +
330-
$"than {latestLambdaDeployedVersion}, the latest version available in the Lambda Runtime environment. Please update your project to " +
345+
throw new LambdaToolsException($"Project is referencing version {projectAspNetCoreVersion} of {ASPNET_CORE_ALL} which is newer " +
346+
$"than {latestLambdaDeployedVersion}, the latest version available in the Lambda Runtime environment. Please update your project to " +
331347
$"use version {latestLambdaDeployedVersion} and then redeploy your Lambda function.", LambdaToolsException.ErrorCode.AspNetCoreAllValidation);
332348
}
333-
catch(LambdaToolsException)
349+
catch (LambdaToolsException)
334350
{
335351
throw;
336352
}

0 commit comments

Comments
 (0)