Exemplo n.º 1
0
        public bool Execute(string source, string target, bool overwrite, bool addFolder, bool addSameGuid)
        {
            var isDirty        = false;
            var sourceSolution = EditableSolutionFile.FromFile(Path.GetFullPath(source));
            var targetSolution = EditableSolutionFile.FromFile(Path.GetFullPath(target));
            var mergedResult   = Merge(sourceSolution, targetSolution, addFolder, addSameGuid);

            if (overwrite)
            {
                mergedResult.MergedSolution.Save();
                mergedResult.ConflictingSolutions.ForEach(item =>
                {
                    Logger.Warn($"Updating solutions with project guids conflicts: {item.SolutionFullPath}");
                    item.Save();
                });

                mergedResult.ConflictingProjects.ForEach(item =>
                {
                    Logger.Warn($"Updating project guids/reference conflicts: {item.FullPath}");
                    item.Save();
                });
                isDirty = true;
            }

            return(isDirty);
        }
Exemplo n.º 2
0
 protected SolutionFile CheckForWarnings(SolutionFile original, bool ignoreWarning)
 {
     if (!ignoreWarning && original.Warnings.Count > 0)
     {
         throw new Exception(original.Warnings[0]);
     }
     return original;
 }
Exemplo n.º 3
0
 public void WriteSolutionFile(SolutionFile solutionFile)
 {
     lock (m_writer)
     {
         WriteHeader(solutionFile);
         WriteProjects(solutionFile);
         WriteGlobal(solutionFile);
     }
 }
        private void WriteGlobalSections(SolutionFile solutionFile)
        {
            foreach (var globalSection in solutionFile.GlobalSections)
            {
                var propertyLines = new List<PropertyLine>(globalSection.PropertyLines);
                switch (globalSection.Name)
                {
                    case "NestedProjects":
                        foreach (var project in solutionFile.Projects)
                        {
                            if (project.ParentFolderGuid != null)
                            {
                                propertyLines.Add(new PropertyLine(project.ProjectGuid, project.ParentFolderGuid));
                            }
                        }
                        break;

                    case "ProjectConfigurationPlatforms":
                        foreach (var project in solutionFile.Projects)
                        {
                            foreach (var propertyLine in project.ProjectConfigurationPlatformsLines)
                            {
                                propertyLines.Add(
                                            new PropertyLine(
                                                string.Format("{0}.{1}", project.ProjectGuid, propertyLine.Name),
                                                propertyLine.Value));
                            }
                        }
                        break;

                    default:
                        if (globalSection.Name.EndsWith("Control", StringComparison.InvariantCultureIgnoreCase))
                        {
                            var index = 1;
                            foreach (var project in solutionFile.Projects)
                            {
                                if (project.VersionControlLines.Count > 0)
                                {
                                    foreach (var propertyLine in project.VersionControlLines)
                                    {
                                        propertyLines.Add(
                                                    new PropertyLine(
                                                        string.Format("{0}{1}", propertyLine.Name, index),
                                                        propertyLine.Value));
                                    }
                                    index++;
                                }
                            }

                            propertyLines.Insert(0, new PropertyLine("SccNumberOfProjects", index.ToString()));
                        }
                        break;
                }

                WriteSection(globalSection, propertyLines);
            }
        }
Exemplo n.º 5
0
 public Project(SolutionFile container, Project original)
     : this(container,
             original.ProjectGuid,
             original.ProjectTypeGuid,
             original.ProjectName,
             original.RelativePath,
             original.ParentFolderGuid,
             original.ProjectSections,
             original.VersionControlLines,
             original.ProjectConfigurationPlatformsLines)
 {
 }
        public UnityProjectConfigurator(string directory)
        {
            ProjectDirectory = new DirectoryInfo(directory);
            ProjectName      = ProjectDirectory.Name;
            ProjectVersion   = FindProjectVersion(ProjectDirectory);

            var slnPath = Path.Combine(ProjectDirectory.FullName, ProjectName + ".sln");

            if (File.Exists(slnPath))
            {
                SolutionFile = SolutionFile.FromFile(slnPath);
            }
        }
Exemplo n.º 7
0
        private void WriteHeader(SolutionFile solutionFile)
        {
            // If the header doesn't start with an empty line, add one
            // (The first line of sln files saved as UTF-8 with BOM must be blank, otherwise Visual Studio Version Selector will not detect VS version correctly.)
            if (solutionFile.Headers.Count == 0 || solutionFile.Headers[0].Trim().Length > 0)
            {
                m_writer.WriteLine();
            }

            foreach (var line in solutionFile.Headers)
            {
                m_writer.WriteLine(line);
            }
        }
Exemplo n.º 8
0
 private void WriteProjects(SolutionFile solutionFile)
 {
     foreach (var project in solutionFile.Projects)
     {
         m_writer.WriteLine("Project(\"{0}\") = \"{1}\", \"{2}\", \"{3}\"",
                     project.ProjectTypeGuid,
                     project.ProjectName,
                     project.RelativePath,
                     project.ProjectGuid);
         foreach (var projectSection in project.ProjectSections)
         {
             WriteSection(projectSection, projectSection.PropertyLines);
         }
         m_writer.WriteLine("EndProject");
     }
 }
Exemplo n.º 9
0
    public void WriteNewSlnFile(Sln slnObject)
    {
        _sln     = slnObject;
        _slnFile = new SlnFile();                 // or you can read sln file like below
        using (SolutionFileReader reader = new SolutionFileReader(_sln.ObjectFullPath))
            _slnFile = reader.ReadSolutionFile();
        AddHeaders();
        AddGlobalSections();
        // add projects
        List <SolutionNode> sns = _sln.GetAllSubItemsOf(typeof(SolutionFolder), typeof(ProjectNode));

        foreach (SolutionNode sn in sns)
        {
            _slnFile.Projects.Add(CreateProject(sn));
        }
        using (SolutionFileWriter writer = new SolutionFileWriter(_sln.ObjectFullPath))
            writer.WriteSolutionFile(_slnFile);
    }
Exemplo n.º 10
0
        public Project CreateRelativeSolutionFolder(EditableSolutionFile target, string source)
        {
            var targetPath         = new Uri(target.SolutionFullPath);
            var sourcePath         = new Uri(source);
            var relativeSourcePath = targetPath.MakeRelativeUri(sourcePath);
            var parentSourceFolder = Path.GetDirectoryName(relativeSourcePath.OriginalString);

            if (string.IsNullOrEmpty(parentSourceFolder))
            {
                return(null);
            }

            var     folders      = parentSourceFolder.Split('\\');
            Project parentFolder = null;

            // Search existing folder
            var existingFolderList =
                target.Projects.Where(item => item.ProjectTypeGuid == MsBuildExtensions.solutionFolderGuid)
                .ToList();

            foreach (var folder in folders)
            {
                // Check existing solution folder
                var existingFolder = existingFolderList.FirstOrDefault(item => item.ProjectName == folder);
                if (existingFolder != null)
                {
                    var compareParentFolder = existingFolder.ParentFolder;
                    if (compareParentFolder == parentFolder)
                    {
                        parentFolder = existingFolder;
                        continue;
                    }
                }

                var project = new Project(target, Guid.NewGuid().ToString("B").ToUpper(), MsBuildExtensions.solutionFolderGuid, folder, folder, parentFolder?.ProjectGuid, new List <Section>()
                {
                }, new List <PropertyLine>(), new List <PropertyLine>());
                target.Projects.Add(project);
                parentFolder = project;
            }

            return(parentFolder);
        }
Exemplo n.º 11
0
 public Project(
             SolutionFile container,
             string projectGuid,
             string projectTypeGuid,
             string projectName,
             string relativePath,
             string parentFolderGuid,
             IEnumerable<Section> projectSections,
             IEnumerable<PropertyLine> versionControlLines,
             IEnumerable<PropertyLine> projectConfigurationPlatformsLines)
 {
     r_container = container;
     r_projectGuid = projectGuid;
     m_projectTypeGuid = projectTypeGuid;
     m_projectName = projectName;
     m_relativePath = relativePath;
     m_parentFolderGuid = parentFolderGuid;
     r_projectSections = new SectionHashList(projectSections);
     r_versionControlLines = new PropertyLineHashList(versionControlLines);
     r_projectConfigurationPlatformsLines = new PropertyLineHashList(projectConfigurationPlatformsLines);
 }
Exemplo n.º 12
0
        public SolutionFile ReadSolutionFile()
        {
            lock (m_reader)
            {
                m_solutionFile = new SolutionFile();

                var isHeader = true;
                for (var line = ReadLine(); line != null; line = ReadLine())
                {
                    if (isHeader && rs_regexParseHeader.IsMatch(line))
                    {
                        m_solutionFile.Headers.Add(line);
                        continue;
                    }

                    isHeader = false;
                    if (line.StartsWith("Project(", StringComparison.InvariantCultureIgnoreCase))
                    {
                        m_solutionFile.Projects.Add(ReadProject(line));
                    }
                    else if (String.Compare(line, "Global", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        ReadGlobal();
                        // TODO valide end of file
                        break;
                    }
                    else
                    {
                        throw new SolutionFileException(string.Format("Invalid line read on line #{0}.\nFound: {1}\nExpected: A line beginning with 'Project(' or 'Global'.",
                                        m_currentLineNumber,
                                        line));
                    }
                }
                return m_solutionFile;
            }
        }
Exemplo n.º 13
0
 private void WriteGlobal(SolutionFile solutionFile)
 {
     m_writer.WriteLine("Global");
     WriteGlobalSections(solutionFile);
     m_writer.WriteLine("EndGlobal");
 }
 public CSharpLibraryProject(SolutionFile container, string projectGuid, string projectTypeGuid, string projectName, string relativePath, string parentFolderGuid, IEnumerable<Section> projectSections, IEnumerable<PropertyLine> versionControlLines, IEnumerable<PropertyLine> projectConfigurationPlatformsLines)
     : base(container, projectGuid, projectTypeGuid, projectName, relativePath, parentFolderGuid, projectSections, versionControlLines, projectConfigurationPlatformsLines)
 {
     configurations = new ConfigurationHashList(this);
 }
Exemplo n.º 15
0
 public SolutionFile(SolutionFile original)
             : this(original.SolutionFullPath, original.Headers, original.Projects, original.GlobalSections)
 {
 }
Exemplo n.º 16
0
 public NodeDifference CompareTo(SolutionFile oldSolution)
 {
     return (NodeDifference) this.ToElement().CompareTo(oldSolution.ToElement());
 }
Exemplo n.º 17
0
        public SolutionMergeResult Merge(EditableSolutionFile source, EditableSolutionFile target, bool addFolder = true, bool addSameGuid = true)
        {
            var conflictingProjects  = new List <MsBuildProject>();
            var conflictingSolutions = new List <EditableSolutionFile>();

            // Compare project diff
            var sourceProjects = source.Projects
                                 .Where(item => item.ProjectTypeGuid == MsBuildExtensions.csProjectGuid || item.ProjectTypeGuid == MsBuildExtensions.solutionFolderGuid)
                                 .ToList();

            var targetProjects = target.Projects
                                 .Where(item => item.ProjectTypeGuid == MsBuildExtensions.csProjectGuid || item.ProjectTypeGuid == MsBuildExtensions.solutionFolderGuid)
                                 .ToDictionary(item => item.ProjectGuid, item => item);

            // Correct relative project paths for cs projects
            var directoryPath = Path.GetDirectoryName(source.SolutionFullPath);

            if (string.IsNullOrEmpty(directoryPath))
            {
                directoryPath = Directory.GetCurrentDirectory();
            }

            var targetPath = new Uri(target.SolutionFullPath);

            // Add root solution folder based on relative uri
            Project parentProjectFolder = null;

            if (addFolder)
            {
                parentProjectFolder = CreateRelativeSolutionFolder(target, source.SolutionFullPath);
            }

            // Add diff guids
            List <Project> diffProjects = sourceProjects
                                          .Where(item => !targetProjects.ContainsKey(item.ProjectGuid))
                                          .Select(item => new Project(target, item))
                                          .ToList();

            // Add conflicts guids
            var conflicts = sourceProjects
                            .Where(item => targetProjects.ContainsKey(item.ProjectGuid) && targetProjects[item.ProjectGuid].ProjectName != item.ProjectName)
                            .ToList();

            // Auto replace duplicate guids
            if (addSameGuid && conflicts.Count > 0)
            {
                // Add duplicate guids
                var conflitingProjects = conflicts
                                         .Select(item =>
                {
                    // Clone to new GUID
                    var clone = new Project(target, Guid.NewGuid().ToString("B"), item.ProjectTypeGuid,
                                            item.ProjectName, item.RelativePath, item.ParentFolderGuid, item.ProjectSections,
                                            item.VersionControlLines,
                                            item.ProjectConfigurationPlatformsLines);

                    // Hope there's only 1-2 duplicates per solution
                    // Update reference projects to new project guids
                    var temp = source.UpdateProjectGuidReference(new Guid(item.ProjectGuid), new Guid(clone.ProjectGuid));
                    conflictingProjects.AddRange(temp);

                    return(clone);
                })
                                         .ToList();

                // Solution needs updating
                if (conflictingProjects.Count > 0)
                {
                    conflictingSolutions.Add(source);
                }

                // Add newly cloned projects
                diffProjects.AddRange(conflitingProjects);
            }

            // Add projects to solution
            diffProjects.ForEach(project =>
            {
                // Correct relative project paths for cs projects
                if (project.ProjectTypeGuid == MsBuildExtensions.csProjectGuid)
                {
                    var projectPath  = new Uri(Path.Combine(directoryPath, project.RelativePath));
                    var relativePath = targetPath.MakeRelativeUri(projectPath);

                    project.RelativePath = relativePath.OriginalString;
                }

                // Add root solution folder based on relative uri
                if (parentProjectFolder != null && string.IsNullOrEmpty(project.ParentFolderGuid))
                {
                    project.ParentFolderGuid = parentProjectFolder.ProjectGuid;
                }

                // Add same guid projects
                target.Projects.Add(project);
            });

            return(new SolutionMergeResult
            {
                MergedSolution = target,
                ConflictingSolutions = conflictingSolutions,
                ConflictingProjects = conflictingProjects
            });
        }
Exemplo n.º 18
0
 private void WriteGlobal(SolutionFile solutionFile)
 {
     m_writer.WriteLine("Global");
     WriteGlobalSections(solutionFile);
     m_writer.WriteLine("EndGlobal");
 }