diff --git a/PMF/src/Managers/LocalPackageManager.cs b/PMF/src/Managers/LocalPackageManager.cs index 4fc8c3d..c96e443 100644 --- a/PMF/src/Managers/LocalPackageManager.cs +++ b/PMF/src/Managers/LocalPackageManager.cs @@ -34,13 +34,19 @@ namespace PMF.Managers var json = JsonConvert.SerializeObject(PackageManager.PackageList); File.WriteAllText(Config.ManifestFileName, json); 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)) File.Create(Config.ManifestFileName).Close(); + + PMF.InvokePackageMessageEvent("Successfully validated manifest file"); } /// @@ -48,10 +54,11 @@ namespace PMF.Managers /// /// The id of the package /// This value is defined if the package exists, its contents will be the actual package - /// The directory which the package is installed + /// The directory in which the package is installed /// True if package is installed, false otherwise - 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; packageDirectory = Path.Combine(Config.PackageInstallationFolder, id); @@ -61,10 +68,13 @@ namespace PMF.Managers try { package = PackageManager.PackageList.GetPackage(id); + PMF.InvokePackageMessageEvent($"Found {id} with version {package.Assets[0].Version}"); return true; } catch { + if (reportInexistence) + PMF.InvokePackageMessageEvent($"Couldn't find {id}"); return false; } } @@ -76,8 +86,9 @@ namespace PMF.Managers /// True if uninstalled correctly, false otherwise public static bool RemovePackage(string id) { + PMF.InvokePackageMessageEvent($"Removing {id}"); if (string.IsNullOrEmpty(id)) - throw new ArgumentNullException(); + throw new ArgumentNullException("Package id must be defined"); try { @@ -99,11 +110,14 @@ namespace PMF.Managers /// The package which is to be installed /// The version of the asset being installed /// The path to the zip file that is to be installed - /// The package that was installed - public static Package InstallPackage(Package remotePackage, Asset asset, string zipPath) + /// State of the installation + 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)); + PMF.InvokePackageMessageEvent($"Finished extracting package {remotePackage.ID}"); + + bool error = false; // Maybe a library folder and check if is installed foreach (var dependency in asset.Dependencies) @@ -112,6 +126,7 @@ namespace PMF.Managers { 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)); + PMF.InvokePackageMessageEvent($"Finished extracting dependency {dependency.ID}"); } else // DependencyType.Package { @@ -126,14 +141,17 @@ namespace PMF.Managers else if (success == PackageState.VersionNotFound) { PMF.InvokePackageMessageEvent("Asset not found"); + error = true; } else if (success == PackageState.NotExisting) { PMF.InvokePackageMessageEvent("Package not found"); + error = true; } else { PMF.InvokePackageMessageEvent($"Something went wrong installing dependency with id: {dependency.ID}"); + error = true; } } } @@ -143,9 +161,12 @@ namespace PMF.Managers 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; } } } diff --git a/PMF/src/Managers/PackageManager.cs b/PMF/src/Managers/PackageManager.cs index b799937..09039c9 100644 --- a/PMF/src/Managers/PackageManager.cs +++ b/PMF/src/Managers/PackageManager.cs @@ -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"); - Environment.Exit(0); + if (!initialized) + { + PMF.InvokePackageMessageEvent("You must initialize PMF first before using it.\nClosing"); + Environment.Exit(0); + } } /// @@ -65,11 +68,10 @@ namespace PMF.Managers /// /// The id of the package /// The version of the asset - /// true Installation successful, false already installed + /// State of the package public static PackageState InstallPackage(Package package, Asset asset) { - if (!initialized) - notInitialized(); + checkInitialization(); string zipFile = RemotePackageManager.DownloadAsset(package.ID, asset); LocalPackageManager.InstallPackage(package, asset, zipFile); @@ -87,11 +89,10 @@ namespace PMF.Managers { package = null; - if (!initialized) - notInitialized(); + checkInitialization(); // 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); @@ -118,16 +119,15 @@ namespace PMF.Managers /// /// The id of the package /// The package that was installed - /// true Installation successful, false already installed + /// State of the package public static PackageState InstallLatest(string id, out Package package) { package = null; - if (!initialized) - notInitialized(); + checkInitialization(); // 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 Package remotePackage = RemotePackageManager.GetPackageInfo(id); @@ -135,7 +135,7 @@ namespace PMF.Managers if (remotePackage == null) return PackageState.NotExisting; - Asset asset = RemotePackageManager.GetAssetLatestVersion(package); + Asset asset = package.GetAssetLatestVersion(); if (asset == null) return PackageState.VersionNotFound; @@ -155,23 +155,22 @@ namespace PMF.Managers /// /// The id of the package /// The package that was installed - /// true update succes, false update failed or cancelled + /// State of the package public static PackageState InstallBySdkVersion(string id, out Package package) { package = null; - if (!initialized) - notInitialized(); + checkInitialization(); // 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); if (remotePackage == null) return PackageState.NotExisting; - Asset asset = RemotePackageManager.GetAssetLatestVersionBySdkVersion(remotePackage); + Asset asset = remotePackage.GetAssetLatestVersionBySdkVersion(); if (asset == null) return PackageState.VersionNotFound; @@ -193,8 +192,7 @@ namespace PMF.Managers /// True if success, false otherwise public static bool Uninstall(string id) { - if (!initialized) - notInitialized(); + checkInitialization(); return LocalPackageManager.RemovePackage(id); } @@ -204,23 +202,22 @@ namespace PMF.Managers /// /// The id of the package /// The package that was installed - /// true update succes, false update failed or cancelled + /// State of the package public static PackageState UpdateLatest(string id, out Package package) { package = null; - if (!initialized) - notInitialized(); + checkInitialization(); // 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); if (remotePackage == null) return PackageState.NotExisting; - var asset = RemotePackageManager.GetAssetLatestVersion(remotePackage); + var asset = remotePackage.GetAssetLatestVersion(); // You already have the latest version if (localPackage.Assets[0].Version == asset.Version) @@ -241,16 +238,15 @@ namespace PMF.Managers /// /// The id of the package /// The package that was installed - /// True update success, false update failed or cancelled + /// State of the package public static PackageState UpdatePackage(string id, Version version, out Package package) { package = null; - if (!initialized) - notInitialized(); + checkInitialization(); // 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 if (localPackage.Assets[0].Version == version) @@ -283,15 +279,14 @@ namespace PMF.Managers /// /// The id of the package /// The package that was installed - /// True if update success, false if package is not installed + /// State of the package public static PackageState UpdateBySdkVersion(string id, out Package package) { package = null; - if (!initialized) - notInitialized(); + checkInitialization(); - 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"); return PackageState.NotInstalled; @@ -302,7 +297,7 @@ namespace PMF.Managers if (remotePackage == null) return PackageState.NotExisting; - var asset = RemotePackageManager.GetAssetLatestVersionBySdkVersion(remotePackage); + var asset = remotePackage.GetAssetLatestVersionBySdkVersion(); // doesn't exist for provided sdk version if (asset == null) diff --git a/PMF/src/Managers/RemotePackageManager.cs b/PMF/src/Managers/RemotePackageManager.cs index 308283e..305430d 100644 --- a/PMF/src/Managers/RemotePackageManager.cs +++ b/PMF/src/Managers/RemotePackageManager.cs @@ -8,10 +8,10 @@ namespace PMF.Managers internal static class RemotePackageManager { /// - /// 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 /// /// The id of the package - /// The package object downloaded + /// The package downloaded public static Package GetPackageInfo(string id) { try @@ -36,7 +36,7 @@ namespace PMF.Managers /// /// The id of the package /// The asset that is to be downloaded - /// The zip file which was downloaded + /// The zip file path which was downloaded public static string DownloadAsset(string id, Asset asset) { using (WebClient client = new WebClient()) @@ -57,53 +57,5 @@ namespace PMF.Managers return zipPath; } } - - /// - /// Gets you the latest version of a package - /// - /// The package object to get the latest version - /// The latest asset version of a given package - 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; - } - - /// - /// Gets you the latest version of a package given an SDK version - /// - /// The package object to get the asset - /// The latest asset version of a given package and given SDK version - 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; - } } } diff --git a/PMF/src/Package/Asset.cs b/PMF/src/Package/Asset.cs index e5f612b..8f1f22d 100644 --- a/PMF/src/Package/Asset.cs +++ b/PMF/src/Package/Asset.cs @@ -13,7 +13,7 @@ namespace PMF /// /// The version of this asset /// - [JsonConverter(typeof(VersionConverter))] // This ensures the version object is correctly converted + [JsonConverter(typeof(VersionConverter))] public Version Version { get; set; } /// diff --git a/PMF/src/Package/Dependency.cs b/PMF/src/Package/Dependency.cs index 9da39b8..4a3a651 100644 --- a/PMF/src/Package/Dependency.cs +++ b/PMF/src/Package/Dependency.cs @@ -22,13 +22,13 @@ namespace PMF /// /// Type of dependency /// - [JsonConverter(typeof(StringEnumConverter))] // This converts enum to string and vice versa when generating or parsing json + [JsonConverter(typeof(StringEnumConverter))] public DependencyType Type { get; set; } /// /// Version of the dependency if type is Package /// - [JsonConverter(typeof(VersionConverter))] // This ensures the version object is correctly converted + [JsonConverter(typeof(VersionConverter))] public Version Version { get; set; } /// diff --git a/PMF/src/Package/Package.cs b/PMF/src/Package/Package.cs index 3fb18a7..5ad5e8c 100644 --- a/PMF/src/Package/Package.cs +++ b/PMF/src/Package/Package.cs @@ -15,7 +15,7 @@ namespace PMF /// /// The type of this package /// - [JsonConverter(typeof(StringEnumConverter))] // This converts enum to string and vice versa when generating or parsing json + [JsonConverter(typeof(StringEnumConverter))] public PackageType Type { get; set; } /// @@ -57,6 +57,49 @@ namespace PMF return null; } + /// + /// Gets you the latest version of a package + /// + /// The package object to get the latest version + /// The latest asset version of a given package + 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; + } + + /// + /// Gets you the latest version of a package given an SDK version + /// + /// The package object to get the asset + /// The latest asset version of a given package and given SDK version + 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: // - an id // - a type diff --git a/README.md b/README.md index 90fe028..eea0592 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ This is the basic information a package would have ```json { "ID": "something_cool", - "Type": "Plugin", + "Type": "Package", "Name": "Im Something cool", "Description": "This is a package that makes you cool and awesome", "Assets":