Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            try
            {
                var arguments = CommandLineArgumentParser.Parse(args);

                try
                {
                    ParseCommand(arguments);
                }
                catch (ArgumentCommadnException e)
                {
                    Console.WriteLine(e.Message + "\r\n" + Usage);
                    Environment.Exit(-1);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(-1);
            }
            Environment.Exit(0);
        }
Exemplo n.º 2
0
        private static void ParseCommand(CommandLineArgumentParser arguments)
        {
            ConformCommand(arguments);
            string folder = arguments.Get(FolderCommand).Next;

            if (string.IsNullOrEmpty(folder))
            {
                throw new ArgumentCommadnException(NoFolderArgException);
            }

            // 如果输入 "E:\lindexi\EncodingNormalior\" 那么命令行将会传入 E:\lindexi\EncodingNormalior\" 字符串
            if (folder.EndsWith("\""))
            {
                folder = folder.Substring(0, folder.Length - 1);
            }

            folder = Path.GetFullPath(folder);
            if (!Directory.Exists(folder))
            {
                throw new ArgumentCommadnException("不存在文件夹" + folder);
            }

            EncodingScrutatorFolder encodingScrutatorFolder = new EncodingScrutatorFolder(new DirectoryInfo(folder));

            if (arguments.Has(IncludeFileCommand))
            {
                var includeFile  = arguments.Get(IncludeFileCommand).Take();
                var wildCardFile = /*IncludeFileSetting.ReadIncludeFile(includeFile);*/
                                   new FileInfo(includeFile);

                IncludeFileSetting includeFileSetting = new IncludeFileSetting(wildCardFile);
                encodingScrutatorFolder.IncludeFileSetting = includeFileSetting;
            }

            if (arguments.Has(EncodingCommand))
            {
                string encodingCommand = arguments.Get(EncodingCommand).Take();
                encodingCommand = encodingCommand.ToLower();
                Encoding encoding;
                switch (encodingCommand)
                {
                case "utf8":
                case "utf-8":
                    encoding = Encoding.UTF8;
                    break;

                case "gbk":
                    encoding = Encoding.GetEncoding("gbk");
                    break;

                case "bigendianunicode":
                    encoding = Encoding.BigEndianUnicode;
                    break;

                default:
                    throw new ArgumentException("输入无法识别编码");
                }

                encodingScrutatorFolder.SitpulationEncodingSetting = new SitpulationEncodingSetting()
                {
                    SitpulationEncoding = encoding
                };
            }

            if (arguments.Has(WhiteListCommand))
            {
                var whiteListFile = arguments.Get(WhiteListCommand).Take();
                encodingScrutatorFolder.InspectFileWhiteListSetting =
                    InspectFileWhiteListSetting.ReadWhiteListSetting(whiteListFile);
            }

            encodingScrutatorFolder.InspectFolderEncoding();

            var output = PintnoConformEncodingFile(encodingScrutatorFolder);

            if (arguments.Has(Quiet) && bool.TryParse(arguments.Get(Quiet).Take(), out var shouldQuiet) &&
                shouldQuiet is true)
            {
                // 如果需要静默,那么什么都不输出
            }
            else
            {
                Console.WriteLine(output);
            }

            if (IllicitFile.Count > 0)
            {
                if (arguments.Has(TryFixCommand))
                {
                    var tryFix = arguments.Get(TryFixCommand).Take();
                    if (bool.TryParse(tryFix.ToString(), out var value) && value is true)
                    {
                        TryFix(encodingScrutatorFolder);
                        return;
                    }
                }

                StringBuilder str = new StringBuilder();
                str.Append($"存在不符合规范文件:  {IllicitFile.Count}\r\n");
                foreach (var temp in IllicitFile)
                {
                    str.Append(temp.File.FullName + "\r\n");
                }

                str.Append("可采用如下命令修复文件编码:")
                .Append("\r\n")     // 强行规定换行
                // 下面使用 dotnet tool update 的 update 是预期的,这个命令的意思是在没有安装的时候安装,有安装的时候更新
                .Append("dotnet tool update -g dotnetCampus.EncodingNormalior")
                .Append("\r\n")
                .Append($"EncodingNormalior -f \"{encodingScrutatorFolder.FaceFolder.FullName}\" --TryFix true")
                .Append("\r\n")
                .Append("或安装编码规范 VS 插件工具:https://marketplace.visualstudio.com/items?itemName=lindexigd.vs-extension-18109")
                .Append("\r\n");

                throw new EncodingNormaliorException(str.ToString());
            }
            else
            {
                Console.WriteLine("恭喜你,没有存在不规范文件\r\n");
            }
        }
Exemplo n.º 3
0
        private static void ParseCommand(CommandLineArgumentParser arguments)
        {
            ConformCommand(arguments);
            var folder = arguments.Get(FolderCommand).Next;

            if (string.IsNullOrEmpty(folder))
            {
                throw new ArgumentCommadnException(NoFolderArgException);
            }
            if (!Directory.Exists(folder))
            {
                throw new ArgumentCommadnException("不存在文件夹" + folder);
            }

            EncodingScrutatorFolder encodingScrutatorFolder = new EncodingScrutatorFolder(new DirectoryInfo(folder));

            if (arguments.Has(IncludeFileCommand))
            {
                var includeFile  = arguments.Get(IncludeFileCommand).Take();
                var wildCardFile = /*IncludeFileSetting.ReadIncludeFile(includeFile);*/
                                   new FileInfo(includeFile);

                IncludeFileSetting includeFileSetting = new IncludeFileSetting(wildCardFile);
                encodingScrutatorFolder.IncludeFileSetting = includeFileSetting;
            }
            if (arguments.Has(EncodingCommand))
            {
                string encodingCommand = arguments.Get(EncodingCommand).Take();
                encodingCommand = encodingCommand.ToLower();
                Encoding encoding;
                switch (encodingCommand)
                {
                case "utf8":
                case "utf-8":
                    encoding = Encoding.UTF8;
                    break;

                case "gbk":
                    encoding = Encoding.GetEncoding("gbk");
                    break;

                case "bigendianunicode":
                    encoding = Encoding.BigEndianUnicode;
                    break;

                default:
                    throw new ArgumentException("输入无法识别编码");
                }
                encodingScrutatorFolder.SitpulationEncodingSetting = new SitpulationEncodingSetting()
                {
                    SitpulationEncoding = encoding
                };
            }
            if (arguments.Has(WhiteListCommand))
            {
                var whiteListFile = arguments.Get(WhiteListCommand).Take();
                encodingScrutatorFolder.InspectFileWhiteListSetting = InspectFileWhiteListSetting.ReadWhiteListSetting(whiteListFile);
            }

            encodingScrutatorFolder.InspectFolderEncoding();
            Console.WriteLine(PintnoConformEncodingFile(encodingScrutatorFolder));
            if (_illicitFile.Count > 0)
            {
                StringBuilder str = new StringBuilder();
                str.Append($"存在不符合规范文件:  {_illicitFile.Count}\r\n");
                foreach (var temp in _illicitFile)
                {
                    str.Append(temp.File.FullName + "\r\n");
                }
                throw new Exception(str.ToString());
            }
            else
            {
                Console.WriteLine("恭喜你,没有存在不规范文件\r\n");
            }
        }