From bb2339505debce51f9aed13703227668537da0ba Mon Sep 17 00:00:00 2001 From: Charles Milette Date: Mon, 5 Apr 2021 18:53:15 -0400 Subject: [PATCH] Support non top-level root namespaces in item templates --- vsix/ItemTemplates/BlankPage/BlankPage.cpp | 2 +- vsix/ItemTemplates/BlankPage/BlankPage.h | 4 +- .../BlankPage/cppwinrt_BlankPage.vstemplate | 4 +- .../BlankUserControl/BlankUserControl.cpp | 2 +- .../BlankUserControl/BlankUserControl.h | 4 +- .../cppwinrt_BlankUserControl.vstemplate | 4 +- vsix/ItemTemplates/ViewModel/ViewModel.cpp | 2 +- vsix/ItemTemplates/ViewModel/ViewModel.h | 4 +- .../ViewModel/cppwinrt_ViewModel.vstemplate | 4 +- vsix/WizardExtension.cs | 70 ++++++++++++++ vsix/packages.config | 4 - vsix/source.extension.vsixmanifest | 65 ++++++------- vsix/vsix.csproj | 95 +++++++++---------- 13 files changed, 162 insertions(+), 102 deletions(-) create mode 100644 vsix/WizardExtension.cs delete mode 100644 vsix/packages.config diff --git a/vsix/ItemTemplates/BlankPage/BlankPage.cpp b/vsix/ItemTemplates/BlankPage/BlankPage.cpp index 9af284cec..bd829f6ef 100644 --- a/vsix/ItemTemplates/BlankPage/BlankPage.cpp +++ b/vsix/ItemTemplates/BlankPage/BlankPage.cpp @@ -7,7 +7,7 @@ using namespace winrt; using namespace Windows::UI::Xaml; -namespace winrt::$rootnamespace$::implementation +namespace winrt::$cpprootnamespace$::implementation { $safeitemname$::$safeitemname$() { diff --git a/vsix/ItemTemplates/BlankPage/BlankPage.h b/vsix/ItemTemplates/BlankPage/BlankPage.h index f1dc903e8..c09c89901 100644 --- a/vsix/ItemTemplates/BlankPage/BlankPage.h +++ b/vsix/ItemTemplates/BlankPage/BlankPage.h @@ -2,7 +2,7 @@ #include "$safeitemname$.g.h" -namespace winrt::$rootnamespace$::implementation +namespace winrt::$cpprootnamespace$::implementation { struct $safeitemname$ : $safeitemname$T<$safeitemname$> { @@ -15,7 +15,7 @@ namespace winrt::$rootnamespace$::implementation }; } -namespace winrt::$rootnamespace$::factory_implementation +namespace winrt::$cpprootnamespace$::factory_implementation { struct $safeitemname$ : $safeitemname$T<$safeitemname$, implementation::$safeitemname$> { diff --git a/vsix/ItemTemplates/BlankPage/cppwinrt_BlankPage.vstemplate b/vsix/ItemTemplates/BlankPage/cppwinrt_BlankPage.vstemplate index 325ce0b2c..123b1cc88 100644 --- a/vsix/ItemTemplates/BlankPage/cppwinrt_BlankPage.vstemplate +++ b/vsix/ItemTemplates/BlankPage/cppwinrt_BlankPage.vstemplate @@ -17,8 +17,8 @@ BlankPage.idl - NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - NuGet.VisualStudio.TemplateWizard + Microsoft.Windows.CppWinRT, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + Microsoft.Windows.CppWinRT.WizardExtension diff --git a/vsix/ItemTemplates/BlankUserControl/BlankUserControl.cpp b/vsix/ItemTemplates/BlankUserControl/BlankUserControl.cpp index 9af284cec..bd829f6ef 100644 --- a/vsix/ItemTemplates/BlankUserControl/BlankUserControl.cpp +++ b/vsix/ItemTemplates/BlankUserControl/BlankUserControl.cpp @@ -7,7 +7,7 @@ using namespace winrt; using namespace Windows::UI::Xaml; -namespace winrt::$rootnamespace$::implementation +namespace winrt::$cpprootnamespace$::implementation { $safeitemname$::$safeitemname$() { diff --git a/vsix/ItemTemplates/BlankUserControl/BlankUserControl.h b/vsix/ItemTemplates/BlankUserControl/BlankUserControl.h index 7b994b1df..95f8e6314 100644 --- a/vsix/ItemTemplates/BlankUserControl/BlankUserControl.h +++ b/vsix/ItemTemplates/BlankUserControl/BlankUserControl.h @@ -6,7 +6,7 @@ #include "winrt/Windows.UI.Xaml.Controls.Primitives.h" #include "$safeitemname$.g.h" -namespace winrt::$rootnamespace$::implementation +namespace winrt::$cpprootnamespace$::implementation { struct $safeitemname$ : $safeitemname$T<$safeitemname$> { @@ -19,7 +19,7 @@ namespace winrt::$rootnamespace$::implementation }; } -namespace winrt::$rootnamespace$::factory_implementation +namespace winrt::$cpprootnamespace$::factory_implementation { struct $safeitemname$ : $safeitemname$T<$safeitemname$, implementation::$safeitemname$> { diff --git a/vsix/ItemTemplates/BlankUserControl/cppwinrt_BlankUserControl.vstemplate b/vsix/ItemTemplates/BlankUserControl/cppwinrt_BlankUserControl.vstemplate index 2e271ff86..706d85fd4 100644 --- a/vsix/ItemTemplates/BlankUserControl/cppwinrt_BlankUserControl.vstemplate +++ b/vsix/ItemTemplates/BlankUserControl/cppwinrt_BlankUserControl.vstemplate @@ -17,8 +17,8 @@ BlankUserControl.idl - NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - NuGet.VisualStudio.TemplateWizard + Microsoft.Windows.CppWinRT, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + Microsoft.Windows.CppWinRT.WizardExtension diff --git a/vsix/ItemTemplates/ViewModel/ViewModel.cpp b/vsix/ItemTemplates/ViewModel/ViewModel.cpp index af07f03db..9d0ddd849 100644 --- a/vsix/ItemTemplates/ViewModel/ViewModel.cpp +++ b/vsix/ItemTemplates/ViewModel/ViewModel.cpp @@ -4,7 +4,7 @@ #include "$safeitemname$.g.cpp" #endif -namespace winrt::$rootnamespace$::implementation +namespace winrt::$cpprootnamespace$::implementation { int32_t $safeitemname$::MyProperty() { diff --git a/vsix/ItemTemplates/ViewModel/ViewModel.h b/vsix/ItemTemplates/ViewModel/ViewModel.h index a5b640773..58be75014 100644 --- a/vsix/ItemTemplates/ViewModel/ViewModel.h +++ b/vsix/ItemTemplates/ViewModel/ViewModel.h @@ -2,7 +2,7 @@ #include "$safeitemname$.g.h" -namespace winrt::$rootnamespace$::implementation +namespace winrt::$cpprootnamespace$::implementation { struct $safeitemname$ : $safeitemname$T<$safeitemname$> { @@ -13,7 +13,7 @@ namespace winrt::$rootnamespace$::implementation }; } -namespace winrt::$rootnamespace$::factory_implementation +namespace winrt::$cpprootnamespace$::factory_implementation { struct $safeitemname$ : $safeitemname$T<$safeitemname$, implementation::$safeitemname$> { diff --git a/vsix/ItemTemplates/ViewModel/cppwinrt_ViewModel.vstemplate b/vsix/ItemTemplates/ViewModel/cppwinrt_ViewModel.vstemplate index 2a85b6f8e..6621ce41b 100644 --- a/vsix/ItemTemplates/ViewModel/cppwinrt_ViewModel.vstemplate +++ b/vsix/ItemTemplates/ViewModel/cppwinrt_ViewModel.vstemplate @@ -16,8 +16,8 @@ ViewModel.h - NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - NuGet.VisualStudio.TemplateWizard + Microsoft.Windows.CppWinRT, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + Microsoft.Windows.CppWinRT.WizardExtension diff --git a/vsix/WizardExtension.cs b/vsix/WizardExtension.cs new file mode 100644 index 000000000..e63b97104 --- /dev/null +++ b/vsix/WizardExtension.cs @@ -0,0 +1,70 @@ +using EnvDTE; +using Microsoft.VisualStudio.ComponentModelHost; +using Microsoft.VisualStudio.OLE.Interop; +using Microsoft.VisualStudio.Shell; +using Microsoft.VisualStudio.TemplateWizard; +using NuGet.VisualStudio; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.ComponentModel.Composition.Hosting; + +namespace Microsoft.Windows.CppWinRT +{ + internal sealed class WizardExtension : IWizard + { + [Import] + internal IVsTemplateWizard Wizard { get; set; } + + private void Initialize(object automationObject) + { + ThreadHelper.ThrowIfNotOnUIThread(); + using (var serviceProvider = new ServiceProvider((IServiceProvider)automationObject)) + { + var componentModel = (IComponentModel)serviceProvider.GetService(typeof(SComponentModel)); + Assumes.Present(componentModel); + using (var container = new CompositionContainer(componentModel.DefaultExportProvider)) + { + container.ComposeParts(this); + } + } + } + + void IWizard.BeforeOpeningFile(ProjectItem projectItem) + { + Wizard.BeforeOpeningFile(projectItem); + } + + void IWizard.ProjectFinishedGenerating(Project project) + { + Wizard.ProjectFinishedGenerating(project); + } + + void IWizard.ProjectItemFinishedGenerating(ProjectItem projectItem) + { + Wizard.ProjectItemFinishedGenerating(projectItem); + } + + void IWizard.RunFinished() + { + Wizard.RunFinished(); + } + + void IWizard.RunStarted(object automationObject, Dictionary replacementsDictionary, WizardRunKind runKind, object[] customParams) + { + ThreadHelper.ThrowIfNotOnUIThread(); + Initialize(automationObject); + + if (replacementsDictionary.TryGetValue("$rootnamespace$", out var rootNamespace)) + { + replacementsDictionary.Add("$cpprootnamespace$", rootNamespace.Replace(".", "::")); + } + + Wizard.RunStarted(automationObject, replacementsDictionary, runKind, customParams); + } + + bool IWizard.ShouldAddProjectItem(string filePath) + { + return Wizard.ShouldAddProjectItem(filePath); + } + } +} diff --git a/vsix/packages.config b/vsix/packages.config deleted file mode 100644 index c38f5c6f2..000000000 --- a/vsix/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/vsix/source.extension.vsixmanifest b/vsix/source.extension.vsixmanifest index 4dc229ae9..de750437e 100644 --- a/vsix/source.extension.vsixmanifest +++ b/vsix/source.extension.vsixmanifest @@ -1,35 +1,36 @@  - - - Microsoft.Windows.CppWinRT - C++/WinRT - Tools for authoring and consuming Windows Runtime classes in standard C++. - https://go.microsoft.com/fwlink/?linkid=869449 - LICENSE - https://docs.microsoft.com/windows/uwp/cpp-and-winrt-apis/intro-to-using-cpp-with-winrt - https://docs.microsoft.com/windows/uwp/cpp-and-winrt-apis/faq - cppwinrt.ico - cppwinrt.png - WinRT, C++, cppwinrt, native - - - - - - - - - - - - - - - - - - - - + + + Microsoft.Windows.CppWinRT + C++/WinRT + Tools for authoring and consuming Windows Runtime classes in standard C++. + https://go.microsoft.com/fwlink/?linkid=869449 + LICENSE + https://docs.microsoft.com/windows/uwp/cpp-and-winrt-apis/intro-to-using-cpp-with-winrt + https://docs.microsoft.com/windows/uwp/cpp-and-winrt-apis/faq + cppwinrt.ico + cppwinrt.png + WinRT, C++, cppwinrt, native + + + + + + + + + + + + + + + + + + + + + diff --git a/vsix/vsix.csproj b/vsix/vsix.csproj index 9279dee1a..69a5b450d 100644 --- a/vsix/vsix.csproj +++ b/vsix/vsix.csproj @@ -1,29 +1,8 @@  - - + - 15.0 - - - true - - - true - bin\x86\Debug\$(Deployment)\ - DEBUG;TRACE - full - x86 - prompt - MinimumRecommendedRules.ruleset - - - bin\x86\Release\$(Deployment)\ - TRACE - true - pdbonly - x86 - prompt - MinimumRecommendedRules.ruleset + 16.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) @@ -31,59 +10,79 @@ x86 2.0 {82b43b9b-a64c-4715-b499-d71e9ca2bd60};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - false Standalone {2F0C4AFA-FEFA-4D37-B824-0426CEB32DBA} Library Properties Microsoft.Windows.CppWinRT Microsoft.Windows.CppWinRT - v4.5.2 + v4.7.2 false - false + true + true false - false - false - false + true + true + true + Program + $(DevEnvDir)devenv.exe + /rootsuffix Exp + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 true - VCTargets + MSBuild true - VCTargets + MSBuild true - VCTargets + MSBuild true - VCTargets + MSBuild true - VCTargets + MSBuild true - VCTargets + MSBuild true - VCTargets + MSBuild true - VCTargets + MSBuild true - VCTargets + MSBuild true @@ -117,23 +116,17 @@ true - - Designer - - + + + + + + - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - -