Browse Source

Upgrade to .NET 5

NextTurn 5 năm trước cách đây
mục cha
commit
3bfa46f5b0

+ 4 - 4
eng/build.yml

@@ -40,10 +40,10 @@ jobs:
       projects: src\WinSW.sln
       arguments: -c $(BuildConfiguration) -p:Version=$(BuildVersion)
   - script: |
-      dotnet publish -c $(BuildConfiguration) -f netcoreapp3.1 -r win-x64 src\WinSW\WinSW.csproj -p:Version=$(BuildVersion)
-      dotnet publish -c $(BuildConfiguration) -f netcoreapp3.1 -r win-x86 src\WinSW\WinSW.csproj -p:Version=$(BuildVersion)
-      dotnet publish -c $(BuildConfiguration) -f netcoreapp3.1 -r win-x64 src\WinSW\WinSW.csproj -p:PublishSingleFile=true -p:Version=$(BuildVersion)
-      dotnet publish -c $(BuildConfiguration) -f netcoreapp3.1 -r win-x86 src\WinSW\WinSW.csproj -p:PublishSingleFile=true -p:Version=$(BuildVersion)
+      dotnet publish src\WinSW\WinSW.csproj -c $(BuildConfiguration) -f net5.0 -r win-x64 -p:Version=$(BuildVersion)
+      dotnet publish src\WinSW\WinSW.csproj -c $(BuildConfiguration) -f net5.0 -r win-x86 -p:Version=$(BuildVersion)
+      dotnet publish src\WinSW\WinSW.csproj -c $(BuildConfiguration) -f net5.0 -r win-x64 -p:Version=$(BuildVersion) -p:IncludeNativeLibrariesInSingleFile=true
+      dotnet publish src\WinSW\WinSW.csproj -c $(BuildConfiguration) -f net5.0 -r win-x86 -p:Version=$(BuildVersion) -p:IncludeNativeLibrariesInSingleFile=true
     displayName: Build
   - task: DotNetCoreCLI@2
     displayName: Test

+ 1 - 1
src/WinSW.Core/Configuration/ServiceConfig.cs

@@ -22,7 +22,7 @@ namespace WinSW.Configuration
 
         public abstract string Executable { get; }
 
-        public virtual string ExecutablePath => Process.GetCurrentProcess().MainModule.FileName;
+        public virtual string ExecutablePath => Process.GetCurrentProcess().MainModule!.FileName!;
 
         public virtual bool HideWindow => false;
 

+ 1 - 1
src/WinSW.Core/Download.cs

@@ -187,7 +187,7 @@ namespace WinSW
             }
             catch (WebException e)
             {
-                if (supportsIfModifiedSince && ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.NotModified)
+                if (supportsIfModifiedSince && ((HttpWebResponse?)e.Response)?.StatusCode == HttpStatusCode.NotModified)
                 {
                     Logger.Info($"Skipped downloading unmodified resource '{this.From}'");
                 }

+ 4 - 4
src/WinSW.Core/WinSW.Core.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
-    <TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
+    <TargetFrameworks>net461;net5.0</TargetFrameworks>
     <LangVersion>preview</LangVersion>
     <Nullable>enable</Nullable>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@@ -15,13 +15,13 @@
     </PackageReference>
   </ItemGroup>
 
-  <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
+  <ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
     <PackageReference Include="System.Diagnostics.EventLog" Version="4.7.0" />
     <PackageReference Include="System.Security.AccessControl" Version="4.7.0" />
   </ItemGroup>
 
   <!-- error NU1605: Detected package downgrade: log4net 2.0.8 -->
-  <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
+  <ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
     <PackageReference Include="System.Diagnostics.Debug" Version="4.3.0" />
     <PackageReference Include="System.IO.FileSystem" Version="4.3.0" />
     <PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
@@ -31,7 +31,7 @@
     <PackageReference Include="System.Threading" Version="4.3.0" />
   </ItemGroup>
 
-  <ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
+  <ItemGroup Condition="'$(TargetFramework)' != 'net5.0'">
     <Reference Include="System.ServiceProcess" />
   </ItemGroup>
 

+ 1 - 1
src/WinSW.Plugins/WinSW.Plugins.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
-    <TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
+    <TargetFrameworks>net461;net5.0</TargetFrameworks>
     <LangVersion>latest</LangVersion>
     <Nullable>enable</Nullable>
   </PropertyGroup>

+ 1 - 1
src/WinSW.Tests/WinSW.Tests.csproj

@@ -40,7 +40,7 @@
   <Target Name="Copy" BeforeTargets="AfterBuild">
 
     <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp5.0'">
-      <_FilesToCopy Include="$(ArtifactsBinDir)WinSW\$(Configuration)\netcoreapp3.1\WinSW.runtimeconfig*.json" />
+      <_FilesToCopy Include="$(ArtifactsBinDir)WinSW\$(Configuration)\net5.0\WinSW.runtimeconfig*.json" />
     </ItemGroup>
 
     <ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp5.0'">

+ 5 - 5
src/WinSW/Program.cs

@@ -46,7 +46,7 @@ namespace WinSW
                 }
 
                 using Process current = Process.GetCurrentProcess();
-                return current.MainModule.FileName;
+                return current.MainModule!.FileName!;
             }
         }
 
@@ -463,7 +463,7 @@ namespace WinSW
                     if (username is null)
                     {
                         Console.Write("Username: ");
-                        username = Console.ReadLine();
+                        username = Console.ReadLine()!;
                     }
 
                     if (password is null && !IsSpecialAccount(username))
@@ -798,7 +798,7 @@ namespace WinSW
                         _ = evt.WaitOne();
                         Console.CancelKeyPress -= CancelKeyPress;
 
-                        void CancelKeyPress(object sender, ConsoleCancelEventArgs e)
+                        void CancelKeyPress(object? sender, ConsoleCancelEventArgs e)
                         {
                             e.Cancel = true;
                             evt.Set();
@@ -968,14 +968,14 @@ namespace WinSW
                 {
                     UseShellExecute = true,
                     Verb = "runas",
-                    FileName = current.MainModule.FileName,
+                    FileName = current.MainModule!.FileName!,
                     Arguments = arguments,
                     WindowStyle = ProcessWindowStyle.Hidden,
                 };
 
                 try
                 {
-                    using Process elevated = Process.Start(startInfo);
+                    using Process elevated = Process.Start(startInfo)!;
 
                     elevated.WaitForExit();
                     Environment.Exit(elevated.ExitCode);

+ 13 - 9
src/WinSW/WinSW.csproj

@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <OutputType>Exe</OutputType>
-    <TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
+    <TargetFrameworks>net461;net5.0</TargetFrameworks>
     <LangVersion>preview</LangVersion>
     <Nullable>enable</Nullable>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@@ -15,7 +15,11 @@
     <Copyright>Copyright (c) 2008-2020 Kohsuke Kawaguchi, Sun Microsystems, Inc., CloudBees, Inc., Oleg Nenashev and other contributors</Copyright>
   </PropertyGroup>
 
-  <PropertyGroup Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
+  <PropertyGroup Condition="'$(TargetFramework)' == 'net5.0' AND '$(RuntimeIdentifier)' != ''">
+    <PublishSingleFile>true</PublishSingleFile>
+  </PropertyGroup>
+
+  <PropertyGroup Condition="'$(TargetFramework)' != 'net5.0'">
     <ILMergeVersion>3.0.40</ILMergeVersion>
   </PropertyGroup>
 
@@ -23,11 +27,11 @@
     <PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20303.1" />
   </ItemGroup>
 
-  <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
+  <ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
     <PackageReference Include="System.ServiceProcess.ServiceController" Version="4.7.0" />
   </ItemGroup>
 
-  <ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
+  <ItemGroup Condition="'$(TargetFramework)' != 'net5.0'">
     <PackageReference Include="ilmerge" Version="$(ILMergeVersion)" />
     <Reference Include="System.ServiceProcess" />
   </ItemGroup>
@@ -37,20 +41,20 @@
     <ProjectReference Include="..\WinSW.Plugins\WinSW.Plugins.csproj" />
   </ItemGroup>
 
-  <ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
+  <ItemGroup Condition="'$(TargetFramework)' != 'net5.0'">
     <ProjectReference Include="..\WinSW.Tasks\WinSW.Tasks.csproj">
       <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
     </ProjectReference>
   </ItemGroup>
 
-  <Target Name="PublishCoreZip" AfterTargets="Publish" Condition="'$(TargetFramework)' == 'netcoreapp3.1' and '$(PublishSingleFile)' != 'true'">
+  <Target Name="PublishCoreZip" AfterTargets="Publish" Condition="'$(TargetFramework)' == 'net5.0' and '$(IncludeNativeLibrariesInSingleFile)' != 'true'">
 
     <MakeDir Directories="$(ArtifactsPublishDir)" />
     <ZipDirectory SourceDirectory="$(PublishDir)" DestinationFile="$(ArtifactsPublishDir)WinSW.NETCore.$(PlatformTarget).zip" Overwrite="true" />
 
   </Target>
 
-  <Target Name="PublishCoreExe" AfterTargets="Publish" Condition="'$(TargetFramework)' == 'netcoreapp3.1' and '$(PublishSingleFile)' == 'true'">
+  <Target Name="PublishCoreExe" AfterTargets="Publish" Condition="'$(TargetFramework)' == 'net5.0' and '$(IncludeNativeLibrariesInSingleFile)' == 'true'">
 
     <MakeDir Directories="$(ArtifactsPublishDir)" />
     <Copy SourceFiles="$(PublishDir)$(TargetName).exe" DestinationFiles="$(ArtifactsPublishDir)WinSW.NETCore.$(PlatformTarget).exe" />
@@ -58,7 +62,7 @@
   </Target>
 
   <!-- Merge plugins and other DLLs into the executable -->
-  <Target Name="Merge" BeforeTargets="AfterBuild" Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
+  <Target Name="Merge" BeforeTargets="AfterBuild" Condition="'$(TargetFramework)' != 'net5.0'">
 
     <PropertyGroup Condition="'$(TargetFramework)' == 'net461'">
       <TargetFrameworkSuffix>NET461</TargetFrameworkSuffix>
@@ -90,7 +94,7 @@
   </Target>
 
   <UsingTask TaskName="WinSW.Tasks.Trim" AssemblyFile="$(ArtifactsBinDir)WinSW.Tasks\$(Configuration)\net461\WinSW.Tasks.dll" />
-  <Target Name="Trim" AfterTargets="Merge" Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
+  <Target Name="Trim" AfterTargets="Merge" Condition="'$(TargetFramework)' != 'net5.0'">
     <Trim Path="$(ArtifactsPublishDir)WinSW.$(TargetFrameworkSuffix).exe" />
   </Target>
 

+ 2 - 2
src/WinSW/WrapperService.cs

@@ -540,7 +540,7 @@ namespace WinSW
         /// </summary>
         private Process StartProcess(string executable, string? arguments, LogHandler? logHandler = null, Action<Process>? onExited = null)
         {
-            var startInfo = new ProcessStartInfo(executable, arguments)
+            var startInfo = new ProcessStartInfo(executable, arguments ?? string.Empty)
             {
                 UseShellExecute = false,
                 WorkingDirectory = this.config.WorkingDirectory,
@@ -572,7 +572,7 @@ namespace WinSW
             Process process;
             try
             {
-                process = Process.Start(startInfo);
+                process = Process.Start(startInfo)!;
             }
             finally
             {