Skip to content

Commit

Permalink
Use MSBuild to find target framework
Browse files Browse the repository at this point in the history
  • Loading branch information
gcbeattyAWS committed Nov 18, 2024
1 parent 21a1bec commit c33ef14
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.300.81" />
<PackageReference Include="AWSSDK.SSO" Version="3.7.300.80" />
<PackageReference Include="AWSSDK.SSOOIDC" Version="3.7.301.75" />
<None Include="Assets\**\*" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<Folder Include="Assets\" />
</ItemGroup>
<PropertyGroup>
<NoWarn>1701;1702;1705;1591</NoWarn>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>
<Target Name="_AmazonCommonToolsExtractTargetFrameworks">
<ItemGroup>
<_TargetFrameworks Include="$(TargetFramework)" Condition="'$(TargetFramework)' != ''" />
<_TargetFrameworks Include="$(TargetFrameworks)" Condition="'$(TargetFrameworks)' != ''" />
</ItemGroup>
<WriteLinesToFile File="$(_AmazonCommonToolsTargetFrameworksFile)" Lines="@(_TargetFrameworks)" Overwrite="true" />
</Target>
</Project>

77 changes: 74 additions & 3 deletions src/Amazon.Common.DotNetCli.Tools/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,85 @@ 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 outputFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var targetsFile = FindTargetsFile();

if (!File.Exists(targetsFile))
{
throw new FileNotFoundException($"Could not find the custom .targets file at {targetsFile}");
}

var arguments = new[]
{
"msbuild",
projectFile,
"/nologo",
"/t:_AmazonCommonToolsExtractTargetFrameworks",
$"/p:_AmazonCommonToolsTargetFrameworksFile={outputFile}",
"/p:Configuration=Debug",
$"\"/p:CustomAfterMicrosoftCommonTargets={targetsFile}\"",
$"\"/p:CustomAfterMicrosoftCommonCrossTargetingTargets={targetsFile}\""
};

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();
process.WaitForExit();

var element = xdoc.XPathSelectElement("//PropertyGroup/TargetFramework");
return element?.Value;
if (process.ExitCode != 0)
{
throw new Exception($"MSBuild process exited with code {process.ExitCode}. See log for details.");
}

if (File.Exists(outputFile))
{
var targetFrameworks = File.ReadAllLines(outputFile);
File.Delete(outputFile); // Clean up the temporary file

if (targetFrameworks.Length > 0)
{
return targetFrameworks[0]; // Return the first framework if there are multiple
}
}

return null;
}


private static string FindTargetsFile()
{
var assemblyDir = Path.GetDirectoryName(typeof(DotNetCLIWrapper).Assembly.Location);
var searchPaths = new[]
{
Path.Combine(AppContext.BaseDirectory, "Assets"),
Path.Combine(assemblyDir, "Assets"),
AppContext.BaseDirectory,
assemblyDir,
};

return searchPaths.Select(p => Path.Combine(p, "AmazonCommonDotNetCliTools.targets")).FirstOrDefault(File.Exists);
}


/// <summary>
/// Retrieve the `OutputType` property of a given project
/// </summary>
Expand Down

0 comments on commit c33ef14

Please sign in to comment.