From 838d53999317b511610c4172a5267b21168137b2 Mon Sep 17 00:00:00 2001 From: "Denis Kuzmin (github/3F)" Date: Sat, 4 May 2024 00:44:49 +0300 Subject: [PATCH] +Logic to reload project due to possible lag in early VS environment --- vsSolutionBuildEvent/EnvAbstract.cs | 55 ++++++++++++++++++++++------- vsSolutionBuildEvent/IsolatedEnv.cs | 10 +++--- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/vsSolutionBuildEvent/EnvAbstract.cs b/vsSolutionBuildEvent/EnvAbstract.cs index a978c89..b8ddb60 100644 --- a/vsSolutionBuildEvent/EnvAbstract.cs +++ b/vsSolutionBuildEvent/EnvAbstract.cs @@ -11,6 +11,7 @@ using net.r_eg.MvsSln; using net.r_eg.MvsSln.Core; using net.r_eg.MvsSln.Exceptions; +using net.r_eg.MvsSln.Extensions; using net.r_eg.vsSBE.API.Commands; using BuildType = net.r_eg.vsSBE.Bridge.BuildType; using EProject = Microsoft.Build.Evaluation.Project; @@ -102,16 +103,18 @@ public virtual EProject getProject(string name = null) if(string.IsNullOrEmpty(name)) name = StartupProjectString; ProjectItem project = Sln.ProjectItems.FirstOrDefault(p => p.name == name); - if(project.fullPath == null) { + if(project.fullPath == null) + { throw new NotFoundException($"Project '{name}' was not found. ['{project.name}', '{project.pGuid}']"); } - IConfPlatformPrj cfg = Sln.ProjectItemsConfigs - .FirstOrDefault(p => p.project.name == name && ActiveSlnConf?.Equals(p.solutionConfig) == true) - .projectConfig; - - return (cfg == null) ? SlnEnv?.GetOrLoadProject(project) - : SlnEnv?.GetOrLoadProject(project, cfg); + return LoadOrReload + ( + project, + Sln.ProjectItemsConfigs + .FirstOrDefault(p => p.project.name == name && ActiveSlnConf?.Equals(p.solutionConfig) == true) + .projectConfig + ); } /// @@ -119,15 +122,35 @@ public virtual EProject getProject(string name = null) /// public string SolutionCfgFormat(EnvDTE80.SolutionConfiguration2 cfg) { - if(cfg == null) { - return formatCfg(PropertyNames.UNDEFINED); - } - return formatCfg(cfg.Name, cfg.PlatformName); + return (cfg == null) ? FormatConf(PropertyNames.UNDEFINED) + : FormatConf(cfg.Name, cfg.PlatformName); } - protected string formatCfg(string name, string platform = null) + protected static string FormatConf(string name, string platform = null) + => ConfigItem.Format(name, platform ?? name); + + /// + /// Load or reload the project if necessary due to possible lag in early VS environment. + /// + /// ~ https://github.com/3F/vsSolutionBuildEvent/issues/80 + protected EProject LoadOrReload(ProjectItem item, IConfPlatformPrj cfg) { - return ConfigItem.Format(name, platform ?? name); + EProject loaded = LoadProject(item, cfg); + + if(loaded == null + || loaded.GetPropertyValue(PropertyNames.SLN_DIR) == PropertyNames.UNDEFINED) + { + IXProjectEnv env = SlnEnv; + + env?.Projects + .Where(p => p.ProjectItem.project.fullPath == item.fullPath) + .ToArray() // since we will change the collection below + .ForEach(p => env.Unload(p)); + + return LoadProject(item, cfg); + } + + return loaded; } protected void AssignEnv(IXProjectEnv env) @@ -139,6 +162,12 @@ protected void AssignEnv(IXProjectEnv env) SlnEnv = env; } + private EProject LoadProject(ProjectItem project, IConfPlatformPrj cfg) + { + return (cfg == null) ? SlnEnv?.GetOrLoadProject(project) + : SlnEnv?.GetOrLoadProject(project, cfg); + } + private ISlnResult UpdateSln() { var input = SolutionFile; diff --git a/vsSolutionBuildEvent/IsolatedEnv.cs b/vsSolutionBuildEvent/IsolatedEnv.cs index a805f51..f313da9 100644 --- a/vsSolutionBuildEvent/IsolatedEnv.cs +++ b/vsSolutionBuildEvent/IsolatedEnv.cs @@ -74,7 +74,7 @@ public List ProjectsList /// /// Formatted string with an active configuration for current solution. /// - public string SolutionActiveCfgString => formatCfg(slnProperties); + public string SolutionActiveCfgString => FormatConf(slnProperties); /// /// All configurations for current solution @@ -301,18 +301,18 @@ protected ConfigItem extractCfg(IDictionary properties) return currentSlnConf; } - protected string formatCfg(IDictionary properties) + protected string FormatConf(IDictionary properties) { IConfPlatform def = extractCfg(properties); - return formatCfg(def.Configuration, def.Platform); + return FormatConf(def.Configuration, def.Platform); } - private void __disabled(string name) + private static void __disabled(string name) { Log.Debug($"Accessing to '{name}' is disabled in Isolated environment."); } - private T __disabled(string name, T val = default) + private static T __disabled(string name, T val = default) { __disabled(name); return val;