public bool PerformInstallation(PluginUpdates updates, Form form)
        {
            if (updates.Plugins.Any(p => p.RequireRestart))
            {
                XmlSerializerHelper.SerializeToFile(updates, Path.Combine(Paths.XrmToolBoxPath, "Update.xml"));

                if (form is StoreFormFromPortal storeForm)
                {
                    form.Invoke(new Action(() =>
                    {
                        if (DialogResult.Yes == MessageBox.Show(form,
                                                                @"This application needs to restart to install updated tools (or new tools that share some files with already installed tools). Click Yes to restart this application now",
                                                                @"Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                        {
                            storeForm.AskForPluginsClosing();

                            Application.Restart();
                        }
                    }));
                }

                return(false);
            }

            foreach (var pu in updates.Plugins)
            {
                try
                {
                    // Can install plugin directly
                    var destinationDirectory = Path.GetDirectoryName(pu.Destination);
                    if (destinationDirectory == null)
                    {
                        continue;
                    }

                    if (!Directory.Exists(destinationDirectory))
                    {
                        Directory.CreateDirectory(destinationDirectory);
                    }
                    File.Copy(pu.Source, pu.Destination, true);
                }
                catch (Exception error)
                {
                    MessageBox.Show("An error occured while copying files: " + error.Message +
                                    "\r\n\r\nCopy has been aborted", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
            }

            PluginsUpdated?.Invoke(this, new EventArgs());
            return(true);
        }
Example #2
0
        public bool PrepareConnectionControlsUpdate(Control parentControl, bool installOnNextRestart, out string version)
        {
            var nugetPlugin = manager.SourceRepository.FindPackage("MscrmTools.Xrm.Connection", new VersionSpec(), AllowConnectionControlPreRelease, false);

            manager.InstallPackage(nugetPlugin, true, false);

            var packageFolder = Path.Combine(nugetPluginsFolder, $"{nugetPlugin.Id}.{nugetPlugin.Version}");

            var currentLocation = Assembly.GetExecutingAssembly().Location;
            var folder          = Path.GetDirectoryName(currentLocation);

            var updates = new PluginUpdates {
                PreviousProcessId = Process.GetCurrentProcess().Id
            };

            updates.Plugins.Add(new PluginUpdate
            {
                Source         = Path.Combine(packageFolder, "lib\\net462\\McTools.Xrm.Connection.dll"),
                Destination    = Path.Combine(folder, "McTools.Xrm.Connection.dll"),
                RequireRestart = true
            });
            updates.Plugins.Add(new PluginUpdate
            {
                Source         = Path.Combine(packageFolder, "lib\\net462\\McTools.Xrm.Connection.WinForms.dll"),
                Destination    = Path.Combine(folder, "McTools.Xrm.Connection.WinForms.dll"),
                RequireRestart = true
            });

            XmlSerializerHelper.SerializeToFile(updates, Path.Combine(Paths.XrmToolBoxPath, "Update.xml"));
            bool returnedValue = false;

            parentControl.Invoke(new Action(() =>
            {
                if (!installOnNextRestart && DialogResult.Yes == MessageBox.Show(parentControl,
                                                                                 @"This application needs to restart to install new connection controls. Click Yes to restart this application now",
                                                                                 @"Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                {
                    returnedValue = true;
                }
            }));

            version = nugetPlugin.Version.Version + (!string.IsNullOrEmpty(nugetPlugin.Version.SpecialVersion) ? "-" + nugetPlugin.Version.SpecialVersion : "");

            return(returnedValue);
        }
Example #3
0
        public void PerformInstallation(PluginUpdates updates)
        {
            if (updates.Plugins.Any(p => p.RequireRestart))
            {
                XmlSerializerHelper.SerializeToFile(updates, Path.Combine(applicationDataFolder, "Update.xml"));

                if (DialogResult.Yes == MessageBox.Show(
                        "This application needs to restart to install updated plugins (or new plugins that share some files with already installed plugins). Click Yes to restart this application now",
                        "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                {
                    Application.Restart();
                }
            }
            else
            {
                foreach (var pu in updates.Plugins)
                {
                    try
                    {
                        // Can install plugin directly
                        var destinationDirectory = Path.GetDirectoryName(pu.Destination);
                        if (!Directory.Exists(destinationDirectory))
                        {
                            Directory.CreateDirectory(destinationDirectory);
                        }
                        File.Copy(pu.Source, pu.Destination, true);
                    }
                    catch (Exception error)
                    {
                        MessageBox.Show(this,
                                        "An error occured while copying files: " + error.Message +
                                        "\r\n\r\nCopy has been aborted", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

                PluginsUpdated?.Invoke(this, new EventArgs());

                // Refresh plugins list when installation is done
                RefreshPluginsList();
                CalculateCacheFolderSize();
                MessageBox.Show("Installation done!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public async Task <PluginUpdates> PrepareInstallationPackages(List <XtbPlugin> pluginsToInstall)
        {
            var pus = new PluginUpdates {
                PreviousProcessId = Process.GetCurrentProcess().Id
            };
            int i = 0;

            foreach (var plugin in pluginsToInstall)
            {
                OnDownloadingTool?.Invoke(this, new ToolInformationEventArgs {
                    ToolName = plugin.Name, ProgressPercentage = i * 100 / pluginsToInstall.Count
                });
                i++;

                var version = new NuGetVersion(plugin.Version);

                using (MemoryStream packageStream = new MemoryStream())
                {
                    if (!await findPackageById.CopyNupkgToStreamAsync(
                            plugin.NugetId.ToLower(),
                            version,
                            packageStream,
                            cache,
                            logger,
                            cancellationToken))
                    {
                        throw new Exception($"The Nuget package for tool {plugin.NugetId} ({version}) has not been found");
                    }

                    if (plugin.Action == PackageInstallAction.Unavailable)
                    {
                        if (!string.IsNullOrEmpty(plugin.ProjectUrl))
                        {
                            var message =
                                $"{plugin.Name} is incompatible with this version of XrmToolBox.\nOpen project URL?";
                            if (DialogResult.Yes == MessageBox.Show(message, "Incompatible tool", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
                            {
                                Process.Start(plugin.ProjectUrl);
                            }
                        }
                        else
                        {
                            MessageBox.Show(
                                $"{plugin.Name} is incompatible with this version of XrmToolBox.",
                                "Incompatible tool", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        continue;
                    }

                    var packageFolder = Path.Combine(nugetPluginsFolder, $"{plugin.NugetId}.{version.Version}");

                    using (PackageArchiveReader packageReader = new PackageArchiveReader(packageStream))
                    {
                        foreach (var packageFile in await packageReader.GetFilesAsync(cancellationToken))
                        {
                            if (packageFile.ToLower().IndexOf("/plugins/") > 0)
                            {
                                if (!Directory.Exists(packageFolder))
                                {
                                    Directory.CreateDirectory(packageFolder);
                                }

                                var relativeFilePath = packageFile.Remove(0, packageFile.ToLower().IndexOf("/plugins/") + 9);
                                var filePath         = Path.Combine(packageFolder, relativeFilePath);
                                var fi = new FileInfo(filePath);

                                if (!Directory.Exists(fi.Directory.FullName))
                                {
                                    Directory.CreateDirectory(fi.Directory.FullName);
                                }

                                using (var fileStream = File.OpenWrite(filePath))
                                    using (var stream = await packageReader.GetStreamAsync(packageFile, cancellationToken))
                                    {
                                        await stream.CopyToAsync(fileStream);
                                    }

                                var destinationFile = Path.Combine(Paths.PluginsPath, relativeFilePath);

                                // XrmToolBox restart is required when a plugin has to be
                                // updated or when a new plugin shares files with other
                                // plugin(s) already installed
                                if (plugin.RequiresXtbRestart)
                                {
                                    pus.Plugins.Add(new PluginUpdate
                                    {
                                        Source         = filePath,
                                        Destination    = destinationFile,
                                        RequireRestart = true
                                    });
                                }
                                else if (plugin.Action == PackageInstallAction.Install)
                                {
                                    pus.Plugins.Add(new PluginUpdate
                                    {
                                        Source         = filePath,
                                        Destination    = destinationFile,
                                        RequireRestart = false
                                    });
                                }
                            }
                        }
                    }
                }
            }

            return(pus);
        }
        public async Task <ConnectionControlsUpdateSettings> PrepareConnectionControlsUpdate(Control parentControl, bool installOnNextRestart)
        {
            var metadata     = (await packageSearch.SearchAsync("mscrmtools.xrm.connection", new SearchFilter(AllowConnectionControlPreRelease, SearchFilterType.IsLatestVersion), 0, 1, logger, cancellationToken)).FirstOrDefault();
            var nugetVersion = (await metadata.GetVersionsAsync()).Max(v => v.Version);
            var updates      = new PluginUpdates {
                PreviousProcessId = Process.GetCurrentProcess().Id
            };

            using (MemoryStream packageStream = new MemoryStream())
            {
                if (!await findPackageById.CopyNupkgToStreamAsync(
                        "mscrmtools.xrm.connection",
                        nugetVersion,
                        packageStream,
                        cache,
                        logger,
                        cancellationToken))
                {
                    throw new Exception($"The Nuget package for connection controls ({nugetVersion.Version}) has not been found");
                }

                var packageFolder   = Path.Combine(nugetPluginsFolder, $"mscrmtools.xrm.connection.{nugetVersion.Version}");
                var currentLocation = Assembly.GetExecutingAssembly().Location;
                var folder          = Path.GetDirectoryName(currentLocation);

                if (!Directory.Exists(packageFolder))
                {
                    Directory.CreateDirectory(packageFolder);
                }

                using (PackageArchiveReader packageReader = new PackageArchiveReader(packageStream))
                {
                    packageReader.NuspecReader.GetReleaseNotes();

                    foreach (var packageFile in await packageReader.GetFilesAsync(cancellationToken))
                    {
                        if (packageFile.ToLower().EndsWith("mctools.xrm.connection.dll") ||
                            packageFile.ToLower().EndsWith("mctools.xrm.connection.winforms.dll"))
                        {
                            using (var fileStream = File.OpenWrite(Path.Combine(packageFolder, Path.GetFileName(packageFile))))
                                using (var stream = await packageReader.GetStreamAsync(packageFile, cancellationToken))
                                {
                                    await stream.CopyToAsync(fileStream);
                                }
                        }
                    }
                }

                updates.Plugins.Add(new PluginUpdate
                {
                    Source         = Path.Combine(packageFolder, "McTools.Xrm.Connection.dll"),
                    Destination    = Path.Combine(folder, "McTools.Xrm.Connection.dll"),
                    RequireRestart = true
                });
                updates.Plugins.Add(new PluginUpdate
                {
                    Source         = Path.Combine(packageFolder, "McTools.Xrm.Connection.WinForms.dll"),
                    Destination    = Path.Combine(folder, "McTools.Xrm.Connection.WinForms.dll"),
                    RequireRestart = true
                });
            }

            XmlSerializerHelper.SerializeToFile(updates, Path.Combine(Paths.XrmToolBoxPath, "Update.xml"));

            bool returnedValue = false;

            parentControl.Invoke(new Action(() =>
            {
                if (!installOnNextRestart && DialogResult.Yes == MessageBox.Show(parentControl,
                                                                                 @"This application needs to restart to install new connection controls. Click Yes to restart this application now",
                                                                                 @"Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                {
                    returnedValue = true;
                }
            }));

            return(new ConnectionControlsUpdateSettings
            {
                RestartNow = returnedValue,
                Version = nugetVersion.Version + (!string.IsNullOrEmpty(nugetVersion.Release) ? "-" + nugetVersion.Release : "")
            });
        }
        public PluginUpdates PrepareInstallationPackages(List <XtbPlugin> pluginsToInstall, BackgroundWorker worker = null)
        {
            var pus = new PluginUpdates {
                PreviousProcessId = Process.GetCurrentProcess().Id
            };
            int i = 0;

            foreach (var plugin in pluginsToInstall)
            {
                i++;
                worker?.ReportProgress(i * 100 / pluginsToInstall.Count, plugin.Name);

                var nugetPlugin =
                    manager.SourceRepository.FindPackage(plugin.NugetId, new SemanticVersion(plugin.Version), false, false);

                if (nugetPlugin == null)
                {
                    continue;
                }

                if (plugin.Action == PackageInstallAction.Unavailable)
                {
                    if (!string.IsNullOrEmpty(plugin.ProjectUrl))
                    {
                        var message =
                            $"{plugin.Name} is incompatible with this version of XrmToolBox.\nOpen project URL?";
                        if (DialogResult.Yes == MessageBox.Show(message, "Incompatible plugin", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
                        {
                            Process.Start(plugin.ProjectUrl);
                        }
                    }
                    else
                    {
                        MessageBox.Show(
                            $"{plugin.Name} is incompatible with this version of XrmToolBox.",
                            "Incompatible plugin", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    continue;
                }

                manager.InstallPackage(nugetPlugin, true, false);

                var packageFolder = Path.Combine(nugetPluginsFolder, $"{nugetPlugin.Id}.{nugetPlugin.Version}");

                foreach (var fi in nugetPlugin.GetFiles())
                {
                    var destinationFile = Path.Combine(Paths.XrmToolBoxPath, fi.EffectivePath);

                    // XrmToolBox restart is required when a plugin has to be
                    // updated or when a new plugin shares files with other
                    // plugin(s) already installed
                    if (plugin.RequiresXtbRestart)
                    {
                        pus.Plugins.Add(new PluginUpdate
                        {
                            Source         = Path.Combine(packageFolder, fi.Path),
                            Destination    = destinationFile,
                            RequireRestart = true
                        });
                    }
                    else if (plugin.Action == PackageInstallAction.Install)
                    {
                        pus.Plugins.Add(new PluginUpdate
                        {
                            Source         = Path.Combine(packageFolder, fi.Path),
                            Destination    = destinationFile,
                            RequireRestart = false
                        });
                    }
                }
            }

            return(pus);
        }
Example #7
0
        public PluginUpdates PrepareInstallationPackages(List <XtbNuGetPackage> packages)
        {
            var pus = new PluginUpdates {
                PreviousProcessId = Process.GetCurrentProcess().Id
            };

            foreach (var xtbPackage in packages)
            {
                if (xtbPackage.Action == PackageInstallAction.Unavailable)
                {
                    if (xtbPackage.Package.ProjectUrl != null &&
                        !string.IsNullOrEmpty(xtbPackage.Package.ProjectUrl.ToString()))
                    {
                        if (DialogResult.Yes ==
                            MessageBox.Show(
                                $"{xtbPackage.Package.Title}\nis incompatible with this version of XrmToolBox.\nOpen project URL?",
                                "Incompatible tool", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation))
                        {
                            Process.Start(xtbPackage.Package.ProjectUrl.ToString());
                        }
                    }
                    else
                    {
                        MessageBox.Show(
                            $"{xtbPackage.Package.Title}\nis incompatible with this version of XrmToolBox.",
                            "Incompatible tool", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    continue;
                }
                manager.InstallPackage(xtbPackage.Package, true, false);

                var packageFolder = Path.Combine(nugetPluginsFolder,
                                                 xtbPackage.Package.Id + "." + xtbPackage.Package.Version);

                foreach (var fi in xtbPackage.Package.GetFiles())
                {
                    var destinationFile = Path.Combine(Paths.XrmToolBoxPath, fi.EffectivePath);

                    // XrmToolBox restart is required when a plugin has to be
                    // updated or when a new plugin shares files with other
                    // plugin(s) already installed
                    if (xtbPackage.RequiresXtbRestart)
                    {
                        pus.Plugins.Add(new PluginUpdate
                        {
                            Source         = Path.Combine(packageFolder, fi.Path),
                            Destination    = destinationFile,
                            RequireRestart = true
                        });
                    }
                    else if (xtbPackage.Action == PackageInstallAction.Install)
                    {
                        pus.Plugins.Add(new PluginUpdate
                        {
                            Source         = Path.Combine(packageFolder, fi.Path),
                            Destination    = destinationFile,
                            RequireRestart = false
                        });
                    }
                }
            }

            return(pus);
        }