Fix some stuff

This commit is contained in:
CreepyCrafter24 2020-03-24 21:27:41 +01:00
parent d7306150e8
commit 101ea5843a
5 changed files with 79 additions and 38 deletions

View File

@ -51,7 +51,7 @@ namespace UpTool2
try try
{ {
#endif #endif
AppInstall.Install((App) action_install.Tag); AppInstall.Install((App) action_install.Tag, true);
ReloadElements(); ReloadElements();
trying = false; trying = false;
#if !DEBUG #if !DEBUG
@ -91,7 +91,7 @@ namespace UpTool2
id = Guid.NewGuid(); id = Guid.NewGuid();
App appI = new App(AppNameDialog.Show(), "Locally installed package, removal only", App appI = new App(AppNameDialog.Show(), "Locally installed package, removal only",
GlobalVariables.minimumVer, "", true, "", id, Color.Red, Resources.C_64.ToBitmap(), false, ""); GlobalVariables.minimumVer, "", true, "", id, Color.Red, Resources.C_64.ToBitmap(), false, "");
AppInstall.InstallZip(searchPackageDialog.FileName, appI); AppInstall.InstallZip(searchPackageDialog.FileName, appI, true);
#if !DEBUG #if !DEBUG
} }
catch (Exception e1) catch (Exception e1)
@ -173,7 +173,7 @@ namespace UpTool2
{ {
try try
{ {
AppExtras.Update((App) action_install.Tag); AppExtras.Update((App) action_install.Tag, false);
} }
catch (Exception e1) catch (Exception e1)
{ {

View File

@ -22,23 +22,26 @@ namespace UpToolCLI
Command install = new Command("install", "Install a package") Command install = new Command("install", "Install a package")
{ {
Handler = CommandHandler.Create<string>(Install) Handler = CommandHandler.Create<string, bool>(Install)
}; };
install.AddOption(new Option<string>(new[] { "--identifier", "-i" }, "Something to identify the app")); install.AddOption(new Option<string>(new[] { "--identifier", "-i" }, "Something to identify the app"));
install.AddOption(new Option<bool>(new[] { "--force", "-f" }, "Overwrites older files"));
rootCommand.AddCommand(install); rootCommand.AddCommand(install);
Command upgrade = new Command("upgrade", "Upgrade a package") Command upgrade = new Command("upgrade", "Upgrade a package")
{ {
Handler = CommandHandler.Create<string>(Upgrade) Handler = CommandHandler.Create<string, bool>(Upgrade)
}; };
upgrade.AddOption(new Option<string>(new[] { "--identifier", "-i" }, "Something to identify the app")); upgrade.AddOption(new Option<string>(new[] { "--identifier", "-i" }, "Something to identify the app"));
upgrade.AddOption(new Option<bool>(new[] { "--force", "-f" }, "Overwrites older files"));
rootCommand.AddCommand(upgrade); rootCommand.AddCommand(upgrade);
Command reinstall = new Command("reinstall", "Reinstall a package") Command reinstall = new Command("reinstall", "Reinstall a package")
{ {
Handler = CommandHandler.Create<string>(Reinstall) Handler = CommandHandler.Create<string, bool>(Reinstall)
}; };
reinstall.AddOption(new Option<string>(new[] { "--identifier", "-i" }, "Something to identify the app")); reinstall.AddOption(new Option<string>(new[] { "--identifier", "-i" }, "Something to identify the app"));
reinstall.AddOption(new Option<bool>(new[] { "--force", "-f" }, "Overwrites older files"));
rootCommand.AddCommand(reinstall); rootCommand.AddCommand(reinstall);
Command remove = new Command("remove", "Remove a package") Command remove = new Command("remove", "Remove a package")
@ -109,8 +112,9 @@ namespace UpToolCLI
foreach (KeyValuePair<Guid, App> app in GlobalVariables.Apps.Where(s => (s.Value.status & Status.Updatable) == Status.Updatable)) foreach (KeyValuePair<Guid, App> app in GlobalVariables.Apps.Where(s => (s.Value.status & Status.Updatable) == Status.Updatable))
{ {
Console.WriteLine($"Updating {app.Value.Name}"); Console.WriteLine($"Updating {app.Value.Name}");
AppExtras.Update(app.Value); AppExtras.Update(app.Value, false);
} }
Console.WriteLine("Done!");
} }
private static void Show(string identifier) private static void Show(string identifier)
@ -132,7 +136,7 @@ namespace UpToolCLI
Console.WriteLine($"{apps[i].Name} ({apps[i].Id}"); Console.WriteLine($"{apps[i].Name} ({apps[i].Id}");
} }
private static void Upgrade(string identifier) private static void Upgrade(string identifier, bool force)
{ {
RepoManagement.GetReposFromDisk(); RepoManagement.GetReposFromDisk();
App[] apps = AppExtras.FindApps(identifier); App[] apps = AppExtras.FindApps(identifier);
@ -144,14 +148,15 @@ namespace UpToolCLI
if ((tmp.status & Status.Updatable) == Status.Updatable) if ((tmp.status & Status.Updatable) == Status.Updatable)
{ {
Console.WriteLine($"Upgrading {tmp.Name}"); Console.WriteLine($"Upgrading {tmp.Name}");
AppExtras.Update(tmp); AppExtras.Update(tmp, force);
} }
else else
Console.WriteLine("Package is up-to-date"); Console.WriteLine("Package is up-to-date");
} }
Console.WriteLine("Done!");
} }
private static void Reinstall(string identifier) private static void Reinstall(string identifier, bool force)
{ {
RepoManagement.GetReposFromDisk(); RepoManagement.GetReposFromDisk();
App[] apps = AppExtras.FindApps(identifier); App[] apps = AppExtras.FindApps(identifier);
@ -161,8 +166,9 @@ namespace UpToolCLI
{ {
App tmp = apps.First(); App tmp = apps.First();
Console.WriteLine($"Reinstalling {tmp.Name}"); Console.WriteLine($"Reinstalling {tmp.Name}");
AppExtras.Update(tmp); AppExtras.Update(tmp, force);
} }
Console.WriteLine("Done!");
} }
private static void Remove(string identifier) private static void Remove(string identifier)
@ -182,6 +188,7 @@ namespace UpToolCLI
else else
Console.WriteLine("Package is not installed"); Console.WriteLine("Package is not installed");
} }
Console.WriteLine("Done!");
} }
private static void Purge(string identifier) private static void Purge(string identifier)
@ -201,9 +208,10 @@ namespace UpToolCLI
else else
Console.WriteLine("Package is not installed"); Console.WriteLine("Package is not installed");
} }
Console.WriteLine("Done!");
} }
private static void Install(string identifier) private static void Install(string identifier, bool force)
{ {
RepoManagement.GetReposFromDisk(); RepoManagement.GetReposFromDisk();
App[] apps = AppExtras.FindApps(identifier); App[] apps = AppExtras.FindApps(identifier);
@ -217,9 +225,10 @@ namespace UpToolCLI
else else
{ {
Console.WriteLine($"Installing {tmp.Name}"); Console.WriteLine($"Installing {tmp.Name}");
AppInstall.Install(tmp); AppInstall.Install(tmp, true);
} }
} }
Console.WriteLine("Done!");
} }
} }
} }

View File

@ -5,6 +5,10 @@
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>uptool</AssemblyName> <AssemblyName>uptool</AssemblyName>
</PropertyGroup> </PropertyGroup>
<PropertyGroup>
<ApplicationIcon>..\UpTool2.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" /> <PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" />

View File

@ -18,10 +18,10 @@ namespace UpToolLib.Tool
WorkingDirectory = app.dataPath WorkingDirectory = app.dataPath
}); });
public static void Update(App app) public static void Update(App app, bool overwrite)
{ {
Remove(app, false); Remove(app, overwrite);
AppInstall.Install(app); AppInstall.Install(app, overwrite);
} }
public static void Remove(App app, bool deleteAll) public static void Remove(App app, bool deleteAll)
@ -30,22 +30,25 @@ namespace UpToolLib.Tool
if (Directory.Exists(tmp)) if (Directory.Exists(tmp))
Directory.Delete(tmp, true); Directory.Delete(tmp, true);
Directory.CreateDirectory(tmp); Directory.CreateDirectory(tmp);
ZipFile.ExtractToDirectory(Path.Combine(app.appPath, "package.zip"), tmp); if (File.Exists(Path.Combine(app.appPath, "package.zip")))
Process.Start(new ProcessStartInfo
{ {
FileName = "cmd.exe", ZipFile.ExtractToDirectory(Path.Combine(app.appPath, "package.zip"), tmp);
Arguments = $"/C \"{Path.Combine(tmp, "Remove.bat")}\"", Process.Start(new ProcessStartInfo
WorkingDirectory = Path.Combine(app.appPath, "app"), {
CreateNoWindow = true, FileName = "cmd.exe",
WindowStyle = ProcessWindowStyle.Hidden Arguments = $"/C \"{Path.Combine(tmp, "Remove.bat")}\"",
}).WaitForExit(); WorkingDirectory = Path.Combine(app.appPath, "app"),
if (!deleteAll) CheckDirecory(Path.Combine(tmp, "Data"), app.dataPath); CreateNoWindow = true,
Directory.Delete(tmp, true); WindowStyle = ProcessWindowStyle.Hidden
}).WaitForExit();
if (!deleteAll) CheckDirecory(Path.Combine(tmp, "Data"), app.dataPath);
Directory.Delete(tmp, true);
}
if (File.Exists(app.infoPath)) if (File.Exists(app.infoPath))
File.Delete(app.infoPath); File.Delete(app.infoPath);
if (File.Exists(Path.Combine(app.appPath, "package.zip"))) if (File.Exists(Path.Combine(app.appPath, "package.zip")))
File.Delete(Path.Combine(app.appPath, "package.zip")); File.Delete(Path.Combine(app.appPath, "package.zip"));
if (deleteAll || Directory.GetFiles(app.dataPath).Length + Directory.GetDirectories(app.dataPath).Length == 0) if (deleteAll || (Directory.Exists(app.dataPath) && Directory.GetFiles(app.dataPath).Length + Directory.GetDirectories(app.dataPath).Length == 0))
Directory.Delete(app.appPath, true); Directory.Delete(app.appPath, true);
} }

View File

@ -14,8 +14,8 @@ namespace UpToolLib.Tool
/// Install an application /// Install an application
/// </summary> /// </summary>
/// <param name="appI">The app to install</param> /// <param name="appI">The app to install</param>
/// <param name="download">A method to download files. Input: app file, Outputs: whether the download was successful and the data</param> /// <param name="force">Set to true to overwrite all old data</param>
public static void Install(App appI) public static void Install(App appI, bool force)
{ {
string app = ""; string app = "";
string tmp = ""; string tmp = "";
@ -28,9 +28,17 @@ namespace UpToolLib.Tool
if (Directory.Exists(tmp)) if (Directory.Exists(tmp))
Directory.Delete(tmp, true); Directory.Delete(tmp, true);
Directory.CreateDirectory(tmp); Directory.CreateDirectory(tmp);
if (Directory.Exists(app)) if (force)
Directory.Delete(app, true); {
Directory.CreateDirectory(app); if (Directory.Exists(app))
Directory.Delete(app, true);
Directory.CreateDirectory(app);
}
else
{
if (!Directory.Exists(app))
Directory.CreateDirectory(app);
}
(bool dlSuccess, byte[] dlData) = ExternalFunctionalityManager.instance.Download(new Uri(appI.File)); (bool dlSuccess, byte[] dlData) = ExternalFunctionalityManager.instance.Download(new Uri(appI.File));
if (!dlSuccess) if (!dlSuccess)
throw new Exception("Download failed"); throw new Exception("Download failed");
@ -44,7 +52,7 @@ Package: {pkgHash}
Online: {appI.Hash.ToUpper()}"); Online: {appI.Hash.ToUpper()}");
} }
File.WriteAllBytes(Path.Combine(app, "package.zip"), dlData); File.WriteAllBytes(Path.Combine(app, "package.zip"), dlData);
CompleteInstall(appI); CompleteInstall(appI, force);
#if !DEBUG #if !DEBUG
} }
catch catch
@ -63,7 +71,7 @@ Online: {appI.Hash.ToUpper()}");
#endif #endif
} }
public static void InstallZip(string zipPath, App meta) public static void InstallZip(string zipPath, App meta, bool force)
{ {
string app = ""; string app = "";
string tmp = ""; string tmp = "";
@ -76,7 +84,7 @@ Online: {appI.Hash.ToUpper()}");
Directory.Delete(tmp, true); Directory.Delete(tmp, true);
Directory.CreateDirectory(tmp); Directory.CreateDirectory(tmp);
File.Copy(zipPath, Path.Combine(app, "package.zip")); File.Copy(zipPath, Path.Combine(app, "package.zip"));
CompleteInstall(meta); CompleteInstall(meta, force);
} }
catch catch
{ {
@ -94,13 +102,19 @@ Online: {appI.Hash.ToUpper()}");
//Use //Use
//PowerShell -Command "Add-Type -AssemblyName PresentationFramework;[System.Windows.MessageBox]::Show('Hello World')" //PowerShell -Command "Add-Type -AssemblyName PresentationFramework;[System.Windows.MessageBox]::Show('Hello World')"
//for message boxes //for message boxes
private static void CompleteInstall(App app) => CompleteInstall(app.appPath, app.Name, app.Description, app.Version, app.MainFile); private static void CompleteInstall(App app, bool force) => CompleteInstall(app.appPath, app.Name, app.Description, app.Version, app.MainFile, force);
private static void CompleteInstall(string appPath, string name, string description, Version version, string mainFile) private static void CompleteInstall(string appPath, string name, string description, Version version, string mainFile, bool force)
{ {
string tmp = PathTool.tempPath; string tmp = PathTool.tempPath;
ZipFile.ExtractToDirectory(Path.Combine(appPath, "package.zip"), tmp); ZipFile.ExtractToDirectory(Path.Combine(appPath, "package.zip"), tmp);
Directory.Move(Path.Combine(tmp, "Data"), Path.Combine(appPath, "app")); if (force)
Directory.Move(Path.Combine(tmp, "Data"), Path.Combine(appPath, "app"));
else
{
CopyAll(Path.Combine(tmp, "Data"), Path.Combine(appPath, "app", "Data"));
Directory.Delete(Path.Combine(tmp, "Data"), true);
}
XElement el = new XElement("app", new XElement("Name", name), new XElement("Description", description), XElement el = new XElement("app", new XElement("Name", name), new XElement("Description", description),
new XElement("Version", version)); new XElement("Version", version));
if (mainFile != null) if (mainFile != null)
@ -115,5 +129,16 @@ Online: {appI.Hash.ToUpper()}");
WindowStyle = ProcessWindowStyle.Hidden WindowStyle = ProcessWindowStyle.Hidden
}).WaitForExit(); }).WaitForExit();
} }
private static void CopyAll(string source, string target)
{
if (string.Equals(Path.GetFullPath(source), Path.GetFullPath(target), StringComparison.CurrentCultureIgnoreCase))
return;
if (!Directory.Exists(target))
Directory.CreateDirectory(target);
foreach (string file in Directory.GetFiles(source)) File.Copy(file, Path.Combine(target, Path.GetFileName(file)), true);
foreach (string dir in Directory.GetDirectories(source))
CopyAll(dir, Path.Combine(target, Path.GetFileName(dir)));
}
} }
} }