diff options
-rw-r--r-- | QtVsTools.Core/CMake/CMakeProject.Presets.cs | 4 | ||||
-rw-r--r-- | QtVsTools.Core/HelperFunctions.cs | 2 | ||||
-rw-r--r-- | QtVsTools.Core/MsBuild/MsBuildProjectReaderWriter.V2.cs | 4 | ||||
-rw-r--r-- | QtVsTools.Core/Options/QtVersionsPage.cs | 16 | ||||
-rw-r--r-- | QtVsTools.Core/ProjectImport/ProjectImporter.cs | 14 | ||||
-rw-r--r-- | QtVsTools.Core/QtVersionManager.cs | 66 | ||||
-rw-r--r-- | QtVsTools.Package/Editors/Editor.cs | 2 | ||||
-rw-r--r-- | QtVsTools.Package/MsBuild/QtModulesEditor.cs | 2 | ||||
-rw-r--r-- | QtVsTools.Package/MsBuild/QtVersionProvider.cs | 17 | ||||
-rw-r--r-- | QtVsTools.Package/Package/QtHelp.cs | 2 | ||||
-rw-r--r-- | QtVsTools.Package/Package/Translation.cs | 2 | ||||
-rw-r--r-- | QtVsTools.Package/QtVsToolsPackage.cs | 10 | ||||
-rw-r--r-- | QtVsTools.Wizards/ProjectWizard/ConfigPage.xaml.cs | 12 | ||||
-rw-r--r-- | QtVsTools.Wizards/ProjectWizard/ProjectTemplateWizard.cs | 2 |
14 files changed, 71 insertions, 84 deletions
diff --git a/QtVsTools.Core/CMake/CMakeProject.Presets.cs b/QtVsTools.Core/CMake/CMakeProject.Presets.cs index 591364ba..49c3be1d 100644 --- a/QtVsTools.Core/CMake/CMakeProject.Presets.cs +++ b/QtVsTools.Core/CMake/CMakeProject.Presets.cs @@ -126,7 +126,7 @@ namespace QtVsTools.Core.CMake var versionRecords = GetRecords(UserPresets, "qt-project.org/Version") .ToDictionary(x => x["name"], x => x); - var missingVersionNames = VersionManager.GetVersions() + var missingVersionNames = QtVersionManager.GetVersions() .Where(x => !versionRecords.ContainsKey(x)); var missingVersions = missingVersionNames @@ -200,7 +200,7 @@ namespace QtVsTools.Core.CMake return; } - var versionNames = VersionManager.GetVersions().Prepend("Qt-Default").ToHashSet(); + var versionNames = QtVersionManager.GetVersions().Prepend("Qt-Default").ToHashSet(); // All visible presets must have a reference to a Qt version bool isQtVersion(JToken presetName) => versionNames.Contains(presetName.ToString()); diff --git a/QtVsTools.Core/HelperFunctions.cs b/QtVsTools.Core/HelperFunctions.cs index 108c730d..edbd1b47 100644 --- a/QtVsTools.Core/HelperFunctions.cs +++ b/QtVsTools.Core/HelperFunctions.cs @@ -488,7 +488,7 @@ namespace QtVsTools.Core public static string VcPath { get; set; } public static bool SetVcVars(VersionInformation versionInfo, ProcessStartInfo startInfo) { - var vm = QtVersionManager.The(); + var vm = QtVersionManager.The; versionInfo ??= vm.GetVersionInfo(vm.GetDefaultVersion()); if (string.IsNullOrEmpty(VcPath)) diff --git a/QtVsTools.Core/MsBuild/MsBuildProjectReaderWriter.V2.cs b/QtVsTools.Core/MsBuild/MsBuildProjectReaderWriter.V2.cs index 86bd5ba6..4d3dff7f 100644 --- a/QtVsTools.Core/MsBuild/MsBuildProjectReaderWriter.V2.cs +++ b/QtVsTools.Core/MsBuild/MsBuildProjectReaderWriter.V2.cs @@ -19,7 +19,7 @@ namespace QtVsTools.Core.MsBuild { private bool UpgradeFromV2() { - var qtInstallValue = QtVersionManager.The().GetDefaultVersion(); + var qtInstallValue = QtVersionManager.The.GetDefaultVersion(); // Get project user properties (old format) XElement refreshUserProps() => this[Files.Project].Xml @@ -171,7 +171,7 @@ namespace QtVsTools.Core.MsBuild } Commit("Removing Qt module macros from resource compiler properties"); - var qtVersion = QtVersionManager.The().GetVersionInfo(qtInstallValue); + var qtVersion = QtVersionManager.The.GetVersionInfo(qtInstallValue); moduleNames = QtModules.Instance.GetAvailableModules(qtVersion.qtMajor) // remove proVarQT values not provided by the used Qt version .SelectMany(x => x.proVarQT?.Split(' ') ?? Array.Empty<string>()) diff --git a/QtVsTools.Core/Options/QtVersionsPage.cs b/QtVsTools.Core/Options/QtVersionsPage.cs index 2fde93ab..7bf9c96e 100644 --- a/QtVsTools.Core/Options/QtVersionsPage.cs +++ b/QtVsTools.Core/Options/QtVersionsPage.cs @@ -23,7 +23,7 @@ namespace QtVsTools.Core.Options { static LazyFactory Lazy { get; } = new(); - QtVersionManager VersionManager => QtVersionManager.The(); + QtVersionManager VersionManager => QtVersionManager.The; QtVersionsTable VersionsTable => Lazy.Get(() => VersionsTable, () => new QtVersionsTable()); @@ -33,7 +33,7 @@ namespace QtVsTools.Core.Options public override void LoadSettingsFromStorage() { var versions = new List<Row>(); - foreach (var versionName in VersionManager.GetVersions()) { + foreach (var versionName in QtVersionManager.GetVersions()) { var versionPath = VersionManager.GetInstallPath(versionName); if (string.IsNullOrEmpty(versionPath)) continue; @@ -68,8 +68,8 @@ namespace QtVsTools.Core.Options void RemoveVersion(string versionName) { try { - if (VersionManager.HasVersion(versionName)) - VersionManager.RemoveVersion(versionName); + if (QtVersionManager.HasVersion(versionName)) + QtVersionManager.RemoveVersion(versionName); } catch (Exception exception) { exception.Log(); } @@ -91,16 +91,16 @@ namespace QtVsTools.Core.Options string compiler = version.Compiler; if (compiler == "g++") compiler = string.Empty; - VersionManager.SaveVersion(name, $"{access}:{path}:{compiler}", + QtVersionManager.SaveVersion(name, $"{access}:{path}:{compiler}", checkPath: false); } else { if (version.State.HasFlag((State)Column.Path)) - VersionManager.SaveVersion(version.VersionName, version.Path); + QtVersionManager.SaveVersion(version.VersionName, version.Path); } if (version.State.HasFlag((State)Column.VersionName)) { try { - VersionManager.SaveVersion(version.VersionName, version.Path); + QtVersionManager.SaveVersion(version.VersionName, version.Path); } catch (Exception exception) { exception.Log(); } @@ -117,7 +117,7 @@ namespace QtVsTools.Core.Options var defaultVersion = versions.FirstOrDefault(v => v is { IsDefault: true, State: not State.Removed }) ?? versions.FirstOrDefault(v => v.State != State.Removed); - VersionManager.SaveDefaultVersion(defaultVersion?.VersionName ?? ""); + QtVersionManager.SaveDefaultVersion(defaultVersion?.VersionName ?? ""); } catch (Exception exception) { exception.Log(); } diff --git a/QtVsTools.Core/ProjectImport/ProjectImporter.cs b/QtVsTools.Core/ProjectImport/ProjectImporter.cs index 46be0cfc..ff144f94 100644 --- a/QtVsTools.Core/ProjectImport/ProjectImporter.cs +++ b/QtVsTools.Core/ProjectImport/ProjectImporter.cs @@ -67,9 +67,9 @@ namespace QtVsTools.Core } } - ImportSolution(mainInfo, QtVersionManager.The().GetDefaultVersion()); + ImportSolution(mainInfo, QtVersionManager.The.GetDefaultVersion()); } else { - ImportProject(mainInfo, QtVersionManager.The().GetDefaultVersion()); + ImportProject(mainInfo, QtVersionManager.The.GetDefaultVersion()); } } @@ -127,7 +127,7 @@ namespace QtVsTools.Core { ThreadHelper.ThrowIfNotOnUIThread(); - var versionInfo = QtVersionManager.The().GetVersionInfo(qtVersion); + var versionInfo = QtVersionManager.The.GetVersionInfo(qtVersion); var vcInfo = RunQmake(mainInfo, ".sln", true, versionInfo); if (null == vcInfo) return; @@ -140,7 +140,7 @@ namespace QtVsTools.Core foreach (var vcProject in HelperFunctions.ProjectsInSolution(_dteObject)) { if (MsBuildProject.GetOrAdd(vcProject) is not {} project) continue; - QtVersionManager.The().SaveProjectQtVersion(project, qtVersion); + QtVersionManager.SaveProjectQtVersion(project, qtVersion); ApplyPostImportSteps(project); } } @@ -156,7 +156,7 @@ namespace QtVsTools.Core { ThreadHelper.ThrowIfNotOnUIThread(); - var versionInfo = QtVersionManager.The().GetVersionInfo(qtVersion); + var versionInfo = QtVersionManager.The.GetVersionInfo(qtVersion); var vcInfo = RunQmake(mainInfo, ProjectFileExtension, false, versionInfo); if (null == vcInfo) return; @@ -189,7 +189,7 @@ namespace QtVsTools.Core return; if (qtVersion is not null) - QtVersionManager.The().SaveProjectQtVersion(project, qtVersion); + QtVersionManager.SaveProjectQtVersion(project, qtVersion); var platformName = versionInfo.GetVSPlatformName(); if (!SelectSolutionPlatform(platformName) || !HasPlatform(vcPro, platformName)) { @@ -918,7 +918,7 @@ namespace QtVsTools.Core private static string GetQtInstallPath() { - var vm = QtVersionManager.The(); + var vm = QtVersionManager.The; var qtVersion = vm.GetDefaultVersion(); var path = vm.GetInstallPath(qtVersion); diff --git a/QtVsTools.Core/QtVersionManager.cs b/QtVsTools.Core/QtVersionManager.cs index 2f808ff8..25a04c52 100644 --- a/QtVsTools.Core/QtVersionManager.cs +++ b/QtVsTools.Core/QtVersionManager.cs @@ -4,7 +4,7 @@ ***************************************************************************************************/ using System; -using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -15,12 +15,11 @@ using Microsoft.Win32; namespace QtVsTools.Core { - using Common; using MsBuild; public static partial class Instances { - public static QtVersionManager VersionManager => QtVersionManager.The(); + public static QtVersionManager VersionManager => QtVersionManager.The; } /// <summary> @@ -31,44 +30,37 @@ namespace QtVsTools.Core private const string VersionsKey = "Versions"; private const string RegistryVersionsPath = Resources.RegistryRootPath + "\\" + VersionsKey; - private static QtVersionManager instance; - private Hashtable versionCache; + private static readonly SemaphoreSlim CacheSemaphore = new (1, 1); + private static readonly ConcurrentDictionary<string, VersionInformation> VersionCache = new(); - private static readonly EventWaitHandle packageInit = new(false, EventResetMode.ManualReset); - private static EventWaitHandle packageInitDone; - - public static QtVersionManager The(EventWaitHandle initDone = null) - { - if (initDone == null) { - packageInit.WaitOne(); - packageInitDone.WaitOne(); - } else { - packageInitDone = initDone; - packageInit.Set(); - } - - return instance ??= new QtVersionManager(); - } + private static readonly Lazy<QtVersionManager> Instance = new(() => new QtVersionManager()); + public static QtVersionManager The => Instance.Value; public VersionInformation GetVersionInfo(string name) { - if (name == null) - return null; if (name == "$(DefaultQtVersion)") name = GetDefaultVersion(); - versionCache ??= new Hashtable(); + if (name == null) + return null; - if (versionCache[name] is VersionInformation vi) + if (VersionCache.TryGetValue(name, out var vi)) return vi; - var qtdir = GetInstallPath(name); - versionCache[name] = vi = VersionInformation.Get(qtdir); - if (vi != null) - vi.name = name; - return vi; + CacheSemaphore.Wait(); + try { + vi = VersionCache.GetOrAdd(name, VersionInformation.Get(GetInstallPath(name))); + if (vi != null) + vi.name = name; + return vi; + } catch (Exception exception) { + exception.Log(); + return null; + } finally { + CacheSemaphore.Release(); + } } - public string[] GetVersions() + public static string[] GetVersions() { var key = Registry.CurrentUser.OpenSubKey(Resources.RegistryRootPath, false); if (key == null) @@ -168,7 +160,7 @@ namespace QtVsTools.Core return version == null ? null : GetInstallPath(version); } - public void SaveVersion(string versionName, string path, bool checkPath = true) + public static void SaveVersion(string versionName, string path, bool checkPath = true) { var verName = versionName?.Trim().Replace(@"\", "_"); if (string.IsNullOrEmpty(verName)) @@ -204,7 +196,7 @@ namespace QtVsTools.Core } } - public bool HasVersion(string versionName) + public static bool HasVersion(string versionName) { if (string.IsNullOrEmpty(versionName)) return false; @@ -212,7 +204,7 @@ namespace QtVsTools.Core false) != null; } - public void RemoveVersion(string versionName) + public static void RemoveVersion(string versionName) { var key = Registry.CurrentUser.OpenSubKey(RegistryVersionsPath, true); if (key == null) @@ -221,12 +213,12 @@ namespace QtVsTools.Core key.Close(); } - private bool IsVersionAvailable(string version) + private static bool IsVersionAvailable(string version) { return GetVersions().Any(ver => version == ver); } - public void SaveProjectQtVersion(MsBuildProject project, string version) + public static void SaveProjectQtVersion(MsBuildProject project, string version) { ThreadHelper.ThrowIfNotOnUIThread(); @@ -279,7 +271,7 @@ namespace QtVsTools.Core return VerifyIfQtVersionExists(defaultVersion) ? defaultVersion : null; } - private string GetDefaultVersionString() + private static string GetDefaultVersionString() { string defaultVersion = null; try { @@ -292,7 +284,7 @@ namespace QtVsTools.Core return defaultVersion ?? Path.GetFileName(Environment.GetEnvironmentVariable("QTDIR")); } - public bool SaveDefaultVersion(string version) + public static bool SaveDefaultVersion(string version) { if (version == "$(DefaultQtVersion)") return false; diff --git a/QtVsTools.Package/Editors/Editor.cs b/QtVsTools.Package/Editors/Editor.cs index a504920a..1b7f8aa2 100644 --- a/QtVsTools.Package/Editors/Editor.cs +++ b/QtVsTools.Package/Editors/Editor.cs @@ -61,7 +61,7 @@ namespace QtVsTools.Editors string GetDefaultQtToolsPath() { - var versionMgr = QtVersionManager.The(); + var versionMgr = QtVersionManager.The; if (versionMgr == null) return null; diff --git a/QtVsTools.Package/MsBuild/QtModulesEditor.cs b/QtVsTools.Package/MsBuild/QtModulesEditor.cs index 0bbf98d9..25eda983 100644 --- a/QtVsTools.Package/MsBuild/QtModulesEditor.cs +++ b/QtVsTools.Package/MsBuild/QtModulesEditor.cs @@ -38,7 +38,7 @@ namespace QtVsTools.Package.MsBuild var qtSettings = ruleProperty.ContainingRule; var qtVersion = await qtSettings.GetPropertyValueAsync("QtInstall"); - var vm = QtVersionManager.The(); + var vm = QtVersionManager.The; var versionInfo = vm.GetVersionInfo(qtVersion) ?? vm.GetVersionInfo(vm.GetDefaultVersion()); diff --git a/QtVsTools.Package/MsBuild/QtVersionProvider.cs b/QtVsTools.Package/MsBuild/QtVersionProvider.cs index 4aeb5f35..dd74df25 100644 --- a/QtVsTools.Package/MsBuild/QtVersionProvider.cs +++ b/QtVsTools.Package/MsBuild/QtVersionProvider.cs @@ -32,15 +32,14 @@ namespace QtVsTools.Package.MsBuild public async Task<ICollection<IEnumValue>> GetListedValuesAsync() { - return await Task.FromResult( - QtVersionManager.The().GetVersions() - .Select(x => new PageEnumValue(new EnumValue - { - Name = x, - DisplayName = x - })) - .Cast<IEnumValue>() - .ToList()); + return await Task.FromResult(QtVersionManager.GetVersions() + .Select(x => new PageEnumValue(new EnumValue + { + Name = x, + DisplayName = x + })) + .Cast<IEnumValue>() + .ToList()); } public async Task<IEnumValue> TryCreateEnumValueAsync(string userSuppliedValue) diff --git a/QtVsTools.Package/Package/QtHelp.cs b/QtVsTools.Package/Package/QtHelp.cs index aa24a543..8f424ce8 100644 --- a/QtVsTools.Package/Package/QtHelp.cs +++ b/QtVsTools.Package/Package/QtHelp.cs @@ -131,7 +131,7 @@ namespace QtVsTools if (HelperFunctions.GetSelectedQtProject(dte) is {} project) qtVersion = project.QtVersion; - var info = QtVersionManager.The().GetVersionInfo(qtVersion); + var info = QtVersionManager.The.GetVersionInfo(qtVersion); var docPath = info?.QtInstallDocs; if (string.IsNullOrEmpty(docPath) || !Directory.Exists(docPath)) return false; diff --git a/QtVsTools.Package/Package/Translation.cs b/QtVsTools.Package/Package/Translation.cs index 36195113..5d236f33 100644 --- a/QtVsTools.Package/Package/Translation.cs +++ b/QtVsTools.Package/Package/Translation.cs @@ -123,7 +123,7 @@ namespace QtVsTools var qtToolsPath = project.GetPropertyValue("QtToolsPath"); if (string.IsNullOrEmpty(qtToolsPath)) { - var qtInstallPath = QtVersionManager.The().GetInstallPath(project.QtVersion); + var qtInstallPath = QtVersionManager.The.GetInstallPath(project.QtVersion); if (string.IsNullOrEmpty(qtInstallPath)) return false; qtToolsPath = Path.Combine(qtInstallPath, "bin"); diff --git a/QtVsTools.Package/QtVsToolsPackage.cs b/QtVsTools.Package/QtVsToolsPackage.cs index ab5cbcb9..c1d5c8d8 100644 --- a/QtVsTools.Package/QtVsToolsPackage.cs +++ b/QtVsTools.Package/QtVsToolsPackage.cs @@ -104,7 +104,6 @@ namespace QtVsTools public Editors.QtLinguist QtLinguist { get; private set; } private Editors.QtResourceEditor QtResourceEditor { get; set; } - private EventWaitHandle Ready { get; } = new(false, EventResetMode.ManualReset); public EventWaitHandle Initialized { get; } = new(false, EventResetMode.ManualReset); private bool InitializationAwaited { get; set; } = false; @@ -113,7 +112,6 @@ namespace QtVsTools { get { - instance.Ready.WaitOne(); return instance; } } @@ -181,7 +179,7 @@ namespace QtVsTools MoveRegistryKeys(Resources.RegistryRootPath + "\\Qt5VS2017", Resources.RegistryPackagePath); - var vm = QtVersionManager.The(Ready); + var vm = QtVersionManager.The; if (vm.HasInvalidVersions(out var error, out var defaultInvalid)) { if (defaultInvalid) vm.SetLatestQtVersionAsDefault(); @@ -268,7 +266,6 @@ namespace QtVsTools CopyTextMateLanguageFiles(); initTimer.Stop(); - Ready.Set(); var initMsecs = initTimer.Elapsed.TotalMilliseconds; var uiMsecs = uiTimer.Elapsed.TotalMilliseconds; @@ -283,7 +280,6 @@ namespace QtVsTools ); } catch (Exception exception) { - Ready.Set(); exception.Log(); } } @@ -372,7 +368,7 @@ namespace QtVsTools await VsShell.UiThreadAsync(() => StatusBar.SetText("Checking installed Qt versions...")); - var versions = Core.Instances.VersionManager.GetVersions(); + var versions = QtVersionManager.GetVersions(); var statusCenter = await VsServiceProvider .GetServiceAsync<SVsTaskStatusCenterService, IVsTaskStatusCenterService>(); var status = statusCenter?.PreRegister( @@ -444,7 +440,7 @@ namespace QtVsTools public void VsMainWindowActivated() { - if (QtVersionManager.The().GetVersions()?.Length == 0) + if (QtVersionManager.GetVersions().Length == 0) Notifications.NoQtVersion.Show(); if (Options.NotifyInstalled && TestVersionInstalled()) Notifications.NotifyInstall.Show(); diff --git a/QtVsTools.Wizards/ProjectWizard/ConfigPage.xaml.cs b/QtVsTools.Wizards/ProjectWizard/ConfigPage.xaml.cs index 8ece4590..e069dece 100644 --- a/QtVsTools.Wizards/ProjectWizard/ConfigPage.xaml.cs +++ b/QtVsTools.Wizards/ProjectWizard/ConfigPage.xaml.cs @@ -119,7 +119,7 @@ namespace QtVsTools.Wizards.ProjectWizard private IEnumerable<string> qtVersionList; - readonly QtVersionManager qtVersionManager = QtVersionManager.The(); + readonly QtVersionManager qtVersionManager = QtVersionManager.The; readonly VersionInformation defaultQtVersionInfo; CloneableList<Config> defaultConfigs; @@ -147,7 +147,7 @@ namespace QtVsTools.Wizards.ProjectWizard Loaded -= OnLoaded; qtVersionList = new[] { QT_VERSION_DEFAULT, QT_VERSION_BROWSE } - .Union(QtVersionManager.The().GetVersions()); + .Union(QtVersionManager.GetVersions()); if (defaultQtVersionInfo != null) SetupDefaultConfigsAndConfigTable(defaultQtVersionInfo); @@ -343,7 +343,7 @@ namespace QtVsTools.Wizards.ProjectWizard comboBoxQtVersion.Text = config.QtVersionName; break; default: - if (qtVersionManager.GetVersions().Contains(comboBoxQtVersion.Text)) { + if (QtVersionManager.GetVersions().Contains(comboBoxQtVersion.Text)) { config.QtVersion = qtVersionManager.GetVersionInfo(comboBoxQtVersion.Text); config.QtVersionName = comboBoxQtVersion.Text; config.QtVersionPath = qtVersionManager.GetInstallPath(comboBoxQtVersion.Text); @@ -532,15 +532,15 @@ namespace QtVsTools.Wizards.ProjectWizard var versionName = $"{Path.GetFileName(qtVersionDir)}" + $"_{Path.GetFileName(qmakePath)}".Replace(" ", "_"); - qtVersionManager.SaveVersion(versionName, qmakePath); - qtVersionManager.SaveDefaultVersion(versionName); + QtVersionManager.SaveVersion(versionName, qmakePath); + QtVersionManager.SaveDefaultVersion(versionName); versionInfo.name = versionName; } catch (Exception exception) { Messages.Print("Could not save Qt version."); exception.Log(); } - qtVersionList = new[] { QT_VERSION_BROWSE }.Union(QtVersionManager.The().GetVersions()); + qtVersionList = new[] { QT_VERSION_BROWSE }.Union(QtVersionManager.GetVersions()); SetupDefaultConfigsAndConfigTable(versionInfo); diff --git a/QtVsTools.Wizards/ProjectWizard/ProjectTemplateWizard.cs b/QtVsTools.Wizards/ProjectWizard/ProjectTemplateWizard.cs index 777893eb..6a6ff696 100644 --- a/QtVsTools.Wizards/ProjectWizard/ProjectTemplateWizard.cs +++ b/QtVsTools.Wizards/ProjectWizard/ProjectTemplateWizard.cs @@ -183,7 +183,7 @@ namespace QtVsTools.Wizards.ProjectWizard protected TemplateParameters Parameter => Lazy.Get(() => Parameter, () => new TemplateParameters { Template = this }); - protected QtVersionManager VersionManager => QtVersionManager.The(); + protected QtVersionManager VersionManager => QtVersionManager.The; public virtual void ProjectItemFinishedGenerating(ProjectItem projectItem) { } public virtual void BeforeOpeningFile(ProjectItem projectItem) { } |