Skip to content

Commit df02b01

Browse files
authored
[net10.0] [dotnet] Use the 'ComputeRunArguments' target to compute properties for 'dotnet run'. (#22816)
In .NET 10 there's a new target we can hook into ("ComputeRunArguments") to do anything we need to do before 'dotnet run' runs the target app. For mobile (iOS and tvOS), we now run the "_PrepareRunMobile" target before "ComputeRunArguments", and this will: * Install the app on device for device builds. * Run mlaunch to run launch the app on device or in the simulator. For desktop (macOS and Mac Catalyst), we run the new "_PrepareRunDesktop" target before "ComputeRunArguments" to set the `RunCommand` and `RunArguments` properties. I've also added the capability to _not_ launch apps on desktop with `open`, by setting the property `RunWithOpen=false`. Also add support for passing arguments to the app when running on iOS and tvOS (by passing '-- <extra args' to mlaunch). Fixes #12459. Fixes #14845. References: * dotnet/sdk#42155 * dotnet/android#9470
1 parent c37c231 commit df02b01

File tree

7 files changed

+124
-61
lines changed

7 files changed

+124
-61
lines changed

docs/building-apps/build-properties.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,99 @@ only scan libraries with the `[LinkWith]` attribute for Objective-C classes:
916916
</PropertyGroup>
917917
```
918918

919+
## RunWithOpen
920+
921+
This property determines whether apps are launched using the `open` command on
922+
macOS, or if the app's executable is executed directly.
923+
924+
This only applies to macOS and Mac Catalyst apps.
925+
926+
The default value is `true`. In this mode, the app will be launched by macOS as any other UI application, any stdout/stderr output will be swallowed by macOS, and the `dotnet run` command will finish as soon as the app has launched.
927+
928+
If set to `false`, any stdout/stderr output will be printed to the current terminal, and the `dotnet run` command won't finish until the app has exited.
929+
930+
The following properties can be used to configure the behavior when set to `true` (i.e. using the `open` command):
931+
932+
### OpenNewInstance
933+
934+
If a new instance will be opened if the app is already running (defaults to `false`).
935+
936+
This will pass `-n` to `open` if set to `true`.
937+
938+
Example:
939+
940+
```shell
941+
$ dotnet run -p:OpenNewInstance=false
942+
```
943+
944+
### OpenArguments
945+
946+
This property can be used to pass additional arguments to the `open` command.
947+
948+
Example (to set environment variables):
949+
950+
```shell
951+
$ dotnet run -p:OpenArguments="--env VARIABLE1=VALUE1 --env VARIABLE2=value2"
952+
```
953+
954+
Example (to redirect stdout and stderr to a file):
955+
956+
```shell
957+
$ dotnet run -p:OpenArguments="--stdout /tmp/stdout.txt --stderr /tmp/stderr.txt"
958+
```
959+
960+
Run `man open` to see a list of all the options `open` accepts.
961+
962+
### StandardOutputPath
963+
964+
This property can be used to redirect the stdout output from the app to a file.
965+
966+
Example writing to a file:
967+
968+
```shell
969+
$ dotnet run -p:StandardOutputPath=stdout.txt
970+
```
971+
972+
Example writing to the current terminal:
973+
974+
```shell
975+
$ dotnet run -p:StandardOutputPath=$(tty)
976+
[... Console.WriteLine output from app ...]
977+
```
978+
979+
Note: this can also be accomplished by passing `--stdout ...` using the [OpenArguments](#openarguments) property.
980+
981+
### StandardErrorPath
982+
983+
This property can be used to redirect the stderr output from the app to a file.
984+
985+
Example writing to a file:
986+
987+
```shell
988+
$ dotnet run -p:StandardErrorPath=stderr.txt
989+
```
990+
991+
Example writing to the current terminal:
992+
993+
```shell
994+
$ dotnet run -p:StandardErrorPath=$(tty)
995+
[... Console.Error.WriteLine output from app ...]
996+
```
997+
998+
Note: this can also be accomplished by passing `--stderr ...` using the [OpenArguments](#openarguments) property.
999+
1000+
### StandardInputPath
1001+
1002+
This property can be used to redirect the stdin input to the app from a file.
1003+
1004+
Example:
1005+
1006+
```shell
1007+
$ dotnet run -p:StandardInputPath=stdin.txt
1008+
```
1009+
1010+
Note: this can also be accomplished by passing `--stdin ...` using the [OpenArguments](#openarguments) property.
1011+
9191012
## SkipStaticLibraryValidation
9201013

9211014
Hot Restart doesn't support linking with static libraries, so by default we'll
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="Xamarin.Shared.Sdk.targets" />
4-
5-
<PropertyGroup>
6-
<RunEnvironment Condition="'$(XamarinDebugMode)' != ''">$(RunEnvironment) --env '__XAMARIN_DEBUG_MODE__=$(XamarinDebugMode)'</RunEnvironment>
7-
<RunEnvironment Condition="'$(XamarinDebugPort)' != ''">$(RunEnvironment) --env '__XAMARIN_DEBUG_PORT__=$(XamarinDebugPort)'</RunEnvironment>
8-
<RunEnvironment Condition="'$(XamarinDebugHosts)' != ''">$(RunEnvironment) --env '__XAMARIN_DEBUG_HOSTS__=$(XamarinDebugHosts)'</RunEnvironment>
9-
<RunEnvironment Condition="'$(XamarinDebugConnectTimeout)' != ''">$(RunEnvironment) --env '__XAMARIN_DEBUG_CONNECT_TIMEOUT__=$(XamarinDebugConnectTimeout)'</RunEnvironment>
10-
<OpenArguments>$(OpenArguments) $(RunEnvironment)</OpenArguments>
11-
<OpenArguments Condition="'$(StandardOutputPath)' != ''">$(OpenArguments) --stdout '$(StandardOutputPath)'</OpenArguments>
12-
<OpenArguments Condition="'$(StandardErrorPath)' != ''">$(OpenArguments) --stderr '$(StandardErrorPath)'</OpenArguments>
13-
<OpenArguments Condition="'$(StandardInputPath)' != ''">$(OpenArguments) --stdin '$(StandardInputPath)'</OpenArguments>
14-
<OpenArguments Condition="'$(OpenNewInstance)' == 'true'">$(OpenArguments) -n</OpenArguments>
15-
<RunCommand>open</RunCommand>
16-
<RunArguments>$(OpenArguments) "$(TargetDir)/$(AssemblyName).app" --args</RunArguments>
17-
</PropertyGroup>
184
</Project>
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="Xamarin.Shared.Sdk.targets" />
4-
5-
<!--
6-
There's no way to execute a task (for instance to calculate the arguments to mlaunch) in "dotnet run",
7-
so we rely on the 'Run' target instead. Quite ugly, but it seems to work, even though it won't support
8-
"dotnet run" flags such as /no-build. OTOH we try to support /configuration and /runtime.
9-
Ref: https://github.com/dotnet/sdk/issues/18436
10-
-->
11-
<PropertyGroup>
12-
<RunCommand>$(NetCoreRoot)/dotnet</RunCommand>
13-
<RunArguments>build "$(MSBuildProjectFullPath)" /t:Run /p:RuntimeIdentifier=$(RuntimeIdentifier) /p:Configuration=$(Configuration)</RunArguments>
14-
</PropertyGroup>
154
</Project>
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="Xamarin.Shared.Sdk.targets" />
4-
5-
<PropertyGroup>
6-
<RunEnvironment Condition="'$(XamarinDebugMode)' != ''">$(RunEnvironment) --env '__XAMARIN_DEBUG_MODE__=$(XamarinDebugMode)'</RunEnvironment>
7-
<RunEnvironment Condition="'$(XamarinDebugPort)' != ''">$(RunEnvironment) --env '__XAMARIN_DEBUG_PORT__=$(XamarinDebugPort)'</RunEnvironment>
8-
<RunEnvironment Condition="'$(XamarinDebugHosts)' != ''">$(RunEnvironment) --env '__XAMARIN_DEBUG_HOSTS__=$(XamarinDebugHosts)'</RunEnvironment>
9-
<RunEnvironment Condition="'$(XamarinDebugConnectTimeout)' != ''">$(RunEnvironment) --env '__XAMARIN_DEBUG_CONNECT_TIMEOUT__=$(XamarinDebugConnectTimeout)'</RunEnvironment>
10-
<OpenArguments>$(OpenArguments) $(RunEnvironment)</OpenArguments>
11-
<OpenArguments Condition="'$(StandardOutputPath)' != ''">$(OpenArguments) --stdout '$(StandardOutputPath)'</OpenArguments>
12-
<OpenArguments Condition="'$(StandardErrorPath)' != ''">$(OpenArguments) --stderr '$(StandardErrorPath)'</OpenArguments>
13-
<OpenArguments Condition="'$(StandardInputPath)' != ''">$(OpenArguments) --stdin '$(StandardInputPath)'</OpenArguments>
14-
<OpenArguments Condition="'$(OpenNewInstance)' == 'true'">$(OpenArguments) -n</OpenArguments>
15-
<RunCommand>open</RunCommand>
16-
<RunArguments>$(OpenArguments) "$(TargetDir)/$(AssemblyName).app" --args</RunArguments>
17-
</PropertyGroup>
184
</Project>
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="Xamarin.Shared.Sdk.targets" />
4-
5-
<!--
6-
There's no way to execute a task (for instance to calculate the arguments to mlaunch) in "dotnet run",
7-
so we rely on the 'Run' target instead. Quite ugly, but it seems to work, even though it won't support
8-
"dotnet run" flags such as /no-build. OTOH we try to support /configuration and /runtime.
9-
Ref: https://github.com/dotnet/sdk/issues/18436
10-
-->
11-
<PropertyGroup>
12-
<RunCommand>$(NetCoreRoot)/dotnet</RunCommand>
13-
<RunArguments>build "$(MSBuildProjectFullPath)" /t:Run /p:RuntimeIdentifier=$(RuntimeIdentifier) /p:Configuration=$(Configuration)</RunArguments>
14-
</PropertyGroup>
154
</Project>

dotnet/targets/Xamarin.Shared.Sdk.targets

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,21 +2259,41 @@
22592259
</Target>
22602260

22612261
<!-- This is only needed for mobile platforms, RunCommand and RunArguments are defined for macOS in Microsoft.macOS.Sdk.targets. -->
2262-
<Target Name="_PrepareRunMobile" DependsOnTargets="_InstallMobile;ComputeMlaunchRunArguments" Condition="'$(_PlatformName)' != 'macOS' And '$(_PlatformName)' != 'MacCatalyst'">
2262+
<Target
2263+
Name="_PrepareRunMobile"
2264+
BeforeTargets="ComputeRunArguments"
2265+
DependsOnTargets="_InstallMobile;ComputeMlaunchRunArguments"
2266+
Condition="'$(_PlatformName)' != 'macOS' And '$(_PlatformName)' != 'MacCatalyst'">
22632267
<PropertyGroup>
2264-
<RunCommand>'$(MlaunchPath)'</RunCommand>
2265-
<RunArguments>$(MlaunchRunArguments)</RunArguments>
2268+
<RunCommand>$(MlaunchPath)</RunCommand>
2269+
<RunArguments>$(MlaunchRunArguments) -- </RunArguments>
22662270
</PropertyGroup>
22672271
</Target>
22682272

2269-
<PropertyGroup>
2270-
<_PrepareRunDependsOn>
2271-
Build;
2272-
_PrepareRunMobile;
2273-
</_PrepareRunDependsOn>
2274-
</PropertyGroup>
2273+
<Target
2274+
Name="_PrepareRunDesktop"
2275+
BeforeTargets="ComputeRunArguments"
2276+
Condition="'$(_PlatformName)' == 'macOS' Or '$(_PlatformName)' == 'MacCatalyst'">
2277+
2278+
<PropertyGroup Condition="'$(RunWithOpen)' != 'false'">
2279+
<_OpenArguments Condition="'$(XamarinDebugMode)' != ''">$(OpenArguments) --env __XAMARIN_DEBUG_MODE__=$(XamarinDebugMode)</_OpenArguments>
2280+
<_OpenArguments Condition="'$(XamarinDebugPort)' != ''">$(OpenArguments) --env __XAMARIN_DEBUG_PORT__=$(XamarinDebugPort)</_OpenArguments>
2281+
<_OpenArguments Condition="'$(XamarinDebugHosts)' != ''">$(OpenArguments) --env "__XAMARIN_DEBUG_HOSTS__=$(XamarinDebugHosts)"</_OpenArguments>
2282+
<_OpenArguments Condition="'$(XamarinDebugConnectTimeout)' != ''">$(OpenArguments) --env __XAMARIN_DEBUG_CONNECT_TIMEOUT__=$(XamarinDebugConnectTimeout)</_OpenArguments>
2283+
<_OpenArguments Condition="'$(StandardOutputPath)' != ''">$(OpenArguments) --stdout "$(StandardOutputPath)"</_OpenArguments>
2284+
<_OpenArguments Condition="'$(StandardErrorPath)' != ''">$(OpenArguments) --stderr "$(StandardErrorPath)"</_OpenArguments>
2285+
<_OpenArguments Condition="'$(StandardInputPath)' != ''">$(OpenArguments) --stdin "$(StandardInputPath)"</_OpenArguments>
2286+
<_OpenArguments Condition="'$(OpenNewInstance)' == 'true'">$(OpenArguments) -n</_OpenArguments>
2287+
<_OpenArguments>$(OpenArguments) $(RunEnvironment)</_OpenArguments>
2288+
<RunCommand>open</RunCommand>
2289+
<RunArguments>$(_OpenArguments) "$(TargetDir)/$(AssemblyName).app" --args</RunArguments>
2290+
</PropertyGroup>
22752291

2276-
<Target Name="_PrepareRun" DependsOnTargets="$(_PrepareRunDependsOn)" BeforeTargets="Run" />
2292+
<PropertyGroup Condition="'$(RunWithOpen)' == 'false'">
2293+
<RunCommand>$(TargetDir)/$(AssemblyName).app/Contents/MacOS/$(AssemblyName)</RunCommand>
2294+
<RunArguments />
2295+
</PropertyGroup>
2296+
</Target>
22772297

22782298
<!--
22792299
Add a 'global using nfloat = System.Runtime.InteropServices.NFloat' to ease migration from Xamarin.

mk/xamarin.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ endif
1010

1111
# Available versions can be seen here:
1212
# https://dev.azure.com/dnceng/public/_artifacts/feed/dotnet-eng/NuGet/Microsoft.Tools.Mlaunch/versions
13-
MLAUNCH_NUGET_VERSION=1.0.272
13+
MLAUNCH_NUGET_VERSION=1.1.26
1414

1515
define CheckVersionTemplate
1616
check-$(1)::

0 commit comments

Comments
 (0)