//Design standard public static void RemovePackages(Validator val, PackageOverview overview) { if (val.removeAll) { foreach (Package i in overview.packages) { //It's redundant, but prevents spamming errors. if (PackageUtil.DoesPackageExist(i)) { Remove(i, val, overview); } } } else { foreach (string i in val.packageNames) { Package current = overview.GetPackageWithName(i); if (current != null) { Remove(current, val, overview); } else { Console.WriteLine(("WARNING: No package with name " + i + ".").Pastel("ffff00")); } } } }
//Design standard public static void UpdatePackages(Validator val, PackageOverview overview) { //If we update all with wildcard *. This method doesn't require a list of previous packages. if (val.updateAll) { foreach (Package i in overview.packages) { if (PackageUtil.DoesPackageExist(i)) { Update(i, val, overview).Wait(); } } } //If we list packages else { foreach (string i in val.packageNames) { if (overview.GetPackageWithName(i) != null) { Update(overview.GetPackageWithName(i), val, overview).Wait(); } else { Console.WriteLine(("WARNING: No package with name " + i + ".").Pastel("ffff00")); } } } }
public async static Task Update(Package package, Validator val, PackageOverview overview) { //Create tmp dir so files are gone after execution. using (TmpDir dir = new TmpDir(".")) using (WebClient client = new WebClient()) { //Check if it exists if (PackageUtil.DoesPackageExist(package)) { string filename = Path.GetFileName(package.downloadLocation); Console.WriteLine("Getting newest version of package " + package.name); //Setup loading bar client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(LoadingBar.DownloadProgressCallback); //Get latest version await client.DownloadFileTaskAsync(package.downloadLink, dir.dirName + filename); Console.Write("\n\r"); //Compare. The && ! will make it so that it will always go to 2nd case if val.forceUpdate is ture. if (FileCompare.CompareFiles(dir.dirName + filename, "../sm_plugins/" + package.downloadLocation) && !val.forceUpdate) { Console.WriteLine(package.name + " is up to date."); } else { File.Move(dir.dirName + filename, "../sm_plugins/" + package.downloadLocation, true); Console.WriteLine(package.name + " was updated."); } } else { Console.WriteLine(("WARNING: Package " + package.name + " is not installed. Skipping.").Pastel("ffff00")); } } }
//This is the way commands should be set up. 1st func loops over packages and triggers 2nd for each. 1st is public, 2nd is private. public static void InstallPackages(Validator val, PackageOverview overview) { foreach (string i in val.packageNames) { //If it exists, install Package info = overview.GetPackageWithName(i); if (info != null) { GetFile(info, overview, val).Wait(); } else { Console.WriteLine(("ERROR: No package with name " + i).Pastel("ff0000")); } } }
private static void Remove(Package package, Validator val, PackageOverview overview) { //Check if it exists. if (PackageUtil.DoesPackageExist(package)) { Console.WriteLine("Removing package " + package.name); //Remove if (PackageUtil.DoesDirectoryExist(package)) { File.Delete("../sm_plugins/" + package.downloadLocation); } else { Console.WriteLine("ERROR: sm_plugins and/or dependencies could not be found. Did you unzip all the app files into a folder at the same level as sm_plugins?".Pastel("ff0000")); } } else { Console.WriteLine(("WARNING: You have not installed " + package.name + ", skipping.").Pastel("ffff00")); } }
static void Main(string[] args) { string version = "0.3-alpha1"; //Validate arguments passed to program Validator validator = new Validator(args); //Get the available packages if (validator.success) { PackageOverview overview = new PackageOverview(); if (overview.GenPackages(version)) { //What command are we running? switch (validator.opType) { case Validator.OP_TYPE.MAIN_HELP: Help.MainHelp(); break; case Validator.OP_TYPE.INSTALL: Installer.InstallPackages(validator, overview); break; case Validator.OP_TYPE.INSTALL_HELP: Help.InstallHelp(); break; case Validator.OP_TYPE.REMOVE: Remover.RemovePackages(validator, overview); break; case Validator.OP_TYPE.REMOVE_HELP: Help.RemoveHelp(); break; case Validator.OP_TYPE.UPDATE: Updater.UpdatePackages(validator, overview); break; case Validator.OP_TYPE.UPDATE_HELP: Help.UpdateHelp(); break; case Validator.OP_TYPE.SERVER: Server.CreateServer(validator); break; case Validator.OP_TYPE.SERVER_HELP: Help.ServerHelp(); break; case Validator.OP_TYPE.CREATEPACKAGE: Packager.PackagePlugin(); break; case Validator.OP_TYPE.CREATEPACKAGE_HELP: Help.CreatePackageHelp(); break; default: Console.WriteLine("Unknown error".Pastel("ff0000")); break; } } } //In case we need to display help else { switch (validator.opType) { case Validator.OP_TYPE.MAIN_HELP: Help.MainHelp(); break; case Validator.OP_TYPE.INSTALL_HELP: Help.InstallHelp(); break; case Validator.OP_TYPE.REMOVE_HELP: Help.RemoveHelp(); break; case Validator.OP_TYPE.UPDATE_HELP: Help.UpdateHelp(); break; case Validator.OP_TYPE.SERVER_HELP: Help.ServerHelp(); break; case Validator.OP_TYPE.CREATEPACKAGE_HELP: Help.CreatePackageHelp(); break; default: Console.WriteLine("Unknown error".Pastel("ff0000")); break; } } #if (DEBUG) Console.ReadKey(); #endif }
private static async Task GetFile(Package package, PackageOverview overview, Validator val) { Console.WriteLine("Downloading " + package.name); //Download dependencies first foreach (string i in package.dependencies) { await GetFile(overview.GetPackageWithName(i), overview, val); } //Check if any incompatible package are installed bool incompat = false; foreach (string i in package.incompatibilities) { if (PackageUtil.DoesPackageExist(overview.GetPackageWithName(i))) { bool res = Dialog.YNDialog(("WARNING: " + package.name + " is incompatiable with " + i + ". Would you like to remove the other package (Yes/No)? ").Pastel("ffff00")); if (res) { Validator newVal = ClassCopy.DeepCopy(val); newVal.packageNames = new List <string> { i }; Remover.RemovePackages(newVal, overview); } else { incompat = true; } } } if (!incompat) { //Check if file already exists if (!PackageUtil.DoesPackageExist(package)) { using (WebClient client = new WebClient()) { //Setup loading bar client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(LoadingBar.DownloadProgressCallback); //Download file to directory after checking if the folders existba if (PackageUtil.DoesDirectoryExist(package)) { await client.DownloadFileTaskAsync(package.downloadLink, "../sm_plugins/" + package.downloadLocation); Console.Write("\n\r"); } else { if (val.createDir) { Directory.CreateDirectory(Path.GetDirectoryName("../sm_plugins/" + package.downloadLocation)); await client.DownloadFileTaskAsync(package.downloadLink, "../sm_plugins/" + package.downloadLocation); Console.Write("\n\r"); } else { Console.WriteLine("ERROR: The folder to install to does not exist. Did you unzip all the app files into a folder at the same level as sm_plugins?".Pastel("ff0000")); } } } } else { Console.WriteLine(("WARNING: Plugin " + package.name + " is already installed, skipping.").Pastel("ffff000")); } } }