Exemple #1
0
        /// <inheritdoc />
        public override bool AnalyzeSourceFile(Workspace workspace, AbsolutePath path, ISourceFile sourceFile)
        {
            var filePath = path.ToString(PathTable, PathFormat.HostOs);

            using (var writer = new ScriptWriter())
            {
                var visitor = GetPrettyPrintVisitor(writer);
                sourceFile.Cast <IVisitableNode>().Accept(visitor);
                var formattedText = writer.ToString();

                string existingText = sourceFile.Text.Substring(0, sourceFile.Text.Length);

                bool matches = string.Equals(existingText, formattedText, StringComparison.Ordinal);
                if (!matches)
                {
                    if (Fix)
                    {
                        try
                        {
                            File.WriteAllText(filePath, formattedText);
                        }
                        catch (IOException ex)
                        {
                            Logger.PrettyPrintErrorWritingSpecFile(LoggingContext, new Location()
                            {
                                File = filePath
                            }, ex.Message);
                            return(false);
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            Logger.PrettyPrintErrorWritingSpecFile(LoggingContext, new Location()
                            {
                                File = filePath
                            }, ex.Message);
                            return(false);
                        }
                    }
                    else
                    {
                        ReportFirstDifference(Logger, LoggingContext, existingText, formattedText, filePath);
                        return(false);
                    }
                }

                return(true);
            }
        }