//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; } }
//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 + ")."); } }
/// <summary> /// Remove sources from the source list, and all associated packages. /// </summary> public void RemoveSource(string[] args) { //Check the argument length. if (args.Length < 1) { Error.FatalNoContext("No source(s) given to remove."); return; } //Get the source and package lists from file. SharpieSources sources = JsonConvert.DeserializeObject <SharpieSources>(File.ReadAllText(SourcesFile)); SharpiePackages packages = JsonConvert.DeserializeObject <SharpiePackages>(File.ReadAllText(PackagesFile)); foreach (var source in args) { //Check if the source is actually installed. if (!sources.SourceExists(source)) { Error.WarningNoContext("A source with the name '" + source + "' is not installed, skipping."); continue; } //Yes, loop through all packages with the source name attached and uninstall them. Console.WriteLine("Attempting to remove packages for source '" + source + "'..."); string[] toRemove = packages.Packages.Where(x => x.ParentSource == source).Where(x => x.Installed == true).Select(x => x.PackageName).ToArray(); RemovePackage(toRemove); //Actually deleting the packages from the master list which have this source as a parent. packages.Packages.RemoveAll(x => x.ParentSource == source); //Removing the source itself from the source list. Console.WriteLine("Deleting source '" + source + "' from master list..."); int amtRemoved = sources.Sources.RemoveAll(x => x.SourceName == source); if (amtRemoved <= 0) { Error.WarningNoContext("Failed to remove source from master list, skipping."); continue; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Successfully removed source '" + source + "'."); Console.ForegroundColor = ConsoleColor.White; } //Done modifying the lists, serialize them back to file. File.WriteAllText(PackagesFile, JsonConvert.SerializeObject(packages)); File.WriteAllText(SourcesFile, JsonConvert.SerializeObject(sources)); }
//List all current packages. public void ListPackages() { //Load package data. SharpiePackages packages = JsonConvert.DeserializeObject <SharpiePackages>(File.ReadAllText(PackagesFile)); //Loop over packages and write. Console.WriteLine("Sharpie Package Master List"); Console.WriteLine("key: name | version | installed? | link"); Console.WriteLine("--------------------------"); foreach (var pkg in packages.Packages) { Console.WriteLine(pkg.PackageName + " | " + pkg.PackageVersion + " | " + pkg.Installed.ToString() + " | " + pkg.Link); } if (packages.Packages.Count == 0) { Console.WriteLine("No packages in master list."); } Console.WriteLine("--------------------------\n"); }
/// <summary> /// Adds a source to the Sharpie master list. /// </summary> public void AddSource(string[] args) { //Check the argument length. if (args.Length < 1) { Error.FatalNoContext("No source link(s) supplied to add."); } //Deserialize source list and package list. SharpieSources sources = JsonConvert.DeserializeObject <SharpieSources>(File.ReadAllText(SourcesFile)); SharpiePackages packages = JsonConvert.DeserializeObject <SharpiePackages>(File.ReadAllText(PackagesFile)); //Has the warning been read already? If not, read the warning. if (!sources.SourceWarningRead) { DisplayWarning(); sources.SourceWarningRead = true; } //Loop the sources, and add them. foreach (var source in args) { //Try and download the source. string srcString = ""; using (var client = new WebClient()) { try { srcString = client.DownloadString(source); } catch (Exception e) { Error.WarningNoContext("An unknown error occured when downloading the source '" + source + "' (" + e.Message + "), source skipped."); continue; } } //Attempt to parse the source into a source object. Tuple <SharpieSource, List <SharpiePackage> > parsed = SharpieSourceParser.Parse(srcString, source); //Check if a source with this name already exists. if (sources.SourceExists(parsed.Item1.SourceName)) { Error.WarningNoContext("A source with the name '" + parsed.Item1.SourceName + "' is already installed, so skipping."); continue; } //No, so add the packages and source. Console.WriteLine("Attempting to add source packages for source '" + parsed.Item1.SourceName + "'..."); foreach (var pkg in parsed.Item2) { if (packages.PackageExists(pkg.PackageName)) { Error.WarningNoContext("A package with the name '" + pkg.PackageName + "' already exists, so skipping."); continue; } packages.Packages.Add(pkg); Console.WriteLine("Added package '" + pkg.PackageName + "'."); } sources.Sources.Add(parsed.Item1); //Serializing and saving. File.WriteAllText(SourcesFile, JsonConvert.SerializeObject(sources)); File.WriteAllText(PackagesFile, JsonConvert.SerializeObject(packages)); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Successfully added source '" + parsed.Item1.SourceName + "'."); Console.ForegroundColor = ConsoleColor.White; } }
//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; } }