2020-05-16 17:59:44 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.CommandLine;
|
|
|
|
|
using System.CommandLine.Invocation;
|
2020-05-16 18:19:43 +02:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
2020-05-16 17:59:44 +02:00
|
|
|
|
using System.Linq;
|
2020-05-16 18:19:43 +02:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using UpToolLib;
|
2020-05-16 17:59:44 +02:00
|
|
|
|
using UpToolLib.DataStructures;
|
|
|
|
|
using UpToolLib.Tool;
|
|
|
|
|
using Process = System.Diagnostics.Process;
|
|
|
|
|
|
|
|
|
|
namespace UpToolCLI
|
|
|
|
|
{
|
|
|
|
|
public class Other
|
|
|
|
|
{
|
|
|
|
|
public static void RegisterCommands(RootCommand rootCommand)
|
|
|
|
|
{
|
|
|
|
|
Command command = new Command("upgrade-self", "Upgrades UpToolCLI")
|
|
|
|
|
{
|
|
|
|
|
new Option<bool>(new[] {"--force", "-f"}, "Overwrites older files")
|
|
|
|
|
};
|
|
|
|
|
command.Handler = CommandHandler.Create<bool>(UpgradeSelf);
|
|
|
|
|
rootCommand.AddCommand(command);
|
|
|
|
|
|
|
|
|
|
Command start = new Command("start", "Starts an app")
|
|
|
|
|
{
|
2020-08-31 18:58:15 +02:00
|
|
|
|
new Argument<string>("identifier", "Something to identify the app"),
|
2020-08-31 22:11:40 +02:00
|
|
|
|
new Option<string>(new[] {"--arguments", "--args", "-a"}, () => "", "The arguments to run the file with"),
|
|
|
|
|
new Option<string>(new[] {"--file", "-f"}, () => null, "The file to run instead of the MainFile"),
|
2020-08-31 21:12:00 +02:00
|
|
|
|
new Option<bool>(new[] {"--waitForExit", "-wait"}, "Waits until the program quits")
|
2020-05-16 17:59:44 +02:00
|
|
|
|
};
|
2020-08-31 22:11:40 +02:00
|
|
|
|
start.Handler = CommandHandler.Create<string, string, string, bool>(Start);
|
2020-05-16 17:59:44 +02:00
|
|
|
|
rootCommand.AddCommand(start);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-16 18:19:43 +02:00
|
|
|
|
public static void UpgradeSelf(bool force)
|
2020-05-16 17:59:44 +02:00
|
|
|
|
{
|
|
|
|
|
if (!force && Assembly.GetExecutingAssembly().GetName().Version >= UpdateCheck.OnlineVersion)
|
|
|
|
|
Console.WriteLine("Already up-to-date");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Downloading latest");
|
2020-05-16 18:19:43 +02:00
|
|
|
|
(bool success, byte[] dl) = Program.Functions.Download(UpdateCheck.Installer);
|
2020-05-16 17:59:44 +02:00
|
|
|
|
if (!success)
|
|
|
|
|
throw new Exception("Failed to update");
|
|
|
|
|
Console.WriteLine("Verifying");
|
|
|
|
|
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
|
|
|
|
|
{
|
|
|
|
|
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper();
|
|
|
|
|
if (pkgHash != UpdateCheck.InstallerHash)
|
|
|
|
|
throw new Exception($@"The hash is not equal to the one stored in the repo:
|
|
|
|
|
Package: {pkgHash}
|
|
|
|
|
Online: {UpdateCheck.InstallerHash}");
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("Installing");
|
|
|
|
|
if (Directory.Exists(PathTool.GetRelative("Install", "tmp")))
|
|
|
|
|
Directory.Delete(PathTool.GetRelative("Install", "tmp"), true);
|
|
|
|
|
Directory.CreateDirectory(PathTool.GetRelative("Install", "tmp"));
|
|
|
|
|
using (MemoryStream ms = new MemoryStream(dl))
|
|
|
|
|
{
|
|
|
|
|
using ZipArchive ar = new ZipArchive(ms);
|
|
|
|
|
ar.ExtractToDirectory(PathTool.GetRelative("Install", "tmp"), true);
|
|
|
|
|
}
|
|
|
|
|
string file = PathTool.GetRelative("Install", "tmp", "Installer.exe");
|
|
|
|
|
Console.WriteLine($"Starting {file}");
|
|
|
|
|
Process.Start(new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = file,
|
|
|
|
|
Arguments = "i",
|
|
|
|
|
WorkingDirectory = PathTool.GetRelative("Install"),
|
|
|
|
|
UseShellExecute = false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 22:11:40 +02:00
|
|
|
|
private static void Start(string identifier, string arguments, string file, bool waitForExit)
|
2020-05-16 17:59:44 +02:00
|
|
|
|
{
|
|
|
|
|
RepoManagement.GetReposFromDisk();
|
|
|
|
|
App[] apps = AppExtras.FindApps(identifier);
|
|
|
|
|
if (apps.Length == 0)
|
|
|
|
|
Console.WriteLine("Package not found.");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
App tmp = apps.First();
|
|
|
|
|
if (tmp.Runnable)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Starting {tmp.Name}");
|
2020-08-31 22:11:40 +02:00
|
|
|
|
Process tmp1 = AppExtras.RunApp(tmp, file ?? tmp.MainFile, arguments);
|
2020-05-16 17:59:44 +02:00
|
|
|
|
if (waitForExit)
|
|
|
|
|
tmp1.WaitForExit();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Console.WriteLine($"{tmp.Name} is not runnable");
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("Done!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|