public void SuccessfullyAddNewProject_When_AllIsCorrect()
        {
            SolutionProject solutionProject = new SolutionProject(this.projectGuid, this.csProjFilePath, this.slnFilePathWithElements, SolutionProjectType.WebProject);

            SolutionFileEditor.AddProject(this.slnFilePathWithElements, solutionProject);

            var slnContents = File.ReadAllText(this.slnFilePathWithElements);

            Assert.IsFalse(string.IsNullOrEmpty(slnContents));

            IEnumerable <SolutionProject> solutionProjects = SolutionFileEditor.GetProjects(this.slnFilePathWithElements);
            bool hasProject = solutionProjects.Any(sp => sp.ProjectGuid == this.projectGuid &&
                                                   sp.AbsolutePath.Equals(this.csProjFilePath, StringComparison.InvariantCultureIgnoreCase) &&
                                                   sp.ProjectType == SolutionProjectType.WebProject);

            Assert.IsTrue(hasProject);
        }
        private IEnumerable <string> GetProjectsPathsFromSolution(string solutionPath, bool onlySitefinityProjects = false)
        {
            if (!solutionPath.EndsWith(Constants.SlnFileExtension, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new Exception(string.Format(Constants.FileIsNotSolutionMessage, solutionPath));
            }

            IEnumerable <string> projectFilesAbsolutePaths = SolutionFileEditor.GetProjects(solutionPath)
                                                             .Select(sp => sp.AbsolutePath)
                                                             .Where(ap => (ap.EndsWith(Constants.CsprojFileExtension, StringComparison.InvariantCultureIgnoreCase) || ap.EndsWith(Constants.VBProjFileExtension, StringComparison.InvariantCultureIgnoreCase)));

            if (onlySitefinityProjects)
            {
                projectFilesAbsolutePaths = projectFilesAbsolutePaths.Where(ap => this.HasSitefinityReferences(ap));
            }

            return(projectFilesAbsolutePaths);
        }
        public void AddMissingProjects_ForEmptySolution_Should_AddMissingSections()
        {
            var result   = SolutionFileEditor.AddMissingProjects(null, GetProjectsWithSharedReference());
            var expected = @"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.329
MinimumVisualStudioVersion = 10.0.40219.1
Project(""{D954291E-2A0B-460D-934E-DC6B0785DB48}"") = ""P1"", ""P1\P1.shproj"", ""{21E587DF-00A7-4015-8992-6AF82C55C970}""
EndProject
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""P2"", ""P2\P2.csproj"", ""{7956EBA3-330F-4CCB-AAAA-221DFC833B46}""
EndProject
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""P3"", ""P3\P3.csproj"", ""{80F91288-CC6C-49D7-A4D0-7E06FEDF3888}""
EndProject
Global
	GlobalSection(SharedMSBuildProjectFiles) = preSolution
		P1\P1.projitems*{21e587df-00a7-4015-8992-6af82c55c970}*SharedItemsImports = 13
		P1\P1.projitems*{80f91288-cc6c-49d7-a4d0-7e06fedf3888}*SharedItemsImports = 4
	EndGlobalSection
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{7956eba3-330f-4ccb-aaaa-221dfc833b46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{7956eba3-330f-4ccb-aaaa-221dfc833b46}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{7956eba3-330f-4ccb-aaaa-221dfc833b46}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{7956eba3-330f-4ccb-aaaa-221dfc833b46}.Release|Any CPU.Build.0 = Release|Any CPU
		{80f91288-cc6c-49d7-a4d0-7e06fedf3888}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{80f91288-cc6c-49d7-a4d0-7e06fedf3888}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{80f91288-cc6c-49d7-a4d0-7e06fedf3888}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{80f91288-cc6c-49d7-a4d0-7e06fedf3888}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
		SolutionGuid = {A603CAAE-8991-405D-906A-4D5ABE1E9314}
	EndGlobalSection
EndGlobal";

            Assert.That.LinesAreEqual(expected, result);
        }
Example #4
0
        /// <summary>
        /// A method containing the logic of the command
        /// </summary>
        /// <param name="config"></param>
        /// <returns>0 for success; 1 for failure</returns>
        public override int OnExecute(CommandLineApplication config)
        {
            var currentPath = this.ProjectRootPath;

            if (!Directory.Exists(currentPath))
            {
                currentPath = Path.GetDirectoryName(currentPath);
            }

            var webAppProjectName = Path.GetFileName(Directory.EnumerateFiles(currentPath, "*.csproj", SearchOption.TopDirectoryOnly).FirstOrDefault());

            while (Directory.EnumerateFiles(currentPath, @"*.sln", SearchOption.TopDirectoryOnly).FirstOrDefault() == null)
            {
                currentPath = Directory.GetParent(currentPath)?.ToString();
                if (string.IsNullOrEmpty(currentPath))
                {
                    Utils.WriteLine(Constants.SolutionNotFoundMessage, ConsoleColor.Red);
                    return((int)ExitCode.GeneralError);
                }
            }

            this.SolutionPath = Directory.EnumerateFiles(currentPath, @"*.sln", SearchOption.TopDirectoryOnly).FirstOrDefault();

            var sitefinityPath = Directory.EnumerateFiles(currentPath, "Telerik.Sitefinity.dll", SearchOption.AllDirectories).FirstOrDefault();

            var binFolder = Path.GetDirectoryName(sitefinityPath);

            if (string.IsNullOrEmpty(binFolder))
            {
                Utils.WriteLine(Constants.ProjectNotFound, ConsoleColor.Red);
                return((int)ExitCode.GeneralError);
            }

            this.SitefinityVersion = FileVersionInfo.GetVersionInfo(sitefinityPath).ProductVersion;
            this.ProjectRootPath   = Path.Combine(currentPath, this.PascalCaseName);

            if (Path.IsPathRooted(binFolder))
            {
                binFolder = Path.GetRelativePath(this.ProjectRootPath, binFolder);
            }

            this.BinFolder = binFolder;

            Directory.CreateDirectory(this.ProjectRootPath);

            if (base.OnExecute(config) == 1)
            {
                return((int)ExitCode.GeneralError);
            }

            var project = this.createdFiles.FirstOrDefault(x => x.EndsWith(Constants.CsprojFileExtension));

            try
            {
                SolutionProject solutionProject = new SolutionProject(this.ProjectGuid, project, this.SolutionPath, SolutionProjectType.ManagedCsProject);
                SolutionFileEditor.AddProject(this.SolutionPath, solutionProject);

                Utils.WriteLine(string.Format(Constants.AddFilesToSolutionSuccessMessage, project), ConsoleColor.Green);
            }
            catch (Exception ex)
            {
                Utils.WriteLine(ex.Message, ConsoleColor.Yellow);
            }

            return((int)ExitCode.OK);
        }