Example #1
0
        public override ValidationResult Validate(RepositoryInfo repositoryInfo, Action <ValidationResult> notify = null)
        {
            var result    = new ValidationResult(this);
            var fileNames = _fileSystemHelper.GetFiles(repositoryInfo.RepositoryRootPath, _fileNamePattern, _sourceFileFilters).ToList();

            string message;

            var backupFileName = string.Empty;

            if (_isBackupEnabled)
            {
                try
                {
                    backupFileName = Backup(repositoryInfo.RepositoryRootPath, fileNames);
                }
                catch (Exception e)
                {
                    message = string.Format(Resources.TransformRule_Validate_rule_can_not_create_backup_file, TransformMessage.ToLower());
                    result.AddResult(ResultLevel.Error, message, notify);
                    Logger.Error(message, e);
                    return(result);
                }
            }

            foreach (var fileName in fileNames)
            {
                string code;
                try
                {
                    code = _fileSystemHelper.ReadAllText(fileName);
                }
                catch (Exception e)
                {
                    message = string.Format(Resources.TransformRule_Validate_Can_not_read_file, TransformingMessage.ToLower(), fileName);
                    result.AddResult(ResultLevel.Error, message, notify);
                    Logger.Error(message, e);
                    continue;
                }

                if (_isBackupEnabled)
                {
                    if (!BackupExists(backupFileName, fileName, code))
                    {
                        message = string.Format(Resources.TransformRule_Validate_Not_file_because_can_not_verify_backup, TransformingMessage.ToLower(), fileName);
                        result.AddResult(ResultLevel.Error, message, notify);
                        Logger.Error(message);
                        continue;
                    }
                }

                string transformedCode;
                try
                {
                    transformedCode = Transform(code, fileName, notify);
                }
                catch (Exception e)
                {
                    message = string.Format(Resources.TransformRule_Validate_error_in_file, TransformerMessage, e.Message, fileName);
                    result.AddResult(ResultLevel.Error, message, notify);
                    Logger.Error(message, e);
                    continue;
                }
                try
                {
                    _fileSystemHelper.WriteAllText(fileName, transformedCode, Encoding.UTF8);
                }
                catch (Exception e)
                {
                    message = string.Format(Resources.TransformRule_Validate_Code_error_in_file, TransformingMessage.ToLower(), fileName);
                    result.AddResult(ResultLevel.Error, message, notify);
                    Logger.Error(message, e);
                    continue;
                }

                message = string.Format(Resources.TransformRule_Validate_File_successfully, TransformedMessage.ToLower(), fileName);
                result.AddResult(ResultLevel.Passed, message, notify);
            }
            return(result);
        }