Ejemplo n.º 1
0
        /// <summary>
        /// Install button click event
        /// </summary>
        /// <param name="asset"></param>
        /// <param name="version"></param>
        /// <returns>If "cencel" returned to webclient halt the installation. If not cancel then this method returns the | delimitted list of package and its version to be installed</returns>
        /// TODO: Rename this method to OnInstallPackage
        public string PackageOnExecuted(dynamic asset, dynamic version)
        {
            string downloadPath = this.PackageInstallPath;

            List <Tuple <dynamic, dynamic> > packageVersionData = new List <Tuple <dynamic, dynamic> >();

            string msg = String.IsNullOrEmpty(downloadPath) ?
                         String.Format(Resources.MessageConfirmToInstallPackage, asset["asset_name"], version["version"]) :
                         String.Format(Resources.MessageConfirmToInstallPackageToFolder, asset["asset_name"], version["version"], downloadPath);

            var result = MessageBox.Show(msg,
                                         Resources.PackageDownloadConfirmMessageBoxTitle,
                                         MessageBoxButton.OKCancel, MessageBoxImage.Question);

            if (PackagesToInstall == null)
            {
                PackagesToInstall = new List <string>();
            }
            else
            {
                PackagesToInstall.Clear();
            }

            if (result == MessageBoxResult.OK)
            {
                if (!string.IsNullOrEmpty(version["dependencies"]))
                {
                    GetDependencies(version, out packageVersionData);
                }

                //Add selected package to the list of packges to install
                PackagesToInstall.Add(string.Format("{0},{1},{2}", asset["asset_id"], version["file_id"], asset["asset_name"]));

                // determine if any of the packages contain binaries or python scripts.
                result = CheckForBinariesPythonScripts(version, packageVersionData);
                if (result == MessageBoxResult.Cancel)
                {
                    return("cancel");
                }

                result = CheckForNewerDynamoVersion(packageVersionData);
                if (result == MessageBoxResult.Cancel)
                {
                    return("cancel");
                }


                var localPkgs = Loader.LocalPackages;

                var uninstallsRequiringRestart          = new List <Package>();
                var uninstallRequiringUserModifications = new List <Package>();
                var immediateUninstalls = new List <Package>();

                // if a package is already installed we need to uninstall it, allowing
                // the user to cancel if they do not want to uninstall the package
                foreach (var localPkg in packageVersionData.Select(x => localPkgs.FirstOrDefault(v => v.Name == x.Item1.asset_name.ToString())))
                {
                    if (localPkg == null)
                    {
                        continue;
                    }

                    if (localPkg.LoadedAssemblies.Any())
                    {
                        uninstallsRequiringRestart.Add(localPkg);
                        continue;
                    }

                    if (localPkg.InUse(Model))
                    {
                        uninstallRequiringUserModifications.Add(localPkg);
                        continue;
                    }

                    immediateUninstalls.Add(localPkg);
                }

                if (uninstallRequiringUserModifications.Any())
                {
                    MessageBox.Show(String.Format(Resources.MessageUninstallToContinue,
                                                  ProductName,
                                                  JoinPackageNames(uninstallRequiringUserModifications)),
                                    Resources.CannotDownloadPackageMessageBoxTitle,
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                    return("cancel");
                }

                var settings = Model.PreferenceSettings;

                if (uninstallsRequiringRestart.Any())
                {
                    // mark for uninstallation
                    uninstallsRequiringRestart.ForEach(
                        x => x.MarkForUninstall(settings));

                    MessageBox.Show(String.Format(Resources.MessageUninstallToContinue2,
                                                  ProductName,
                                                  JoinPackageNames(uninstallsRequiringRestart)),
                                    Resources.CannotDownloadPackageMessageBoxTitle,
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                    return("cancel");
                }

                if (immediateUninstalls.Any())
                {
                    // if the package is not in use, tell the user we will be uninstall it and give them the opportunity to cancel
                    if (MessageBox.Show(String.Format(Resources.MessageAlreadyInstallDynamo,
                                                      ProductName,
                                                      JoinPackageNames(immediateUninstalls)),
                                        Resources.DownloadWarningMessageBoxTitle,
                                        MessageBoxButton.OKCancel, MessageBoxImage.Warning) == MessageBoxResult.Cancel)
                    {
                        return("cancel");
                    }
                }

                // add custom path to custom package folder list
                if (!String.IsNullOrEmpty(downloadPath))
                {
                    if (!settings.CustomPackageFolders.Contains(downloadPath))
                    {
                        settings.CustomPackageFolders.Add(downloadPath);
                    }
                }
            }
            else
            {
                return("cancel");
            }
            return(String.Join("|", PackagesToInstall));
        }