예제 #1
0
        public void SaveProject(IVsHierarchy project)
        {
            if (!cmdPackage.IsUseSolutionDirEnabled)
            {
                SaveJsonForProject(project);
            }
            else
            {
                string slnFilename  = vsHelper.GetSolutionFilename();
                string jsonFilename = Path.ChangeExtension(slnFilename, "args.json");

                using (solutionFsWatcher?.TemporarilyDisable())
                {
                    if (cmdPackage.ToolWindowViewModel.TreeViewModel.AllArguments.Any())
                    {
                        if (!vsHelper.CanEditFile(jsonFilename))
                        {
                            Logger.Error($"VS or the user did no let us edit our file :/ '{jsonFilename}'");
                        }
                        else
                        {
                            try
                            {
                                using (Stream fileStream = File.Open(jsonFilename, FileMode.Create, FileAccess.Write))
                                {
                                    SolutionDataSerializer.Serialize(cmdPackage.ToolWindowViewModel, fileStream);
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Warn($"Failed to write to file '{jsonFilename}' with error '{e}'.");
                            }
                        }
                    }
                    else
                    {
                        Logger.Info("Deleting solution json file because no project has command arguments but json file exists.");

                        try
                        {
                            File.Delete(jsonFilename);
                        }
                        catch (Exception e)
                        {
                            Logger.Warn($"Failed to delete file '{jsonFilename}' with error '{e}'.");
                        }
                    }
                }
            }
        }
예제 #2
0
        private void SaveJsonForSolution()
        {
            if (!cmdPackage.IsVcsSupportEnabled)
            {
                return;
            }

            string jsonFilename = FullFilenameForSolutionJsonFile();

            using (solutionFsWatcher?.TemporarilyDisable())
            {
                if (cmdPackage.ToolWindowViewModel.TreeViewModel.AllArguments.Any())
                {
                    if (!vsHelper.CanEditFile(jsonFilename))
                    {
                        Logger.Error($"VS or the user did no let us edit our file :/ '{jsonFilename}'");
                    }
                    else
                    {
                        try
                        {
                            using (Stream fileStream = File.Open(jsonFilename, FileMode.Create, FileAccess.Write))
                            {
                                SolutionDataSerializer.Serialize(cmdPackage.ToolWindowViewModel, fileStream);
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Warn($"Failed to write to file '{jsonFilename}' with error '{e}'.");
                        }
                    }
                }
                else
                {
                    Logger.Info("Deleting solution json file because no project has command arguments but json file exists.");

                    try
                    {
                        File.Delete(jsonFilename);
                    }
                    catch (Exception e)
                    {
                        Logger.Warn($"Failed to delete file '{jsonFilename}' with error '{e}'.");
                    }
                }
            }
        }
예제 #3
0
        public ProjectDataJson ReadDataForProject(IVsHierarchy project)
        {
            ProjectDataJson result = null;

            if (!cmdPackage.IsUseSolutionDirEnabled)
            {
                string filePath = FullFilenameForProjectJsonFileFromProject(project);

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

                return(result);
            }
            else
            {
                Guid   projectGui   = project.GetGuid();
                string slnFilename  = vsHelper.GetSolutionFilename();
                string jsonFilename = Path.ChangeExtension(slnFilename, "args.json");

                if (File.Exists(jsonFilename))
                {
                    try
                    {
                        using (Stream fileStream = File.Open(jsonFilename, FileMode.Open, FileAccess.Read))
                        {
                            SolutionDataJson slnData = SolutionDataSerializer.Deserialize(fileStream);

                            result = slnData.ProjectArguments.FirstOrDefault(p => p.Id == projectGui);
                        }
                        Logger.Info($"Read {result?.Items?.Count} commands for project '{project.GetName()}' from json-file '{jsonFilename}'.");
                    }
                    catch (Exception e)
                    {
                        Logger.Warn($"Failed to read file '{jsonFilename}' with error '{e}'.");
                        result = null;
                    }
                }
                else
                {
                    Logger.Info($"Json-file '{jsonFilename}' doesn't exists.");
                }

                return(result);
            }
        }