public static void Serialize(ToolWindowViewModel vm, Stream stream)
        {
            if (vm == null)
                throw new ArgumentNullException(nameof(vm));
            if (stream == null)
                throw new ArgumentNullException(nameof(stream));

            var data = new ToolWindowStateSolutionData();

            foreach (var kvp in vm.SolutionArguments)
            {
                var list = new ToolWindowStateProjectData();
                data.Add(kvp.Key.UniqueName, list);

                foreach (var item in kvp.Value.DataCollection)
                {
                    list.DataCollection.Add(new ToolWindowStateProjectData.ListEntryData()
                    {
                        Id = item.Id,
                        Command = item.Command,
                        //Project = item.Project,   // deprecated
                        Enabled = item.Enabled
                    });
                }
            }

            string jsonStr = JsonConvert.SerializeObject(data);

            StreamWriter sw = new StreamWriter(stream);
            sw.Write(jsonStr);
            sw.Flush();
        }
        public static void Serialize(ListViewModel vm, Stream stream)
        {
            if (vm == null)
                throw new ArgumentNullException(nameof(vm));
            if (stream == null)
                throw new ArgumentNullException(nameof(stream));

            var data = new ToolWindowStateProjectData();

            foreach (var item in vm.DataCollection)
            {
                data.DataCollection.Add(new ToolWindowStateProjectData.ListEntryData()
                {
                    Id = item.Id,
                    Command = item.Command,
                    //Project = item.Project,   // deprecated
                    //Enabled = item.Enabled
                });
            }

            string jsonStr = JsonConvert.SerializeObject(data, Formatting.Indented);

            StreamWriter sw = new StreamWriter(stream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
            sw.Write(jsonStr);
            sw.Flush();
        }
Ejemplo n.º 3
0
        public void PopulateFromProjectData(string projectName, ToolWindowStateProjectData data)
        {
            var curListVM = GetListViewModel(projectName);

            curListVM.DataCollection.Clear();
            curListVM.DataCollection.AddRange(
                data.DataCollection.Select(
                    // TODO check dup key
                    item => new CmdArgItem {
                Id      = item.Id,
                Command = item.Command,
                Enabled = item.Enabled
            }));
        }
        private void UpdateCommandsForProject(IVsHierarchy project)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            Logger.Info($"Update commands for project '{project?.GetName()}'. IsVcsSupportEnabled={IsVcsSupportEnabled}. SolutionData.Count={toolWindowStateLoadedFromSolution?.ProjectArguments?.Count}.");

            var projectGuid = project.GetGuid();

            if (projectGuid == Guid.Empty)
            {
                Logger.Info("Skipping project because guid euqals empty.");
                return;
            }

            var solutionData = toolWindowStateLoadedFromSolution ?? new ToolWindowStateSolutionData();

            ToolWindowViewModel.TreeViewModel.ShowAllProjects = solutionData.ShowAllProjects;

            // joins data from solution and project
            //  => overrides solution commands for a project if a project json file exists
            //  => keeps all data from the suo file for projects without a json
            //  => if we have data in our ViewModel we use this instad of the suo file

            // get project json data
            ToolWindowStateProjectData projectData = null;

            if (IsVcsSupportEnabled)
            {
                string filePath = FullFilenameForProjectJsonFileFromProject(project);

                if (File.Exists(filePath))
                {
                    try
                    {
                        using (Stream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read))
                        {
                            projectData = Logic.ToolWindowProjectDataSerializer.Deserialize(fileStream);
                        }
                        Logger.Info($"Read {projectData?.Items?.Count} commands for project '{project.GetName()}' from json-file '{filePath}'.");
                    }
                    catch (Exception e)
                    {
                        Logger.Warn($"Failed to read file '{filePath}' with error '{e}'.");
                        projectData = null;
                    }
                }
                else
                {
                    Logger.Info($"Json-file '{filePath}' doesn't exists.");
                }
            }

            // project json overrides if it exists
            if (projectData != null)
            {
                Logger.Info($"Setting {projectData?.Items?.Count} commands for project '{project.GetName()}' from json-file.");

                var projectListViewModel = ToolWindowViewModel.TreeViewModel.Projects.GetValueOrDefault(projectGuid);

                // update enabled state of the project json data (source prio: ViewModel > suo file)
                if (projectData.Items != null)
                {
                    var argumentDataFromProject = projectData.AllArguments;
                    var argumentDataFromLVM     = projectListViewModel?.AllArguments.ToDictionary(arg => arg.Id, arg => arg);
                    foreach (var dataFromProject in argumentDataFromProject)
                    {
                        if (argumentDataFromLVM != null && argumentDataFromLVM.TryGetValue(dataFromProject.Id, out CmdArgument argFromVM))
                        {
                            dataFromProject.Enabled = argFromVM.IsChecked;
                        }
                        else
                        {
                            dataFromProject.Enabled = solutionData.CheckedArguments.Contains(dataFromProject.Id);
                        }
                    }

                    var containerDataFromProject = projectData.AllContainer;
                    var containerDataFromLVM     = projectListViewModel?.AllContainer.ToDictionary(con => con.Id, con => con);
                    foreach (var dataFromProject in containerDataFromProject)
                    {
                        if (containerDataFromLVM != null && containerDataFromLVM.TryGetValue(dataFromProject.Id, out CmdContainer conFromVM))
                        {
                            dataFromProject.Expanded = conFromVM.IsExpanded;
                        }
                        else
                        {
                            dataFromProject.Expanded = solutionData.ExpandedContainer.Contains(dataFromProject.Id);
                        }
                    }

                    if (projectListViewModel != null)
                    {
                        projectData.Expanded = projectListViewModel.IsExpanded;
                    }
                    else
                    {
                        projectData.Expanded = solutionData.ExpandedContainer.Contains(projectData.Id);
                    }
                }
                else
                {
                    projectData = new ToolWindowStateProjectData();
                    Logger.Info($"DataCollection for project '{project.GetName()}' is null.");
                }
            }
            // if we have data in the ViewModel we keep it
            else if (ToolWindowViewModel.TreeViewModel.Projects.ContainsKey(projectGuid))
            {
                return;
            }
            else if (IsVcsSupportEnabled)
            {
                projectData = new ToolWindowStateProjectData();
                Logger.Info("Will clear all data because of missing json file and enabled VCS support.");
            }
            // we try to read the suo file data
            else if (solutionData.ProjectArguments.TryGetValue(projectGuid, out projectData))
            {
                Logger.Info($"Will use commands from suo file for project '{project.GetName()}'.");
                var argumentDataFromProject = projectData.AllArguments;
                foreach (var arg in argumentDataFromProject)
                {
                    arg.Enabled = solutionData.CheckedArguments.Contains(arg.Id);
                }

                var containerDataFromProject = projectData.AllContainer;
                foreach (var con in containerDataFromProject)
                {
                    con.Expanded = solutionData.ExpandedContainer.Contains(con.Id);
                }

                projectData.Expanded = solutionData.ExpandedContainer.Contains(projectData.Id);
            }
            else
            {
                Logger.Info($"Gathering commands from configurations for project '{project.GetName()}'.");
                // if we don't have suo file data we read cmd args from the project configs
                projectData = new ToolWindowStateProjectData();
                projectData.Items.AddRange(
                    ReadCommandlineArgumentsFromProject(project)
                    .Select(cmdLineArg => new ListEntryData {
                    Command = cmdLineArg
                }));
            }

            // push projectData to the ViewModel
            ToolWindowViewModel.PopulateFromProjectData(project, projectData);

            Logger.Info($"Updated Commands for project '{project.GetName()}'.");
        }
 public void PopulateFromProjectData(Project projectName, ToolWindowStateProjectData data)
 {
     var curListVM = GetListViewModel(projectName);
     curListVM.DataCollection.Clear();
     curListVM.DataCollection.AddRange(
         data.DataCollection.Select(
             // TODO check dup key
             item => new CmdArgItem {
                 Id = item.Id,
                 Command = item.Command,
                 Enabled = item.Enabled
             }));
 }
        private void UpdateCommandsForProject(string projectName)
        {
            if (string.IsNullOrEmpty(projectName))
            {
                throw new ArgumentNullException(nameof(projectName));
            }

            Project project = vsHelper.ProjectForProjectName(projectName);

            Logger.Info($"Update commands for project '{projectName}'. IsVcsSupportEnabled={IsVcsSupportEnabled}. SolutionData.Count={toolWindowStateLoadedFromSolution?.Count}.");

            var solutionData = toolWindowStateLoadedFromSolution ?? new ToolWindowStateSolutionData();

            // joins data from solution and project
            //  => overrides solution commands for a project if a project json file exists
            //  => keeps all data from the suo file for projects without a json
            //  => if we have data in our ViewModel we use this instad of the suo file

            // get project json data
            ToolWindowStateProjectData projectData = null;

            if (IsVcsSupportEnabled)
            {
                string filePath = FullFilenameForProjectJsonFileFromProject(project);

                if (File.Exists(filePath))
                {
                    try
                    {
                        using (Stream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read))
                        {
                            projectData = Logic.ToolWindowProjectDataSerializer.Deserialize(fileStream);
                        }
                        Logger.Info($"Read {projectData?.DataCollection?.Count} commands for project '{projectName}' from json-file '{filePath}'.");
                    }
                    catch (Exception e)
                    {
                        Logger.Warn($"Failed to read file '{filePath}' with error '{e}'.");
                        projectData = null;
                    }
                }
                else
                {
                    Logger.Info($"Json-file '{filePath}' doesn't exists.");
                }
            }

            // project json overrides if it exists
            if (projectData != null)
            {
                Logger.Info($"Setting {projectData?.DataCollection?.Count} commands for project '{projectName}' from json-file.");

                ToolWindowStateProjectData curSolutionProjectData = solutionData.GetValueOrDefault(projectName);
                ListViewModel projectListViewModel = ToolWindowViewModel.SolutionArguments.GetValueOrDefault(projectName);

                // check if we have data in the suo file or the ViewModel
                if (curSolutionProjectData != null || projectListViewModel != null)
                {
                    // update enabled state of the project json data (source prio: ViewModel > suo file)
                    var dataCollectionFromProject = projectData?.DataCollection;
                    if (dataCollectionFromProject != null)
                    {
                        var dataCollectionFromSolution = curSolutionProjectData?.DataCollection;
                        var dataCollectionFromLVM      = projectListViewModel?.DataCollection;
                        foreach (var dataFromProject in dataCollectionFromProject)
                        {
                            var dataFromVM = dataCollectionFromLVM?.FirstOrDefault(data => data.Id == dataFromProject.Id);

                            if (dataFromVM != null)
                            {
                                dataFromProject.Enabled = dataFromVM.Enabled;
                            }
                            else
                            {
                                var dataFromSolution =
                                    dataCollectionFromSolution?.Find(data => data.Id == dataFromProject.Id);

                                if (dataFromSolution != null)
                                {
                                    dataFromProject.Enabled = dataFromSolution.Enabled;
                                }
                            }
                        }
                    }
                    else
                    {
                        projectData = new ToolWindowStateProjectData();
                        Logger.Info($"DataCollection for project '{projectName}' is null.");
                    }
                }
            }
            // if we have data in the ViewModel we keep it
            else if (ToolWindowViewModel.SolutionArguments.ContainsKey(projectName))
            {
                return;
            }
            // we try to read the suo file data
            else if (!solutionData.TryGetValue(projectName, out projectData))
            {
                Logger.Info($"Gathering commands from configurations for project '{projectName}'.");
                // if we don't have suo file data we read cmd args from the project configs
                projectData = new ToolWindowStateProjectData();
                projectData.DataCollection.AddRange(
                    ReadCommandlineArgumentsFromProject(project)
                    .Select(cmdLineArg => new ToolWindowStateProjectData.ListEntryData {
                    Command = cmdLineArg
                }));
            }
            else if (IsVcsSupportEnabled)
            {
                projectData = new ToolWindowStateProjectData();
                Logger.Info("Will clear all data because of missing json file and enabled VCS support.");
            }
            else
            {
                Logger.Info($"Will use commands from suo file for project '{projectName}'.");
            }

            // push projectData to the ViewModel
            ToolWindowViewModel.PopulateFromProjectData(projectName, projectData);

            Logger.Info($"Updated Commands for project '{projectName}'.");
        }