using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; using CTA.WebForms.Extensions; using CTA.WebForms.Helpers; namespace CTA.WebForms.DirectiveConverters { public class RegisterDirectiveConverter : DirectiveConverter { private const string IncorrectRegisterDirectiveWarning = ""; private const string FileNameDoesNotContainDirectoryErrorTemplate = ""; private const string ControlTagName = "TagName"; private const string ControlTagPrefix = "TagPrefix"; private const string ControlSourceFile = "Src"; private protected override IEnumerable AttributeAllowList { get { return new[] { ControlSourceFile, }; } } private protected override IEnumerable GetMigratedDirectives(string directiveName, string originalFilePath) { return Enumerable.Empty(); } private protected override IEnumerable GetMigratedAttributes(string directiveString, string directiveName, string projectName) { var attrMap = new Dictionary(StringComparer.InvariantCultureIgnoreCase); var migratedDirectives = new List(); var attrMatches = AttributeSplitRegex.Matches(directiveString); foreach (Match match in attrMatches) { var attrName = match.Groups[AttributeNameRegexGroupName].Value; var attrValue = match.Groups[AttributeValueRegexGroupName].Value; attrMap.Add(attrName, attrValue.RemoveOuterQuotes()); } if (attrMap.ContainsKey(ControlTagName) && attrMap.ContainsKey(ControlTagPrefix)) { var oldControlName = attrMap[ControlTagPrefix] + ":" + attrMap[ControlTagName]; var newControlName = Path.GetFileNameWithoutExtension(attrMap[ControlSourceFile]); //_registeredUserControls.UserControlRulesMap.Add(oldControlName, new UserControlConverter(newControlName)); var filePath = attrMap[ControlSourceFile]; var blazorNamespace = FilePathHelper.GetNamespaceFromRelativeFilePath(filePath, projectName); if(string.IsNullOrEmpty(blazorNamespace)) { var errorMessageCommentString = string.Format(FileNameDoesNotContainDirectoryErrorTemplate, filePath); var commentedDirectiveString = directiveString.ConvertToRazorComment(); var content = Utilities.SeparateStringsWithNewLine(errorMessageCommentString, commentedDirectiveString); migratedDirectives.Add(new DirectiveMigrationResult(DirectiveMigrationResultType.Comment, content)); } else { var content = $"@using {blazorNamespace}"; migratedDirectives.Add(new DirectiveMigrationResult(DirectiveMigrationResultType.GeneralDirective, content)); } } else { var commentedDirectiveString = directiveString.ConvertToRazorComment(); var content = Utilities.SeparateStringsWithNewLine(IncorrectRegisterDirectiveWarning, commentedDirectiveString); migratedDirectives.Add(new DirectiveMigrationResult(DirectiveMigrationResultType.Comment, content)); } return migratedDirectives; } } }