Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions eng/targets/Sfx.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@
<GetSharedFrameworkFilesForReadyToRunDependsOn>$(GetSharedFrameworkFilesForReadyToRunDependsOn);_AddOptimizationDataForReadyToRun</GetSharedFrameworkFilesForReadyToRunDependsOn>
</PropertyGroup>

<PropertyGroup>
<_RestoreRuntimeTargetCommand Condition="$([MSBuild]::IsOSPlatform('Windows'))">.\restore.cmd -Architecture $(TargetArchitecture) -p:RestoreForce=true</_RestoreRuntimeTargetCommand>
<_RestoreRuntimeTargetCommand Condition="!$([MSBuild]::IsOSPlatform('Windows'))">./restore.sh --os-name $(TargetOsName) --arch $(TargetArchitecture) -p:RestoreForce=true</_RestoreRuntimeTargetCommand>
</PropertyGroup>

<Target Name="_ValidateProjectAssetsRuntimeTarget"
BeforeTargets="ResolvePackageAssets"
Condition="'$(TargetFramework)' != '' AND
'$(RuntimeIdentifier)' != '' AND
Exists('$(ProjectAssetsFile)')">
<CheckProjectAssetsRuntimeTarget
ProjectAssetsFile="$(ProjectAssetsFile)"
TargetFramework="$(TargetFramework)"
RuntimeIdentifier="$(RuntimeIdentifier)">
<Output TaskParameter="RuntimeTargetExists" PropertyName="_ProjectAssetsRuntimeTargetExists" />
</CheckProjectAssetsRuntimeTarget>

<Error
Condition="'$(_ProjectAssetsRuntimeTargetExists)' != 'true'"
Text="The restore assets for $(MSBuildProjectName) do not contain the requested target '$(TargetFramework)/$(RuntimeIdentifier)'. From the repository root, run '$(_RestoreRuntimeTargetCommand)'." />
</Target>

<!-- IBC data packages don't follow NuGet conventions. -->
<Target Name="_AddOptimizationDataForReadyToRun">
<ItemGroup>
Expand Down
62 changes: 62 additions & 0 deletions eng/tools/RepoTasks/CheckProjectAssetsRuntimeTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Text.Json;
using Microsoft.Build.Framework;

namespace RepoTasks;

/// <summary>
/// Checks whether a NuGet assets file contains a target for a target framework
/// and runtime identifier.
/// </summary>
public sealed class CheckProjectAssetsRuntimeTarget : Microsoft.Build.Utilities.Task
{
/// <summary>
/// Gets or sets the NuGet project assets file to inspect.
/// </summary>
[Required]
public string ProjectAssetsFile { get; set; }

/// <summary>
/// Gets or sets the target framework to find.
/// </summary>
[Required]
public string TargetFramework { get; set; }

/// <summary>
/// Gets or sets the runtime identifier to find.
/// </summary>
[Required]
public string RuntimeIdentifier { get; set; }

/// <summary>
/// Gets whether the requested runtime target exists.
/// </summary>
[Output]
public bool RuntimeTargetExists { get; private set; }

/// <inheritdoc />
public override bool Execute()
{
try
{
using var stream = File.OpenRead(ProjectAssetsFile);
using var document = JsonDocument.Parse(stream);
Comment thread
PureWeen marked this conversation as resolved.
Resolved
var targetName = $"{TargetFramework}/{RuntimeIdentifier}";

RuntimeTargetExists =
document.RootElement.TryGetProperty("targets", out var targets) &&
targets.TryGetProperty(targetName, out _);

return true;
}
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or JsonException or InvalidOperationException)
{
Log.LogError($"Unable to inspect NuGet assets file '{ProjectAssetsFile}': {exception.Message}");
Comment thread
PureWeen marked this conversation as resolved.
Resolved
return false;
}
}
}
1 change: 1 addition & 0 deletions eng/tools/RepoTasks/RepoTasks.tasks
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<UsingTask TaskName="RepoTasks.GenerateGuid" AssemblyFile="$(_RepoTaskAssembly)" TaskFactory="TaskHostFactory" Runtime="NET" />
<UsingTask TaskName="RepoTasks.GenerateTestDevCert" AssemblyFile="$(_RepoTaskAssembly)" TaskFactory="TaskHostFactory" Runtime="NET" />
<UsingTask TaskName="RepoTasks.BatchHelixWorkItems" AssemblyFile="$(_RepoTaskAssembly)" TaskFactory="TaskHostFactory" Runtime="NET" />
<UsingTask TaskName="RepoTasks.CheckProjectAssetsRuntimeTarget" AssemblyFile="$(_RepoTaskAssembly)" TaskFactory="TaskHostFactory" Runtime="NET" />
</Project>
Loading