Exemple #1
0
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if the task successfully executed; otherwise, <c>false</c>.
        /// </returns>
        public override bool Execute()
        {
            var settings    = this.LoadSettings();
            var sourceFiles = this.GetSourceFiles(settings);
            var output      = settings.Output.GetValueOrDefault();

            if (sourceFiles.Count > 0)
            {
                var logger        = this.GetLogger(output);
                var errorLimit    = settings.ErrorLimitOrDefault();
                var fileLimit     = settings.FileLimitOrDefault();
                var exceptions    = 0;
                var reportBuilder = new JSLintReportBuilder();

                using (var context = this.jsLintFactory())
                {
                    reportBuilder.SourceDirectory = this.SourceDirectory;
                    reportBuilder.AddSettings(settings.Files);

                    foreach (var file in sourceFiles)
                    {
                        var result = ExecutionHelper.Try(() =>
                        {
                            var source = this.fileSystemWrapper.ReadAllText(file.Absolute, Encoding.UTF8);

                            return(context.Lint(source, settings.Options, settings.GlobalVariables));
                        });

                        if (result.Success)
                        {
                            var data = result.Data;
                            reportBuilder.AddFile(file.Virtual, data);

                            foreach (var error in data.Warnings)
                            {
                                logger(
                                    subcategory: AssemblyInfo.Product,
                                    errorCode: null,
                                    helpKeyword: null,
                                    file: file.Absolute,
                                    lineNumber: error.Line + 1,
                                    columnNumber: error.Column + 1,
                                    endLineNumber: 0,
                                    endColumnNumber: 0,
                                    message: string.Concat(Resources.ErrorTextPrefix, error.Message));
                            }

                            if (reportBuilder.ErrorCount >= errorLimit)
                            {
                                this.Log.LogError(Resources.ErrorLimitReachedFormat, reportBuilder.ErrorCount);

                                break;
                            }
                        }
                        else
                        {
                            this.Log.LogError(
                                Resources.ErrorEncounteredFormat,
                                file.Virtual,
                                Environment.NewLine,
                                result.Exception.Message);

                            exceptions += 1;

                            if (exceptions >= JSLintNetSettings.ExceptionLimit)
                            {
                                this.Log.LogError(Resources.ExceptionLimitReachedFormat, exceptions);

                                break;
                            }
                        }

                        if (reportBuilder.ProcessedFileCount >= fileLimit)
                        {
                            this.Log.LogError(Resources.FileLimitReachedFormat, reportBuilder.ProcessedFileCount);

                            break;
                        }
                    }

                    var reportFile = this.ReportFile;
                    if (!string.IsNullOrEmpty(reportFile))
                    {
                        if (!Path.IsPathRooted(reportFile))
                        {
                            reportFile = Path.Combine(this.SourceDirectory, reportFile);
                        }

                        this.fileSystemWrapper.WriteAllText(reportFile, reportBuilder.ToString(), Encoding.UTF8);
                    }

                    this.ErrorCount         = reportBuilder.ErrorCount;
                    this.ErrorFileCount     = reportBuilder.ErrorFileCount;
                    this.ProcessedFileCount = reportBuilder.ProcessedFileCount;
                }
            }
            else
            {
                this.ErrorCount         = 0;
                this.ErrorFileCount     = 0;
                this.ProcessedFileCount = 0;
            }

            return(output != Output.Error || this.ErrorCount == 0);
        }
Exemple #2
0
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if the task successfully executed; otherwise, <c>false</c>.
        /// </returns>
        public override bool Execute()
        {
            var settings = this.LoadSettings();
            var sourceFiles = this.GetSourceFiles(settings);
            var output = settings.Output.GetValueOrDefault();

            if (sourceFiles.Count > 0)
            {
                var logger = this.GetLogger(output);
                var errorLimit = settings.ErrorLimitOrDefault();
                var fileLimit = settings.FileLimitOrDefault();
                var exceptions = 0;
                var reportBuilder = new JSLintReportBuilder();

                using (var context = this.jsLintFactory())
                {
                    reportBuilder.SourceDirectory = this.SourceDirectory;
                    reportBuilder.AddSettings(settings.Files);

                    foreach (var file in sourceFiles)
                    {
                        var result = ExecutionHelper.Try(() =>
                        {
                            var source = this.fileSystemWrapper.ReadAllText(file.Absolute, Encoding.UTF8);

                            return context.Lint(source, settings.Options, settings.GlobalVariables);
                        });

                        if (result.Success)
                        {
                            var data = result.Data;
                            reportBuilder.AddFile(file.Virtual, data);

                            foreach (var error in data.Warnings)
                            {
                                logger(
                                    subcategory: AssemblyInfo.Product,
                                    errorCode: null,
                                    helpKeyword: null,
                                    file: file.Absolute,
                                    lineNumber: error.Line + 1,
                                    columnNumber: error.Column + 1,
                                    endLineNumber: 0,
                                    endColumnNumber: 0,
                                    message: string.Concat(Resources.ErrorTextPrefix, error.Message));
                            }

                            if (reportBuilder.ErrorCount >= errorLimit)
                            {
                                this.Log.LogError(Resources.ErrorLimitReachedFormat, reportBuilder.ErrorCount);

                                break;
                            }
                        }
                        else
                        {
                            this.Log.LogError(
                                Resources.ErrorEncounteredFormat,
                                file.Virtual,
                                Environment.NewLine,
                                result.Exception.Message);

                            exceptions += 1;

                            if (exceptions >= JSLintNetSettings.ExceptionLimit)
                            {
                                this.Log.LogError(Resources.ExceptionLimitReachedFormat, exceptions);

                                break;
                            }
                        }

                        if (reportBuilder.ProcessedFileCount >= fileLimit)
                        {
                            this.Log.LogError(Resources.FileLimitReachedFormat, reportBuilder.ProcessedFileCount);

                            break;
                        }
                    }

                    var reportFile = this.ReportFile;
                    if (!string.IsNullOrEmpty(reportFile))
                    {
                        if (!Path.IsPathRooted(reportFile))
                        {
                            reportFile = Path.Combine(this.SourceDirectory, reportFile);
                        }

                        this.fileSystemWrapper.WriteAllText(reportFile, reportBuilder.ToString(), Encoding.UTF8);
                    }

                    this.ErrorCount = reportBuilder.ErrorCount;
                    this.ErrorFileCount = reportBuilder.ErrorFileCount;
                    this.ProcessedFileCount = reportBuilder.ProcessedFileCount;
                }
            }
            else
            {
                this.ErrorCount = 0;
                this.ErrorFileCount = 0;
                this.ProcessedFileCount = 0;
            }

            return output != Output.Error || this.ErrorCount == 0;
        }