-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds a way to store what we intend to install
using a package-manifest file.
- Loading branch information
1 parent
8553e5e
commit c0dc372
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
agsget/agsget/EditorPlugin/AGS.Plugin.AgsGet/AgsGetCore/IntentDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MinimalPackageDescriptor> GetManifestAsList() | ||
{ | ||
if (!File.Exists(GetManifestFilePath())) | ||
{ | ||
return new List<MinimalPackageDescriptor>(); | ||
} | ||
|
||
var manifestAsString = File.ReadAllText(GetManifestFilePath()); | ||
|
||
return JsonConvert.DeserializeObject<List<MinimalPackageDescriptor>>(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<MinimalPackageDescriptor>(_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<MinimalPackageDescriptor>(_mpd => | ||
{ | ||
return _mpd.id.Equals(package_id.ToLower()); | ||
}); | ||
|
||
if (previouslyAddedPackage.Count() == 0) | ||
{ | ||
return false; | ||
} | ||
|
||
manifestList = manifestList.Where<MinimalPackageDescriptor>(_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; } | ||
} | ||
} | ||
} |