aboutsummaryrefslogtreecommitdiffstats
path: root/src/qtvstools
diff options
context:
space:
mode:
Diffstat (limited to 'src/qtvstools')
-rw-r--r--src/qtvstools/DteEventsHandler.cs5
-rw-r--r--src/qtvstools/QtMsBuild/Components/QtVarsDesignTime.cs184
-rw-r--r--src/qtvstools/QtVsTools.csproj5
3 files changed, 193 insertions, 1 deletions
diff --git a/src/qtvstools/DteEventsHandler.cs b/src/qtvstools/DteEventsHandler.cs
index 516bc585..7ec13f4c 100644
--- a/src/qtvstools/DteEventsHandler.cs
+++ b/src/qtvstools/DteEventsHandler.cs
@@ -33,6 +33,7 @@ using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.VCProjectEngine;
using QtProjectLib;
using QtProjectLib.QtMsBuild;
+using QtVsTools.QtMsBuild;
using System;
using System.IO;
using System.Linq;
@@ -542,8 +543,10 @@ namespace QtVsTools
void SolutionEvents_Opened()
{
foreach (var p in HelperFunctions.ProjectsInSolution(Vsix.Instance.Dte)) {
- if (HelperFunctions.IsQtProject(p))
+ if (HelperFunctions.IsQtProject(p)) {
InitializeVCProject(p);
+ QtVarsDesignTime.AddProject(p);
+ }
}
}
diff --git a/src/qtvstools/QtMsBuild/Components/QtVarsDesignTime.cs b/src/qtvstools/QtMsBuild/Components/QtVarsDesignTime.cs
new file mode 100644
index 00000000..85dcd399
--- /dev/null
+++ b/src/qtvstools/QtMsBuild/Components/QtVarsDesignTime.cs
@@ -0,0 +1,184 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt VS Tools.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Collections.Immutable;
+using System.IO;
+using System.Threading.Tasks;
+using Microsoft.Build.Evaluation;
+using Microsoft.Build.Execution;
+using Microsoft.VisualStudio.ProjectSystem;
+using Microsoft.VisualStudio.ProjectSystem.Properties;
+using EnvDTE;
+using QtProjectLib;
+using System.Diagnostics;
+
+namespace QtVsTools.QtMsBuild
+{
+ class QtVarsDesignTime
+ {
+ static ConcurrentDictionary<EnvDTE.Project, QtVarsDesignTime> Instances { get; set; }
+
+ EnvDTE.Project Project { get; set; }
+
+ UnconfiguredProject UnconfiguredProject { get; set; }
+ IEnumerable<ConfiguredProject> ConfiguredProjects { get; set; }
+ IProjectLockService LockService { get; set; }
+
+ static readonly object criticalSection = new object();
+ public static void AddProject(EnvDTE.Project project)
+ {
+ lock (criticalSection) {
+ if (Instances == null)
+ Instances = new ConcurrentDictionary<EnvDTE.Project, QtVarsDesignTime>();
+ }
+ Instances[project] = new QtVarsDesignTime(project);
+ }
+
+ private QtVarsDesignTime(EnvDTE.Project project)
+ {
+ Project = project;
+ Task.Run(Initialize).Wait(5000);
+ }
+
+ bool initialized = false;
+
+ private async Task Initialize()
+ {
+ var context = Project.Object as IVsBrowseObjectContext;
+ if (context == null)
+ return;
+
+ UnconfiguredProject = context.UnconfiguredProject;
+ if (UnconfiguredProject == null
+ || UnconfiguredProject.ProjectService == null
+ || UnconfiguredProject.ProjectService.Services == null)
+ return;
+
+ LockService = UnconfiguredProject.ProjectService.Services.ProjectLockService;
+ if (LockService == null)
+ return;
+
+ var configs = await UnconfiguredProject.Services
+ .ProjectConfigurationsService.GetKnownProjectConfigurationsAsync();
+
+ initialized = true;
+
+ foreach (var config in configs) {
+ var configProject = await UnconfiguredProject.LoadConfiguredProjectAsync(config);
+ configProject.ProjectChanged += OnProjectChanged;
+ configProject.ProjectUnloading += OnProjectUnloading;
+ await DesignTimeUpdateQtVarsAsync(configProject);
+ }
+ }
+
+ private void OnProjectChanged(object sender, EventArgs e)
+ {
+ if (!initialized)
+ return;
+
+ var project = sender as ConfiguredProject;
+ if (project == null || project.Services == null)
+ return;
+
+ Task.Run(async () => await DesignTimeUpdateQtVarsAsync(project));
+ }
+
+ private async Task DesignTimeUpdateQtVarsAsync(ConfiguredProject project)
+ {
+ if (project == null)
+ return;
+
+ try {
+ ProjectWriteLockReleaser writeAccess;
+ var timer = Stopwatch.StartNew();
+ while (timer.IsRunning) {
+ try {
+ writeAccess = await LockService.WriteLockAsync();
+ timer.Stop();
+ } catch (InvalidOperationException) {
+ if (timer.ElapsedMilliseconds >= 5000)
+ throw;
+ using (var readAccess = await LockService.ReadLockAsync())
+ await readAccess.ReleaseAsync();
+ }
+ }
+
+ using (writeAccess)
+ using (var buildManager = new BuildManager()) {
+ var msBuildProject = await writeAccess.GetProjectAsync(project);
+
+ var configProps = project.ProjectConfiguration
+ .Dimensions.ToImmutableDictionary();
+
+ var projectInstance = new ProjectInstance(msBuildProject.Xml,
+ new Dictionary<string, string>(configProps)
+ { { "DesignTimeBuild", "true" } },
+ null, new ProjectCollection());
+
+ var buildRequest = new BuildRequestData(projectInstance,
+ targetsToBuild: new[] { "QtVars" },
+ hostServices: null,
+ flags: BuildRequestDataFlags.ProvideProjectStateAfterBuild);
+
+ var result = buildManager.Build(new BuildParameters(), buildRequest);
+
+ if (result == null
+ || result.ResultsByTarget == null
+ || result.OverallResult != BuildResultCode.Success) {
+ Messages.PaneMessageSafe(Vsix.Instance.Dte, timeout: 5000,
+ str: string.Format("{0}: design-time pre-build FAILED!",
+ Path.GetFileName(UnconfiguredProject.FullPath)));
+ } else {
+ var resultQtVars = result.ResultsByTarget["QtVars"];
+ if (resultQtVars.ResultCode == TargetResultCode.Success) {
+ msBuildProject.MarkDirty();
+ }
+ }
+ }
+ } catch (Exception e) {
+ Messages.PaneMessageSafe(Vsix.Instance.Dte, timeout: 5000,
+ str: string.Format("{0}: design-time pre-build ERROR: {1}",
+ Path.GetFileName(UnconfiguredProject.FullPath), e.Message));
+ }
+ }
+
+ private async Task OnProjectUnloading(object sender, EventArgs args)
+ {
+ var project = sender as ConfiguredProject;
+ if (project == null || project.Services == null)
+ return;
+ project.ProjectChanged -= OnProjectChanged;
+ project.ProjectUnloading -= OnProjectUnloading;
+ Instances[Project] = null;
+ await Task.Yield();
+ }
+ }
+}
diff --git a/src/qtvstools/QtVsTools.csproj b/src/qtvstools/QtVsTools.csproj
index 2134d88a..7418b515 100644
--- a/src/qtvstools/QtVsTools.csproj
+++ b/src/qtvstools/QtVsTools.csproj
@@ -153,6 +153,7 @@
<Compile Include="QtMsBuildConverter.cs" />
<Compile Include="QtMsBuild\Components\QtModulesEditor.cs" />
<Compile Include="QtMsBuild\Components\QtVersionProvider.cs" />
+ <Compile Include="QtMsBuild\Components\QtVarsDesignTime.cs" />
<Compile Include="QtProjectContextMenu.cs" />
<Compile Include="QtSolutionContextMenu.cs" />
<Compile Include="QtVsToolsDefaultEditors.cs" />
@@ -462,17 +463,21 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
+ <Reference Include="Microsoft.Build" />
+ <Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.VisualStudio.Composition">
<HintPath>$(VsInstallRoot)\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.Composition.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.ProjectSystem">
<HintPath>$(VsInstallRoot)\Common7\IDE\CommonExtensions\Microsoft\Project\Microsoft.VisualStudio.ProjectSystem.dll</HintPath>
</Reference>
+ <Reference Include="System.Collections.Immutable, Version=1.2.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System.Data" />
<Reference Include="System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.Core.1.0.102.0\lib\net45\System.Data.SQLite.dll</HintPath>
<Private>True</Private>
</Reference>
+ <Reference Include="System.Threading.Tasks.Dataflow, Version=4.5.24.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
<ItemGroup>
<Page Include="QtHelpLinkChooser.xaml">