Browse Source

elevator.sh

Krzysztof Krysiński 2 months ago
parent
commit
c592592198

+ 3 - 1
src/PixiEditor.MacOs/MacOsProcessUtility.cs

@@ -7,9 +7,11 @@ internal class MacOsProcessUtility : IProcessUtility
 {
     public Process RunAsAdmin(string path, string args)
     {
+        string assemblyDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) ?? string.Empty;
+        string elevatorScript = Path.Combine(assemblyDirectory, "elevator.sh");
         string script = $"""
 
-                                     do shell script "'{path}' {args}" with administrator privileges
+                                     do shell script "/bin/bash '{elevatorScript}' '{path}' {args}" with administrator privileges
                          """;
         ProcessStartInfo startInfo = new ProcessStartInfo
         {

+ 6 - 0
src/PixiEditor.MacOs/PixiEditor.MacOs.csproj

@@ -10,4 +10,10 @@
       <ProjectReference Include="..\PixiEditor.OperatingSystem\PixiEditor.OperatingSystem.csproj" />
     </ItemGroup>
 
+    <ItemGroup>
+      <None Include="elevator.sh">
+        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+      </None>
+    </ItemGroup>
+
 </Project>

+ 32 - 0
src/PixiEditor.MacOs/elevator.sh

@@ -0,0 +1,32 @@
+#!/bin/bash
+
+# Get the real path to the updater
+EXECUTABLE="$1"
+shift
+
+# Variables for postExecute
+POST_EXECUTE=""
+
+# Rebuild the argument list without --postExecute and its argument
+ARGS=()
+while [[ $# -gt 0 ]]; do
+    case "$1" in
+        --postExecute)
+            shift
+            POST_EXECUTE="$1"
+            shift
+            ;;
+        *)
+            ARGS+=("$1")
+            shift
+            ;;
+    esac
+done
+
+# Run the updater
+"$EXECUTABLE" "${ARGS[@]}"
+
+# Run the postExecute command if set
+if [[ -n "$POST_EXECUTE" ]]; then
+    eval "$POST_EXECUTE"
+fi

+ 31 - 18
src/PixiEditor.UpdateInstaller.Exe/Program.cs

@@ -31,38 +31,48 @@ catch (Exception ex)
 }
 finally
 {
-    if (startAfterUpdate)
+    try
     {
-        log.AppendLine($"{DateTime.Now}: Starting PixiEditor after update.");
-        if (OperatingSystem.IsMacOS())
-        {
-            StartPixiEditorOnMacOS(controller);
-        }
-        else
+        if (startAfterUpdate)
         {
-            string binaryName = OperatingSystem.IsWindows() ? "PixiEditor.exe" : "PixiEditor";
-            string path = Path.Join(controller.UpdateDirectory, binaryName);
-            if (File.Exists(path))
+            log.AppendLine($"{DateTime.Now}: Starting PixiEditor after update.");
+            if (OperatingSystem.IsMacOS())
             {
-                log.AppendLine($"{DateTime.Now}: Starting PixiEditor from {path}");
-                StartPixiEditor(path);
+                // Handled by elevator.sh script, I couldn't get Process.Start to work correctly under osascript environment
+                //StartPixiEditorOnMacOS(controller);
             }
             else
             {
-                binaryName = OperatingSystem.IsWindows() ? "PixiEditor.Desktop.exe" : "PixiEditor.Desktop";
-                path = Path.Join(controller.UpdateDirectory, binaryName);
+                string binaryName = OperatingSystem.IsWindows() ? "PixiEditor.exe" : "PixiEditor";
+                string path = Path.Join(controller.UpdateDirectory, binaryName);
                 if (File.Exists(path))
                 {
+                    log.AppendLine($"{DateTime.Now}: Starting PixiEditor from {path}");
                     StartPixiEditor(path);
                 }
                 else
                 {
-                    log.AppendLine("PixiEditor executable not found.");
+                    binaryName = OperatingSystem.IsWindows() ? "PixiEditor.Desktop.exe" : "PixiEditor.Desktop";
+                    path = Path.Join(controller.UpdateDirectory, binaryName);
+                    if (File.Exists(path))
+                    {
+                        StartPixiEditor(path);
+                    }
+                    else
+                    {
+                        log.AppendLine("PixiEditor executable not found.");
+                    }
                 }
             }
         }
     }
-    
+    catch (Exception ex)
+    {
+        log.AppendLine($"{DateTime.Now}: Error starting PixiEditor: {ex.Message}");
+        string errorLogPath = Path.Combine(logDirectory, "ErrorLog.txt");
+        File.AppendAllText(errorLogPath, $"Error starting PixiEditor: {DateTime.Now}\n{ex.Message}\n{ex.StackTrace}\n-----\n");
+    }
+
     try
     {
         string updateLogPath = Path.Combine(logDirectory, "UpdateLog.txt");
@@ -96,16 +106,18 @@ finally
     }
 }
 
+/*
 void StartPixiEditorOnMacOS(UpdateController controller)
 {
     string pixiEditorExecutablePath = Path.Combine(controller.UpdateDirectory, "PixiEditor.app");
     if (Directory.Exists(pixiEditorExecutablePath))
     {
-        log.AppendLine($"{DateTime.Now}: Starting PixiEditor with open -a PixiEditor");
+        log.AppendLine($"{DateTime.Now}: Starting PixiEditor with open {pixiEditorExecutablePath}");
         Process.Start(new ProcessStartInfo
         {
             FileName = "open",
-            Arguments = $"-a PixiEditor",
+            Arguments = $"\"{pixiEditorExecutablePath}\"",
+            UseShellExecute = true
         });
     }
     else
@@ -113,3 +125,4 @@ void StartPixiEditorOnMacOS(UpdateController controller)
         log.AppendLine($"{DateTime.Now}: PixiEditor.app not found at {pixiEditorExecutablePath}");
     }
 }
+*/

+ 2 - 0
src/PixiEditor/ViewModels/SubViewModels/UpdateViewModel.cs

@@ -397,6 +397,8 @@ internal class UpdateViewModel : SubViewModel<ViewModelMain>
             }
             else
             {
+                // couldn't get Process.Start to work correctly with osascript environment
+                args = IOperatingSystem.Current.IsMacOs ? args + " --postExecute 'open -a PixiEditor'" : args;
                 var proc = IOperatingSystem.Current.ProcessUtility.RunAsAdmin(updateExeFile, args);
                 if (IOperatingSystem.Current.IsMacOs)
                 {