From c0dc3720ce3e02f35c473ba2ecde0e99e5774812 Mon Sep 17 00:00:00 2001 From: ericoporto Date: Sun, 2 Feb 2020 00:55:47 +0000 Subject: [PATCH] adds a way to store what we intend to install using a package-manifest file. --- .../AgsGetCore/IntentDescriptor.cs | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/IntentDescriptor.cs diff --git a/agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/IntentDescriptor.cs b/agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/IntentDescriptor.cs new file mode 100644 index 0000000..55d7c3e --- /dev/null +++ b/agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/IntentDescriptor.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using Newtonsoft.Json; + +// this file deals with writing intent as a manifest file +// when we want to add a package, we add it to the list +// when we want to remove a package, we remove it from the list +// we will use this file to store the state of which packages we need to install later on +// in another step we will get these packages, and figure out dependencies and the order to insert! + +namespace AgsGetCore +{ + class IntentDescriptor + { + private const string ManifestFile = "agsget-manifest.json"; + + private static string GetManifestFilePath() + { + return Path.Combine(BaseFiles.GetRunDirectory(), ManifestFile); + } + + public static List GetManifestAsList() + { + if (!File.Exists(GetManifestFilePath())) + { + return new List(); + } + + var manifestAsString = File.ReadAllText(GetManifestFilePath()); + + return JsonConvert.DeserializeObject>(manifestAsString); + } + + public static bool AddPackage(string package_id) + { + var manifestList = GetManifestAsList(); + + if (!BaseFiles.ExistsIndexFile()) + { + // we don't even have an index, we need one before adding packages for installation + return false; + } + + if (!PackageCacheIO.PackageOnIndex(package_id)) + { + // the package_id is wrong or our index is wrong + return false; + } + + var previouslyAddedPackage = manifestList.Where(_mpd => + { + return _mpd.id.Equals(package_id.ToLower()); + }); + + if (previouslyAddedPackage.Count() > 0) + { + return true; + } + + var mpd = new MinimalPackageDescriptor(); + mpd.id = package_id; + + manifestList.Add(mpd); + + string resulting_json_manifest = JsonConvert.SerializeObject(manifestList); + const string bkp_ext = ".bkp"; + string manifest_file = GetManifestFilePath(); + string bkp_manifest_file = manifest_file + bkp_ext; + + if (File.Exists(manifest_file)) File.Move(manifest_file, bkp_manifest_file); + System.IO.File.WriteAllText(manifest_file, resulting_json_manifest); + if (File.Exists(bkp_manifest_file)) File.Delete(bkp_manifest_file); + + return true; + } + public static bool RemovePackage(string package_id) + { + var manifestList = GetManifestAsList(); + + var previouslyAddedPackage = manifestList.Where(_mpd => + { + return _mpd.id.Equals(package_id.ToLower()); + }); + + if (previouslyAddedPackage.Count() == 0) + { + return false; + } + + manifestList = manifestList.Where(_mpd => + { + return !_mpd.id.Equals(package_id.ToLower()); + }).ToList(); + + string resulting_json_manifest = JsonConvert.SerializeObject(manifestList); + const string bkp_ext = ".bkp"; + string manifest_file = GetManifestFilePath(); + string bkp_manifest_file = manifest_file + bkp_ext; + + if (File.Exists(manifest_file)) File.Move(manifest_file, bkp_manifest_file); + System.IO.File.WriteAllText(manifest_file, resulting_json_manifest); + if (File.Exists(bkp_manifest_file)) File.Delete(bkp_manifest_file); + + return true; + } + + public class MinimalPackageDescriptor + { + public string id { get; set; } + } + } +}