Self-update using installer

This commit is contained in:
CreepyCrafter24 2020-04-08 18:03:50 +02:00
parent 29e35dbec0
commit 2ca1bd5408
4 changed files with 83 additions and 8 deletions

5
CLI.md
View File

@ -21,4 +21,7 @@ Search for a package: uptool search \<text>
Show package info: uptool show \<package>
Start an app: uptool start \<package>
Start an app: uptool start \<package>
Upgrading UpToolCLI: uptool upgrade-self

View File

@ -47,8 +47,15 @@ namespace Installer
$"The hash is not equal to the one stored in the repo:\r\nPackage: {pkgHash}\r\nOnline: {meta.Element("Hash").Value.ToUpper()}");
}
Console.WriteLine("Extracting");
//if (Directory.Exists(PathTool.GetRelative("Install")))
// Directory.Delete(PathTool.GetRelative("Install"), true);
if (Directory.Exists(PathTool.GetRelative("Install")))
Directory.Delete(PathTool.GetRelative("Install"), true);
{
foreach (string file in Directory.GetFiles(PathTool.GetRelative("Install"))) File.Delete(file);
foreach (string dir in Directory.GetDirectories(PathTool.GetRelative("Install")))
if (System.IO.Path.GetFileName(dir) != "tmp")
Directory.Delete(dir, true);
}
Directory.CreateDirectory(PathTool.GetRelative("Install"));
using (MemoryStream ms = new MemoryStream(dl))
{

View File

@ -192,7 +192,7 @@ namespace UpTool2
return true;
byte[] dl;
SetSplash(4, "Downloading latest");
using (DownloadDialog dlg = new DownloadDialog(meta.Element("File").Value))
using (DownloadDialog dlg = new DownloadDialog(meta.Element("Installer").Value))
{
if (dlg.ShowDialog() != DialogResult.OK)
throw new Exception("Failed to update");
@ -202,9 +202,9 @@ namespace UpTool2
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
{
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper();
if (pkgHash != meta.Element("Hash").Value.ToUpper())
if (pkgHash != meta.Element("InstallerHash").Value.ToUpper())
throw new Exception("The hash is not equal to the one stored in the repo:\r\nPackage: " + pkgHash +
"\r\nOnline: " + meta.Element("Hash").Value.ToUpper());
"\r\nOnline: " + meta.Element("InstallerHash").Value.ToUpper());
}
SetSplash(9, "Installing");
if (Directory.Exists(PathTool.GetRelative("Install", "tmp")))
@ -218,8 +218,8 @@ namespace UpTool2
Splash.Hide();
Process.Start(new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = @"/C timeout /t 2 & xcopy /s /e /y tmp\* .",
FileName = PathTool.GetRelative("Install", "tmp", "Installer.exe"),
Arguments = "-i",
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = PathTool.GetRelative("Install")

View File

@ -2,24 +2,31 @@
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Xml.Linq;
using UpToolLib;
using UpToolLib.DataStructures;
using UpToolLib.Tool;
using Process = System.CommandLine.Invocation.Process;
namespace UpToolCLI
{
public static class Program
{
private static readonly UtLibFunctions Functions = new UtLibFunctions();
public static int Main(string[] args)
{
MutexLock.Lock();
try
{
XmlTool.FixXml();
ExternalFunctionalityManager.Init(new UtLibFunctions());
ExternalFunctionalityManager.Init(Functions);
RootCommand rootCommand = new RootCommand();
rootCommand.AddCommand(new Command("update", "Updates the cache")
{
@ -42,6 +49,11 @@ namespace UpToolCLI
upgrade.Handler = CommandHandler.Create<string, bool>(Upgrade);
rootCommand.AddCommand(upgrade);
rootCommand.AddCommand(new Command("upgrade-self", "Upgrades UpToolCLI")
{
Handler = CommandHandler.Create(UpgradeSelf)
});
Command reinstall = new Command("reinstall", "Reinstall a package")
{
new Option<string>(new[] {"--identifier", "-i"}, "Something to identify the app"),
@ -103,6 +115,45 @@ namespace UpToolCLI
}
}
private static void UpgradeSelf()
{
#if DEBUG
Console.WriteLine("Not enabled in debug builds");
#else
XElement meta = XDocument.Load(XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value).Element("meta");
Console.WriteLine("Downloading latest");
(bool success, byte[] dl) = Functions.Download(new Uri(meta.Element("Installer").Value));
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 != meta.Element("InstallerHash").Value.ToUpper())
throw new Exception("The hash is not equal to the one stored in the repo:\r\nPackage: " + pkgHash +
"\r\nOnline: " + meta.Element("InstallerHash").Value.ToUpper());
}
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}");
System.Diagnostics.Process.Start(new ProcessStartInfo
{
FileName = file,
Arguments = "-i",
WorkingDirectory = PathTool.GetRelative("Install"),
UseShellExecute = false
});
#endif
}
private static void Update()
{
Console.WriteLine("Fetching Repos...");
@ -117,6 +168,13 @@ namespace UpToolCLI
? "All up-to-date"
: $@"Found {updatableCount} Updates:
{string.Join(Environment.NewLine, apps.Select(s => $"- {s.Name} ({s.Version})"))}");
#if !DEBUG
XElement meta = XDocument.Load(XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value).Element("meta");
Version vLocal = Assembly.GetExecutingAssembly().GetName().Version;
Version vOnline = Version.Parse(meta.Element("Version").Value);
if (vLocal < vOnline)
Console.WriteLine($"uptool is outdated ({vLocal} vs {vOnline}), update using \"uptool upgrade-self\"");
#endif
}
private static void List()
@ -143,6 +201,13 @@ namespace UpToolCLI
Console.WriteLine($"Updating {app.Value.Name}");
AppExtras.Update(app.Value, false);
}
#if !DEBUG
if (Assembly.GetExecutingAssembly().GetName().Version < Version.Parse(XDocument.Load(XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value).Element("meta").Element("Version").Value))
{
Console.WriteLine("Updating self");
UpgradeSelf();
}
#endif
Console.WriteLine("Done!");
}