public void MoveProjectFileIntoSolutionFolder(SolutionFile solutionFile, string solutionFilePath, string projectFilePath, string solutionFolderPath) { var hasProjectReference = this.HasProjectReference(solutionFile, solutionFilePath, projectFilePath, out var projectReference); if (!hasProjectReference) { throw new Exception($"Project does not exist: {projectFilePath}"); } var hasSolutionFolder = this.HasSolutionFolder(solutionFile, solutionFolderPath, out var solutionFolder); if (!hasSolutionFolder) { throw new Exception($"Solution folder does not exist: {solutionFolderPath}"); } // Test if project file is already in the solution folder. var nestedProjectsGlobalSection = solutionFile.GlobalSections.AcquireNestedProjectsGlobalSection(); var solutionFolderContainsProjectFile = nestedProjectsGlobalSection.ProjectNestings.Where(x => x.ParentProjectGUID == solutionFolder.ProjectGUID && x.ChildProjectGUID == projectReference.ProjectGUID).Any(); if (solutionFolderContainsProjectFile) { throw new Exception($"Solution folder already contains project file.\nSolution Folder:{solutionFolder.ProjectName}\nProject File: {projectFilePath}"); } var projectNesting = new ProjectNesting() { ParentProjectGUID = solutionFolder.ProjectGUID, ChildProjectGUID = projectReference.ProjectGUID, }; nestedProjectsGlobalSection.ProjectNestings.Add(projectNesting); }
private SolutionFileProjectReference AcquireAndAddChildSolutionFolderProject(SolutionFile solutionFile, string solutionFolderName, SolutionFileProjectReference parentSolutionFolderProject) { var solutionFolderExists = this.HasChildSolutionFolderProject(solutionFile, solutionFolderName, parentSolutionFolderProject, out var solutionFolderProject); if (!solutionFolderExists) { // Solution folder does NOT exist. Create it. solutionFolderProject = this.CreateNewSolutionFolder(solutionFolderName); // Add the project reference. solutionFile.SolutionFileProjectReferences.Add(solutionFolderProject); // But also add a nested project. var solutionFolderNesting = new ProjectNesting() { ChildProjectGUID = solutionFolderProject.ProjectGUID, ParentProjectGUID = parentSolutionFolderProject.ProjectGUID, }; var nestedProjectsGlobalSection = solutionFile.GlobalSections.AcquireNestedProjectsGlobalSection(); nestedProjectsGlobalSection.ProjectNestings.Add(solutionFolderNesting); } return(solutionFolderProject); }
private static NestedProjectsSolutionFileGlobalSection DeserializeNestedProjectsGlobalSection(TextReader reader, ref string currentLine, PreOrPostSolution preOrPostSolution) { var nestedProjectGlobalSection = new NestedProjectsSolutionFileGlobalSection { Name = NestedProjectsSolutionFileGlobalSection.SolutionFileGlobalSectionName, PreOrPostSolution = preOrPostSolution }; currentLine = reader.ReadLine().Trim(); while (!SolutionFileTextSerializer.GlobalSectionEndRegex.IsMatch(currentLine)) { var projectNesting = ProjectNesting.Deserialize(currentLine); nestedProjectGlobalSection.ProjectNestings.Add(projectNesting); currentLine = reader.ReadLine().Trim(); } return(nestedProjectGlobalSection); }