From e1830b9692aa16838e84cda30d19294df9e047dc Mon Sep 17 00:00:00 2001 From: Mahdi Hosseini Date: Thu, 13 Feb 2025 14:38:15 +0330 Subject: [PATCH] Fix Template issues --- .../DevWinUI_Template/Common/WizardConfig.cs | 1 + .../TemplateWizard/SharedWizard.cs | 261 +++++++++--------- .../source.extension.vsixmanifest | 2 +- .../WinUIApp-MVVM-NavigationView/App.xaml.cs | 2 +- .../WinUIApp-MVVM/App.xaml.cs | 2 +- .../WinUIApp-NavigationView/App.xaml.cs | 2 +- VSIX/DevWinUI_Template/WinUIApp/App.xaml.cs | 2 +- 7 files changed, 131 insertions(+), 141 deletions(-) diff --git a/VSIX/DevWinUI_Template/DevWinUI_Template/Common/WizardConfig.cs b/VSIX/DevWinUI_Template/DevWinUI_Template/Common/WizardConfig.cs index 1989eb6..2a8b6a6 100644 --- a/VSIX/DevWinUI_Template/DevWinUI_Template/Common/WizardConfig.cs +++ b/VSIX/DevWinUI_Template/DevWinUI_Template/Common/WizardConfig.cs @@ -31,6 +31,7 @@ public static class WizardConfig public static bool IsUnPackagedMode; public static bool IsBlank; public static bool HasPages; + public static bool IsMVVM; public static bool UseHomeLandingPage; public static bool UseSettingsPage; public static bool UseGeneralSettingPage; diff --git a/VSIX/DevWinUI_Template/DevWinUI_Template/TemplateWizard/SharedWizard.cs b/VSIX/DevWinUI_Template/DevWinUI_Template/TemplateWizard/SharedWizard.cs index ad44f8d..18b142e 100644 --- a/VSIX/DevWinUI_Template/DevWinUI_Template/TemplateWizard/SharedWizard.cs +++ b/VSIX/DevWinUI_Template/DevWinUI_Template/TemplateWizard/SharedWizard.cs @@ -149,6 +149,7 @@ public async void RunStarted(object automationObject, Dictionary _shouldAddProjectItem = false; WizardConfig.HasPages = templateConfig.HasPages; WizardConfig.IsBlank = templateConfig.IsBlank; + WizardConfig.IsMVVM = templateConfig.IsMVVM; await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); @@ -173,7 +174,6 @@ public async void RunStarted(object automationObject, Dictionary if (result.HasValue && result.Value) { - AddReplacementsDictionary(replacementsDictionary); var vsix = await GetVSIXPathAsync(templateConfig.TemplateType.ToString()); VSTemplateFilePath = vsix.VSTemplatePath; ProjectTemplatesFolderPath = vsix.ProjectTemplatesFolder; @@ -183,101 +183,32 @@ public async void RunStarted(object automationObject, Dictionary AddEditorConfigFile(); - var libs = WizardConfig.LibraryDic; + AddReplacementsDictionary(replacementsDictionary); #region Libs - foreach (var lib in libs.Values) + // Assuming package list is passed via a custom parameter in the .vstemplate file + if (replacementsDictionary.TryGetValue("$NuGetPackages$", out string packages)) { - _nuGetPackages?.Add(new Library(lib.Name, lib.IncludePreRelease)); - } - - if (WizardConfig.UseJsonSettings) - { - _nuGetPackages.Add(new Library("nucs.JsonSettings", WizardConfig.UsePreReleaseVersion)); - _nuGetPackages.Add(new Library("nucs.JsonSettings.AutoSaveGenerator", WizardConfig.UsePreReleaseVersion)); - } - - _nuGetPackages = _nuGetPackages.Distinct().ToList(); - #endregion - - #region CSProjectElements - // Add CSProjectElements - if (WizardConfig.CSProjectElements != null && WizardConfig.CSProjectElements.Count > 0) - { - StringBuilder sb = new StringBuilder(); - - foreach (var entity in WizardConfig.CSProjectElements) + _nuGetPackages = new(); + var basePackages = packages.Split(';').Where(p => !string.IsNullOrEmpty(p)); + foreach (var baseItem in basePackages) { - sb.AppendLine($"{entity.Value}"); + _nuGetPackages.Add(new Library(baseItem, WizardConfig.UsePreReleaseVersion)); } - - replacementsDictionary.Add("$CustomCSProjectElement$", Environment.NewLine + $"{sb.ToString().Trim()}"); } - else - { - replacementsDictionary.Add("$CustomCSProjectElement$", ""); - } - - #endregion - - #region AppxManifest - if (WizardConfig.UnvirtualizedResources != null && WizardConfig.UnvirtualizedResources.Count > 0) - { - StringBuilder sb = new StringBuilder(); - - foreach (var entity in WizardConfig.UnvirtualizedResources) - { - sb.AppendLine($"{entity.Value}"); - } - replacementsDictionary.Add("$UnvirtualizedResourcesCapability$", Environment.NewLine + " "); - replacementsDictionary.Add("$UnvirtualizedResources$", Environment.NewLine + $"{sb.ToString().Trim()}"); - replacementsDictionary.Add("$AppxManifestDesktop6$", Environment.NewLine + " xmlns:desktop6=\"http://schemas.microsoft.com/appx/manifest/desktop/windows10/6\""); - } - else + foreach (var lib in WizardConfig.LibraryDic.Values) { - replacementsDictionary.Add("$UnvirtualizedResourcesCapability$", ""); - replacementsDictionary.Add("$UnvirtualizedResources$", ""); - replacementsDictionary.Add("$AppxManifestDesktop6$", ""); - } - #endregion - - #region Add Xaml Dictionary if User Use Extra Lib - #region Blank - - if (templateConfig.IsBlank) - { - if (libs != null && libs.ContainsKey(Constants.DevWinUI_Controls)) - { - replacementsDictionary.Add("$DevWinUI.Controls$", Environment.NewLine + Constants.DevWinUI_Controls_Xaml); - } - else - { - replacementsDictionary.Add("$DevWinUI.Controls$", ""); - } + _nuGetPackages?.Add(new Library(lib.Name, lib.IncludePreRelease)); } - #endregion - - if (libs != null && libs.ContainsKey(Constants.DevWinUI_ContextMenu)) - { - WizardConfig.UseWindow11ContextMenu = true; - replacementsDictionary.Add("$CLSID$", Guid.NewGuid().ToString()); - var windows11ContextMenu = PredefinedCodes.Windows11ContextMenuInitializer; - var windows11ContextMenuMVVM = PredefinedCodes.Windows11ContextMenuMVVMInitializer; - windows11ContextMenu = windows11ContextMenu.Replace("$projectname$", ProjectName); - windows11ContextMenuMVVM = windows11ContextMenuMVVM.Replace("$projectname$", ProjectName); - replacementsDictionary.Add("$Windows11ContextMenuInitializer$", Environment.NewLine + windows11ContextMenu); - replacementsDictionary.Add("$Windows11ContextMenuMVVMInitializer$", Environment.NewLine + Environment.NewLine + windows11ContextMenuMVVM); - } - else + if (WizardConfig.UseJsonSettings) { - WizardConfig.UseWindow11ContextMenu = false; - replacementsDictionary.Add("$CLSID$", ""); - replacementsDictionary.Add("$Windows11ContextMenuInitializer$", ""); - replacementsDictionary.Add("$Windows11ContextMenuMVVMInitializer$", ""); + _nuGetPackages.Add(new Library("nucs.JsonSettings", WizardConfig.UsePreReleaseVersion)); + _nuGetPackages.Add(new Library("nucs.JsonSettings.AutoSaveGenerator", WizardConfig.UsePreReleaseVersion)); } + _nuGetPackages = _nuGetPackages.Distinct().ToList(); #endregion if (templateConfig.HasNavigationView) @@ -341,7 +272,7 @@ public async void RunStarted(object automationObject, Dictionary #region Serilog var serilog = new SerilogOption(); - serilog.ConfigSerilog(replacementsDictionary, libs, WizardConfig.UseJsonSettings, WizardConfig.UseDeveloperModeSetting); + serilog.ConfigSerilog(replacementsDictionary, WizardConfig.LibraryDic, WizardConfig.UseJsonSettings, WizardConfig.UseDeveloperModeSetting); UseFileLogger = serilog.UseFileLogger; UseDebugLogger = serilog.UseDebugLogger; @@ -354,7 +285,7 @@ public async void RunStarted(object automationObject, Dictionary replacementsDictionary.AddIfNotExists("$GeneralSettingsCards$", ""); } - if (libs.ContainsKey("Serilog.Sinks.Debug") || libs.ContainsKey("Serilog.Sinks.File")) + if (WizardConfig.LibraryDic.ContainsKey("Serilog.Sinks.Debug") || WizardConfig.LibraryDic.ContainsKey("Serilog.Sinks.File")) { if (WizardConfig.UseJsonSettings && WizardConfig.UseDeveloperModeSetting && WizardConfig.UseSettingsPage && WizardConfig.UseGeneralSettingPage) { @@ -374,40 +305,6 @@ public async void RunStarted(object automationObject, Dictionary } #endregion - #region Json Settings - if (WizardConfig.UseJsonSettings) - { - if (templateConfig.IsMVVM) - { - replacementsDictionary.Add("$AppUpdateMVVMGetDateTime$", Environment.NewLine + """LastUpdateCheck = Settings.LastUpdateCheck;"""); - replacementsDictionary.Add("$AppUpdateMVVMSetDateTime$", Environment.NewLine + """Settings.LastUpdateCheck = DateTime.Now.ToShortDateString();"""); - } - else - { - replacementsDictionary.Add("$AppUpdateGetDateTime$", Environment.NewLine + Environment.NewLine + """TxtLastUpdateCheck.Text = Settings.LastUpdateCheck;"""); - replacementsDictionary.Add("$AppUpdateSetDateTime$", Environment.NewLine + """Settings.LastUpdateCheck = DateTime.Now.ToShortDateString();"""); - } - - replacementsDictionary.Add("$AppConfigFilePath$", Environment.NewLine + """public static readonly string AppConfigPath = Path.Combine(RootDirectoryPath, "AppConfig.json");"""); - - if (WizardConfig.UseAppUpdatePage && WizardConfig.UseSettingsPage) - { - replacementsDictionary.Add("$AppUpdateConfig$", Environment.NewLine + """private string lastUpdateCheck { get; set; }"""); - } - else - { - replacementsDictionary.Add("$AppUpdateConfig$", ""); - } - } - else - { - replacementsDictionary.Add("$AppUpdateMVVMGetDateTime$", ""); - replacementsDictionary.Add("$AppUpdateMVVMSetDateTime$", ""); - replacementsDictionary.Add("$AppConfigFilePath$", ""); - replacementsDictionary.Add("$AppUpdateConfig$", ""); - } - #endregion - new GlobalUsingOption(replacementsDictionary, SafeProjectName, UseFileLogger, UseDebugLogger); WizardConfig.LibraryDic?.Clear(); @@ -444,6 +341,87 @@ private void AddReplacementsDictionary(Dictionary replacementsDi replacementsDictionary.Add("$AddAboutPage$", WizardConfig.UseAboutPage.ToString()); replacementsDictionary.Add("$T4_NAMESPACE$", SafeProjectName); + #region CSProjectElements + // Add CSProjectElements + if (WizardConfig.CSProjectElements != null && WizardConfig.CSProjectElements.Count > 0) + { + StringBuilder sb = new StringBuilder(); + + foreach (var entity in WizardConfig.CSProjectElements) + { + sb.AppendLine($"{entity.Value}"); + } + + replacementsDictionary.Add("$CustomCSProjectElement$", Environment.NewLine + $"{sb.ToString().Trim()}"); + } + else + { + replacementsDictionary.Add("$CustomCSProjectElement$", ""); + } + + #endregion + + #region AppxManifest + if (WizardConfig.UnvirtualizedResources != null && WizardConfig.UnvirtualizedResources.Count > 0) + { + StringBuilder sb = new StringBuilder(); + + foreach (var entity in WizardConfig.UnvirtualizedResources) + { + sb.AppendLine($"{entity.Value}"); + } + + replacementsDictionary.Add("$UnvirtualizedResourcesCapability$", Environment.NewLine + " "); + replacementsDictionary.Add("$UnvirtualizedResources$", Environment.NewLine + $"{sb.ToString().Trim()}"); + replacementsDictionary.Add("$AppxManifestDesktop6$", Environment.NewLine + " xmlns:desktop6=\"http://schemas.microsoft.com/appx/manifest/desktop/windows10/6\""); + } + else + { + replacementsDictionary.Add("$UnvirtualizedResourcesCapability$", ""); + replacementsDictionary.Add("$UnvirtualizedResources$", ""); + replacementsDictionary.Add("$AppxManifestDesktop6$", ""); + } + #endregion + + #region Blank + + if (WizardConfig.IsBlank) + { + if (WizardConfig.LibraryDic != null && WizardConfig.LibraryDic.ContainsKey(Constants.DevWinUI_Controls)) + { + replacementsDictionary.Add("$DevWinUI.Controls$", Environment.NewLine + Constants.DevWinUI_Controls_Xaml); + } + else + { + replacementsDictionary.Add("$DevWinUI.Controls$", ""); + } + } + + #endregion + + #region Add Xaml Dictionary if User Use Extra Lib + if (WizardConfig.LibraryDic != null && WizardConfig.LibraryDic.ContainsKey(Constants.DevWinUI_ContextMenu)) + { + WizardConfig.UseWindow11ContextMenu = true; + replacementsDictionary.Add("$CLSID$", Guid.NewGuid().ToString()); + var windows11ContextMenu = PredefinedCodes.Windows11ContextMenuInitializer; + var windows11ContextMenuMVVM = PredefinedCodes.Windows11ContextMenuMVVMInitializer; + windows11ContextMenu = windows11ContextMenu.Replace("$projectname$", ProjectName); + windows11ContextMenuMVVM = windows11ContextMenuMVVM.Replace("$projectname$", ProjectName); + replacementsDictionary.Add("$Windows11ContextMenuInitializer$", Environment.NewLine + windows11ContextMenu); + replacementsDictionary.Add("$Windows11ContextMenuMVVMInitializer$", Environment.NewLine + Environment.NewLine + windows11ContextMenuMVVM); + replacementsDictionary.Add("$AsyncKeyword$", "async "); + } + else + { + WizardConfig.UseWindow11ContextMenu = false; + replacementsDictionary.Add("$CLSID$", ""); + replacementsDictionary.Add("$Windows11ContextMenuInitializer$", ""); + replacementsDictionary.Add("$Windows11ContextMenuMVVMInitializer$", ""); + replacementsDictionary.Add("$AsyncKeyword$", ""); + } + #endregion + #region IsUnPackaged if (WizardConfig.IsUnPackagedMode) { @@ -459,7 +437,7 @@ private void AddReplacementsDictionary(Dictionary replacementsDi if (WizardConfig.UseGeneralSettingPage && WizardConfig.UseStartupSetting) { var content = PredefinedCodes.SetPackagedAppTaskId.Replace("$safeprojectname$", SafeProjectName); - replacementsDictionary.Add("$PackagedAppTaskId$", Environment.NewLine + content); + replacementsDictionary.Add("$PackagedAppTaskId$", content); replacementsDictionary.Add("$UAP5$", Environment.NewLine + " xmlns:uap5=\"http://schemas.microsoft.com/appx/manifest/uap/windows10/5\""); if (WizardConfig.UseWindow11ContextMenu) @@ -472,7 +450,6 @@ private void AddReplacementsDictionary(Dictionary replacementsDi var taskContent = PredefinedCodes.StartupTask.Replace("$safeprojectname$", SafeProjectName); replacementsDictionary.Add("$StartupTask$", Environment.NewLine + taskContent); } - } else { @@ -483,26 +460,38 @@ private void AddReplacementsDictionary(Dictionary replacementsDi } #endregion - if (WizardConfig.UseWindow11ContextMenu) - { - replacementsDictionary.Add("$OnLaunchedAsyncKeyword$", "async "); - } - else + #region Json Settings + if (WizardConfig.UseJsonSettings) { - replacementsDictionary.Add("$OnLaunchedAsyncKeyword$", ""); - } + if (WizardConfig.IsMVVM) + { + replacementsDictionary.Add("$AppUpdateMVVMGetDateTime$", Environment.NewLine + """LastUpdateCheck = Settings.LastUpdateCheck;"""); + replacementsDictionary.Add("$AppUpdateMVVMSetDateTime$", Environment.NewLine + """Settings.LastUpdateCheck = DateTime.Now.ToShortDateString();"""); + } + else + { + replacementsDictionary.Add("$AppUpdateGetDateTime$", Environment.NewLine + Environment.NewLine + """TxtLastUpdateCheck.Text = Settings.LastUpdateCheck;"""); + replacementsDictionary.Add("$AppUpdateSetDateTime$", Environment.NewLine + """Settings.LastUpdateCheck = DateTime.Now.ToShortDateString();"""); + } - #region Libs - // Assuming package list is passed via a custom parameter in the .vstemplate file - if (replacementsDictionary.TryGetValue("$NuGetPackages$", out string packages)) - { - _nuGetPackages = new(); - var basePackages = packages.Split(';').Where(p => !string.IsNullOrEmpty(p)); - foreach (var baseItem in basePackages) + replacementsDictionary.Add("$AppConfigFilePath$", Environment.NewLine + """public static readonly string AppConfigPath = Path.Combine(RootDirectoryPath, "AppConfig.json");"""); + + if (WizardConfig.UseAppUpdatePage && WizardConfig.UseSettingsPage) + { + replacementsDictionary.Add("$AppUpdateConfig$", Environment.NewLine + """private string lastUpdateCheck { get; set; }"""); + } + else { - _nuGetPackages.Add(new Library(baseItem, WizardConfig.UsePreReleaseVersion)); + replacementsDictionary.Add("$AppUpdateConfig$", ""); } } + else + { + replacementsDictionary.Add("$AppUpdateMVVMGetDateTime$", ""); + replacementsDictionary.Add("$AppUpdateMVVMSetDateTime$", ""); + replacementsDictionary.Add("$AppConfigFilePath$", ""); + replacementsDictionary.Add("$AppUpdateConfig$", ""); + } #endregion } public bool ShouldAddProjectItem() diff --git a/VSIX/DevWinUI_Template/DevWinUI_Template/source.extension.vsixmanifest b/VSIX/DevWinUI_Template/DevWinUI_Template/source.extension.vsixmanifest index 2c2e0b4..6e2e4a4 100644 --- a/VSIX/DevWinUI_Template/DevWinUI_Template/source.extension.vsixmanifest +++ b/VSIX/DevWinUI_Template/DevWinUI_Template/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + DevWinUI Templates for WinUI DevWinUI Project Template, help you quickly create a new WinUI 3 App with DevWinUI and MVVM Packages. We prepare your project with the following features: NavigationView, Custom TitleBar, HomeLandingPage and Settings Page (with Theme settings). We also always use the latest version of WindowsAppSDK. diff --git a/VSIX/DevWinUI_Template/WinUIApp-MVVM-NavigationView/App.xaml.cs b/VSIX/DevWinUI_Template/WinUIApp-MVVM-NavigationView/App.xaml.cs index c53d7e1..8854569 100644 --- a/VSIX/DevWinUI_Template/WinUIApp-MVVM-NavigationView/App.xaml.cs +++ b/VSIX/DevWinUI_Template/WinUIApp-MVVM-NavigationView/App.xaml.cs @@ -49,7 +49,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) InitializeApp(); } - private $OnLaunchedAsyncKeyword$void InitializeApp() + private $AsyncKeyword$void InitializeApp() { $PackagedAppTaskId$$Windows11ContextMenuMVVMInitializer$$ConfigLogger$$UnhandeledException$ } diff --git a/VSIX/DevWinUI_Template/WinUIApp-MVVM/App.xaml.cs b/VSIX/DevWinUI_Template/WinUIApp-MVVM/App.xaml.cs index 40ff7c2..ee3e3dc 100644 --- a/VSIX/DevWinUI_Template/WinUIApp-MVVM/App.xaml.cs +++ b/VSIX/DevWinUI_Template/WinUIApp-MVVM/App.xaml.cs @@ -47,7 +47,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) InitializeApp(); } - private $OnLaunchedAsyncKeyword$void InitializeApp() + private $AsyncKeyword$void InitializeApp() { $Windows11ContextMenuMVVMInitializer$$ConfigLogger$$UnhandeledException$ } diff --git a/VSIX/DevWinUI_Template/WinUIApp-NavigationView/App.xaml.cs b/VSIX/DevWinUI_Template/WinUIApp-NavigationView/App.xaml.cs index f6fd7cc..1abd427 100644 --- a/VSIX/DevWinUI_Template/WinUIApp-NavigationView/App.xaml.cs +++ b/VSIX/DevWinUI_Template/WinUIApp-NavigationView/App.xaml.cs @@ -27,7 +27,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) InitializeApp(); } - private $OnLaunchedAsyncKeyword$void InitializeApp() + private $AsyncKeyword$void InitializeApp() { $PackagedAppTaskId$$Windows11ContextMenuInitializer$$ConfigLogger$$UnhandeledException$ } diff --git a/VSIX/DevWinUI_Template/WinUIApp/App.xaml.cs b/VSIX/DevWinUI_Template/WinUIApp/App.xaml.cs index 57b4920..96de442 100644 --- a/VSIX/DevWinUI_Template/WinUIApp/App.xaml.cs +++ b/VSIX/DevWinUI_Template/WinUIApp/App.xaml.cs @@ -25,7 +25,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) InitializeApp(); } - private $OnLaunchedAsyncKeyword$void InitializeApp() + private $AsyncKeyword$void InitializeApp() { $Windows11ContextMenuInitializer$$ConfigLogger$$UnhandeledException$ }