Ejemplo n.º 1
0
        private static List <string> GetFileListFromGit(string gitArgs)
        {
            var files    = new HashSet <string>();
            var exitCode = CmdUtil.Run("git", gitArgs,
                                       data => files.Add(data.Trim()));

            if (exitCode != 0)
            {
                throw new Exception($"Failed to run git command {gitArgs}");
            }

            return(files.ToList());
        }
Ejemplo n.º 2
0
        private static bool DoesCommitExist(string sha)
        {
            var args   = $"cat-file -t {sha}";
            var exists = false;

            void OutputCallback(string data)
            {
                if (data.StartsWith("commit"))
                {
                    exists = true;
                }
                Console.WriteLine(data);
            }

            var returnCode = CmdUtil.Run("git", args,
                                         OutputCallback
                                         );

            return(exists);
        }
Ejemplo n.º 3
0
        private int RunCleanupCode(
            string include,
            string slnFile
            )
        {
            if (!SkipToolCheck && !DoesJbToolExist())
            {
                Console.WriteLine(@"
looks like jb dotnet tool isn't installed...
you can install it by running the following command:

dotnet tool install JetBrains.ReSharper.GlobalTools");
                return(1);
            }

            var jbArgs = new HashSet <string>(JbArgs);

            if (!jbArgs.Contains("--profile"))
            {
                if (FormatOnly)
                {
                    jbArgs.Add(@"--profile=""Built-in: Reformat Code""");
                }
                else
                {
                    jbArgs.Add(@"--profile=""Built-in: Full Cleanup""");
                }
            }

            if (!jbArgs.Contains("-dsl") &&
                !jbArgs.Contains("--disable-settings-layers"))
            {
                // ignore settings that might conflict with .editorconfig
                jbArgs.Add("-dsl=GlobalAll");
                jbArgs.Add("-dsl=SolutionPersonal");
                jbArgs.Add("-dsl=ProjectPersonal");
            }

            var exclude = "";

            if (UsePrettier)
            {
                var extensions = new[] {
                    "js", "jsx", "json", "html", "ts", "tsx", "css", "less",
                    "scss", "md", "yaml"
                };
                exclude = @"--exclude=""";

                foreach (var extension in extensions)
                {
                    exclude += $"**/*.{extension};";
                }

                exclude += @"""";
            }

            var args = $@"tool run jb cleanupcode ""{slnFile}"" "
                       + $@"{exclude} --include=""{include}"" "
                       + string.Join(" ", jbArgs);

            if (PrintCommand)
            {
                Console.WriteLine($"dotnet {args}");
            }

            // jb returns non zero when there's nothing to format
            // capture that so we can return zero
            var nothingToFormat = false;

            void ErrorCallback(string data)
            {
                if (data.Contains("No items were found to cleanup"))
                {
                    nothingToFormat = true;
                }
                Console.WriteLine($"error: {data}");
            }

            var returnCode = CmdUtil.Run("dotnet", args,
                                         errorCallback: ErrorCallback,

                                         // this can take a really long time on large code bases
                                         cmdTimeout: TimeSpan.FromHours(24),

                                         // but don't wait longer than 10 minutes for a single file
                                         // to get formatted
                                         outputTimeout: TimeSpan.FromMinutes(10)
                                         );

            if (nothingToFormat)
            {
                return(0);
            }
            return(returnCode);
        }
Ejemplo n.º 4
0
        private static bool DoesJbToolExist()
        {
            var exitCode = CmdUtil.Run("dotnet", "tool run jb cleanupcode -v");

            return(exitCode == 0);
        }
Ejemplo n.º 5
0
        public override int Run(string[] remainingArguments)
        {
            if (Jenkins)
            {
                SetJenkinsOptions();
            }

            if (AssumeHead && !string.IsNullOrEmpty(CommitA) &&
                !DoesCommitExist(CommitA))
            {
                CommitA = "HEAD";
                Console.WriteLine($"commit {CommitA} not found, using HEAD");
            }

            if (AssumeHead && !string.IsNullOrEmpty(CommitB) &&
                !DoesCommitExist(CommitB))
            {
                CommitB = "HEAD";
                Console.WriteLine($"commit {CommitB} not found, using HEAD");
            }

            var files = GetFilesToFormat(
                FilePattern, FilesToFormat, CommitA, CommitB);

            if (!files.Any())
            {
                Console.WriteLine("Nothing to format.");
                return(0);
            }

            if (string.IsNullOrEmpty(SolutionFile))
            {
                Console.WriteLine(
                    "No sln file specified. Searching for one...");
                SolutionFile = FindSlnFile(".");
                Console.WriteLine($"Found {SolutionFile}. Using that.");
            }

            var solutionDir = Path.GetDirectoryName(SolutionFile);

            if (solutionDir.StartsWith(".\\") || solutionDir.StartsWith("./"))
            {
                solutionDir = solutionDir.Substring(2);
            }

            // windows doesn't allow args > ~8100 so call cleanupcode in batches
            var remain = new HashSet <string>(files);

            while (remain.Any())
            {
                var include = new StringBuilder();
                foreach (var file in remain.ToArray())
                {
                    if (include.Length + file.Length > 7000)
                    {
                        break;
                    }

                    // jb codecleanup requires file paths relative to the sln
                    var jbFilePath = file;
                    if (file.StartsWith(solutionDir))
                    {
                        jbFilePath = file.Substring(solutionDir.Length);
                    }

                    include.Append($";{jbFilePath}");
                    remain.Remove(file);
                }

                var returnCode = RunCleanupCode(
                    include.ToString(), SolutionFile);

                if (returnCode != 0)
                {
                    return(returnCode);
                }
            }

            if (FailOnDiff)
            {
                var diffFiles =
                    GetFileListFromGit("diff --name-only --diff-filter=ACM")
                    .ToList();

                if (diffFiles.Any())
                {
                    if (FilesToFormat == FileMatch.Staged ||
                        FilesToFormat == FileMatch.Commits)
                    {
                        // we only care about files we formatted
                        diffFiles = diffFiles.Intersect(files).ToList();
                    }

                    if (diffFiles.Any())
                    {
                        Console.WriteLine();
                        Console.WriteLine("!!!! Process Aborted !!!!");
                        Console.WriteLine(
                            "The following files do not match .editorconfig:");
                        diffFiles.ForEach(
                            x => { Console.WriteLine($" * {x}"); });
                        PrintFixCommand();

                        if (PrintDiff)
                        {
                            CmdUtil.Run("git", "diff");
                        }

                        return(1);
                    }
                }
            }

            return(0);
        }