//Remove a package from the packages directory. public void RemovePackage(string[] args) { //Check the argument is there. if (args.Length < 1) { Error.FatalNoContext("No packages given to remove."); } //Deserialize package file. SharpiePackages packages = JsonConvert.DeserializeObject <SharpiePackages>(File.ReadAllText(PackagesFile)); //For each argument, process the package. foreach (var pkg in args) { //Does the package exist? if (!packages.PackageExists(pkg)) { Error.WarningNoContext("A package with the name '" + pkg + "' does not exist in the master list, so has been skipped."); continue; } //Package does exist, get it and check if it's installed. SharpiePackage pkgObj = packages.GetPackage(pkg); if (!pkgObj.Installed) { Error.WarningNoContext("The package '" + pkg + "' was not installed, so has not been removed. Skipping."); continue; } //Delete the package directory, mark as uninstalled. string pkgDir = CPFilePath.GetPlatformFilePath(new string[] { PackagesDirectory, pkg }); try { Directory.Delete(pkgDir, true); } catch (Exception e) { Error.WarningNoContext("Failed to delete package directory for the package '" + pkg + "' (" + e.Message + "), skipping."); continue; } //Mark as uninstalled, serialize back. pkgObj.Installed = false; packages.SetPackage(pkg, pkgObj); File.WriteAllText(PackagesFile, JsonConvert.SerializeObject(packages)); //Confirm success. Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Successfully removed package '" + pkg + "'."); Console.ForegroundColor = ConsoleColor.White; } }
//Sets a package in the master list. public void SetPackage(string pkg, SharpiePackage pkgObj) { if (!PackageExists(pkg)) { Error.FatalNoContext("Failed to set package data for the package '" + pkg + "', exiting."); return; } //Get index, set package. int index = Packages.FindIndex(x => x.PackageName == pkg); //Set. Packages[index] = pkgObj; }
//Update a given Algo package. public void UpdatePackage(string[] args) { //Check the argument is there. if (args.Length < 1) { Error.FatalNoContext("No packages given to update."); } //Deserialize package file. SharpiePackages packages = JsonConvert.DeserializeObject <SharpiePackages>(File.ReadAllText(PackagesFile)); //For each argument, process the package. if (args[0] == "*") { //If the argument is '*', it means all installed packages. args = packages.Packages.Where(x => x.Installed == true).Select(x => x.PackageName).ToArray(); } foreach (var pkg in args) { //Does the package exist? if (!packages.PackageExists(pkg)) { Error.WarningNoContext("No package with the name '" + pkg + "' exists in the master list, skipping."); continue; } //Is it installed? SharpiePackage pkgObj = packages.GetPackage(pkg); if (!pkgObj.Installed) { Error.WarningNoContext("The package '" + pkg + "' is not installed, so cannot update. Skipping."); continue; } //Is the current version less than the version? if (pkgObj.CurrentPackageVersion >= pkgObj.PackageVersion) { Console.WriteLine("No update required for package '" + pkg + "'."); continue; } //It needs updating. Attempt to grab from the source. Console.WriteLine("Package '" + pkg + "' requires an update, reinstalling..."); RemovePackage(new string[] { pkg }); AddPackage(new string[] { pkg }); Console.WriteLine("Successfully updated package '" + pkg + "' to the latest version (" + pkgObj.PackageVersion + ")."); } }
//Add a package to the packages directory. public void AddPackage(string[] args) { //Check the argument is there. if (args.Length < 1) { Error.FatalNoContext("No packages given to install."); } //Deserialize package file. SharpiePackages packages = JsonConvert.DeserializeObject <SharpiePackages>(File.ReadAllText(PackagesFile)); //For each argument, process the package. foreach (var pkg in args) { //Check if this package exists in the master list. if (!packages.PackageExists(pkg)) { Error.WarningNoContext("No package exists in the master list with name '" + pkg + "' to install, it has been skipped."); continue; } //It does, so attempt to do a HTTP grab from the link to get the file. SharpiePackage pkgObj = packages.GetPackage(pkg); if (pkgObj.Installed == true) { Error.WarningNoContext("The package '" + pkg + "' is already installed, so it has been skipped."); continue; } string pkgZipFile = CPFilePath.GetPlatformFilePath(new string[] { PackagesDirectory, pkg + ".zip" }); using (var client = new WebClient()) { try { client.DownloadFile(pkgObj.Link, pkgZipFile); } catch (Exception e) { Error.WarningNoContext("An unknown error occured when downloading the given package '" + pkg + "' (" + e.Message + "), package skipped."); continue; } } //Unzip the file into the packages directory. (/packages/somepkgname) try { ZipFile.ExtractToDirectory(pkgZipFile, CPFilePath.GetPlatformFilePath(new string[] { PackagesDirectory, pkg })); } catch (Exception e) { Error.WarningNoContext("An unknown error occured when extracting the given package '" + pkg + "' (" + e.Message + "), package skipped."); continue; } //Deleting the zip file. try { File.Delete(pkgZipFile); } catch (Exception e) { Error.FatalNoContext("Failed to delete temporary file '" + pkgZipFile + "', given error: " + e.Message); return; } //Mark as installed in sharpie packages. pkgObj.Installed = true; pkgObj.CurrentPackageVersion = pkgObj.PackageVersion; //Save to object, serialize again. packages.SetPackage(pkg, pkgObj); File.WriteAllText(PackagesFile, JsonConvert.SerializeObject(packages)); //Notify of successful installation. Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Successfully installed package '" + pkg + "'."); Console.ForegroundColor = ConsoleColor.White; } }