private void SolutionServiceOnSolutionLoadStateChanged(object sender, EventArgs eventArgs) { // Something happened in the solution or a project, respond by reloading solution // and project state. Current state is ignored/discarded. // Start by preventing a recursive call. _solutionService.SolutionLoadStateChanged -= SolutionServiceOnSolutionLoadStateChanged; IsBusy = true; // Load settings. This takes a while because the solution may not be fully // loaded yet or some other solution or project operation may be under way. var loadSettingsTask = SettingsSvc.LoadSettings(); loadSettingsTask.ContinueWith( task => { IsBusy = false; var settings = task.Result; this.TemplateBrowseUserControlViewModel.Init(settings.LocalTemplateFolder); // Save the original, unmodified settings. _unmodifiedSettings = settings; // This actually applies the _unmodifiedSettings to the properties. RevertSettings().Forget(); // Now subscribe to the Solution Service's SolutionLoadStateChanged event so // we can do all this over again when the solution or projects change again. _solutionService.SolutionLoadStateChanged += SolutionServiceOnSolutionLoadStateChanged; }, TaskContinuationOptions.ExecuteSynchronously); }
public void CheckpointSettings() { // Saves the view model properties into _unmodifiedSettings so that future calls // to RevertSettings() can go back to this point. This method is typically called // when the user hits Apply or OK on the settings window. _unmodifiedSettings = GetCurrentSettings(); }
public MvvmToolsSettings GetCurrentSettings() { if (ProjectsOptions == null || ViewSuffixes == null) { return(null); } // Extracts settings from view model properties. These are the // 'current' settings, while the unmodified settings values are stored in // _unmodifiedSettings. var settings = new MvvmToolsSettings { GoToViewOrViewModelOption = SelectedGoToViewOrViewModelOption, GoToViewOrViewModelSearchSolution = GoToViewOrViewModelSearchSolution, ViewSuffixes = ViewSuffixes.Select(vs => vs.Value).ToArray(), LocalTemplateFolder = LocalTemplateFolder }; for (int index = 0; index < ProjectsOptions.Count; index++) { // First item is the solution, subsequent items are the projects. if (index == 0) { settings.SolutionOptions = ConvertToProjectOptions(ProjectsOptions[0]); } else { var po = ProjectsOptions[index]; settings.ProjectOptions.Add(ConvertToProjectOptions(po)); } } return(settings); }
public void SaveSettings([NotNull] MvvmToolsSettings settings) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (!_userSettingsStore.CollectionExists(SettingsPropName)) { _userSettingsStore.CreateCollection(SettingsPropName); } SetEnum(GoToViewOrViewModelPropName, settings.GoToViewOrViewModelOption); SetBool(GoToViewOrViewModelSearchSolutionPropName, settings.GoToViewOrViewModelSearchSolution); SetStringCollection(ViewSuffixesPropName, settings.ViewSuffixes); SetString(LocalTemplateFolderPropName, settings.LocalTemplateFolder); // If a solution is loaded... if (settings.SolutionOptions != null) { // Save solution and project option files, or delete them if they // match the inherited values. SaveOrDeleteSettingsFile(settings.SolutionOptions, SolutionDefaultProjectOptions); foreach (var p in settings.ProjectOptions) { if (!string.IsNullOrWhiteSpace(p?.ProjectModel?.SettingsFile)) { SaveOrDeleteSettingsFile(p, settings.SolutionOptions); } } } }
/// <summary> /// This is async because it must wait for the solution to finish its operation(s). /// </summary> /// <returns></returns> public async Task <MvvmToolsSettings> LoadSettings() { // rval starts out containing default values, exceptLocalTemplateFolder, which is null // so we use our DefaultLocalTemplateFolder, which is a subfolder off My Docs. var rval = new MvvmToolsSettings { GoToViewOrViewModelOption = GetEnum(GoToViewOrViewModelPropName, GoToViewOrViewModelOption.ShowUi), GoToViewOrViewModelSearchSolution = GetBool(GoToViewOrViewModelSearchSolutionPropName, true), ViewSuffixes = GetStringCollection(ViewSuffixesPropName, DefaultViewSuffixes), LocalTemplateFolder = GetString(LocalTemplateFolderPropName, DefaultLocalTemplateFolder) }; // Get any saved settings // Make sure the LocalTemplateFolder setting exists in our saved values. It might not // because the setting is being introduced in version 0.5 of MVVM Tools. if (string.IsNullOrWhiteSpace(rval.LocalTemplateFolder)) { rval.LocalTemplateFolder = DefaultLocalTemplateFolder; } try { if (!Directory.Exists(rval.LocalTemplateFolder)) { // This creates all the intermediate folders as well, if they don't exist. Directory.CreateDirectory(rval.LocalTemplateFolder); } } catch (Exception ex) { Trace.WriteLine($"In {nameof(SettingsService)}.{nameof(LoadSettings)}(), failed to check existence of or create local template folder. Value: {rval.LocalTemplateFolder}" + Environment.NewLine + ex); await _dialogService.ShowMessage("Error", "Couldn't read or create the local template folder, which was:" + Environment.NewLine + Environment.NewLine + "\t" + rval.LocalTemplateFolder + Environment.NewLine + ex); throw; } // Get solution's structure. Waits for solution operations to complete. var solution = await _solutionService.GetSolution(); if (solution == null) { return(rval); } // Get options for solution. Apply global defaults to solution // file if it doesn't exist. var solutionOptions = GetProjectOptionsFromSettingsFile(solution, SolutionDefaultProjectOptions); rval.SolutionOptions = solutionOptions; // Get options for each project. Inherited solution options are // applied for each project file if it doesn't exist. AddProjectOptionsFlattenedRecursive(solutionOptions, rval.ProjectOptions, solution.Children); return(rval); }
public async Task Init() { Title = "Scaffold View and ViewModel"; IsBusy = true; _settings = await SettingsService.LoadSettings(); Projects = _settings.ProjectOptions; _projOptions = new List <ProjectOptions>(Projects); var projId = Package.ActiveDocument?.ProjectItem?.ContainingProject?.UniqueName ?? Projects.FirstOrDefault()?.ProjectModel.ProjectIdentifier; SettingsProject = Projects.FirstOrDefault(p => p.ProjectModel.ProjectIdentifier == projId); Name = null; ViewSuffixes = new ObservableCollection <string>(_settings.ViewSuffixes); ViewSuffixes.Insert(0, string.Empty); SelectedViewSuffix = ViewSuffixes[0]; TemplateBrowse = Kernel.Get <TemplateBrowseUserControlViewModel>(); TemplateBrowse.Init(_settings.LocalTemplateFolder); FieldValues = Kernel.Get <FieldValuesUserControlViewModel>(); View = Kernel.Get <T4UserControlViewModel>(); CodeBehindCSharp = Kernel.Get <T4UserControlViewModel>(); ViewModelCSharp = Kernel.Get <T4UserControlViewModel>(); CodeBehindVisualBasic = Kernel.Get <T4UserControlViewModel>(); ViewModelVisualBasic = Kernel.Get <T4UserControlViewModel>(); IsBusy = false; }