This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
UpTool2/InstallerCLI/Program.cs
2020-08-31 21:14:27 +02:00

85 lines
3.3 KiB
C#

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Security.Cryptography;
using System.Threading;
using UpToolLib;
using UpToolLib.Tool;
namespace Installer
{
public static class Program
{
public static bool Basic;
public static int Main(string[] args)
{
Thread.Sleep(2000);
MutexLock.Lock();
try
{
Basic = args.Length > 0 && args[0].ToLower() == "--basic";
ExternalFunctionalityManager.Init(new UtLibFunctions());
RootCommand rootCommand = new RootCommand();
rootCommand.AddGlobalOption(new Option<bool>("--basic", "Use only basic console functionality. Must be the first parameter in the call"));
Command install = new Command("install", "Install UpTool")
{
new Option<bool>(new[] {"--noPrep", "-p"}, "Doesn't initialize repos. Use with caution!")
};
install.AddAlias("-i");
install.AddAlias("i");
install.Handler = CommandHandler.Create<bool>(Install);
rootCommand.AddCommand(install);
return rootCommand.InvokeAsync(args).Result;
}
catch (Exception e)
{
Console.WriteLine($"FAILED: {e}");
return 1;
}
finally
{
MutexLock.Unlock();
}
}
private static void Install(bool noPrep)
{
WebClient client = new WebClient();
Console.WriteLine("Downloading metadata");
UpdateCheck.Reload("https://gitlab.com/JFronny/UpTool2/-/jobs/artifacts/master/raw/meta.xml?job=uptool");
Console.WriteLine("Downloading binary");
byte[] dl = client.DownloadData(UpdateCheck.App);
Console.WriteLine("Verifying integrity");
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
{
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty)
.ToUpper();
if (pkgHash != UpdateCheck.AppHash)
throw new Exception($@"The hash is not equal to the one stored in the repo:
Package: {pkgHash}
Online: {UpdateCheck.AppHash}");
}
Console.WriteLine("Extracting");
if (Directory.Exists(PathTool.GetRelative("Install")))
{
foreach (string file in Directory.GetFiles(PathTool.GetRelative("Install"))) File.Delete(file);
foreach (string dir in Directory.GetDirectories(PathTool.GetRelative("Install")))
if (Path.GetFileName(dir) != "tmp")
Directory.Delete(dir, true);
}
Directory.CreateDirectory(PathTool.GetRelative("Install"));
using (MemoryStream ms = new MemoryStream(dl))
{
using ZipArchive ar = new ZipArchive(ms);
ar.ExtractToDirectory(PathTool.GetRelative("Install"), true);
}
if (noPrep) return;
Console.WriteLine("Preparing Repos");
XmlTool.FixXml();
RepoManagement.FetchRepos();
}
}
}