Skip to content

Commit 83fe610

Browse files
authored
Crossplatform ResourceUpdater (#34394)
2 parents c4476b9 + 8b6f4da commit 83fe610

File tree

5 files changed

+46
-35
lines changed

5 files changed

+46
-35
lines changed

src/Cli/dotnet/ShellShim/AppHostShimMaker.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using Microsoft.DotNet.Tools;
55
using Microsoft.Extensions.EnvironmentAbstractions;
6-
using Microsoft.NET.HostModel;
76
using Microsoft.NET.HostModel.AppHost;
87

98
namespace Microsoft.DotNet.ShellShim
@@ -40,27 +39,14 @@ public void CreateApphostShellShim(FilePath entryPoint, FilePath shimPath)
4039
string entryPointFullPath = Path.GetFullPath(entryPoint.Value);
4140
var appBinaryFilePath = Path.GetRelativePath(Path.GetDirectoryName(appHostDestinationFilePath), entryPointFullPath);
4241

42+
var windowsGraphicalUserInterfaceBit = PEUtils.GetWindowsGraphicalUserInterfaceBit(entryPointFullPath);
43+
var windowsGraphicalUserInterface = (windowsGraphicalUserInterfaceBit == WindowsGUISubsystem) && OperatingSystem.IsWindows();
4344

44-
if (ResourceUpdater.IsSupportedOS())
45-
{
46-
var windowsGraphicalUserInterfaceBit = PEUtils.GetWindowsGraphicalUserInterfaceBit(entryPointFullPath);
47-
HostWriter.CreateAppHost(appHostSourceFilePath: appHostSourcePath,
48-
appHostDestinationFilePath: appHostDestinationFilePath,
49-
appBinaryFilePath: appBinaryFilePath,
50-
windowsGraphicalUserInterface: (windowsGraphicalUserInterfaceBit == WindowsGUISubsystem) && OperatingSystem.IsWindows(),
51-
assemblyToCopyResourcesFrom: entryPointFullPath);
52-
}
53-
else
54-
{
55-
// by passing null to assemblyToCopyResourcesFrom, it will skip copying resources,
56-
// which is only supported on Windows
57-
HostWriter.CreateAppHost(appHostSourceFilePath: appHostSourcePath,
58-
appHostDestinationFilePath: appHostDestinationFilePath,
59-
appBinaryFilePath: appBinaryFilePath,
60-
windowsGraphicalUserInterface: false,
61-
assemblyToCopyResourcesFrom: null,
62-
enableMacOSCodeSign: OperatingSystem.IsMacOS());
63-
}
45+
HostWriter.CreateAppHost(appHostSourceFilePath: appHostSourcePath,
46+
appHostDestinationFilePath: appHostDestinationFilePath,
47+
appBinaryFilePath: appBinaryFilePath,
48+
windowsGraphicalUserInterface: windowsGraphicalUserInterface,
49+
assemblyToCopyResourcesFrom: entryPointFullPath);
6450

6551
_filePermissionSetter.SetUserExecutionPermission(appHostDestinationFilePath);
6652
}

src/Tasks/Microsoft.NET.Build.Tasks/CreateAppHost.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@ protected override void ExecuteCore()
5353
var isGUI = WindowsGraphicalUserInterface;
5454
var resourcesAssembly = IntermediateAssembly;
5555

56-
if (!ResourceUpdater.IsSupportedOS())
57-
{
58-
if (isGUI)
59-
{
60-
Log.LogWarning(Strings.AppHostCustomizationRequiresWindowsHostWarning);
61-
}
62-
63-
isGUI = false;
64-
resourcesAssembly = null;
65-
}
66-
6756
int attempts = 0;
6857

6958
while (true)

src/Tasks/Microsoft.NET.Build.Tasks/GenerateShims.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using Microsoft.Build.Framework;
55
using Microsoft.Build.Utilities;
6-
using Microsoft.NET.HostModel;
76
using Microsoft.NET.HostModel.AppHost;
87
using NuGet.Versioning;
98

@@ -108,7 +107,7 @@ protected override void ExecuteCore()
108107
try
109108
{
110109
var windowsGraphicalUserInterface = runtimeIdentifier.StartsWith("win") && "WinExe".Equals(OutputType, StringComparison.OrdinalIgnoreCase);
111-
if (ResourceUpdater.IsSupportedOS() && runtimeIdentifier.StartsWith("win"))
110+
if (runtimeIdentifier.StartsWith("win"))
112111
{
113112
HostWriter.CreateAppHost(appHostSourceFilePath: resolvedApphostAssetPath,
114113
appHostDestinationFilePath: appHostDestinationFilePath,

src/Tests/Microsoft.NET.Build.Tests/GivenWeWantToRequireWindowsForDesktopApps.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.IO.MemoryMappedFiles;
45
using System.Runtime.CompilerServices;
56
using Microsoft.NET.Build.Tasks;
67

@@ -104,6 +105,41 @@ public void AppTargetingWindows10CanBuildOnNonWindows()
104105
.Pass();
105106
}
106107

108+
[PlatformSpecificFact(TestPlatforms.Linux | TestPlatforms.OSX | TestPlatforms.FreeBSD)]
109+
public void AppTargetingWindows10WillProduceWindowsGUISubsystemExe()
110+
{
111+
// check subsystem is successfully set as WindowsGUISubsystem
112+
var testProject = new TestProject()
113+
{
114+
TargetFrameworks = ToolsetInfo.CurrentTargetFramework + "-windows10.0.19041.0",
115+
IsWinExe = true
116+
};
117+
testProject.AdditionalProperties["EnableWindowsTargeting"] = "true";
118+
testProject.AdditionalProperties["RuntimeIdentifier"] = "win-x64";
119+
120+
var testAsset = _testAssetsManager.CreateTestProject(testProject);
121+
122+
new PublishCommand(testAsset)
123+
.Execute()
124+
.Should()
125+
.Pass();
126+
127+
var exePath = Path.Combine(testAsset.TestRoot, testAsset.TestProject.Name,
128+
"bin", "Debug", testAsset.TestProject.TargetFrameworks, "win-x64", "publish",
129+
$"{testAsset.TestProject.Name}.exe");
130+
131+
const int PEHeaderPointerOffset = 0x3C;
132+
const int SubsystemOffset = 0x5C;
133+
const ushort WindowsGUISubsystem = 0x2;
134+
135+
using var mmap = MemoryMappedFile.CreateFromFile(exePath);
136+
using var accessor = mmap.CreateViewAccessor();
137+
138+
uint peHeaderOffset = accessor.ReadUInt32(PEHeaderPointerOffset);
139+
ushort subsystem = accessor.ReadUInt16(peHeaderOffset + SubsystemOffset);
140+
subsystem.Should().Be(WindowsGUISubsystem);
141+
}
142+
107143
[PlatformSpecificFact(TestPlatforms.Linux | TestPlatforms.OSX | TestPlatforms.FreeBSD)]
108144
public void WindowsFormsAppCanBuildOnNonWindows()
109145
{

src/Tests/Microsoft.NET.ToolPack.Tests/GivenThatWeWantToPackAToolProjectWithPackagedShim.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public void When_version_and_packageVersion_is_different_It_produces_valid_shims
383383
AssertValidShim(_testRoot, nugetPackage);
384384
}
385385

386-
[WindowsOnlyFact]
386+
[Fact]
387387
public void Given_wpf_project_It_contains_shim_with_WindowsGraphicalUserInterfaceBit()
388388
{
389389
ushort windowsGUISubsystem = 0x2;
@@ -395,6 +395,7 @@ public void Given_wpf_project_It_contains_shim_with_WindowsGraphicalUserInterfac
395395
IsWinExe = true,
396396
};
397397

398+
testProject.AdditionalProperties.Add("EnableWindowsTargeting", "true");
398399
testProject.AdditionalProperties.Add("UseWPF", "true");
399400
testProject.AdditionalProperties.Add("PackAsToolShimRuntimeIdentifiers", $"win-x64;{ToolsetInfo.LatestMacRuntimeIdentifier}-x64");
400401
testProject.AdditionalProperties.Add("ToolCommandName", _customToolCommandName);

0 commit comments

Comments
 (0)