Skip to content

Commit c33ef14

Browse files
committed
Use MSBuild to find target framework
1 parent 21a1bec commit c33ef14

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

src/Amazon.Common.DotNetCli.Tools/Amazon.Common.DotNetCli.Tools.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.300.81" />
1212
<PackageReference Include="AWSSDK.SSO" Version="3.7.300.80" />
1313
<PackageReference Include="AWSSDK.SSOOIDC" Version="3.7.301.75" />
14+
<None Include="Assets\**\*" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
15+
</ItemGroup>
16+
<ItemGroup>
17+
<Folder Include="Assets\" />
1418
</ItemGroup>
1519
<PropertyGroup>
1620
<NoWarn>1701;1702;1705;1591</NoWarn>
1721
</PropertyGroup>
18-
1922
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project>
2+
<Target Name="_AmazonCommonToolsExtractTargetFrameworks">
3+
<ItemGroup>
4+
<_TargetFrameworks Include="$(TargetFramework)" Condition="'$(TargetFramework)' != ''" />
5+
<_TargetFrameworks Include="$(TargetFrameworks)" Condition="'$(TargetFrameworks)' != ''" />
6+
</ItemGroup>
7+
<WriteLinesToFile File="$(_AmazonCommonToolsTargetFrameworksFile)" Lines="@(_TargetFrameworks)" Overwrite="true" />
8+
</Target>
9+
</Project>
10+

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

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,85 @@ public static string DeterminePublishLocation(string workingDirectory, string pr
207207

208208
public static string LookupTargetFrameworkFromProjectFile(string projectLocation)
209209
{
210+
210211
var projectFile = FindProjectFileInDirectory(projectLocation);
212+
if (string.IsNullOrEmpty(projectFile))
213+
{
214+
throw new FileNotFoundException("Could not find a project file in the specified directory.");
215+
}
211216

212-
var xdoc = XDocument.Load(projectFile);
217+
var outputFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
218+
var targetsFile = FindTargetsFile();
219+
220+
if (!File.Exists(targetsFile))
221+
{
222+
throw new FileNotFoundException($"Could not find the custom .targets file at {targetsFile}");
223+
}
224+
225+
var arguments = new[]
226+
{
227+
"msbuild",
228+
projectFile,
229+
"/nologo",
230+
"/t:_AmazonCommonToolsExtractTargetFrameworks",
231+
$"/p:_AmazonCommonToolsTargetFrameworksFile={outputFile}",
232+
"/p:Configuration=Debug",
233+
$"\"/p:CustomAfterMicrosoftCommonTargets={targetsFile}\"",
234+
$"\"/p:CustomAfterMicrosoftCommonCrossTargetingTargets={targetsFile}\""
235+
};
236+
237+
var process = new Process
238+
{
239+
StartInfo = new ProcessStartInfo
240+
{
241+
FileName = "dotnet",
242+
Arguments = string.Join(" ", arguments),
243+
RedirectStandardOutput = true,
244+
RedirectStandardError = true,
245+
UseShellExecute = false,
246+
CreateNoWindow = true
247+
}
248+
};
249+
250+
process.Start();
251+
string output = process.StandardOutput.ReadToEnd();
252+
process.WaitForExit();
213253

214-
var element = xdoc.XPathSelectElement("//PropertyGroup/TargetFramework");
215-
return element?.Value;
254+
if (process.ExitCode != 0)
255+
{
256+
throw new Exception($"MSBuild process exited with code {process.ExitCode}. See log for details.");
257+
}
258+
259+
if (File.Exists(outputFile))
260+
{
261+
var targetFrameworks = File.ReadAllLines(outputFile);
262+
File.Delete(outputFile); // Clean up the temporary file
263+
264+
if (targetFrameworks.Length > 0)
265+
{
266+
return targetFrameworks[0]; // Return the first framework if there are multiple
267+
}
268+
}
269+
270+
return null;
216271
}
217272

273+
274+
private static string FindTargetsFile()
275+
{
276+
var assemblyDir = Path.GetDirectoryName(typeof(DotNetCLIWrapper).Assembly.Location);
277+
var searchPaths = new[]
278+
{
279+
Path.Combine(AppContext.BaseDirectory, "Assets"),
280+
Path.Combine(assemblyDir, "Assets"),
281+
AppContext.BaseDirectory,
282+
assemblyDir,
283+
};
284+
285+
return searchPaths.Select(p => Path.Combine(p, "AmazonCommonDotNetCliTools.targets")).FirstOrDefault(File.Exists);
286+
}
287+
288+
218289
/// <summary>
219290
/// Retrieve the `OutputType` property of a given project
220291
/// </summary>

0 commit comments

Comments
 (0)