|
1 | | -using System; |
| 1 | +using Amazon.Common.DotNetCli.Tools; |
| 2 | +using Amazon.Lambda.Model; |
| 3 | +using Amazon.S3; |
| 4 | +using Amazon.S3.Model; |
| 5 | +using Amazon.SecurityToken; |
| 6 | +using Newtonsoft.Json; |
| 7 | +using Newtonsoft.Json.Linq; |
| 8 | +using System; |
2 | 9 | using System.Collections.Generic; |
3 | 10 | using System.IO; |
4 | 11 | using System.Linq; |
| 12 | +using System.Runtime.InteropServices; |
5 | 13 | using System.Text.Json; |
6 | | -using YamlDotNet.RepresentationModel; |
7 | | -using Newtonsoft.Json; |
8 | | -using Newtonsoft.Json.Linq; |
9 | | -using System.Xml.Linq; |
10 | | -using Amazon.Common.DotNetCli.Tools; |
11 | | - |
12 | | -using Amazon.Lambda.Model; |
13 | | -using Amazon.S3; |
14 | | -using Amazon.S3.Model; |
| 14 | +using System.Text.RegularExpressions; |
15 | 15 | using System.Threading.Tasks; |
| 16 | +using System.Xml.Linq; |
16 | 17 | using System.Xml.XPath; |
| 18 | +using YamlDotNet.RepresentationModel; |
17 | 19 | using Environment = System.Environment; |
18 | | -using Amazon.SecurityToken; |
19 | | -using System.Runtime.InteropServices; |
20 | 20 |
|
21 | 21 | namespace Amazon.Lambda.Tools |
22 | 22 | { |
@@ -83,16 +83,45 @@ public static string DetermineLambdaRuntimeFromTargetFramework(string targetFram |
83 | 83 | return kvp.Key; |
84 | 84 | } |
85 | 85 |
|
86 | | - public static string DetermineTargetFrameworkForSingleFile(string lambdaRuntime) |
| 86 | + public static string DetermineTargetFrameworkForSingleFile(string filePath, string lambdaRuntime) |
87 | 87 | { |
88 | | - string targetFramework; |
| 88 | + return DetermineTargetFrameworkForSingleFile(File.ReadAllLines(filePath), lambdaRuntime); |
| 89 | + } |
| 90 | + |
| 91 | + public static string DetermineTargetFrameworkForSingleFile(string[] lines, string lambdaRuntime) |
| 92 | + { |
| 93 | + string targetFramework = null; |
89 | 94 | if (lambdaRuntime != null && _lambdaRuntimeToDotnetFramework.TryGetValue(lambdaRuntime, out targetFramework)) |
90 | 95 | { |
91 | 96 | return targetFramework; |
92 | 97 | } |
93 | 98 |
|
94 | | - // TODO: Figure out what to do when we don't have a lambdaRuntime to base it on. |
95 | | - targetFramework = "net10.0"; |
| 99 | + // Look for an in-file directive that starts with "#:property" and contains "TargetFramework=<value>" |
| 100 | + // Allow extra spaces but the directive must begin with "#:property". Extract the value after '='. |
| 101 | + if (lines != null && lines.Length > 0) |
| 102 | + { |
| 103 | + // Pattern explanation: |
| 104 | + // ^\s* -> optional leading whitespace |
| 105 | + // #\s*:\s*property -> literal "#", optional spaces, ":", optional spaces, "property" |
| 106 | + // \s+ -> at least one space (separates property from key) |
| 107 | + // TargetFramework -> literal key |
| 108 | + // \s*=\s* -> equals with optional surrounding spaces |
| 109 | + // (\S+) -> capture the value (non-whitespace sequence) |
| 110 | + var directiveRegex = new Regex(@"^\s*#\s*:\s*property\s+TargetFramework\s*=\s*(\S+)", RegexOptions.IgnoreCase); |
| 111 | + |
| 112 | + foreach (var rawLine in lines) |
| 113 | + { |
| 114 | + if (string.IsNullOrWhiteSpace(rawLine)) |
| 115 | + continue; |
| 116 | + |
| 117 | + var m = directiveRegex.Match(rawLine); |
| 118 | + if (m.Success && m.Groups.Count > 1) |
| 119 | + { |
| 120 | + targetFramework = m.Groups[1].Value.Trim(); |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
96 | 125 |
|
97 | 126 | return targetFramework; |
98 | 127 | } |
|
0 commit comments