/// <summary> /// Called when the release file is downloaded and extracted to updater.FolderUnzip /// </summary> /// <param name="updater"></param> private void On3PUpdate(GitHubUpdater updater) { // list all the files of the zip, they should be copied in the notepad++ /plugins/ folder by the updater foreach (var fullPath in Directory.EnumerateFiles(FolderUnzip, "*", SearchOption.TopDirectoryOnly)) { _3PUpdater.Instance.AddFileToMove(fullPath, Path.Combine(Path.GetDirectoryName(AssemblyInfo.Location) ?? "", Path.GetFileName(fullPath) ?? "")); } // write the version log Utils.FileWriteAllText(Config.UpdateVersionLog, updater.VersionLog.ToString(), Encoding.Default); Utils.FileWriteAllText(Config.UpdatePreviousVersion, AssemblyInfo.Version, Encoding.Default); }
/// <summary> /// check if the User Defined Language for "OpenEdgeABL" exists in the /// userDefineLang.xml file, if it does it updates it, if it doesn't exists it creates it and asks the user /// to restart Notepad++ /// Can also only check and not install it by setting onlyCheckInstall to true /// </summary> public static bool InstallUdl(bool onlyCheckInstall = false) { var encoding = TextEncodingDetect.GetFileEncoding(Config.FileNppUdlXml); var fileContent = File.Exists(Config.FileNppUdlXml) ? Utils.ReadAllText(Config.FileNppUdlXml, encoding) : @"<NotepadPlus />"; var regex = new Regex("<UserLang name=\"OpenEdgeABL\".*?</UserLang>", RegexOptions.Singleline | RegexOptions.IgnoreCase); var matches = regex.Match(fileContent); if (matches.Success) { if (onlyCheckInstall) { return(true); } // if it already exists in the file, delete the existing one fileContent = regex.Replace(fileContent, @""); } else { if (onlyCheckInstall) { return(false); } // if it doesn't exist in the file UserCommunication.Notify("It seems to be the first time that you use this plugin.<br>In order to activate the syntax highlighting, you must restart notepad++.<br><br><i>Please note that if a document is opened at the next start, you will have to manually close/reopen it to see the changes.</i><br><br><b>Sorry for the inconvenience</b>!", MessageImg.MsgInfo, "Information", "Installing syntax highlighting"); } if (fileContent.ContainsFast(@"<NotepadPlus />")) { fileContent = fileContent.Replace(@"<NotepadPlus />", "<NotepadPlus>\r\n" + DataResources.UDL + "\r\n</NotepadPlus>"); } else { fileContent = fileContent.Replace(@"<NotepadPlus>", "<NotepadPlus>\r\n" + DataResources.UDL); } // write to userDefinedLang.xml try { Utils.FileWriteAllText(Config.FileNppUdlXml, fileContent, encoding); } catch (Exception e) { if (e is UnauthorizedAccessException) { UserCommunication.Notify("<b>Couldn't access the file :</b><br>" + Config.FileNppUdlXml + "<br><br>This means i couldn't correctly applied the syntax highlighting feature!<br><br><i>Please make sure to allow write access to this file (Right click on file > Security > Check what's needed to allow total control to current user)</i>", MessageImg.MsgError, "Syntax highlighting", "Can't access userDefineLang.xml"); } else { ErrorHandler.ShowErrors(e, "Error while accessing userDefineLang.xml"); } return(false); } return(true); }
/// <summary> /// Called on an update, allows to do special stuff according to the version updated /// </summary> private void UpdateDoneFromVersion(string fromVersion) { // reset the log files Utils.DeleteDirectory(Config.FolderLog, true); if (!fromVersion.IsHigherVersionThan("1.7.3")) { Utils.DeleteDirectory(Path.Combine(Npp.ConfigDirectory, "Libraries"), true); } if (!fromVersion.IsHigherVersionThan("1.7.6")) { // delete old UDL if (File.Exists(Npp.ConfXml.FileNppUserDefinedLang)) { var encoding = TextEncodingDetect.GetFileEncoding(Npp.ConfXml.FileNppUserDefinedLang); var fileContent = Utils.ReadAllText(Npp.ConfXml.FileNppUserDefinedLang, encoding); var regex = new Regex("<UserLang name=\"OpenEdgeABL\".*?</UserLang>", RegexOptions.Singleline | RegexOptions.IgnoreCase); var matches = regex.Match(fileContent); if (matches.Success) { fileContent = regex.Replace(fileContent, ""); // write to userDefinedLang.xml var copyPath = Path.Combine(Config.FolderUpdate, "userDefineLang.xml"); Utils.FileWriteAllText(copyPath, fileContent, encoding); // replace default file by its copy on npp shutdown _3PUpdater.Instance.AddFileToMove(copyPath, Npp.ConfXml.FileNppUserDefinedLang); } } } if (!fromVersion.IsHigherVersionThan("1.7.8")) { // delete old database dump try { if (Directory.Exists(Config.FolderDatabase)) { foreach (string file in Directory.EnumerateFiles(Config.FolderDatabase, "*.dump", SearchOption.TopDirectoryOnly)) { File.Delete(file); } } } catch (Exception e) { ErrorHandler.LogError(e); } } }
/// <summary> /// Called when the latest release download is done /// </summary> /// <param name="sender"></param> /// <param name="asyncCompletedEventArgs"></param> private static void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs asyncCompletedEventArgs) { try { // Extract the .zip file if (Utils.ExtractAll(Config.FileLatestReleaseZip, Config.FolderUpdate)) { // check the presence of the plugin file if (File.Exists(Config.FileDownloadedPlugin)) { // set up the update so the .dll file downloaded replaces the current .dll _3PUpdater.Instance.AddFileToMove(Config.FileDownloadedPlugin, AssemblyInfo.Location); // if the release was containing a .pdb file, we want to copied it as well if (File.Exists(Config.FileDownloadedPdb)) { _3PUpdater.Instance.AddFileToMove(Config.FileDownloadedPdb, Path.Combine(Path.GetDirectoryName(AssemblyInfo.Location) ?? "", Path.GetFileName(Config.FileDownloadedPdb) ?? "")); } // write the version log Utils.FileWriteAllText(Config.FileVersionLog, _latestReleaseInfo.body, Encoding.Default); Utils.FileWriteAllText(Config.FilePreviousVersion, AssemblyInfo.Version, Encoding.Default); NotifyUpdateAvailable(); } else { Utils.DeleteDirectory(Config.FolderUpdate, true); } } else { UserCommunication.Notify("I failed to unzip the following file : <br>" + Config.FileLatestReleaseZip + "<br>It contains the update for 3P, you will have to do a manual update.", MessageImg.MsgError, "Unzip", "Failed"); } } catch (Exception e) { ErrorHandler.ShowErrors(e, "On Download File Completed"); } _isChecking = false; }
/// <summary> /// Called when the gitub api for releases responses /// </summary> private static void WbOnOnRequestEnded(WebServiceJson webServiceJson) { try { if (webServiceJson.StatusCodeResponse == HttpStatusCode.OK && webServiceJson.ResponseException == null) { Config.Instance.TechnicalLastCheckUpdateOk = true; // get the releases var releases = webServiceJson.DeserializeArray <ReleaseInfo>(); if (releases != null && releases.Count > 0) { // sort releases.Sort((o, o2) => o.tag_name.IsHigherVersionThan(o2.tag_name) ? -1 : 1); var localVersion = AssemblyInfo.Version; var outputBody = new StringBuilder(); foreach (var release in releases) { if (string.IsNullOrEmpty(release.tag_name)) { continue; } // For each version higher than the local one, append to the release body // Will be used to display the version log to the user if (release.tag_name.IsHigherVersionThan(localVersion) && (Config.Instance.UserGetsPreReleases || AssemblyInfo.IsPreRelease || !release.prerelease) && release.assets != null && release.assets.Count > 0 && release.assets.Exists(asset => asset.name.EqualsCi(Config.FileGitHubAssetName))) { // in case something is undefined (but shouldn't happen) if (string.IsNullOrEmpty(release.tag_name)) { release.tag_name = "vX.X.X.X"; } if (string.IsNullOrEmpty(release.name)) { release.name = "unknown"; } if (string.IsNullOrEmpty(release.body)) { release.body = "..."; } if (string.IsNullOrEmpty(release.published_at)) { release.published_at = DateTime.Now.ToString(CultureInfo.CurrentCulture); } // h1 outputBody.AppendLine("## " + release.tag_name + " : " + release.name + " ##\n\n"); // body outputBody.AppendLine(release.body + "\n\n"); // the first higher release encountered is the latest if (_latestReleaseInfo == null) { _latestReleaseInfo = release; } } } // There is a distant version higher than the local one if (_latestReleaseInfo != null) { // to display all the release notes _latestReleaseInfo.body = outputBody.ToString(); // delete existing dir Utils.DeleteDirectory(Config.FolderUpdate, true); Utils.DownloadFile(_latestReleaseInfo.assets.First(asset => asset.name.EqualsCi(Config.FileGitHubAssetName)).browser_download_url, Config.FileLatestReleaseZip, OnDownloadFileCompleted); return; } if (_displayResultNotif) { UserCommunication.NotifyUnique("UpdateChecked", "Congratulations! You already possess the latest <b>" + (!AssemblyInfo.IsPreRelease ? "stable" : "beta") + "</b> version of 3P!", MessageImg.MsgOk, "Update check", "You own the version " + AssemblyInfo.Version, null); } } } else { // failed to retrieve the list if (_displayResultNotif || Config.Instance.TechnicalLastCheckUpdateOk) { UserCommunication.NotifyUnique("ReleaseListDown", "For your information, I couldn't manage to retrieve the latest published version on GITHUB.<br><br>A request has been sent to :<br>" + Config.ReleasesApi.ToHtmlLink() + "<br>but was unsuccessful, you might have to check for a new version manually if this happens again.", MessageImg.MsgHighImportance, "Couldn't reach GITHUB", "Connection failed", null); } Config.Instance.TechnicalLastCheckUpdateOk = false; // check if there is an update available in the Shared config folder if (!string.IsNullOrEmpty(Config.Instance.SharedConfFolder) && Directory.Exists(Config.Instance.SharedConfFolder)) { var potentialUpdate = Path.Combine(Config.Instance.SharedConfFolder, AssemblyInfo.AssemblyName); // if the .dll exists, is higher version and (the user get beta releases or it's a stable release) if (File.Exists(potentialUpdate) && Utils.GetDllVersion(potentialUpdate).IsHigherVersionThan(AssemblyInfo.Version) && (Config.Instance.UserGetsPreReleases || AssemblyInfo.IsPreRelease || Utils.GetDllVersion(potentialUpdate).EndsWith(".0"))) { // copy to local update folder and warn the user if (Utils.CopyFile(potentialUpdate, Config.FileDownloadedPlugin)) { _latestReleaseInfo = new ReleaseInfo { name = "Updated from shared directory", tag_name = Utils.GetDllVersion(Config.FileDownloadedPlugin), prerelease = Utils.GetDllVersion(Config.FileDownloadedPlugin).EndsWith(".1"), published_at = "???", html_url = Config.UrlCheckReleases }; // set up the update so the .dll file downloaded replaces the current .dll _3PUpdater.Instance.AddFileToMove(Config.FileDownloadedPlugin, AssemblyInfo.Location); // write the version log Utils.FileWriteAllText(Config.FileVersionLog, @"This version has been updated from the shared directory" + Environment.NewLine + Environment.NewLine + @"Find more information on this release [here](" + Config.UrlCheckReleases + @")", Encoding.Default); Utils.FileWriteAllText(Config.FilePreviousVersion, AssemblyInfo.Version, Encoding.Default); NotifyUpdateAvailable(); } } } } } catch (Exception e) { ErrorHandler.ShowErrors(e, "Error when checking the latest release"); } _isChecking = false; }