-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use MSBuild to find target framework #348
Changes from 3 commits
823e65d
8883689
6f1429b
cb68604
9245d43
cb98714
af70e6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"Projects": [ | ||
{ | ||
"Name": "Amazon.Lambda.Tools", | ||
"Type": "Patch", | ||
"ChangelogMessages": [ | ||
"Use MSBuild to find target framework" | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
using System.Text.RegularExpressions; | ||
using System.Collections; | ||
using System.Xml; | ||
using System.Text.Json; | ||
|
||
namespace Amazon.Common.DotNetCli.Tools | ||
{ | ||
|
@@ -208,11 +209,72 @@ public static string DeterminePublishLocation(string workingDirectory, string pr | |
public static string LookupTargetFrameworkFromProjectFile(string projectLocation) | ||
{ | ||
var projectFile = FindProjectFileInDirectory(projectLocation); | ||
if (string.IsNullOrEmpty(projectFile)) | ||
{ | ||
throw new FileNotFoundException("Could not find a project file in the specified directory."); | ||
} | ||
|
||
var xdoc = XDocument.Load(projectFile); | ||
var arguments = new[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We look up The shared method should have the fallback logic I describe below that if the process fails parse the project xml like it does today so we don't risk any breaking behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure will update! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated in latest commit |
||
{ | ||
"msbuild", | ||
projectFile, | ||
"--getProperty:TargetFramework,TargetFrameworks", | ||
"-nologo" | ||
}; | ||
|
||
var process = new Process | ||
{ | ||
StartInfo = new ProcessStartInfo | ||
{ | ||
FileName = "dotnet", | ||
Arguments = string.Join(" ", arguments), | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true | ||
} | ||
}; | ||
|
||
process.Start(); | ||
string output = process.StandardOutput.ReadToEnd(); | ||
string error = process.StandardError.ReadToEnd(); | ||
process.WaitForExit(); | ||
|
||
if (process.ExitCode != 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the process failed we should fallback to the original XML parsing logic. There could be CI systems that are still deploying .NET 6 and only have .NET 6 SDK installed. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah good point! i will update accordingly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated in latest commit |
||
{ | ||
throw new Exception($"MSBuild process exited with code {process.ExitCode}. Error: {error}"); | ||
} | ||
|
||
var element = xdoc.XPathSelectElement("//PropertyGroup/TargetFramework"); | ||
return element?.Value; | ||
try | ||
{ | ||
using JsonDocument doc = JsonDocument.Parse(output); | ||
JsonElement root = doc.RootElement; | ||
JsonElement properties = root.GetProperty("Properties"); | ||
|
||
if (properties.TryGetProperty("TargetFramework", out JsonElement targetFramework)) | ||
{ | ||
string framework = targetFramework.GetString(); | ||
if (!string.IsNullOrEmpty(framework)) | ||
{ | ||
return framework; | ||
} | ||
} | ||
|
||
if (properties.TryGetProperty("TargetFrameworks", out JsonElement targetFrameworks)) | ||
{ | ||
string frameworks = targetFrameworks.GetString(); | ||
if (!string.IsNullOrEmpty(frameworks)) | ||
{ | ||
return frameworks.Split(';')[0]; | ||
} | ||
} | ||
} | ||
catch (JsonException ex) | ||
{ | ||
throw new Exception($"Failed to parse MSBuild output: {ex.Message}. Output: {output}"); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Amazon.Lambda.Core; | ||
|
||
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. | ||
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] | ||
|
||
namespace TestFunctionBuildProps; | ||
|
||
public class Function | ||
{ | ||
public string FunctionHandler(string input, ILambdaContext context) | ||
{ | ||
return input.ToUpper(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# AWS Lambda Empty Function Project | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't really need the readme for checked in for the test apps. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated in latest commit |
||
|
||
This starter project consists of: | ||
* Function.cs - class file containing a class with a single function handler method | ||
* aws-lambda-tools-defaults.json - default argument settings for use with Visual Studio and command line deployment tools for AWS | ||
|
||
You may also have a test project depending on the options selected. | ||
|
||
The generated function handler is a simple method accepting a string argument that returns the uppercase equivalent of the input string. Replace the body of this method, and parameters, to suit your needs. | ||
|
||
## Here are some steps to follow from Visual Studio: | ||
|
||
To deploy your function to AWS Lambda, right click the project in Solution Explorer and select *Publish to AWS Lambda*. | ||
|
||
To view your deployed function open its Function View window by double-clicking the function name shown beneath the AWS Lambda node in the AWS Explorer tree. | ||
|
||
To perform testing against your deployed function use the Test Invoke tab in the opened Function View window. | ||
|
||
To configure event sources for your deployed function, for example to have your function invoked when an object is created in an Amazon S3 bucket, use the Event Sources tab in the opened Function View window. | ||
|
||
To update the runtime configuration of your deployed function use the Configuration tab in the opened Function View window. | ||
|
||
To view execution logs of invocations of your function use the Logs tab in the opened Function View window. | ||
|
||
## Here are some steps to follow to get started from the command line: | ||
|
||
Once you have edited your template and code you can deploy your application using the [Amazon.Lambda.Tools Global Tool](https://github.com/aws/aws-extensions-for-dotnet-cli#aws-lambda-amazonlambdatools) from the command line. | ||
|
||
Install Amazon.Lambda.Tools Global Tools if not already installed. | ||
``` | ||
dotnet tool install -g Amazon.Lambda.Tools | ||
``` | ||
|
||
If already installed check if new version is available. | ||
``` | ||
dotnet tool update -g Amazon.Lambda.Tools | ||
``` | ||
|
||
Execute unit tests | ||
``` | ||
cd "TestFunctionBuildProps/test/TestFunctionBuildProps.Tests" | ||
dotnet test | ||
``` | ||
|
||
Deploy function to AWS Lambda | ||
``` | ||
cd "TestFunctionBuildProps/src/TestFunctionBuildProps" | ||
dotnet lambda deploy-function | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> | ||
<AWSProjectType>Lambda</AWSProjectType> | ||
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. --> | ||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> | ||
<!-- Generate ready to run images during publishing to improve cold start time. --> | ||
<PublishReadyToRun>true</PublishReadyToRun> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Amazon.Lambda.Core" Version="2.4.0" /> | ||
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"Information": [ | ||
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.", | ||
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.", | ||
"dotnet lambda help", | ||
"All the command line options for the Lambda command can be specified in this file." | ||
], | ||
"profile": "default", | ||
"region": "us-west-2", | ||
"configuration": "Release", | ||
"function-architecture": "x86_64", | ||
"function-runtime": "dotnet8", | ||
"function-memory-size": 512, | ||
"function-timeout": 30, | ||
"function-handler": "TestFunctionBuildProps::TestFunctionBuildProps.Function::FunctionHandler" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add more explanation here for the significance of this for customers. Not sure everyone would know what this implies.