/// <summary> /// Add actions to each node type /// </summary> /// <param name="nodeToken">The token to add the action to</param> /// <param name="actions">The list of actions associated with this token</param> private void ParseActions(NodeToken nodeToken, List <Action> actions) { foreach (var action in actions) { try { var actionType = Enum.Parse(typeof(ActionTypes), action.Type); switch (actionType) { case ActionTypes.Method: { var actionFunc = actionsLoader.GetInvocationExpressionAction(action.Name, action.Value); if (actionFunc != null) { nodeToken.InvocationExpressionActions.Add(new InvocationExpressionAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, InvocationExpressionActionFunc = actionFunc }); } break; } case ActionTypes.Expression: { var actionFunc = actionsLoader.GetExpressionAction(action.Name, action.Value); if (actionFunc != null) { nodeToken.ExpressionActions.Add(new ExpressionAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, ExpressionActionFunc = actionFunc }); } break; } case ActionTypes.Class: { var actionFunc = actionsLoader.GetClassAction(action.Name, action.Value); if (actionFunc != null) { nodeToken.ClassDeclarationActions.Add(new ClassDeclarationAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, ClassDeclarationActionFunc = actionFunc }); } break; } case ActionTypes.Interface: { var actionFunc = actionsLoader.GetInterfaceAction(action.Name, action.Value); if (actionFunc != null) { nodeToken.InterfaceDeclarationActions.Add(new InterfaceDeclarationAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, InterfaceDeclarationActionFunc = actionFunc }); } break; } case ActionTypes.Using: { var actionFunc = actionsLoader.GetCompilationUnitAction(action.Name, action.Value); if (actionFunc != null) { nodeToken.UsingActions.Add(new UsingAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, UsingActionFunc = actionFunc }); } break; } case ActionTypes.Namespace: { var actionFunc = actionsLoader.GetNamespaceActions(action.Name, action.Value); if (actionFunc != null) { nodeToken.NamespaceActions.Add(new NamespaceAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, NamespaceActionFunc = actionFunc }); } break; } case ActionTypes.Identifier: { var actionFunc = actionsLoader.GetIdentifierNameAction(action.Name, action.Value); if (actionFunc != null) { nodeToken.IdentifierNameActions.Add(new IdentifierNameAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, IdentifierNameActionFunc = actionFunc }); } break; } case ActionTypes.Attribute: { var actionFunc = actionsLoader.GetAttributeAction(action.Name, action.Value); if (actionFunc != null) { nodeToken.AttributeActions.Add(new AttributeAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, AttributeActionFunc = actionFunc }); } break; } case ActionTypes.AttributeList: { var actionFunc = actionsLoader.GetAttributeListAction(action.Name, action.Value); if (actionFunc != null) { nodeToken.AttributeListActions.Add(new AttributeAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, AttributeListActionFunc = actionFunc }); } break; } case ActionTypes.ObjectCreation: { var actionFunc = actionsLoader.GetObjectCreationExpressionActions(action.Name, action.Value); if (actionFunc != null) { nodeToken.ObjectCreationExpressionActions.Add(new ObjectCreationExpressionAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, ObjectCreationExpressionGenericActionFunc = actionFunc }); } break; } case ActionTypes.MethodDeclaration: { var actionFunc = actionsLoader.GetMethodDeclarationAction(action.Name, action.Value); if (actionFunc != null) { nodeToken.MethodDeclarationActions.Add(new MethodDeclarationAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, MethodDeclarationActionFunc = actionFunc }); } break; } case ActionTypes.ElementAccess: { var actionFunc = actionsLoader.GetElementAccessExpressionActions(action.Name, action.Value); if (actionFunc != null) { nodeToken.ElementAccessActions.Add(new ElementAccessAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, ElementAccessExpressionActionFunc = actionFunc }); } break; } case ActionTypes.MemberAccess: { var actionFunc = actionsLoader.GetMemberAccessExpressionActions(action.Name, action.Value); if (actionFunc != null) { nodeToken.MemberAccessActions.Add(new MemberAccessAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, MemberAccessActionFunc = actionFunc }); } break; } case ActionTypes.Project: { var actionFunc = actionsLoader.GetProjectLevelActions(action.Name, action.Value); if (actionFunc != null) { nodeToken.ProjectLevelActions.Add(new ProjectLevelAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, ProjectLevelActionFunc = actionFunc }); } break; } case ActionTypes.ProjectFile: { var actionFunc = actionsLoader.GetProjectFileActions(action.Name, action.Value); if (actionFunc != null) { nodeToken.ProjectFileActions.Add(new ProjectLevelAction() { Key = nodeToken.Key, Value = GetActionValue(action.Value), Description = action.Description, ActionValidation = action.ActionValidation, Name = action.Name, Type = action.Type, ProjectFileActionFunc = actionFunc }); } break; } case ActionTypes.Package: { PackageAction packageAction = new PackageAction(); if (action.Value is string) { packageAction.Name = action.Value; } else { Dictionary <string, string> jsonParameters = JsonConvert.DeserializeObject <Dictionary <string, string> >(action.Value.ToString()); if (jsonParameters.ContainsKey(CTA.Rules.Config.Constants.PackageName)) { packageAction.Name = jsonParameters[CTA.Rules.Config.Constants.PackageName]; } else { LogHelper.LogDebug(string.Format("Parameter {0} is not available for action {1}" , Config.Constants.PackageName, action.Name)); continue; } //TODO: If version is not available/valid, we use latest version. Should we reconsider? if (jsonParameters.ContainsKey(CTA.Rules.Config.Constants.PackageVersion)) { packageAction.Version = jsonParameters[CTA.Rules.Config.Constants.PackageVersion]; } } nodeToken.PackageActions.Add(packageAction); break; } } } catch (Exception ex) { LogHelper.LogError(ex, string.Format("Error parsing action type {0}", action.Type)); } } }
public void ClassActionsTest() { var removeBaseClass = _actionLoader.GetClassAction("RemoveBaseClass", "baseClass"); var addBaseClass = _actionLoader.GetClassAction("AddBaseClass", "baseClass"); var changeName = _actionLoader.GetClassAction("ChangeName", "className"); var removeAttribute = _actionLoader.GetClassAction("RemoveAttribute", "attributeName"); var addAttribute = _actionLoader.GetClassAction("AddAttribute", "attribute"); var addComment = _actionLoader.GetClassAction("AddComment", "{comment: \"comment here\", dontUseCTAPrefix: \"true\"}"); var addComment2 = _actionLoader.GetClassAction("AddComment", "comment"); var addMethod = _actionLoader.GetClassAction("AddMethod", "expression"); var removeMethod = _actionLoader.GetClassAction("RemoveMethod", "methodName"); var renameClass = _actionLoader.GetClassAction("RenameClass", "newClassName"); var replaceMethodModifiers = _actionLoader.GetClassAction("ReplaceMethodModifiers", "{ methodName: \"\", modifiers: \"\"}"); var addExpression = _actionLoader.GetClassAction("AddExpression", "expression"); var removeConstructorInitializer = _actionLoader.GetClassAction("RemoveConstructorInitializer", "baseClass"); var appendConstructorExpression = _actionLoader.GetClassAction("AppendConstructorExpression", "expression"); var createConstructor = _actionLoader.GetClassAction("CreateConstructor", "{ types: \"type\", identifiers: \"identify\"}"); var changeMethodName = _actionLoader.GetClassAction("ChangeMethodName", "{ existingMethodName: \"method\", newMethodName: \"newMethod\"}"); var changeMethodToReturnTaskType = _actionLoader.GetClassAction("ChangeMethodToReturnTaskType", "methodName"); var removeMethodParameters = _actionLoader.GetClassAction("RemoveMethodParameters", "methodName"); var commentMethod = _actionLoader.GetClassAction("CommentMethod", "{ methodName: \"SuperMethod\", comment: \"comment here\", dontUseCTAPrefix: \"true\"}"); var addCommentsToMethod = _actionLoader.GetClassAction("AddCommentsToMethod", "{ methodName: \"SuperMethod\", comment: \"comments here\", dontUseCTAPrefix: \"false\"}"); var addCommentsToMethod2 = _actionLoader.GetClassAction("AddCommentsToMethod", "{ methodName: \"SuperMethod\", comment: \"comments here\" }"); var addExpressionToMethod = _actionLoader.GetClassAction("AddExpressionToMethod", "{ methodName: \"DuperMethod\", expression: \"var maths = 1+1;\"}"); var addParametersToMethod = _actionLoader.GetClassAction("AddParametersToMethod", "{ methodName: \"MethodHere\", types: \"type\", identifiers: \"identifier\"}"); var replaceMvcControllerMethodsBody = _actionLoader.GetClassAction("ReplaceMvcControllerMethodsBody", "expression"); var replaceWebApiControllerMethodsBody = _actionLoader.GetClassAction("ReplaceWebApiControllerMethodsBody", "expression"); Assert.IsNotNull(removeBaseClass); Assert.IsNotNull(addBaseClass); Assert.IsNotNull(changeName); Assert.IsNotNull(removeAttribute); Assert.IsNotNull(addAttribute); Assert.IsNotNull(addComment); Assert.IsNotNull(addComment2); Assert.IsNotNull(addMethod); Assert.IsNotNull(removeMethod); Assert.IsNotNull(renameClass); Assert.IsNotNull(replaceMethodModifiers); Assert.IsNotNull(addExpression); Assert.IsNotNull(removeConstructorInitializer); Assert.IsNotNull(appendConstructorExpression); Assert.IsNotNull(createConstructor); Assert.IsNotNull(changeMethodName); Assert.IsNotNull(changeMethodToReturnTaskType); Assert.IsNotNull(removeMethodParameters); Assert.IsNotNull(commentMethod); Assert.IsNotNull(addCommentsToMethod); Assert.IsNotNull(addCommentsToMethod2); Assert.IsNotNull(addExpressionToMethod); Assert.IsNotNull(addParametersToMethod); Assert.IsNotNull(replaceMvcControllerMethodsBody); Assert.IsNotNull(replaceWebApiControllerMethodsBody); }