Fixed some grammar
Moved Package code from RemotePackageManager to Package class
Better logging
Added some checks
This commit is contained in:
GuilhermeStrice
2020-11-13 10:15:34 +00:00
parent bb659eb520
commit e4f117a149
7 changed files with 112 additions and 101 deletions

View File

@ -34,13 +34,19 @@ namespace PMF.Managers
var json = JsonConvert.SerializeObject(PackageManager.PackageList); var json = JsonConvert.SerializeObject(PackageManager.PackageList);
File.WriteAllText(Config.ManifestFileName, json); File.WriteAllText(Config.ManifestFileName, json);
Directory.Delete(Config.TemporaryFolder, true); Directory.Delete(Config.TemporaryFolder, true);
// PMF.InvokePackageMessageEvent("Successfully cleaned PMF"); PMF.InvokePackageMessageEvent("Successfully cleaned PMF");
} }
public static void validateManifestFile() private static void validateManifestFile()
{ {
PMF.InvokePackageMessageEvent("Validating manifest file");
if (string.IsNullOrEmpty(Config.ManifestFileName))
throw new ArgumentNullException("Manifest file name needs to be defined");
if (!File.Exists(Config.ManifestFileName)) if (!File.Exists(Config.ManifestFileName))
File.Create(Config.ManifestFileName).Close(); File.Create(Config.ManifestFileName).Close();
PMF.InvokePackageMessageEvent("Successfully validated manifest file");
} }
/// <summary> /// <summary>
@ -48,10 +54,11 @@ namespace PMF.Managers
/// </summary> /// </summary>
/// <param name="id">The id of the package</param> /// <param name="id">The id of the package</param>
/// <param name="package">This value is defined if the package exists, its contents will be the actual package</param> /// <param name="package">This value is defined if the package exists, its contents will be the actual package</param>
/// <param name="packageDirectory">The directory which the package is installed</param> /// <param name="packageDirectory">The directory in which the package is installed</param>
/// <returns>True if package is installed, false otherwise</returns> /// <returns>True if package is installed, false otherwise</returns>
public static bool IsPackageInstalled(string id, out Package package, out string packageDirectory) public static bool IsPackageInstalled(string id, bool reportInexistence, out Package package, out string packageDirectory)
{ {
PMF.InvokePackageMessageEvent($"Checking if {id} is installed");
package = null; package = null;
packageDirectory = Path.Combine(Config.PackageInstallationFolder, id); packageDirectory = Path.Combine(Config.PackageInstallationFolder, id);
@ -61,10 +68,13 @@ namespace PMF.Managers
try try
{ {
package = PackageManager.PackageList.GetPackage(id); package = PackageManager.PackageList.GetPackage(id);
PMF.InvokePackageMessageEvent($"Found {id} with version {package.Assets[0].Version}");
return true; return true;
} }
catch catch
{ {
if (reportInexistence)
PMF.InvokePackageMessageEvent($"Couldn't find {id}");
return false; return false;
} }
} }
@ -76,8 +86,9 @@ namespace PMF.Managers
/// <returns>True if uninstalled correctly, false otherwise</returns> /// <returns>True if uninstalled correctly, false otherwise</returns>
public static bool RemovePackage(string id) public static bool RemovePackage(string id)
{ {
PMF.InvokePackageMessageEvent($"Removing {id}");
if (string.IsNullOrEmpty(id)) if (string.IsNullOrEmpty(id))
throw new ArgumentNullException(); throw new ArgumentNullException("Package id must be defined");
try try
{ {
@ -99,11 +110,14 @@ namespace PMF.Managers
/// <param name="remotePackage">The package which is to be installed</param> /// <param name="remotePackage">The package which is to be installed</param>
/// <param name="asset">The version of the asset being installed</param> /// <param name="asset">The version of the asset being installed</param>
/// <param name="zipPath">The path to the zip file that is to be installed</param> /// <param name="zipPath">The path to the zip file that is to be installed</param>
/// <returns>The package that was installed</returns> /// <returns>State of the installation</returns>
public static Package InstallPackage(Package remotePackage, Asset asset, string zipPath) public static PackageState InstallPackage(Package remotePackage, Asset asset, string zipPath)
{ {
PMF.InvokePackageMessageEvent("Extracting package"); PMF.InvokePackageMessageEvent($"Extracting package {remotePackage.ID}");
ZipFile.ExtractToDirectory(Path.Combine(zipPath, asset.FileName), Path.Combine(Config.PackageInstallationFolder, remotePackage.ID)); ZipFile.ExtractToDirectory(Path.Combine(zipPath, asset.FileName), Path.Combine(Config.PackageInstallationFolder, remotePackage.ID));
PMF.InvokePackageMessageEvent($"Finished extracting package {remotePackage.ID}");
bool error = false;
// Maybe a library folder and check if is installed // Maybe a library folder and check if is installed
foreach (var dependency in asset.Dependencies) foreach (var dependency in asset.Dependencies)
@ -112,6 +126,7 @@ namespace PMF.Managers
{ {
PMF.InvokePackageMessageEvent($"Extracting dependency with id: {dependency.ID} of type standalone"); PMF.InvokePackageMessageEvent($"Extracting dependency with id: {dependency.ID} of type standalone");
ZipFile.ExtractToDirectory(Path.Combine(zipPath, dependency.FileName), Path.Combine(Config.PackageInstallationFolder, remotePackage.ID, "Dependencies", dependency.ID)); ZipFile.ExtractToDirectory(Path.Combine(zipPath, dependency.FileName), Path.Combine(Config.PackageInstallationFolder, remotePackage.ID, "Dependencies", dependency.ID));
PMF.InvokePackageMessageEvent($"Finished extracting dependency {dependency.ID}");
} }
else // DependencyType.Package else // DependencyType.Package
{ {
@ -126,14 +141,17 @@ namespace PMF.Managers
else if (success == PackageState.VersionNotFound) else if (success == PackageState.VersionNotFound)
{ {
PMF.InvokePackageMessageEvent("Asset not found"); PMF.InvokePackageMessageEvent("Asset not found");
error = true;
} }
else if (success == PackageState.NotExisting) else if (success == PackageState.NotExisting)
{ {
PMF.InvokePackageMessageEvent("Package not found"); PMF.InvokePackageMessageEvent("Package not found");
error = true;
} }
else else
{ {
PMF.InvokePackageMessageEvent($"Something went wrong installing dependency with id: {dependency.ID}"); PMF.InvokePackageMessageEvent($"Something went wrong installing dependency with id: {dependency.ID}");
error = true;
} }
} }
} }
@ -143,9 +161,12 @@ namespace PMF.Managers
PackageManager.PackageList.Add(remotePackage); PackageManager.PackageList.Add(remotePackage);
PMF.InvokePackageMessageEvent($"Successfully installed {remotePackage.ID}@{asset.Version}"); string errorMsg = "";
if (error)
errorMsg = " with errors. See above";
return remotePackage; PMF.InvokePackageMessageEvent($"Successfully installed {remotePackage.ID}@{asset.Version} {errorMsg}");
return PackageState.Installed;
} }
} }
} }

View File

@ -54,10 +54,13 @@ namespace PMF.Managers
} }
} }
private static void notInitialized() private static void checkInitialization()
{ {
PMF.InvokePackageMessageEvent("You must initialize PMF first before using it.\nClosing"); if (!initialized)
Environment.Exit(0); {
PMF.InvokePackageMessageEvent("You must initialize PMF first before using it.\nClosing");
Environment.Exit(0);
}
} }
/// <summary> /// <summary>
@ -65,11 +68,10 @@ namespace PMF.Managers
/// </summary> /// </summary>
/// <param name="id">The id of the package</param> /// <param name="id">The id of the package</param>
/// <param name="version">The version of the asset</param> /// <param name="version">The version of the asset</param>
/// <returns>true Installation successful, false already installed</returns> /// <returns>State of the package</returns>
public static PackageState InstallPackage(Package package, Asset asset) public static PackageState InstallPackage(Package package, Asset asset)
{ {
if (!initialized) checkInitialization();
notInitialized();
string zipFile = RemotePackageManager.DownloadAsset(package.ID, asset); string zipFile = RemotePackageManager.DownloadAsset(package.ID, asset);
LocalPackageManager.InstallPackage(package, asset, zipFile); LocalPackageManager.InstallPackage(package, asset, zipFile);
@ -87,11 +89,10 @@ namespace PMF.Managers
{ {
package = null; package = null;
if (!initialized) checkInitialization();
notInitialized();
// check if is already installed // check if is already installed
if (!LocalPackageManager.IsPackageInstalled(id, out Package localPackage, out string packageDirectory)) if (!LocalPackageManager.IsPackageInstalled(id, false, out Package localPackage, out string packageDirectory))
{ {
Package remotePackage = RemotePackageManager.GetPackageInfo(id); Package remotePackage = RemotePackageManager.GetPackageInfo(id);
@ -118,16 +119,15 @@ namespace PMF.Managers
/// </summary> /// </summary>
/// <param name="id">The id of the package</param> /// <param name="id">The id of the package</param>
/// <param name="package">The package that was installed</param> /// <param name="package">The package that was installed</param>
/// <returns>true Installation successful, false already installed</returns> /// <returns>State of the package</returns>
public static PackageState InstallLatest(string id, out Package package) public static PackageState InstallLatest(string id, out Package package)
{ {
package = null; package = null;
if (!initialized) checkInitialization();
notInitialized();
// check if is already installed // check if is already installed
if (!LocalPackageManager.IsPackageInstalled(id, out Package localPackage, out string packageDirectory)) if (!LocalPackageManager.IsPackageInstalled(id, false, out Package localPackage, out string packageDirectory))
{ {
// get package info for version // get package info for version
Package remotePackage = RemotePackageManager.GetPackageInfo(id); Package remotePackage = RemotePackageManager.GetPackageInfo(id);
@ -135,7 +135,7 @@ namespace PMF.Managers
if (remotePackage == null) if (remotePackage == null)
return PackageState.NotExisting; return PackageState.NotExisting;
Asset asset = RemotePackageManager.GetAssetLatestVersion(package); Asset asset = package.GetAssetLatestVersion();
if (asset == null) if (asset == null)
return PackageState.VersionNotFound; return PackageState.VersionNotFound;
@ -155,23 +155,22 @@ namespace PMF.Managers
/// </summary> /// </summary>
/// <param name="id">The id of the package</param> /// <param name="id">The id of the package</param>
/// <param name="package">The package that was installed</param> /// <param name="package">The package that was installed</param>
/// <returns>true update succes, false update failed or cancelled</returns> /// <returns>State of the package</returns>
public static PackageState InstallBySdkVersion(string id, out Package package) public static PackageState InstallBySdkVersion(string id, out Package package)
{ {
package = null; package = null;
if (!initialized) checkInitialization();
notInitialized();
// check if is already installed // check if is already installed
if (!LocalPackageManager.IsPackageInstalled(id, out Package localPackage, out string packageDirectory)) if (!LocalPackageManager.IsPackageInstalled(id, false, out Package localPackage, out string packageDirectory))
{ {
Package remotePackage = RemotePackageManager.GetPackageInfo(id); Package remotePackage = RemotePackageManager.GetPackageInfo(id);
if (remotePackage == null) if (remotePackage == null)
return PackageState.NotExisting; return PackageState.NotExisting;
Asset asset = RemotePackageManager.GetAssetLatestVersionBySdkVersion(remotePackage); Asset asset = remotePackage.GetAssetLatestVersionBySdkVersion();
if (asset == null) if (asset == null)
return PackageState.VersionNotFound; return PackageState.VersionNotFound;
@ -193,8 +192,7 @@ namespace PMF.Managers
/// <returns>True if success, false otherwise</returns> /// <returns>True if success, false otherwise</returns>
public static bool Uninstall(string id) public static bool Uninstall(string id)
{ {
if (!initialized) checkInitialization();
notInitialized();
return LocalPackageManager.RemovePackage(id); return LocalPackageManager.RemovePackage(id);
} }
@ -204,23 +202,22 @@ namespace PMF.Managers
/// </summary> /// </summary>
/// <param name="id">The id of the package</param> /// <param name="id">The id of the package</param>
/// <param name="package">The package that was installed</param> /// <param name="package">The package that was installed</param>
/// <returns>true update succes, false update failed or cancelled</returns> /// <returns>State of the package</returns>
public static PackageState UpdateLatest(string id, out Package package) public static PackageState UpdateLatest(string id, out Package package)
{ {
package = null; package = null;
if (!initialized) checkInitialization();
notInitialized();
// check if is already installed // check if is already installed
if (LocalPackageManager.IsPackageInstalled(id, out Package localPackage, out string packageDirectory)) if (LocalPackageManager.IsPackageInstalled(id, true, out Package localPackage, out string packageDirectory))
{ {
var remotePackage = RemotePackageManager.GetPackageInfo(id); var remotePackage = RemotePackageManager.GetPackageInfo(id);
if (remotePackage == null) if (remotePackage == null)
return PackageState.NotExisting; return PackageState.NotExisting;
var asset = RemotePackageManager.GetAssetLatestVersion(remotePackage); var asset = remotePackage.GetAssetLatestVersion();
// You already have the latest version // You already have the latest version
if (localPackage.Assets[0].Version == asset.Version) if (localPackage.Assets[0].Version == asset.Version)
@ -241,16 +238,15 @@ namespace PMF.Managers
/// </summary> /// </summary>
/// <param name="id">The id of the package</param> /// <param name="id">The id of the package</param>
/// <param name="package">The package that was installed</param> /// <param name="package">The package that was installed</param>
/// <returns>True update success, false update failed or cancelled</returns> /// <returns>State of the package</returns>
public static PackageState UpdatePackage(string id, Version version, out Package package) public static PackageState UpdatePackage(string id, Version version, out Package package)
{ {
package = null; package = null;
if (!initialized) checkInitialization();
notInitialized();
// check if is already installed // check if is already installed
if (LocalPackageManager.IsPackageInstalled(id, out Package localPackage, out string packageDirectory)) if (LocalPackageManager.IsPackageInstalled(id, true, out Package localPackage, out string packageDirectory))
{ {
// Up to date // Up to date
if (localPackage.Assets[0].Version == version) if (localPackage.Assets[0].Version == version)
@ -283,15 +279,14 @@ namespace PMF.Managers
/// </summary> /// </summary>
/// <param name="id">The id of the package</param> /// <param name="id">The id of the package</param>
/// <param name="package">The package that was installed</param> /// <param name="package">The package that was installed</param>
/// <returns>True if update success, false if package is not installed</returns> /// <returns>State of the package</returns>
public static PackageState UpdateBySdkVersion(string id, out Package package) public static PackageState UpdateBySdkVersion(string id, out Package package)
{ {
package = null; package = null;
if (!initialized) checkInitialization();
notInitialized();
if (!LocalPackageManager.IsPackageInstalled(id, out Package localPackage, out string pd)) if (!LocalPackageManager.IsPackageInstalled(id, true, out Package localPackage, out string pd))
{ {
PMF.InvokePackageMessageEvent("Already up to date"); PMF.InvokePackageMessageEvent("Already up to date");
return PackageState.NotInstalled; return PackageState.NotInstalled;
@ -302,7 +297,7 @@ namespace PMF.Managers
if (remotePackage == null) if (remotePackage == null)
return PackageState.NotExisting; return PackageState.NotExisting;
var asset = RemotePackageManager.GetAssetLatestVersionBySdkVersion(remotePackage); var asset = remotePackage.GetAssetLatestVersionBySdkVersion();
// doesn't exist for provided sdk version // doesn't exist for provided sdk version
if (asset == null) if (asset == null)

View File

@ -8,10 +8,10 @@ namespace PMF.Managers
internal static class RemotePackageManager internal static class RemotePackageManager
{ {
/// <summary> /// <summary>
/// Gets package info from the server along with ALL the assets in the json /// Gets package info from the server along with all the assets in the json
/// </summary> /// </summary>
/// <param name="id">The id of the package</param> /// <param name="id">The id of the package</param>
/// <returns>The package object downloaded</returns> /// <returns>The package downloaded</returns>
public static Package GetPackageInfo(string id) public static Package GetPackageInfo(string id)
{ {
try try
@ -36,7 +36,7 @@ namespace PMF.Managers
/// </summary> /// </summary>
/// <param name="id">The id of the package</param> /// <param name="id">The id of the package</param>
/// <param name="asset">The asset that is to be downloaded</param> /// <param name="asset">The asset that is to be downloaded</param>
/// <returns>The zip file which was downloaded</returns> /// <returns>The zip file path which was downloaded</returns>
public static string DownloadAsset(string id, Asset asset) public static string DownloadAsset(string id, Asset asset)
{ {
using (WebClient client = new WebClient()) using (WebClient client = new WebClient())
@ -57,53 +57,5 @@ namespace PMF.Managers
return zipPath; return zipPath;
} }
} }
/// <summary>
/// Gets you the latest version of a package
/// </summary>
/// <param name="package">The package object to get the latest version</param>
/// <returns>The latest asset version of a given package</returns>
public static Asset GetAssetLatestVersion(Package package)
{
if (package == null)
throw new ArgumentNullException();
if (package.Assets.Count == 0)
return null;
Asset ret_asset = null;
foreach (var asset in package.Assets)
{
if (ret_asset == null || ret_asset.Version < asset.Version)
ret_asset = asset;
}
return ret_asset;
}
/// <summary>
/// Gets you the latest version of a package given an SDK version
/// </summary>
/// <param name="package">The package object to get the asset</param>
/// <returns>The latest asset version of a given package and given SDK version</returns>
public static Asset GetAssetLatestVersionBySdkVersion(Package package)
{
if (package == null)
throw new ArgumentNullException();
if (package.Assets.Count == 0)
return null;
Asset ret_asset = null;
foreach (var asset in package.Assets)
{
if (asset.SdkVersion == Config.CurrentSdkVersion)
{
if (ret_asset == null || ret_asset.Version < asset.Version)
ret_asset = asset;
}
}
return ret_asset;
}
} }
} }

View File

@ -13,7 +13,7 @@ namespace PMF
/// <summary> /// <summary>
/// The version of this asset /// The version of this asset
/// </summary> /// </summary>
[JsonConverter(typeof(VersionConverter))] // This ensures the version object is correctly converted [JsonConverter(typeof(VersionConverter))]
public Version Version { get; set; } public Version Version { get; set; }
/// <summary> /// <summary>

View File

@ -22,13 +22,13 @@ namespace PMF
/// <summary> /// <summary>
/// Type of dependency /// Type of dependency
/// </summary> /// </summary>
[JsonConverter(typeof(StringEnumConverter))] // This converts enum to string and vice versa when generating or parsing json [JsonConverter(typeof(StringEnumConverter))]
public DependencyType Type { get; set; } public DependencyType Type { get; set; }
/// <summary> /// <summary>
/// Version of the dependency if type is Package /// Version of the dependency if type is Package
/// </summary> /// </summary>
[JsonConverter(typeof(VersionConverter))] // This ensures the version object is correctly converted [JsonConverter(typeof(VersionConverter))]
public Version Version { get; set; } public Version Version { get; set; }
/// <summary> /// <summary>

View File

@ -15,7 +15,7 @@ namespace PMF
/// <summary> /// <summary>
/// The type of this package /// The type of this package
/// </summary> /// </summary>
[JsonConverter(typeof(StringEnumConverter))] // This converts enum to string and vice versa when generating or parsing json [JsonConverter(typeof(StringEnumConverter))]
public PackageType Type { get; set; } public PackageType Type { get; set; }
/// <summary> /// <summary>
@ -57,6 +57,49 @@ namespace PMF
return null; return null;
} }
/// <summary>
/// Gets you the latest version of a package
/// </summary>
/// <param name="package">The package object to get the latest version</param>
/// <returns>The latest asset version of a given package</returns>
public Asset GetAssetLatestVersion()
{
if (Assets.Count == 0)
return null;
Asset ret_asset = null;
foreach (var asset in Assets)
{
if (ret_asset == null || ret_asset.Version < asset.Version)
ret_asset = asset;
}
return ret_asset;
}
/// <summary>
/// Gets you the latest version of a package given an SDK version
/// </summary>
/// <param name="package">The package object to get the asset</param>
/// <returns>The latest asset version of a given package and given SDK version</returns>
public Asset GetAssetLatestVersionBySdkVersion()
{
if (Assets.Count == 0)
return null;
Asset ret_asset = null;
foreach (var asset in Assets)
{
if (asset.SdkVersion == Config.CurrentSdkVersion)
{
if (ret_asset == null || ret_asset.Version < asset.Version)
ret_asset = asset;
}
}
return ret_asset;
}
// A valid package must have: // A valid package must have:
// - an id // - an id
// - a type // - a type

View File

@ -125,7 +125,7 @@ This is the basic information a package would have
```json ```json
{ {
"ID": "something_cool", "ID": "something_cool",
"Type": "Plugin", "Type": "Package",
"Name": "Im Something cool", "Name": "Im Something cool",
"Description": "This is a package that makes you cool and awesome", "Description": "This is a package that makes you cool and awesome",
"Assets": "Assets":