public static InstalledPackage InstallLocalPackage(string packagePath, ProjectPackMan openProject, string _namespace, out LogDataList log, bool ignoreClash = false, bool alreadyCopiedToTemp = false, bool procEvents = true, bool skipFileExistenceCheck = false)
            {
                log = new LogDataList();

                if (string.IsNullOrWhiteSpace(packagePath))
                {
                    try
                    {
                        throw new ArgumentNullException(ExceptionMessages.General.PACK_PATH_ARG_NULL);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteErrorLog(InstallerMessages.Error.PACKAGE_PATH_NULL, _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                        throw;
                    }
                }

                if (openProject == null || string.IsNullOrWhiteSpace(openProject.DirectoryPath))
                {
                    try
                    {
                        throw new ArgumentException(ExceptionMessages.General.OPEN_PROJ_ARG_OR_DIRPATH_NULL);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteErrorLog(InstallerMessages.Error.NO_OPEN_PROJECT, _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                        throw;
                    }
                }

                Logger.WriteInformationLog(InstallerMessages.Information.PACKAGE_INSTALL_START_L + packagePath + ".", _namespace);
                Exception   outEx;
                LogDataList outLog         = null;
                string      oldPackagePath = packagePath;

                try
                {
                    if (!alreadyCopiedToTemp)
                    {
                        packagePath = InitPackageInstaller(packagePath, false, _namespace, out outLog);
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteErrorLog(InstallerMessages.Error.INIT_ERROR + packagePath + ".", _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                    if (procEvents)
                    {
                        CleanupTempInstallDir(_namespace, false);
                    }
                    throw;
                }
                log.AppendLogs(outLog);

                RMPackage package = null;

                try
                {
                    package = new RMPackage(packagePath, _namespace, out outLog);
                }
                catch (Exception ex)
                {
                    log.AppendLogs(outLog);
                    Logger.WriteErrorLog(InstallerMessages.Error.XML_READ_ERROR + packagePath + ".", _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                    if (procEvents)
                    {
                        CleanupTempInstallDir(_namespace, false);
                    }
                    throw;
                }
                log.AppendLogs(outLog);

                if (!skipFileExistenceCheck)
                {
                    PerformPackageFilesExistenceCheck(package, _namespace, out outLog);
                    if (outLog != null)
                    {
                        log.Logs.AddRange(outLog.Logs);
                    }
                }

                if (!ignoreClash && openProject.InstalledPackages.FindByUID(package.UniqueID) != null)
                {
                    try
                    {
                        throw new PackageAlreadyExistsException(ExceptionMessages.General.PackWIDExists(package.UniqueID));
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteErrorLog(InstallerMessages.Error.PACKAGE_ALREADY_EXISTS, _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                        if (procEvents)
                        {
                            CleanupTempInstallDir(_namespace, false);
                        }
                        throw;
                    }
                }

                if (procEvents && OnPackageInstallBegin != null)
                {
                    OnPackageInstallBegin.Invoke(package, false, oldPackagePath, openProject);
                }

                try
                {
                    PerformInstallLocal(package, openProject.DirectoryPath, _namespace);
                }
                catch (Exception ex)
                {
                    Logger.WriteErrorLog(InstallerMessages.Error.INSTALL_PACK_FAILED, _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                    if (procEvents)
                    {
                        CleanupTempInstallDir(_namespace, false);
                    }
                    throw;
                }

                string packManStorePath = openProject.DirectoryPath + "\\" + Vars.PACKAGE_MANAGER_DIRECTORY;

                if (!Directory.Exists(packManStorePath) && Helper.CreateFolderSafely(packManStorePath, _namespace, out outEx, LoggerMessages.GeneralError.CREATE_REQUIRED_DIR_FAILED_ARG) == CreateFolderResult.UserCancelled)
                {
                    throw outEx;
                }
                InstalledPackage newInstall = null;

                try
                {
                    newInstall = CreateLocalInstalledFile(package, Path.GetDirectoryName(packagePath), packManStorePath, _namespace, out outLog);
                    openProject.InstalledPackages.AddSafely(newInstall);
                }
                catch
                {
                    if (procEvents)
                    {
                        CleanupTempInstallDir(_namespace, false);
                    }
                    throw;
                }
                if (outLog != null)
                {
                    log.Logs.AddRange(outLog.Logs);
                }

                if (!alreadyCopiedToTemp)
                {
                    CleanupTempInstallDir(_namespace, false);
                }

                if (procEvents && OnPackageInstalled != null)
                {
                    OnPackageInstalled.Invoke(package, false, oldPackagePath, openProject);
                }

                Logger.WriteInformationLog(InstallerMessages.Information.PACKAGE_INSTALL_DONE_L, _namespace);
                return(newInstall);
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempt to install a GUID from a GitHub web package, if such a package can be found.
        /// </summary>
        /// <param name="guid">The guid to try to install.</param>
        /// <param name="andEnable">Should the package be enabled after installing? Otherwise it will be disabled.</param>
        /// <returns><see langword="true"/> if successful, otherwise <see langword="false"/></returns>
        public static bool TryInstallWebPackage(string guid, bool andEnable = true)
        {
            if (OutwardHelper.IsOutwardRunning())
            {
                MessageBox.Show("You need to close Outward to do that.");
                return(false);
            }

            if (!Folders.IsCurrentOutwardPathValid())
            {
                Console.WriteLine("Outward folder is not set! Cannot install package.");
                return(false);
            }

            WebManifestManager.s_webManifests.TryGetValue(guid, out PackageManifest webManifest);

            if (webManifest == null)
            {
                if (!string.IsNullOrEmpty(guid) && TryGetInstalledPackage(guid) != null)
                {
                    Console.WriteLine("Could not find online package by GUID '" + guid + ", but an installed package exists with that GUID, enabling.");
                    TryEnablePackage(guid);
                    return(true);
                }

                Console.WriteLine("Could not find web package by GUID '" + guid + "'");
                return(false);
            }

            var existing = TryGetInstalledPackage(guid);

            if (existing != null && existing.CompareVersionAgainst(webManifest) != InstallState.Outdated)
            {
                //Console.WriteLine("Installed package is already greater or equal version, skipping install.");

                if (existing.IsDisabled)
                {
                    return(TryEnablePackage(guid));
                }

                return(true);
            }

            if (webManifest.dependencies != null && webManifest.dependencies.Length > 0)
            {
                int i     = 1;
                int count = webManifest.dependencies.Length;
                foreach (var dependency in webManifest.dependencies)
                {
                    MefinoGUI.SetProgressMessage($"Downloading dependency {i} of {count}: {dependency}");

                    if (!TryInstallWebPackage(dependency))
                    {
                        var msgResult = MessageBox.Show("Installing dependency '" + dependency + "' failed!\n\nContinue anyway?", "Dependency failed!", MessageBoxButtons.OKCancel);
                        if (msgResult == DialogResult.OK)
                        {
                            continue;
                        }
                        else
                        {
                            return(false);
                        }
                    }

                    i++;
                }
            }

            // check that the package itself wasn't one of the dependencies.
            existing = TryGetInstalledPackage(guid);
            if (existing != null && existing.CompareVersionAgainst(webManifest) != InstallState.Outdated)
            {
                return(true);
            }

            MefinoGUI.SetProgressMessage($"Downloading package '{webManifest.GUID}'");
            MefinoApp.SendAsyncProgress(0);

            if (!DownloadAndInstallPackage(webManifest))
            {
                MessageBox.Show("Failed to download and install " + guid + "!");
                RefreshInstalledPackages();
                return(false);
            }

            RefreshInstalledPackages();

            OnPackageInstalled?.Invoke(webManifest);

            if (andEnable)
            {
                return(TryEnablePackage(guid));
            }
            else
            {
                return(true);
            }
        }
            // -------- Global Packages -------- //
            public static InstalledPackage InstallGlobalPackage(string packagePath, string _namespace, out LogDataList log, bool ignoreClash = false, bool alreadyCopiedToTemp = false, bool procEvents = true, bool skipFileExistenceCheck = false)
            {
                Logger.WriteInformationLog(InstallerMessages.Information.PACKAGE_INSTALL_START_G + packagePath + ".", _namespace);
                log = new LogDataList();
                string      oldPackagePath = packagePath;
                LogDataList outLog         = null;

                try
                {
                    if (!alreadyCopiedToTemp)
                    {
                        packagePath = packagePath = InitPackageInstaller(packagePath, true, _namespace, out outLog);
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteErrorLog(InstallerMessages.Error.INIT_ERROR + packagePath + ".", _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                    if (procEvents)
                    {
                        CleanupTempInstallDir(_namespace, false);
                    }
                    throw;
                }
                log.AppendLogs(outLog);

                RMPackage package = null;

                try
                {
                    package = new RMPackage(packagePath, _namespace, out outLog);
                }
                catch (Exception ex)
                {
                    log.AppendLogs(outLog);
                    Logger.WriteErrorLog(InstallerMessages.Error.XML_READ_ERROR + packagePath + ".", _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                    if (procEvents)
                    {
                        CleanupTempInstallDir(_namespace, false);
                    }
                    throw;
                }
                log.AppendLogs(outLog);

                if (!skipFileExistenceCheck)
                {
                    PerformPackageFilesExistenceCheck(package, _namespace, out outLog);
                    log.AppendLogs(outLog);
                }

                if (!ignoreClash && GlobalPackages.FindByUID(package.UniqueID) != null)
                {
                    try
                    {
                        throw new PackageAlreadyExistsException(ExceptionMessages.General.PackWIDExists(package.UniqueID));
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteErrorLog(InstallerMessages.Error.PACKAGE_ALREADY_EXISTS, _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                        if (procEvents)
                        {
                            CleanupTempInstallDir(_namespace, false);
                        }
                        throw;
                    }
                }

                if (procEvents && OnPackageInstallBegin != null)
                {
                    OnPackageInstallBegin.Invoke(package, true, oldPackagePath, null);
                }

                try
                {
                    PerformInstallGlobal(package, _namespace);
                }
                catch (Exception ex)
                {
                    Logger.WriteErrorLog(InstallerMessages.Error.INSTALL_PACK_FAILED, _namespace, ex, BasicDebugLogger.DebugErrorType.Error);
                    if (procEvents)
                    {
                        CleanupTempInstallDir(_namespace, false);
                    }
                    throw;
                }

                InstalledPackage newInstall = null;

                try
                {
                    newInstall = CreateGlobalInstalledFile(package, Path.GetDirectoryName(packagePath), _namespace, out outLog);
                    GlobalPackages.AddSafely(newInstall);
                }
                catch
                {
                    if (procEvents)
                    {
                        CleanupTempInstallDir(_namespace, false);
                    }
                    throw;
                }
                log.AppendLogs(outLog);

                if (!alreadyCopiedToTemp)
                {
                    CleanupTempInstallDir(_namespace, false);
                }

                if (OnPackageInstalled != null)
                {
                    OnPackageInstalled.Invoke(package, true, oldPackagePath, null);
                }
                Logger.WriteInformationLog(InstallerMessages.Information.PACKAGE_INSTALL_DONE_G, _namespace);
                return(newInstall);
            }