Skip to content

Commit d9def4c

Browse files
authored
Merge pull request #416 from nblumhardt/windows-tfm-publishing
Publish a platform-neutral .NET tool
2 parents 0486124 + 6c58c4a commit d9def4c

32 files changed

+119
-162
lines changed

build/Build.Windows.ps1

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ $version = Get-SemVer
1313
Write-Output "Building version $version"
1414

1515
$framework = 'net9.0'
16-
$windowsTfmSuffix = '-windows'
1716

1817
function Clean-Output
1918
{
@@ -28,7 +27,7 @@ function Restore-Packages
2827

2928
function Execute-Tests($version)
3029
{
31-
& dotnet test ./test/SeqCli.Tests/SeqCli.Tests.csproj -c Release --framework "$framework$windowsTfmSuffix" /p:Configuration=Release /p:Platform=x64 /p:VersionPrefix=$version
30+
& dotnet test ./test/SeqCli.Tests/SeqCli.Tests.csproj -c Release --framework "$framework" /p:Configuration=Release /p:Platform=x64 /p:VersionPrefix=$version
3231
if($LASTEXITCODE -ne 0) { throw "Build failed" }
3332
}
3433

@@ -39,12 +38,9 @@ function Create-ArtifactDir
3938

4039
function Publish-Archives($version)
4140
{
42-
$rids = $([xml](Get-Content .\src\SeqCli\SeqCli.csproj)).Project.PropertyGroup.RuntimeIdentifiers[0].Split(';')
41+
$rids = $([xml](Get-Content .\src\SeqCli\SeqCli.csproj)).Project.PropertyGroup.RuntimeIdentifiers.Split(';')
4342
foreach ($rid in $rids) {
4443
$tfm = $framework
45-
if ($rid -eq "win-x64") {
46-
$tfm = "$tfm$windowsTfmSuffix"
47-
}
4844

4945
& dotnet publish ./src/SeqCli/SeqCli.csproj -c Release -f $tfm -r $rid --self-contained /p:VersionPrefix=$version
5046
if($LASTEXITCODE -ne 0) { throw "Build failed" }

src/SeqCli/Cli/CommandAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ public class CommandAttribute : Attribute, ICommandMetadata
2424
public string HelpText { get; }
2525
public string? Example { get; set; }
2626
public FeatureVisibility Visibility { get; set; }
27+
public SupportedPlatforms Platforms { get; set; }
2728

2829
public CommandAttribute(string name, string helpText)
2930
{
3031
Name = name;
3132
HelpText = helpText;
3233
Visibility = FeatureVisibility.Visible;
34+
Platforms = SupportedPlatforms.All;
3335
}
3436

3537
public CommandAttribute(string name, string subCommand, string helpText) : this(name, helpText)
3638
{
3739
SubCommand = subCommand;
3840
}
39-
}
41+
}

src/SeqCli/Cli/CommandLineHost.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections.Generic;
1717
using System.Linq;
1818
using System.Reflection;
19+
using System.Runtime.InteropServices;
1920
using System.Threading.Tasks;
2021
using Autofac.Features.Metadata;
2122
using Serilog.Core;
@@ -55,8 +56,12 @@ public async Task<int> Run(string[] args, LoggingLevelSwitch levelSwitch)
5556
if (args.Any(a => a.Trim() is prereleaseArg))
5657
featureVisibility |= FeatureVisibility.Preview;
5758

59+
var currentPlatform = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
60+
? SupportedPlatforms.Windows
61+
: SupportedPlatforms.Posix;
62+
5863
var cmd = _availableCommands.SingleOrDefault(c =>
59-
featureVisibility.HasFlag(c.Metadata.Visibility) &&
64+
c.Metadata.Platforms.HasFlag(currentPlatform) && featureVisibility.HasFlag(c.Metadata.Visibility) &&
6065
c.Metadata.Name == commandName &&
6166
(c.Metadata.SubCommand == subCommandName || c.Metadata.SubCommand == null));
6267

src/SeqCli/Cli/CommandMetadata.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ public class CommandMetadata : ICommandMetadata
2121
public required string HelpText { get; set; }
2222
public string? Example { get; set; }
2323
public FeatureVisibility Visibility { get; set; }
24+
public SupportedPlatforms Platforms { get; set; }
2425
}

src/SeqCli/Cli/Commands/Forwarder/InstallCommand.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#if WINDOWS
16-
1715
using System;
1816
using System.Diagnostics.CodeAnalysis;
1917
using System.IO;
18+
using System.Runtime.InteropServices;
2019
using System.Security.AccessControl;
2120
using System.Threading.Tasks;
2221
using SeqCli.Cli.Features;
@@ -29,7 +28,7 @@ namespace SeqCli.Cli.Commands.Forwarder;
2928

3029
// ReSharper disable once ClassNeverInstantiated.Global
3130

32-
[Command("forwarder", "install", "Install the forwarder as a Windows service", Visibility = FeatureVisibility.Preview)]
31+
[Command("forwarder", "install", "Install the forwarder as a Windows service", Visibility = FeatureVisibility.Preview, Platforms = SupportedPlatforms.Windows)]
3332
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
3433
class InstallCommand : Command
3534
{
@@ -100,7 +99,7 @@ void Install()
10099
if (netshResult != 0)
101100
Console.WriteLine($"Could not add URL reservation for {listenUri}: `netsh` returned {netshResult}; ignoring");
102101

103-
var exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Program.BinaryName);
102+
var exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Program.WindowsBinaryName);
104103
var forwarderRunCmdline = $"\"{exePath}\" forwarder run --pre --storage=\"{_storagePath.StorageRootPath}\"";
105104

106105
var binPath = forwarderRunCmdline.Replace("\"", "\\\"");
@@ -149,5 +148,3 @@ static string MakeListenUriReservationPattern(string uri)
149148
return listenUri;
150149
}
151150
}
152-
153-
#endif

src/SeqCli/Cli/Commands/Forwarder/RestartCommand.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#if WINDOWS
16-
1715
using System;
1816
using System.Diagnostics.CodeAnalysis;
1917
using System.ServiceProcess;
@@ -24,7 +22,7 @@
2422

2523
namespace SeqCli.Cli.Commands.Forwarder;
2624

27-
[Command("forwarder", "restart", "Restart the forwarder Windows service", Visibility = FeatureVisibility.Preview)]
25+
[Command("forwarder", "restart", "Restart the forwarder Windows service", Visibility = FeatureVisibility.Preview, Platforms = SupportedPlatforms.Windows)]
2826
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
2927
class RestartCommand : Command
3028
{
@@ -80,5 +78,3 @@ protected override Task<int> Run()
8078
}
8179
}
8280
}
83-
84-
#endif

src/SeqCli/Cli/Commands/Forwarder/RunCommand.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Diagnostics.CodeAnalysis;
1818
using System.IO;
1919
using System.Net;
20+
using System.Runtime.InteropServices;
2021
using System.Threading;
2122
using System.Threading.Tasks;
2223
using Autofac;
@@ -36,10 +37,8 @@
3637
using Serilog.Events;
3738
using Serilog.Formatting.Compact;
3839

39-
#if WINDOWS
4040
using System.Security.Cryptography.X509Certificates;
4141
using SeqCli.Forwarder.ServiceProcess;
42-
#endif
4342

4443
// ReSharper disable UnusedType.Global
4544

@@ -134,12 +133,16 @@ protected override async Task<int> Run(string[] unrecognized)
134133
{
135134
options.Listen(ipAddress, apiListenUri.Port, listenOptions =>
136135
{
137-
#if WINDOWS
138-
listenOptions.UseHttps(StoreName.My, apiListenUri.Host,
139-
location: StoreLocation.LocalMachine, allowInvalid: true);
140-
#else
141-
listenOptions.UseHttps();
142-
#endif
136+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
137+
{
138+
listenOptions.UseHttps(StoreName.My, apiListenUri.Host,
139+
location: StoreLocation.LocalMachine, allowInvalid: true);
140+
}
141+
else
142+
{
143+
listenOptions.UseHttps();
144+
}
145+
143146
});
144147
}
145148
else
@@ -193,14 +196,14 @@ protected override async Task<int> Run(string[] unrecognized)
193196
// ReSharper disable once UnusedParameter.Local
194197
static int RunService(ServerService service)
195198
{
196-
#if WINDOWS
197-
System.ServiceProcess.ServiceBase.Run([
198-
new SeqCliForwarderWindowsService(service)
199-
]);
200-
return 0;
201-
#else
202-
throw new NotSupportedException("Windows services are not supported on this platform.");
203-
#endif
199+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
200+
throw new NotSupportedException("Windows services are not supported on this platform.");
201+
202+
203+
System.ServiceProcess.ServiceBase.Run([
204+
new SeqCliForwarderWindowsService(service)
205+
]);
206+
return 0;
204207
}
205208

206209
static async Task<int> RunStandardIOAsync(ServerService service, TextWriter cout)

src/SeqCli/Cli/Commands/Forwarder/StartCommand.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#if WINDOWS
16-
1715
using System;
1816
using System.Diagnostics.CodeAnalysis;
1917
using System.ServiceProcess;
@@ -22,7 +20,7 @@
2220

2321
namespace SeqCli.Cli.Commands.Forwarder;
2422

25-
[Command("forwarder", "start", "Start the forwarder Windows service", Visibility = FeatureVisibility.Preview)]
23+
[Command("forwarder", "start", "Start the forwarder Windows service", Visibility = FeatureVisibility.Preview, Platforms = SupportedPlatforms.Windows)]
2624
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
2725
class StartCommand : Command
2826
{
@@ -64,5 +62,3 @@ protected override Task<int> Run()
6462
}
6563
}
6664
}
67-
68-
#endif

src/SeqCli/Cli/Commands/Forwarder/StatusCommand.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#if WINDOWS
16-
1715
using System;
1816
using System.Diagnostics.CodeAnalysis;
1917
using System.ServiceProcess;
@@ -22,7 +20,7 @@
2220

2321
namespace SeqCli.Cli.Commands.Forwarder;
2422

25-
[Command("forwarder", "status", "Show the status of the forwarder Windows service", Visibility = FeatureVisibility.Preview)]
23+
[Command("forwarder", "status", "Show the status of the forwarder Windows service", Visibility = FeatureVisibility.Preview, Platforms = SupportedPlatforms.Windows)]
2624
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
2725
class StatusCommand : Command
2826
{
@@ -48,5 +46,3 @@ protected override Task<int> Run()
4846
return Task.FromResult(1);
4947
}
5048
}
51-
52-
#endif

src/SeqCli/Cli/Commands/Forwarder/StopCommand.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#if WINDOWS
16-
1715
using System;
1816
using System.Diagnostics.CodeAnalysis;
1917
using System.ServiceProcess;
@@ -22,7 +20,7 @@
2220

2321
namespace SeqCli.Cli.Commands.Forwarder;
2422

25-
[Command("forwarder", "stop", "Stop the forwarder Windows service", Visibility = FeatureVisibility.Preview)]
23+
[Command("forwarder", "stop", "Stop the forwarder Windows service", Visibility = FeatureVisibility.Preview, Platforms = SupportedPlatforms.Windows)]
2624
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")]
2725
class StopCommand : Command
2826
{
@@ -65,5 +63,3 @@ protected override Task<int> Run()
6563
}
6664
}
6765
}
68-
69-
#endif

0 commit comments

Comments
 (0)