Beispiel #1
0
        public static void Verify(string assemblyName)
        {
            var log = new List <string>();
            var rc  = ProcessUtility.ExecuteCommandLine(ExePath, new[] { "/nologo", assemblyName }, null, log, log);

            // TODO: not great to just throw like this vs. returning an error structure
            // TODO: will it return 0 even if there are warnings?
            if (rc != 0)
            {
                throw new PeVerifyException(assemblyName, rc, log);
            }
        }
Beispiel #2
0
        static bool FormatUncrustify(FormatItem formatItem, List <string> lines)
        {
            var packageRoot    = formatItem.Context.ThisPackageRoot;
            var uncrustifyPath = packageRoot.Combine(UncrustifyRelativePathForCurrentPlatform());
            var settingsPath   = packageRoot.Combine($"Coding~/Configs/Uncrustify/{formatItem.Path.ExtensionWithoutDot.ToLower()}.ini");

            var tempFormatPath = formatItem.Path.ChangeExtension(".tmp" + formatItem.Path.ExtensionWithDot);

            try
            {
                using (var file = File.Create(tempFormatPath))
                    WriteLines(file, lines, Charset.UTF8, EndOfLine.LF);

                var args = new[]
                {
                    "-L1,2",                // only want warnings and errors, and no status
                    "-c", settingsPath,     // config file to use
                    "-f", tempFormatPath,   // file to process
                    "-o", tempFormatPath,   // overwrite the thing
                };

                var stderr   = new List <string>();
                var exitCode = ProcessUtility.ExecuteCommandLine(uncrustifyPath, args, null, null, stderr);

                // we should be able to use `--no-backup` but uncrustify complains that this can't be used with -f/-o. we're already working with a tmp
                // file to get atomic rewrite as well as avoid updating a file that doesn't change (so timestamp not affected).
                // https://gitlab.cds.internal.unity3d.com/upm-packages/core/com.unity.coding/issues/3
                tempFormatPath.Parent.Combine(tempFormatPath.FileName + ".unc-backup.md5~").DeleteIfExists();
                tempFormatPath.Parent.Combine(tempFormatPath.FileName + ".unc-backup~").DeleteIfExists();

                if (stderr.Any())
                {
                    formatItem.Context.LogError($"Errors encountered while formatting '{formatItem.Path}':\n  {stderr.StringJoin("\n  ")}");
                    return(false);
                }

                if (exitCode != 0)
                {
                    formatItem.Context.LogError($"Fatal encountered while formatting '{formatItem.Path}'\n(Uncrustify gave an exit code of 0x{exitCode:X})");
                    return(false);
                }

                lines.SetRange(ReadLines(tempFormatPath).lines);
            }
            finally
            {
                tempFormatPath.DeleteIfExists();
            }

            return(true);
        }
Beispiel #3
0
        public static void RenderGraphViz(NPath png, string dot, params string[] extraArgs)
        {
            var dotPath    = NPath.SystemTemp.Combine(Guid.NewGuid() + ".dot");
            var renderPath = dotPath.ChangeExtension(".png");

            try
            {
                dotPath.WriteAllText(dot);
                ProcessUtility.ExecuteCommandLine("dot", $"-Gdpi=175 -Nfontname=Roboto -Efontname=Roboto -Tpng {dotPath} -o {renderPath}".Split(' ').Concat(extraArgs), null, null, null);
                ProcessUtility.ExecuteCommandLine("magick", $"{renderPath} -channel RGB -negate {png}".Split(' '), null, null, null);
            }
            finally
            {
                dotPath.DeleteIfExists();
                renderPath.DeleteIfExists();
            }
        }
Beispiel #4
0
        public static void Verify(string assemblyName)
        {
            // TODO: find a better way to avoid assemblies that don't peverify out of the box
            if (new[] { "mscorlib", "System.Runtime", "netstandard" }.Contains(new NPath(assemblyName).FileNameWithoutExtension))
            {
                return;
            }

            var log = new List <string>();
            var rc  = ProcessUtility.ExecuteCommandLine(ExePath, new[] { "/nologo", assemblyName }, null, log, log);

            // TODO: not great to just throw like this vs. returning an error structure
            // TODO: will it return 0 even if there are warnings?
            if (rc != 0)
            {
                throw new PeVerifyException(assemblyName, rc, log);
            }
        }
Beispiel #5
0
        public static string OcrSmallText(Int2 size, Func <Int2, bool> isSet)
        {
            var imagePath = Path.GetTempFileName().ToNPath();

            try
            {
                // to detect small text, tesseract needs black on white with a border and scaled up 400%
                using (var image = new MagickImage(MagickColors.White, size.X, size.Y))
                {
                    image.Density = new Density(100, DensityUnit.PixelsPerInch);

                    using (var outPixels = image.GetPixels())
                    {
                        for (var y = 0; y < image.Height; ++y)
                        {
                            for (var x = 0; x < image.Width; ++x)
                            {
                                if (isSet(new Int2(x, y)))
                                {
                                    outPixels[x, y].Set(k_On);
                                }
                            }
                        }
                    }

                    image.Scale(new Percentage(400));

                    image.BorderColor = image.BackgroundColor;
                    image.Border(10);

                    image.Write(imagePath, MagickFormat.Png);
                }

                var(stdout, stderr) = (new List <string>(), new List <string>());
                ProcessUtility.ExecuteCommandLine("tesseract", Arr(imagePath.ToString(), "stdout"), null, stdout, stderr);
                return(stdout[0]);
            }
            finally
            {
                imagePath.DeleteIfExists();
            }
        }
        public void TestDay([Range(1, 25)] int day)
        {
            day.ShouldNotBe(22, "day 22 still WIP");

            var testDir = AocDir.Combine($"day{day}");

            if (!testDir.DirectoryExists())
            {
                Assert.Ignore($"No solution for this day yet ({testDir})");
            }

            var script = testDir.Combine($"day{day}.solver.linq");

            if (!script.FileExists())
            {
                Assert.Ignore("This day solved with something other than linqpad");
            }

            var expected = ExtractResults(day);

            var(stdout, stderr) = (new List <string>(), new List <string>());
            ProcessUtility.ExecuteCommandLine("lprun", new[] { "-optimize", "-recompile", script.FileName }, testDir, stdout, stderr);
            if (stderr.Any())
            {
                Assert.Fail(stderr.StringJoin("\n"));
            }

            var results = stdout.Select(s => s.Trim()).Where(s => s.Any()).ToArray();

            if (results.Any() && results[0].Contains("Exception"))
            {
                Assert.Fail(results.StringJoin("\n"));
            }
            else
            {
                results.ShouldBe(expected);
            }
        }