示例#1
0
        private void ValidateProjectInSolution(Action <SlnProject, ProjectInSolution> customValidator, SlnProject[] projects, bool folders)
        {
            string solutionFilePath = GetTempFileName();

            SlnFile slnFile = new SlnFile();

            slnFile.AddProjects(projects);
            slnFile.Save(solutionFilePath, folders);

            SolutionFile solutionFile = SolutionFile.Parse(solutionFilePath);

            foreach (SlnProject slnProject in projects)
            {
                solutionFile.ProjectsByGuid.ContainsKey(slnProject.ProjectGuid.ToSolutionString()).ShouldBeTrue();

                ProjectInSolution projectInSolution = solutionFile.ProjectsByGuid[slnProject.ProjectGuid.ToSolutionString()];

                projectInSolution.AbsolutePath.ShouldBe(slnProject.FullPath);
                projectInSolution.ProjectGuid.ShouldBe(slnProject.ProjectGuid.ToSolutionString());
                projectInSolution.ProjectName.ShouldBe(slnProject.Name);

                IEnumerable <string> expected = slnProject.Configurations.SelectMany(configuration => slnProject.Platforms, (configuration, platform) => $"{configuration}|{platform}");
                IEnumerable <string> actual   = projectInSolution.ProjectConfigurations.Where(i => i.Value.IncludeInBuild).Select(i => i.Key);

                expected.ShouldBe(actual, ignoreOrder: true);

                customValidator?.Invoke(slnProject, projectInSolution);
            }
        }
示例#2
0
        private void GenerateSolutionFile(ICollection <Project> projects)
        {
            if (String.IsNullOrWhiteSpace(SolutionFileFullPath))
            {
                SolutionFileFullPath = Path.ChangeExtension(ProjectFullPath, ".sln");
            }

            Dictionary <string, Guid> customProjectTypeGuids = ParseCustomProjectTypeGuids();

            LogMessageHigh($"Generating Visual Studio solution \"{SolutionFileFullPath}\" ...");

            if (customProjectTypeGuids.Count > 0)
            {
                LogMessageLow("Custom Project Type GUIDs:");
                foreach (KeyValuePair <string, Guid> item in customProjectTypeGuids)
                {
                    LogMessageLow("  {0} = {1}", item.Key, item.Value);
                }
            }

            SlnFile solution = new SlnFile();

            solution.AddProjects(
                projects.Where(ShouldIncludeInSolution)
                .Select(p => SlnProject.FromProject(p, customProjectTypeGuids, p.FullPath == ProjectFullPath)));

            solution.AddSolutionItems(GetSolutionItems());

            solution.Save(SolutionFileFullPath, Folders);
        }
示例#3
0
        private void ValidateProjectInSolution(Action <SlnProject, ProjectInSolution> customValidator, SlnProject[] projects, bool folders)
        {
            string solutionFilePath = GetTempFileName();

            SlnFile slnFile = new SlnFile();

            slnFile.AddProjects(projects);
            slnFile.Save(solutionFilePath, folders);

            SolutionFile solutionFile = SolutionFile.Parse(solutionFilePath);

            foreach (SlnProject slnProject in projects)
            {
                solutionFile.ProjectsByGuid.ContainsKey(slnProject.ProjectGuid.ToSolutionString()).ShouldBeTrue();

                ProjectInSolution projectInSolution = solutionFile.ProjectsByGuid[slnProject.ProjectGuid.ToSolutionString()];

                projectInSolution.AbsolutePath.ShouldBe(slnProject.FullPath);
                projectInSolution.ProjectGuid.ShouldBe(slnProject.ProjectGuid.ToSolutionString());
                projectInSolution.ProjectName.ShouldBe(slnProject.Name);

                IEnumerable <string> configurationPlatforms = from configuration in slnProject.Configurations from platform in slnProject.Platforms select $"{configuration}|{platform}";

                configurationPlatforms.ShouldBe(projectInSolution.ProjectConfigurations.Keys, ignoreOrder: true);

                customValidator?.Invoke(slnProject, projectInSolution);
            }
        }
示例#4
0
        public void ProjectSolutionFolders()
        {
            string root         = Path.GetTempPath();
            string projectName1 = Path.GetFileName(Path.GetTempFileName());
            string projectName2 = Path.GetFileName(Path.GetTempFileName());
            string projectName3 = Path.GetFileName(Path.GetTempFileName());
            string projectName4 = Path.GetFileName(Path.GetTempFileName());

            Project[] projects =
            {
                new Project
                {
                    FullPath = Path.Combine(root, "SubFolder1", "Project1", projectName1),
                },
                new Project
                {
                    FullPath = Path.Combine(root, "SubFolder2", "Project2", projectName2),
                },
                new Project
                {
                    FullPath = Path.Combine(root, "SubFolder3", "Project3", projectName3),
                },
                new Project
                {
                    FullPath = Path.Combine(root, "SubFolder4", "Project4", projectName4),
                },
            };

            projects[0].SetProperty(MSBuildPropertyNames.SlnGenSolutionFolder, "FolderA");
            projects[1].SetProperty(MSBuildPropertyNames.SlnGenSolutionFolder, "FolderB");
            projects[2].SetProperty(MSBuildPropertyNames.SlnGenSolutionFolder, "FolderB");

            string solutionFilePath = GetTempFileName();

            SlnFile slnFile = new SlnFile();

            slnFile.AddProjects(projects, new Dictionary <string, Guid>(), projects[1].FullPath);
            slnFile.Save(solutionFilePath, useFolders: false);

            SolutionFile s = SolutionFile.Parse(solutionFilePath);

            ProjectInSolution project1 = s.ProjectsByGuid.FirstOrDefault(i => i.Value.ProjectName.Equals(Path.GetFileNameWithoutExtension(projectName1))).Value;
            ProjectInSolution project2 = s.ProjectsByGuid.FirstOrDefault(i => i.Value.ProjectName.Equals(Path.GetFileNameWithoutExtension(projectName2))).Value;
            ProjectInSolution project3 = s.ProjectsByGuid.FirstOrDefault(i => i.Value.ProjectName.Equals(Path.GetFileNameWithoutExtension(projectName3))).Value;
            ProjectInSolution project4 = s.ProjectsByGuid.FirstOrDefault(i => i.Value.ProjectName.Equals(Path.GetFileNameWithoutExtension(projectName4))).Value;
            ProjectInSolution folderA  = s.ProjectsByGuid.FirstOrDefault(i => i.Value.ProjectName.Equals("FolderA")).Value;
            ProjectInSolution folderB  = s.ProjectsByGuid.FirstOrDefault(i => i.Value.ProjectName.Equals("FolderB")).Value;

            project1.ParentProjectGuid.ShouldBe(folderA.ProjectGuid);
            project2.ParentProjectGuid.ShouldBe(folderB.ProjectGuid);
            project3.ParentProjectGuid.ShouldBe(folderB.ProjectGuid);
            project4.ParentProjectGuid.ShouldBeNull();
            folderA.ProjectType.ShouldBe(SolutionProjectType.SolutionFolder);
            folderB.ProjectType.ShouldBe(SolutionProjectType.SolutionFolder);
        }
示例#5
0
        public void WithFoldersDoNotIgnoreMainProject()
        {
            var root         = Path.GetTempPath();
            var projectName1 = Path.GetFileName(Path.GetTempFileName());
            var projectName2 = Path.GetFileName(Path.GetTempFileName());
            var projectName3 = Path.GetFileName(Path.GetTempFileName());

            Project[] projects =
            {
                new Project
                {
                    FullPath = Path.Combine(root, "SubFolder1", "Project1", projectName1),
                },
                new Project
                {
                    FullPath = Path.Combine(root, "SubFolder1", "Project2", projectName2),
                },
                new Project
                {
                    FullPath = Path.Combine(root, "SubFolder2", "Project3", projectName3),
                },
            };

            string solutionFilePath = GetTempFileName();

            SlnFile slnFile = new SlnFile();

            slnFile.AddProjects(projects, new Dictionary <string, Guid>(), projects[1].FullPath);
            slnFile.Save(solutionFilePath, true);

            SolutionFile solutionFile = SolutionFile.Parse(solutionFilePath);

            foreach (var slnProject in solutionFile.ProjectsInOrder)
            {
                if (slnProject.ProjectName == projectName1)
                {
                    slnProject.ParentProjectGuid.ShouldBeNull();
                }
                else if (slnProject.ProjectName == projectName2)
                {
                    slnProject.ParentProjectGuid.ShouldNotBeNull();
                }
                else if (slnProject.ProjectName == projectName3)
                {
                    slnProject.ParentProjectGuid.ShouldNotBeNull();
                }
            }
        }
示例#6
0
        public void CustomConfigurationAndPlatforms_IgnoresInvalidValues()
        {
            SlnProject project = new SlnProject
            {
                Configurations  = new[] { "Debug", "Release" },
                FullPath        = GetTempFileName(),
                IsMainProject   = true,
                Name            = "ProjectA",
                Platforms       = new[] { "AnyCPU" },
                ProjectGuid     = Guid.NewGuid(),
                ProjectTypeGuid = Guid.NewGuid(),
            };

            SlnFile slnFile = new SlnFile()
            {
                Configurations = new[] { "Debug" },
                Platforms      = new[] { "Any CPU", "AnyCPU", "Invalid", "x64", "x86", "X64", "X86" },
            };

            slnFile.AddProjects(new[] { project });

            string solutionFilePath = GetTempFileName();

            slnFile.Save(solutionFilePath, useFolders: false);

            SolutionFile solutionFile = SolutionFile.Parse(solutionFilePath);

            solutionFile.SolutionConfigurations
            .Select(i => i.FullName)
            .ShouldBe(
                new[]
            {
                "Debug|Any CPU",
                "Debug|x64",
                "Debug|x86",
            });

            ProjectInSolution projectInSolution = solutionFile.ProjectsByGuid[project.ProjectGuid.ToSolutionString()];

            projectInSolution.AbsolutePath.ShouldBe(project.FullPath);

            projectInSolution.ProjectConfigurations.Keys.ShouldBe(new[] { "Debug|Any CPU", "Debug|x64", "Debug|x86" });

            ValidateSolutionPlatformAndConfiguration(projectInSolution.ProjectConfigurations, "Debug|Any CPU", "Debug", "AnyCPU");
            ValidateSolutionPlatformAndConfiguration(projectInSolution.ProjectConfigurations, "Debug|x64", "Debug", "AnyCPU");
            ValidateSolutionPlatformAndConfiguration(projectInSolution.ProjectConfigurations, "Debug|x86", "Debug", "AnyCPU");
        }
示例#7
0
        public void ExistingSolutionIsReused()
        {
            string path = GetTempFileName();

            Guid projectGuid = Guid.Parse("7BE5A5CA-169D-4955-AB4D-EDDE662F4AE5");

            SlnProject project = new SlnProject
            {
                FullPath        = GetTempFileName(),
                Name            = "Project",
                ProjectGuid     = Guid.NewGuid(),
                ProjectTypeGuid = Guid.NewGuid(),
                IsMainProject   = true,
            };

            SlnFile slnFile = new SlnFile
            {
                ExistingProjectGuids = new Dictionary <string, Guid>
                {
                    [project.FullPath] = projectGuid,
                },
                SolutionGuid = Guid.NewGuid(),
            };

            slnFile.AddProjects(new[] { project });

            slnFile.Save(path, useFolders: false);

            SlnFile.TryParseExistingSolution(path, out Guid solutionGuid, out _).ShouldBeTrue();

            solutionGuid.ShouldBe(slnFile.SolutionGuid);

            SolutionFile solutionFile = SolutionFile.Parse(path);

            ProjectInSolution projectInSolution = solutionFile.ProjectsInOrder.ShouldHaveSingleItem();

            projectInSolution.ProjectGuid.ShouldBe(projectGuid.ToString("B").ToUpperInvariant());
        }
示例#8
0
        private void ValidateProjectInSolution(Action <SlnProject, ProjectInSolution> customValidator, SlnProject[] projects, bool folders)
        {
            string solutionFilePath = GetTempFileName();

            SlnFile slnFile = new SlnFile();

            slnFile.AddProjects(projects);
            slnFile.Save(solutionFilePath, folders);

            SolutionFile solutionFile = SolutionFile.Parse(solutionFilePath);

            foreach (SlnProject slnProject in projects)
            {
                solutionFile.ProjectsByGuid.ContainsKey(slnProject.ProjectGuid.ToSolutionString()).ShouldBeTrue();

                ProjectInSolution projectInSolution = solutionFile.ProjectsByGuid[slnProject.ProjectGuid.ToSolutionString()];

                projectInSolution.AbsolutePath.ShouldBe(slnProject.FullPath);
                projectInSolution.ProjectGuid.ShouldBe(slnProject.ProjectGuid.ToSolutionString());
                projectInSolution.ProjectName.ShouldBe(slnProject.Name);

                customValidator?.Invoke(slnProject, projectInSolution);
            }
        }
示例#9
0
        public void CustomConfigurationAndPlatforms()
        {
            SlnProject projectA = new SlnProject
            {
                Configurations  = new[] { "Debug", "Release" },
                FullPath        = GetTempFileName(),
                IsMainProject   = true,
                Name            = "ProjectA",
                Platforms       = new[] { "AnyCPU", "x64", "x86" },
                ProjectGuid     = Guid.NewGuid(),
                ProjectTypeGuid = Guid.NewGuid(),
            };

            SlnProject projectB = new SlnProject
            {
                Configurations  = new[] { "Debug", "Release" },
                FullPath        = GetTempFileName(),
                IsMainProject   = true,
                Name            = "ProjectB",
                Platforms       = new[] { "x64", "x86" },
                ProjectGuid     = Guid.NewGuid(),
                ProjectTypeGuid = Guid.NewGuid(),
            };

            SlnProject projectC = new SlnProject
            {
                Configurations  = new[] { "Debug", "Release" },
                FullPath        = GetTempFileName(),
                IsMainProject   = true,
                Name            = "ProjectC",
                Platforms       = new[] { "amd64" },
                ProjectGuid     = Guid.NewGuid(),
                ProjectTypeGuid = Guid.NewGuid(),
            };

            SlnProject projectD = new SlnProject
            {
                Configurations  = new[] { "Debug", "Release" },
                FullPath        = GetTempFileName(),
                IsMainProject   = true,
                Name            = "ProjectD",
                Platforms       = new[] { "Razzle" },
                ProjectGuid     = Guid.NewGuid(),
                ProjectTypeGuid = Guid.NewGuid(),
            };

            SlnProject projectE = new SlnProject
            {
                Configurations  = new[] { "Release" },
                FullPath        = GetTempFileName(),
                IsMainProject   = true,
                Name            = "ProjectE",
                Platforms       = new[] { "AnyCPU" },
                ProjectGuid     = Guid.NewGuid(),
                ProjectTypeGuid = Guid.NewGuid(),
            };

            SlnFile slnFile = new SlnFile()
            {
                Configurations = new[] { "Debug" },
                Platforms      = new[] { "Any CPU" },
            };

            slnFile.AddProjects(new[] { projectA, projectB, projectC, projectD, projectE });

            string solutionFilePath = GetTempFileName();

            slnFile.Save(solutionFilePath, useFolders: false);

            SolutionFile solutionFile = SolutionFile.Parse(solutionFilePath);

            ValidateSolutionPlatformAndConfiguration(projectA, solutionFile, "Debug", "AnyCPU");

            ValidateSolutionPlatformAndConfiguration(projectB, solutionFile, "Debug", "x64");

            ValidateSolutionPlatformAndConfiguration(projectC, solutionFile, "Debug", "amd64");

            ValidateSolutionPlatformAndConfiguration(projectD, solutionFile, "Debug", "Razzle", expectedIncludeInBuild: false);

            ValidateSolutionPlatformAndConfiguration(projectE, solutionFile, "Release", "AnyCPU", expectedIncludeInBuild: false);
        }
示例#10
0
        public void ProjectConfigurationPlatformOrderingSameAsProjects()
        {
            const string projectTypeGuid = "{7E0F1516-6200-48BD-83FC-3EFA3AB4A574}";

            SlnFile slnFile = new SlnFile
            {
                SolutionGuid = new Guid("00CB211B-D6BE-49C6-AF57-2604ECCD9E72"),
            };

            slnFile.AddProjects(new[]
            {
                new SlnProject
                {
                    FullPath        = Path.Combine(TestRootPath, "C", "C.csproj"),
                    Name            = "C",
                    ProjectGuid     = new Guid("0CCA75AE-ED20-431E-8853-B9F54333E87A"),
                    ProjectTypeGuid = new Guid(projectTypeGuid),
                    IsMainProject   = true,
                    Configurations  = new[]
                    {
                        "Debug",
                        "Release",
                    },
                    Platforms = new[]
                    {
                        "AnyCPU",
                    },
                },
                new SlnProject
                {
                    FullPath        = Path.Combine(TestRootPath, "B", "B.csproj"),
                    Name            = "B",
                    ProjectGuid     = new Guid("0CCA75AE-ED20-431E-8853-B9F54333E87A"),
                    ProjectTypeGuid = new Guid(projectTypeGuid),
                    Configurations  = new[]
                    {
                        "Debug",
                        "Release",
                    },
                    Platforms = new[]
                    {
                        "AnyCPU",
                    },
                },
                new SlnProject
                {
                    FullPath        = Path.Combine(TestRootPath, "A", "A.csproj"),
                    Name            = "A",
                    ProjectGuid     = new Guid("D744C26F-1CCB-456A-B490-CEB39334051B"),
                    ProjectTypeGuid = new Guid(projectTypeGuid),
                    Configurations  = new[]
                    {
                        "Debug",
                        "Release",
                    },
                    Platforms = new[]
                    {
                        "AnyCPU",
                    },
                },
            });

            string path = Path.GetTempFileName();

            slnFile.Save(path, useFolders: false);

            string directoryName = new DirectoryInfo(TestRootPath).Name;

            File.ReadAllText(path).ShouldBe(
                $@"Microsoft Visual Studio Solution File, Format Version 12.00
Project(""{{7E0F1516-6200-48BD-83FC-3EFA3AB4A574}}"") = ""C"", ""{directoryName}\C\C.csproj"", ""{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}""
EndProject
Project(""{{7E0F1516-6200-48BD-83FC-3EFA3AB4A574}}"") = ""A"", ""{directoryName}\A\A.csproj"", ""{{D744C26F-1CCB-456A-B490-CEB39334051B}}""
EndProject
Project(""{{7E0F1516-6200-48BD-83FC-3EFA3AB4A574}}"") = ""B"", ""{directoryName}\B\B.csproj"", ""{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}""
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}.Release|Any CPU.Build.0 = Release|Any CPU
		{{D744C26F-1CCB-456A-B490-CEB39334051B}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{D744C26F-1CCB-456A-B490-CEB39334051B}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{D744C26F-1CCB-456A-B490-CEB39334051B}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{D744C26F-1CCB-456A-B490-CEB39334051B}}.Release|Any CPU.Build.0 = Release|Any CPU
		{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{0CCA75AE-ED20-431E-8853-B9F54333E87A}}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
		SolutionGuid = {{00CB211B-D6BE-49C6-AF57-2604ECCD9E72}}
	EndGlobalSection
EndGlobal
", StringCompareShould.IgnoreLineEndings);
        }