public static void Process(string slnFilePath, string workingDirectory, bool removeExtras)
        {
            Console.WriteLine("Starting process with the following args:");
            Console.WriteLine($"Solution File Path: {slnFilePath}");
            Console.WriteLine($"Current Working Directory: {workingDirectory}");
            Console.WriteLine($"Remove Extras: {removeExtras}");

            var csProjFilePaths = GetCsProjFilePaths(workingDirectory);
            //Console.WriteLine($"Found {csProjFilePaths.Length} .csproj files in {currentWorkingDirectory}.");

            var csProjFilesProcessed = new HashSet <string>();

            var slnFileDirectoryPath = Path.GetDirectoryName(slnFilePath);
            var slnFileContents      = File.ReadAllText(slnFilePath);
            var matches = SlnFileCsProjRegex.Matches(slnFileContents);

            foreach (Match match in matches)
            {
                ProcessCsProjFileMatch(match, slnFileDirectoryPath, csProjFilesProcessed, csProjFilePaths);
            }

            if (removeExtras)
            {
                var toDelete = csProjFilePaths.Where(fp => !csProjFilesProcessed.Contains(Path.GetFileName(fp))).ToList();
                toDelete.ForEach(fp => File.Delete(fp));
            }
        }
示例#2
0
        private static void ProcessSolutionFiles(string workingDirectory, string[] csProjFilePaths)
        {
            // Loop over all .sln files
            var slnFilePaths = Directory.GetFiles(workingDirectory, "*.sln", SearchOption.AllDirectories);

            foreach (var slnFilePath in slnFilePaths)
            {
                var slnFileContents = File.ReadAllText(slnFilePath);

                // Recursively loop over all referenced .csproj files to get a
                // list of all .csproj files that should be referenced by the .sln file
                var csProjFilePathsAlreadyInSlnFile = new HashSet <string>();
                var csProjFilePathDependencyTree    = new HashSet <string>();
                var matches = SlnFileCsProjRegex.Matches(slnFileContents);
                foreach (Match match in matches)
                {
                    var csProjFileName = ExtractCsProjName(match.Value);
                    var csProjFilePath = FindCsProjFilePath(csProjFilePaths, csProjFileName);

                    csProjFilePathsAlreadyInSlnFile.Add(csProjFilePath);

                    RecurseProjFiles(csProjFilePath, csProjFilePaths, csProjFilePathDependencyTree);
                }

                var missingCsProjPaths = csProjFilePathDependencyTree
                                         .Except(csProjFilePathsAlreadyInSlnFile)
                                         .ToArray();

                if (missingCsProjPaths.Any())
                {
                    Console.WriteLine($"Solution {slnFilePath} is missing {missingCsProjPaths.Length} reference/s. Adding them...");
                    var slnLevelGuid = Guid.NewGuid();

                    var sb = new StringBuilder(200 * missingCsProjPaths.Length);
                    foreach (var missingCsProjPath in missingCsProjPaths)
                    {
                        var relativeCsProjPath = GetRelativePathTo(slnFilePath, missingCsProjPath);
                        var projName           = Path.GetFileNameWithoutExtension(missingCsProjPath);
                        sb.AppendLine($"Project(\"{slnLevelGuid}\") = \"{projName}\", \"{relativeCsProjPath}\", \"{Guid.NewGuid()}\"");
                        sb.AppendLine("EndProject");
                    }

                    var indexToInsert = slnFileContents.IndexOf("Global\r\n");
                    slnFileContents = slnFileContents.Insert(indexToInsert, sb.ToString());

                    File.WriteAllText(slnFilePath, slnFileContents);
                    Console.WriteLine($"Added references to {slnFilePath}");
                }
                else
                {
                    Console.WriteLine($"No changes required in {slnFilePath}");
                }
            }


            // If a reference is not in the .sln, add it in
        }
示例#3
0
        private static void FixReferencesInSlnFile(string[] csProjFilePaths, string slnFilePath)
        {
            var slnFileContents = File.ReadAllText(slnFilePath);
            var matches         = SlnFileCsProjRegex.Matches(slnFileContents);

            foreach (Match match in matches)
            {
                var csProjFileName         = ExtractCsProjName(match.Value);
                var csProjFilePath         = FindCsProjFilePath(csProjFilePaths, csProjFileName);
                var csProjRelativeFilePath = GetRelativePathTo(slnFilePath, csProjFilePath);
                slnFileContents = slnFileContents.Replace(match.Value, $", \"{csProjRelativeFilePath}\"");
            }

            if (slnFileContents != File.ReadAllText(slnFilePath))
            {
                File.WriteAllText(slnFilePath, slnFileContents);
                Console.WriteLine($"Fixed references in {slnFilePath}");
            }
            else
            {
                Console.WriteLine($"No changes required in {slnFilePath}");
            }
        }