Esempio n. 1
0
        /// <summary>
        /// Compares JSON files in 2 folder and output the result.
        /// </summary>
        /// <param name="path1">The directory path of JSON files.</param>
        /// <param name="path2">The another directory path of JSON files.</param>
        /// <param name="configFile">The configuration file.</param>
        /// <param name="progress">The progress update provider.</param>
        /// <returns>All compare result</returns>
        public async Task <IList <CompareFile> > Compare(string path1, string path2, string configFile, IProgress <IProgressReport> progress = null)
        {
            Preconditions.NotNullOrWhiteSpace(path1, "path1");
            Preconditions.NotNullOrWhiteSpace(path2, "path2");
            Preconditions.NotNullOrWhiteSpace(configFile, "configFile");
            Preconditions.Check(!path1.Equals(path2, StringComparison.OrdinalIgnoreCase), "Path1 should not be same as Path2");

            Trace.Indent();
            Trace.TraceInformation($"Start Read the Compare Config File: {configFile}.");
            var config = configDocumentType.Read(configFile);

            Trace.TraceInformation($"Start Compare the folder {path1} with folder {path2}.");
            var filesFromPath1 = Directory.EnumerateFiles(path1).Where(path => Path.GetExtension(path).Equals(".json", StringComparison.OrdinalIgnoreCase)).ToList();

            Trace.TraceInformation($"Have {filesFromPath1.Count} files from {path1}.");
            var filesFromPath2 = Directory.EnumerateFiles(path2).Where(path => Path.GetExtension(path).Equals(".json", StringComparison.OrdinalIgnoreCase)).ToList();

            Trace.TraceInformation($"Have {filesFromPath2.Count} files from {path2}.");

            var fileNames = filesFromPath1.Select(path => Path.GetFileName(path))
                            .Concat(filesFromPath2.Select(path => Path.GetFileName(path)))
                            .Distinct().ToList();;

            Console.WriteLine($"Start Compare the Files. Total Number: {fileNames.Count}.");
            var result = new List <CompareFile>(fileNames.Count);

            foreach (var fileName in fileNames)
            {
                reportProgress(progress, new ProgressReport(result.Count, fileNames.Count));

                JsonDocument file1, file2;

                try
                {
                    file1 = await readFileService.GetJsonDocument(Path.Combine(path1, fileName), config);
                }
                catch { file1 = null; }
                try
                {
                    file2 = await readFileService.GetJsonDocument(Path.Combine(path2, fileName), config);
                }
                catch { file2 = null; }

                result.Add(await analyzeService.Compare(file1, file2));
            }
            reportProgress(progress, new ProgressReport(fileNames.Count, fileNames.Count));

            Trace.Unindent();
            return(result);
        }