2020-03-08 09:05:19 +01:00
|
|
|
|
using System;
|
2020-03-08 12:51:37 +01:00
|
|
|
|
using System.CommandLine;
|
|
|
|
|
using System.CommandLine.Invocation;
|
2020-03-08 13:10:51 +01:00
|
|
|
|
using System.Diagnostics;
|
2020-03-13 20:16:33 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using CC_Functions.Misc;
|
2020-03-08 09:05:19 +01:00
|
|
|
|
|
|
|
|
|
namespace UpTool_build_tool
|
|
|
|
|
{
|
|
|
|
|
internal static class Program
|
|
|
|
|
{
|
2020-03-08 12:51:37 +01:00
|
|
|
|
public static int Main(string[] args)
|
2020-03-08 09:05:19 +01:00
|
|
|
|
{
|
2020-03-08 12:51:37 +01:00
|
|
|
|
RootCommand rootCommand = new RootCommand();
|
|
|
|
|
Command build = new Command("build", "Builds a generic package with or without shortcuts from a directory");
|
|
|
|
|
build.AddOption(new Option<string>("--binDir", "Directory to package"));
|
|
|
|
|
build.AddOption(new Option<string>("--mainBin", "The applications main binary"));
|
|
|
|
|
build.AddOption(new Option<string>("--packageFile", "Directory to package"));
|
2020-03-13 20:16:33 +01:00
|
|
|
|
build.AddOption(new Option<bool>("--noShortcuts",
|
|
|
|
|
"When this is enabled the scripts will not generate a start-menu item"));
|
2020-03-08 12:51:37 +01:00
|
|
|
|
build.AddOption(new Option<bool>("--noLogo", "Disables the logo"));
|
2020-03-13 20:16:33 +01:00
|
|
|
|
build.Handler = CommandHandler.Create<string, string, string, bool, bool>(Build);
|
2020-03-08 12:51:37 +01:00
|
|
|
|
rootCommand.AddCommand(build);
|
|
|
|
|
return rootCommand.InvokeAsync(args).Result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 20:16:33 +01:00
|
|
|
|
private static void Build(string binDir, string mainBin, string packageFile, bool noLogo, bool noShortcuts)
|
2020-03-08 12:51:37 +01:00
|
|
|
|
{
|
2020-03-08 13:10:51 +01:00
|
|
|
|
Stopwatch watch = Stopwatch.StartNew();
|
2020-03-08 12:51:37 +01:00
|
|
|
|
if (!noLogo)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("-------------------------------");
|
|
|
|
|
Console.WriteLine("| UpTool2 package build tools |");
|
|
|
|
|
Console.WriteLine("-------------------------------");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine($"Using version {Assembly.GetExecutingAssembly().GetName().Version}");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("Parsing arguments...");
|
|
|
|
|
packageFile ??= Path.Combine(binDir, "package.zip");
|
|
|
|
|
if (File.Exists(packageFile))
|
2020-03-13 20:16:33 +01:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Removing previous package...");
|
2020-03-08 12:51:37 +01:00
|
|
|
|
File.Delete(packageFile);
|
2020-03-13 20:16:33 +01:00
|
|
|
|
}
|
2020-03-08 12:51:37 +01:00
|
|
|
|
Console.WriteLine("Copying binary dir...");
|
2020-03-13 20:16:33 +01:00
|
|
|
|
using ZipArchive archive = ZipFile.Open(packageFile, ZipArchiveMode.Create);
|
2020-03-08 12:51:37 +01:00
|
|
|
|
{
|
2020-03-13 20:16:33 +01:00
|
|
|
|
archive.AddDirectory(binDir, "Data", new[] {".xml", ".pdb"}, new[] {packageFile});
|
|
|
|
|
Console.WriteLine("Creating batch scripts...");
|
|
|
|
|
string installBat = "@echo off\r\necho INSTALL";
|
|
|
|
|
string removeBat = "@echo off\r\necho REMOVE";
|
|
|
|
|
if (string.IsNullOrWhiteSpace(mainBin))
|
2020-03-11 20:13:42 +01:00
|
|
|
|
{
|
2020-03-13 20:16:33 +01:00
|
|
|
|
string[] tmp = Directory.GetFiles(binDir, "*.exe");
|
|
|
|
|
if (tmp.Length > 0)
|
|
|
|
|
mainBin = Directory.GetFiles(binDir, "*.exe")[0];
|
|
|
|
|
if (tmp.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
"Detected multiple EXEs. This is not recommended as all processes running in the app folder will need to be terminated for uninstall to succeed");
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
"Please consider removing unnecessary EXEs or notify me that anyone is actually using this.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-16 13:42:37 +01:00
|
|
|
|
string programName = Path.GetFileNameWithoutExtension(mainBin);
|
2020-03-13 20:16:33 +01:00
|
|
|
|
if (!noShortcuts)
|
|
|
|
|
{
|
|
|
|
|
installBat += "\r\n";
|
|
|
|
|
installBat +=
|
2020-03-16 20:16:39 +01:00
|
|
|
|
$@"powershell ""$s=(New-Object -COM WScript.Shell).CreateShortcut('%appdata%\Microsoft\Windows\Start Menu\Programs\{programName}.lnk');$s.TargetPath='%cd%\{mainBin}';$s.Save()""";
|
2020-03-13 20:16:33 +01:00
|
|
|
|
removeBat += "\r\n";
|
2020-03-16 13:42:37 +01:00
|
|
|
|
removeBat += $@"del ""%appdata%\Microsoft\Windows\Start Menu\Programs\{programName}.lnk""";
|
2020-03-13 20:16:33 +01:00
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(mainBin))
|
|
|
|
|
{
|
|
|
|
|
removeBat += "\r\n";
|
2020-03-16 20:13:41 +01:00
|
|
|
|
removeBat += $@"taskkill /f /im ""{Path.GetFileName(mainBin)}""";
|
2020-03-13 20:16:33 +01:00
|
|
|
|
}
|
|
|
|
|
installBat += "\r\ntimeout /t 1";
|
|
|
|
|
removeBat += "\r\ntimeout /t 1";
|
|
|
|
|
using (Stream s = archive.CreateEntry("Install.bat").Open())
|
|
|
|
|
{
|
|
|
|
|
using StreamWriter writer = new StreamWriter(s);
|
|
|
|
|
writer.Write(installBat);
|
|
|
|
|
}
|
|
|
|
|
using (Stream s = archive.CreateEntry("Remove.bat").Open())
|
|
|
|
|
{
|
|
|
|
|
using StreamWriter writer = new StreamWriter(s);
|
|
|
|
|
writer.Write(removeBat);
|
2020-03-11 20:13:42 +01:00
|
|
|
|
}
|
2020-03-08 12:51:37 +01:00
|
|
|
|
}
|
2020-03-08 13:10:51 +01:00
|
|
|
|
watch.Stop();
|
|
|
|
|
Console.WriteLine($"Completed package creation in {watch.Elapsed}");
|
|
|
|
|
Console.WriteLine($"Output file: {Path.GetFullPath(packageFile)}");
|
2020-03-08 09:05:19 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|