NewLife/Stardust

优化本地安装,现在的流程过于复杂
智能石头 authored at 2020-12-02 19:40:37
80b2439
Tree
1 Parent(s) 0844344
Summary: 5 changed files with 49 additions and 46 deletions.
Modified +12 -3
Modified +14 -27
Modified +6 -14
Added +16 -0
Modified +1 -2
Modified +12 -3
diff --git a/StarAgent/StarService.cs b/StarAgent/StarService.cs
index 5acdbd4..2f8acdd 100644
--- a/StarAgent/StarService.cs
+++ b/StarAgent/StarService.cs
@@ -1,6 +1,7 @@
 using System;
 using System.Diagnostics;
 using System.IO;
+using System.Linq;
 using System.Threading;
 using NewLife;
 using NewLife.Agent;
@@ -69,10 +70,18 @@ namespace StarAgent
                 if (delay > 0) Thread.Sleep(delay * 1000);
 
                 // 如果要杀的是自己,则重启服务
-                if (processId == Process.GetCurrentProcess().Id && !fileName.IsNullOrEmpty())
+                var args = Environment.CommandLine.Split(" ");
+                if (processId == Process.GetCurrentProcess().Id && args.Contains("-s"))
                 {
-                    Host.Restart(Service.ServiceName);
-                    return;
+                    try
+                    {
+                        Host.Restart(Service.ServiceName);
+                        return;
+                    }
+                    catch (Exception ex)
+                    {
+                        XTrace.WriteException(ex);
+                    }
                 }
 
                 try
Modified +14 -27
diff --git a/Stardust/LocalStarClient.cs b/Stardust/LocalStarClient.cs
index 2ac5c8e..5d48d61 100644
--- a/Stardust/LocalStarClient.cs
+++ b/Stardust/LocalStarClient.cs
@@ -1,5 +1,4 @@
 using System;
-using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Linq;
@@ -107,6 +106,7 @@ namespace Stardust
                 // 比目标版本高,不需要安装
                 if (String.Compare(info.Version, version) >= 0) return true;
 
+                if (!info.FileName.IsNullOrEmpty()) info.FileName = info.FileName.TrimEnd(" (deleted)");
                 if (target.IsNullOrEmpty()) target = Path.GetDirectoryName(info.FileName);
 
                 XTrace.WriteLine("StarAgent在用版本 v{0},低于目标版本 v{1}", info.Version, version);
@@ -128,16 +128,15 @@ namespace Stardust
 
             // 准备安装,甭管是否能够成功重启,先覆盖了文件再说
             {
-                if (target.IsNullOrEmpty())
-                {
-                    target = Runtime.Windows ? "C:\\StarAgent" : $"\\home\\{Environment.UserName}";
-                }
+                if (target.IsNullOrEmpty()) target = "..\\staragent";
                 target = target.GetFullPath();
                 target.EnsureDirectory(false);
 
+                XTrace.WriteLine("目标:{0}", target);
+
                 var ug = new Upgrade
                 {
-                    SourceFile = Path.GetFileName(url),
+                    SourceFile = Path.GetFileName(url).GetFullPath(),
                     DestinationPath = target,
 
                     Log = XTrace.Log,
@@ -160,10 +159,13 @@ namespace Stardust
                     Process.GetProcessById(info.ProcessId) :
                     Process.GetProcesses().FirstOrDefault(e => e.ProcessName == "StarAgent");
 
+                // 在Linux中设置执行权限
+                var fileName = info?.FileName ?? target.CombinePath(Runtime.Linux ? "StarAgent" : "StarAgent.exe");
+                if (File.Exists(fileName) && Runtime.Linux) Process.Start("chmod", $"+x {fileName}");
+
                 // 让对方自己退出
                 if (info != null)
                 {
-                    //_client.Invoke<String>("KillAndStart", new { processId = info.ProcessId });
                     _client.Invoke<String>("KillAndStart", new
                     {
                         processId = p.Id,
@@ -187,13 +189,14 @@ namespace Stardust
                     }
                 }
 
-                var fileName = info?.FileName ?? target.CombinePath("StarAgent.exe");
                 if (File.Exists(fileName))
                 {
-                    if (Runtime.Linux)
+                    if (info?.Arguments == "-s")
+                    {
+                        Process.Start(fileName, "-restart");
+                    }
+                    else
                     {
-                        Process.Start("chmod", $"+x {fileName}");
-
                         var si = new ProcessStartInfo(fileName, "-run")
                         {
                             WorkingDirectory = Path.GetDirectoryName(fileName),
@@ -201,22 +204,6 @@ namespace Stardust
                         };
                         Process.Start(si);
                     }
-                    else
-                    {
-                        if (info?.Arguments == "-s")
-                        {
-                            Process.Start(fileName, "-start");
-                        }
-                        else
-                        {
-                            var si = new ProcessStartInfo(fileName, "-run")
-                            {
-                                WorkingDirectory = Path.GetDirectoryName(fileName),
-                                UseShellExecute = true
-                            };
-                            Process.Start(si);
-                        }
-                    }
                 }
             }
 
Modified +6 -14
diff --git a/Test/Program.cs b/Test/Program.cs
index b2ab300..809bba2 100644
--- a/Test/Program.cs
+++ b/Test/Program.cs
@@ -1,11 +1,7 @@
 using System;
-using System.Collections.Generic;
-using System.Threading;
 using NewLife.Log;
-using NewLife.Net;
 using NewLife.Remoting;
 using Stardust;
-using XCode.DataAccessLayer;
 
 namespace Test
 {
@@ -46,18 +42,14 @@ namespace Test
             _Server = sc;
         }
 
-        static void Test2()
-        {
-            //DAL.AddConnStr("node", "Data Source=..\\Data\\Node.db", null, "sqlite");
-            DAL.AddConnStr("mysql", "Server=.;Port=3306;Database=Node;Uid=root;Pwd=root;", null, "mysql");
-
-            var dal = DAL.Create("mysql");
-            var rs = dal.RestoreAll("../data/Node_20200903215342.zip", null);
-            //Assert.NotNull(rs);
-        }
-
         static void Test3()
         {
+            //foreach (Environment.SpecialFolder item in Enum.GetValues(typeof(Environment.SpecialFolder)))
+            //{
+            //    var v = Environment.GetFolderPath(item);
+            //    Console.WriteLine("{0}:\t{1}", item, v);
+            //}
+
             var client = new LocalStarClient();
             client.ProbeAndInstall(null, "1.1");
         }
Added +16 -0
diff --git a/Test/Properties/PublishProfiles/FolderProfile.pubxml b/Test/Properties/PublishProfiles/FolderProfile.pubxml
new file mode 100644
index 0000000..21feffa
--- /dev/null
+++ b/Test/Properties/PublishProfiles/FolderProfile.pubxml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+https://go.microsoft.com/fwlink/?LinkID=208121. 
+-->
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration>Release</Configuration>
+    <Platform>Any CPU</Platform>
+    <PublishDir>..\Bin\Test\publish\</PublishDir>
+    <PublishProtocol>FileSystem</PublishProtocol>
+    <TargetFramework>net5.0</TargetFramework>
+    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
+    <SelfContained>false</SelfContained>
+    <PublishSingleFile>False</PublishSingleFile>
+  </PropertyGroup>
+</Project>
\ No newline at end of file
Modified +1 -2
diff --git a/Test/Test.csproj b/Test/Test.csproj
index 5e14a6f..f335a4d 100644
--- a/Test/Test.csproj
+++ b/Test/Test.csproj
@@ -7,6 +7,7 @@
     <Deterministic>false</Deterministic>
     <OutputPath>..\Bin\Test</OutputPath>
     <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
+    <LangVersion>latest</LangVersion>
   </PropertyGroup>
 
   <ItemGroup>
@@ -14,8 +15,6 @@
   </ItemGroup>
 
   <ItemGroup>
-    <ProjectReference Include="..\Stardust.Data\Stardust.Data.csproj" />
-    <ProjectReference Include="..\Stardust.Server\Stardust.Server.csproj" />
     <ProjectReference Include="..\Stardust\Stardust.csproj" />
   </ItemGroup>