/// <summary> /// This downloads and installs a single addin to the Addins folder. /// Note the addin still needs to be in initialized with: /// OnApplicationStart() and InializeAddinUi() /// /// The addin-loader then moves the files. /// </summary> /// <param name="url"></param> /// <param name="targetFolder">Addins folder</param> /// <param name="addin"></param> /// <returns></returns> public DownloadAndInstallResult DownloadAndInstallAddin(string url, string targetFolder, AddinItem addin) { var result = new DownloadAndInstallResult() { ExistingAddin = false }; if (string.IsNullOrEmpty(targetFolder)) { ErrorMessage = "No target folder provided"; result.IsError = true; return(result); } string ver = mmApp.GetVersion(); if (ver.CompareTo(addin.minVersion) < 0) { ErrorMessage = "This addin requires v" + addin.minVersion + " of Markdown Monster to run. You are on v" + ver + ".\r\n\r\nPlease update to the latest version of Markdown Monster if you want to install this addin."; result.IsError = true; return(result); } var addinName = Path.GetFileName(targetFolder); if (!Directory.Exists(Path.Combine(targetFolder, addin.id))) { targetFolder = Path.Combine(targetFolder, addin.id); } else { result.ExistingAddin = true; targetFolder = Path.Combine(targetFolder, "Install", addin.id); } string file = Path.GetTempFileName(); file = Path.ChangeExtension(file, "zip"); try { using (WebClient client = new WebClient()) { client.DownloadFile(url, file); } if (Directory.Exists(targetFolder)) { Directory.Delete(targetFolder, true); } using (ZipArchive archive = ZipFile.OpenRead(file)) { foreach (ZipArchiveEntry zipfile in archive.Entries) { string fullName = Path.Combine(targetFolder, zipfile.FullName); //Calculates what the new full path for the unzipped file should be string fullPath = Path.GetDirectoryName(fullName); //Creates the directory (if it doesn't exist) for the new path Directory.CreateDirectory(fullPath); //Extracts the file to (potentially new) path zipfile.ExtractToFile(fullName, true); } } return(result); } catch (Exception ex) { this.ErrorMessage = ex.Message; result.IsError = true; return(result); } }
/// <summary> /// This downloads and installs a single addin to the Addins folder. /// Note the addin still needs to be in initialized with: /// OnApplicationStart() and InializeAddinUi() /// /// The addin-loader then moves the files. /// </summary> /// <param name="url"></param> /// <param name="targetFolder">Addins folder</param> /// <param name="addin"></param> /// <returns></returns> public DownloadAndInstallResult DownloadAndInstallAddin(string url, string targetFolder, AddinItem addin) { var result = new DownloadAndInstallResult() { ExistingAddin = false }; if (string.IsNullOrEmpty(targetFolder)) { ErrorMessage = "No target folder provided"; result.IsError = true; return(result); } string ver = mmApp.GetVersion(); var verInstalled = new Version(ver); var verMinVersion = new Version(addin.minVersion); if (verInstalled.CompareTo(verMinVersion) < 0) { ErrorMessage = "This addin requires v" + addin.minVersion + " of Markdown Monster to run. You are on v" + ver + ".\r\n\r\nPlease update to the latest version of Markdown Monster if you want to install this addin."; result.IsError = true; return(result); } var addinName = Path.GetFileName(targetFolder); if (!Directory.Exists(Path.Combine(targetFolder, addin.id))) { targetFolder = Path.Combine(targetFolder, addin.id); } else { result.ExistingAddin = true; targetFolder = Path.Combine(targetFolder, "Install", addin.id); } string file = Path.GetTempFileName(); file = Path.ChangeExtension(file, "zip"); try { using (WebClient client = new WebClient()) { client.DownloadFile(url, file); } if (Directory.Exists(targetFolder)) { Directory.Delete(targetFolder, true); } using (ZipArchive archive = ZipFile.OpenRead(file)) { foreach (ZipArchiveEntry zipfile in archive.Entries) { string fullName = Path.Combine(targetFolder, zipfile.FullName); //Calculates what the new full path for the unzipped file should be string fullPath = Path.GetDirectoryName(fullName); //Creates the directory (if it doesn't exist) for the new path Directory.CreateDirectory(fullPath); //Extracts the file to (potentially new) path zipfile.ExtractToFile(fullName, true); } } addin.isInstalled = true; addin.isEnabled = true; addin.installedVersion = addin.version; result.NeedsRestart = false; if (!result.ExistingAddin) { if (!InstallAddin(addin.id)) { ErrorMessage = addin.name + " installation failed."; } else { var installedAddin = AddIns.FirstOrDefault(ai => ai.Id == addin.id); if (installedAddin != null) { installedAddin.OnApplicationStart(); // trigger parser addins to refresh MarkdownParsers if they are provided if (installedAddin.GetMarkdownParser(false, false) != null) { mmApp.Model.OnPropertyChanged(nameof(AppModel.MarkdownParserColumnWidth)); } InitializeAddinsUi(mmApp.Model.Window, new List <MarkdownMonsterAddin>() { installedAddin }); } } } else { result.NeedsRestart = true; } return(result); } catch (Exception ex) { ErrorMessage = ex.Message; result.IsError = true; return(result); } }