1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Prerequisites - Removed RequiresElevation

Prerequisites - Added PowerShell actions
This commit is contained in:
Robert 2021-05-25 14:16:59 +02:00
parent e73acbe192
commit b332614d00
5 changed files with 126 additions and 25 deletions

View File

@ -22,11 +22,6 @@ namespace Artemis.Core
/// </summary>
public abstract string Description { get; }
/// <summary>
/// [NYI] Gets a boolean indicating whether installing or uninstalling this prerequisite requires admin privileges
/// </summary>
public abstract bool RequiresElevation { get; }
/// <summary>
/// Gets a list of actions to execute when <see cref="Install" /> is called
/// </summary>

View File

@ -18,7 +18,7 @@ namespace Artemis.Core
/// <param name="fileName">The target file to execute</param>
/// <param name="arguments">A set of command-line arguments to use when starting the application</param>
/// <param name="waitForExit">A boolean indicating whether the action should wait for the process to exit</param>
/// <param name="elevate">A boolean indicating whether the file should run with administrator privileges (does not require <see cref="PluginPrerequisite.RequiresElevation"/>)</param>
/// <param name="elevate">A boolean indicating whether the file should run with administrator privileges</param>
public ExecuteFileAction(string name, string fileName, string? arguments = null, bool waitForExit = true, bool elevate = false) : base(name)
{
FileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
@ -72,7 +72,7 @@ namespace Artemis.Core
}
}
private static Task<int> RunProcessAsync(string fileName, string? arguments, bool elevate)
internal static Task<int> RunProcessAsync(string fileName, string? arguments, bool elevate)
{
TaskCompletionSource<int> tcs = new();

View File

@ -0,0 +1,70 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Artemis.Core
{
/// <summary>
/// Represents a plugin prerequisite action runs inline powershell
/// </summary>
public class RunInlinePowerShellAction : PluginPrerequisiteAction
{
/// <summary>
/// Creates a new instance of a copy folder action
/// </summary>
/// <param name="name">The name of the action</param>
/// <param name="code">The inline code to run</param>
/// <param name="elevate">A boolean indicating whether the file should run with administrator privileges</param>
public RunInlinePowerShellAction(string name, string code, bool elevate = false) : base(name)
{
Code = code;
Elevate = elevate;
ProgressIndeterminate = true;
}
/// <summary>
/// Gets the inline code to run
/// </summary>
public string Code { get; }
/// <summary>
/// Gets a boolean indicating whether the file should run with administrator privileges
/// </summary>
public bool Elevate { get; }
/// <inheritdoc />
public override async Task Execute(CancellationToken cancellationToken)
{
string file = Path.GetTempFileName().Replace(".tmp", ".ps1");
try
{
string code =
@"try
{
" + Code + @"
Start-Sleep 1
}
catch
{
Write-Error $_.Exception.ToString()
pause
}";
await File.WriteAllTextAsync(file, code, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
Status = "Running PowerShell script and waiting for exit..";
ShowProgressBar = true;
ProgressIndeterminate = true;
int result = await ExecuteFileAction.RunProcessAsync("powershell.exe", $"-File {file}", Elevate);
Status = $"PowerShell exited with code {result}";
}
finally
{
File.Delete(file);
}
}
}
}

View File

@ -0,0 +1,53 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Artemis.Core
{
/// <summary>
/// Represents a plugin prerequisite action that runs a PowerShell script
/// <para>Note: To run an inline script instead, use <see cref="RunInlinePowerShellAction" /></para>
/// </summary>
public class RunPowerShellAction : PluginPrerequisiteAction
{
/// <summary>
/// Creates a new instance of a copy folder action
/// </summary>
/// <param name="name">The name of the action</param>
/// <param name="scriptPath">The full path of the script to run</param>
/// <param name="elevate">A boolean indicating whether the file should run with administrator privileges</param>
public RunPowerShellAction(string name, string scriptPath, bool elevate = false) : base(name)
{
ScriptPath = scriptPath;
Elevate = elevate;
ProgressIndeterminate = true;
}
/// <summary>
/// Gets the inline full path of the script to run
/// </summary>
public string ScriptPath { get; }
/// <summary>
/// Gets a boolean indicating whether the file should run with administrator privileges
/// </summary>
public bool Elevate { get; }
/// <inheritdoc />
public override async Task Execute(CancellationToken cancellationToken)
{
if (!ScriptPath.EndsWith(".ps1"))
throw new ArtemisPluginException($"Script at path {ScriptPath} must have the .ps1 extension or PowerShell will refuse to run it");
if (!File.Exists(ScriptPath))
throw new ArtemisCoreException($"Script not found at path {ScriptPath}");
Status = "Running PowerShell script and waiting for exit..";
ShowProgressBar = true;
ProgressIndeterminate = true;
int result = await ExecuteFileAction.RunProcessAsync("powershell.exe", $"-File {ScriptPath}", Elevate);
Status = $"PowerShell exited with code {result}";
}
}
}

View File

@ -13,18 +13,13 @@ namespace Artemis.UI.Screens.Plugins
public class PluginPrerequisiteViewModel : Conductor<PluginPrerequisiteActionViewModel>.Collection.OneActive
{
private readonly bool _uninstall;
private readonly ICoreService _coreService;
private readonly IDialogService _dialogService;
private bool _installing;
private bool _uninstalling;
private bool _isMet;
public PluginPrerequisiteViewModel(PluginPrerequisite pluginPrerequisite, bool uninstall, ICoreService coreService, IDialogService dialogService)
public PluginPrerequisiteViewModel(PluginPrerequisite pluginPrerequisite, bool uninstall)
{
_uninstall = uninstall;
_coreService = coreService;
_dialogService = dialogService;
PluginPrerequisite = pluginPrerequisite;
}
@ -65,12 +60,6 @@ namespace Artemis.UI.Screens.Plugins
if (Busy)
return;
if (PluginPrerequisite.RequiresElevation && !_coreService.IsElevated)
{
await _dialogService.ShowConfirmDialog("Install plugin prerequisite", "This plugin prerequisite admin rights to install (restart & elevate NYI)");
return;
}
Installing = true;
try
{
@ -88,12 +77,6 @@ namespace Artemis.UI.Screens.Plugins
if (Busy)
return;
if (PluginPrerequisite.RequiresElevation && !_coreService.IsElevated)
{
await _dialogService.ShowConfirmDialog("Install plugin prerequisite", "This plugin prerequisite admin rights to install (restart & elevate NYI)");
return;
}
Uninstalling = true;
try
{