public void Test() { string codeText = File.ReadAllText("Resources\\BasicClass.txt"); var parser = new CSharpParser(); parser.ParseCode(codeText); var codeRoot = (CodeRoot)parser.CreatedCodeRoot; //Actions actions = Test2(codeRoot);//Test1(codeRoot); Actions actions = new Actions(); Entity entity = new EntityImpl("BasicClass"); entity.AddProperty(new PropertyImpl { Name = "Property5", Type = "Entity" }); Entity entity1 = new EntityImpl("Class1"); entity1.AddProperty(new PropertyImpl { Name = "Property5", Type = "Entity" }); entity.MappedClass = codeRoot.Namespaces[0].Classes[0]; entity1.MappedClass = codeRoot.Namespaces[0].Classes[1]; CheckEntity(entity, actions); codeText = actions.RunActions(codeText, codeRoot, true); actions = new Actions(); actions.AddAction(new AddAttributeToPropertyAction(entity.MappedClass.Properties[0], new Attribute(codeRoot.Controller){Name = "Attr"})); CheckEntity(entity1, actions); var output = actions.RunActions(codeText, codeRoot, false); }
private static void CheckEntity(Entity entity, Actions actions) { foreach (var property in entity.Properties) { entity.MappedClass.EnsureHasProperty(property.Name.GetCSharpFriendlyIdentifier(), property.OldNames) .WithType(property.Type) .WithModifiers("public") .WithModifiers(property.IsInherited ? "override" : "virtual") .WithGetAccessorBody(";") .WithSetAccessorBody(";") .ApplyTo(actions); } }
public void ApplyTo(Actions actions) { var usingStatement = Construct(_class); // Try find the UsingStatement in the existing class using the old name var existingUsingStatement = _class.Controller.Root.UsingStatements.FirstOrDefault(u => u.ToString() == _value); if (existingUsingStatement != null) { if (_mustRemove) ApplyUsingStatementDeletion(actions, existingUsingStatement); } else if (!_mustRemove) { // Add the UsingStatement to the class. actions.AddAction(new AddUsingStatementToClassAction(_class, usingStatement)); } }
public void ApplyTo(Actions actions) { var interface1 = Construct(_class); // Try find the Interface in the existing class using the old name var existingInterface = _class.BaseNames.FirstOrDefault(i => i == _value); if (existingInterface != null) { if (_mustRemove) ApplyImplementsDeletion(actions, _class, existingInterface); } else if (!_mustRemove) { // Add the Implements to the class. DataType newImplements = new DataType(_class.Controller, interface1.Name); actions.AddAction(new AddImplementsToClassAction(_class, newImplements, false)); } }
public void ApplyTo(Actions actions) { foreach (var attr in attributes) { var attribute = attr.Build(_class.Controller); if (attribute == null) continue; if (_class.HasAttributeNamed(attribute.Name) || attributesToDelete.Contains(attribute.Name)) continue; actions.AddAction(new AddAttributeToClassAction(_class, attribute)); } foreach (var attr in attributesToDelete) { if (_class.HasAttributeNamed(attr)) { ArchAngel.Providers.CodeProvider.DotNet.Attribute att = _class.Attributes.Find(a => a.Name == attr); actions.AddAction(new RemoveAttributeFromClassAction(att)); } } }
public void They_Are_Added_Correctly() { var parser = new CSharpParser(); parser.ParseCode(Code.Class1Text); var codeRoot = (CodeRoot)parser.CreatedCodeRoot; var basicClass = codeRoot.Namespaces[0].Classes[0]; var property2 = new Property(codeRoot.Controller, "Property1", new DataType(codeRoot.Controller, "string"), "public"); var property3 = new Property(codeRoot.Controller, "Property2", new DataType(codeRoot.Controller, "Class1"), "private"); var action1 = new AddPropertyToClassAction(property2, AdditionPoint.EndOfParent, basicClass); var action2 = new AddPropertyToClassAction(property3, AdditionPoint.EndOfParent, basicClass); Actions actions = new Actions(); actions.AddAction(action1); actions.AddAction(action2); string output = actions.RunActions(parser); output = Slyce.Common.Utility.StandardizeLineBreaks(output, Environment.NewLine); Assert.That(output, Is.EqualTo(Code.Class1Plus2PropertiesText)); }
private static void ApplyTypeChanges(Property existingProperty, Property exampleProperty, Actions actions) { if (exampleProperty.DataType != null && existingProperty.DataType.Equals(exampleProperty.DataType) == false) { // Change datatype actions.AddAction(new ChangeTypeOfPropertyAction(existingProperty, exampleProperty.DataType)); } }
private static void ApplyPropertyChanges(Actions actions, Property existingProperty, Property exampleProperty) { ApplyNameChanges(existingProperty, exampleProperty, actions); ApplyModifierChanges(existingProperty, exampleProperty, actions); ApplyAccessorChanges(existingProperty, exampleProperty, actions); ApplyTypeChanges(existingProperty, exampleProperty, actions); }
private static void ApplyPropertyDeletion(Actions actions, Property existingProperty) { actions.AddAction(new RemovePropertyFromClassAction(existingProperty)); }
private static void ApplyUsingStatementDeletion(Actions actions, UsingStatement existingUsingStatement) { actions.AddAction(new RemoveUsingStatementFromClassAction(existingUsingStatement)); }
private static void ApplyNameChanges(Property existingProperty, Property exampleProperty, Actions actions) { if (existingProperty.Name != exampleProperty.Name) { // Change datatype actions.AddAction(new ChangeNameOfPropertyAction(existingProperty, exampleProperty.Name)); } }
private static void ApplyModifierChanges(Field existingField, Field exampleField, Actions actions) { if (existingField.Modifiers.UnorderedEqual(exampleField.Modifiers) == false) { string modifierString = ""; foreach (var mod in existingField.Modifiers) modifierString += mod + " "; modifierString = modifierString.Trim(); actions.AddAction(new RemoveModifierFromFieldAction(existingField, modifierString)); foreach (var modifier in exampleField.Modifiers) actions.AddAction(new AddModifierToFieldAction(existingField, modifier, false)); } }
private static void ApplyTypeChanges(Field existingField, Field exampleField, Actions actions) { if (exampleField.DataType != null && existingField.DataType.Equals(exampleField.DataType) == false) { // Change datatype actions.AddAction(new ChangeTypeOfFieldAction(existingField, exampleField.DataType)); } }
private static void ApplyImplementsDeletion(Actions actions, Class _class, string existingInterface) { DataType newImplements = new DataType(_class.Controller, existingInterface); actions.AddAction(new RemoveImplementsFromClassAction(_class, existingInterface)); }
private static void ApplyFieldDeletion(Actions actions, Field existingField) { actions.AddAction(new RemoveFieldFromClassAction(existingField)); }
private static void ApplyBodyChanges(Function existingFunction, Function exampleFunction, Actions actions) { actions.AddAction(new ChangeMethodBodyAction(existingFunction, exampleFunction)); }
private static void ApplyFunctionChanges(Actions actions, Function existingFunction, Function exampleFunction) { ApplyHeaderChanges(existingFunction, exampleFunction, actions); ApplyBodyChanges(existingFunction, exampleFunction, actions); }
public void ApplyTo(Actions actions) { var function = Construct(_class); // Try find the function in the existing class using the old name Function existingFunction = null;// _class.Functions.FirstOrDefault(p => p.Name == _name); foreach (var func in _class.Functions.Where(p => p.Name == _name && (_ParameterTypes != null && p.Parameters.Count == _ParameterTypes.Count))) { bool found = true; for (int i = 0; i < func.Parameters.Count; i++) { if (func.Parameters[i].DataType != _ParameterTypes[i].DataType) { found = false; break; } } if (found) { existingFunction = func; break; } } if (existingFunction == null) { for (int i = _oldNames.Count - 1; i >= 0; i--) { // No property found with the new name, so try the old name //existingFunction = _class.Functions.FirstOrDefault(p => p.Name == _oldNames[i]); //if (existingFunction != null) // break; foreach (var func in _class.Functions.Where(p => p.Name == _oldNames[i] && p.Parameters.Count == _ParameterTypes.Count)) { bool found = true; for (int pCounter = 0; pCounter < func.Parameters.Count; pCounter++) { if (func.Parameters[pCounter].DataType != _ParameterTypes[pCounter].DataType) { found = false; break; } } if (found) { existingFunction = func; break; } } if (existingFunction != null) break; } } //if (existingFunction == null) //{ // // As a last ditch effort to find the property, ignore case when searching // existingFunction = _class.Functions.FirstOrDefault(p => p.Name.ToLowerInvariant() == _name.ToLowerInvariant()); //} if (existingFunction != null) { if (_mustRemove) ApplyFunctionDeletion(actions, existingFunction); else { // Make sure the property matches the given property ApplyFunctionChanges(actions, existingFunction, function); } } else if (_mustRemove) { // No existing property found, so we don't need to do anything, it's already removed. // Don't add any actions return; } else { // Add the property to the class. actions.AddAction(new AddFunctionToClassAction(function, AdditionPoint.EndOfParent, _class)); } if (!_mustRemove) { var functionToAddTo = existingFunction ?? function; //foreach (var attrBuilder in attributes) //{ // var attribute = attrBuilder.Build(functionToAddTo.Controller); // if (functionToAddTo.Attributes.Any(a => a.Name == attribute.Name) || // functionToAddTo.AttributeSections.SelectMany(section => section.Attributes).Any(a => a.Name == attribute.Name)) // { // // if there are any attributes on the property currently with this name, then we should skip it. // continue; // } // actions.AddAction(new AddAttributeToMethodAction(functionToAddTo, attribute)); //} } }
/// <summary> /// Changes the entire header, including modifiers, return-type, name and parameters. /// </summary> /// <param name="existingFunction"></param> /// <param name="exampleFunction"></param> /// <param name="actions"></param> private static void ApplyHeaderChanges(Function existingFunction, Function exampleFunction, Actions actions) { if (existingFunction.Name != exampleFunction.Name) { // Change datatype actions.AddAction(new ChangeMethodHeaderAction(existingFunction, exampleFunction)); } }
private static void ApplyFunctionDeletion(Actions actions, Function existingFunction) { actions.AddAction(new RemoveMethodFromClassAction(existingFunction)); }
private static void ApplyFieldChanges(Actions actions, Field existingField, Field exampleField) { ApplyNameChanges(existingField, exampleField, actions); ApplyModifierChanges(existingField, exampleField, actions); ApplyInitialValueChanges(existingField, exampleField, actions); ApplyTypeChanges(existingField, exampleField, actions); }
public string Process(ArchAngel.Interfaces.Scripting.NHibernate.Model.IEntity ientity, Dictionary<string, object> TemplateCache, out string CurrentFileName) { Entity entity = (Entity)ientity.ScriptObject; Actions actions = new Actions(); foreach (var att in ientity.MappedClass.SourceAttributesThatMustExist) { entity.MappedClass.EnsureHas() .AttributeNamed(att.Name) .Finish() .ApplyTo(actions); } foreach (var prop in ientity.MappedClass.SourcePropertiesThatMustExist) { string[] modifiers = prop.Modifiers == null ? new string[0] : prop.Modifiers.ToArray(); PropertyConstraintBuilder propertyConstraint = null; propertyConstraint = entity.MappedClass.EnsureHasProperty(prop.Name, prop.PreviousNames ?? new List<string>()) .WithType(prop.Type) .WithModifiers(modifiers) .WithGetAccessorBody(prop.GetAccessor.Body) .WithSetAccessorBody(prop.SetAccessor.Body); if (!string.IsNullOrEmpty(prop.SetAccessor.Modifier)) propertyConstraint.WithSetAccessorAccessibility(prop.SetAccessor.Modifier); propertyConstraint.ApplyTo(actions); } foreach (var prop in ientity.MappedClass.SourcePropertiesThatMustNotExist) { PropertyConstraintBuilder propertyConstraint = null; propertyConstraint.ApplyTo(actions); } foreach (var field in ientity.MappedClass.SourceFieldsThatMustExist) { string[] modifiers = field.Modifiers == null ? new string[0] : field.Modifiers.ToArray(); FieldConstraintBuilder fieldConstraint = null; fieldConstraint = entity.MappedClass.EnsureHasField(field.Name, field.PreviousNames ?? new List<string>()) .WithType(field.Type) .WithModifiers(modifiers) .WithInitialValue(field.InitialValue); fieldConstraint.ApplyTo(actions); } foreach (var function in ientity.MappedClass.SourceFunctionsThatMustExist) { string[] modifiers = function.Modifiers == null ? new string[0] : function.Modifiers.ToArray(); FunctionConstraintBuilder functionConstraint = null; functionConstraint = entity.MappedClass.EnsureHasFunction(function.Name, function.PreviousNames ?? new List<string>()) .WithBody(function.Body) .WithModifiers(modifiers) .WithParameterTypes(function.Parameters) .WithReturnType(function.ReturnType); functionConstraint.ApplyTo(actions); } foreach (var function in ientity.MappedClass.SourceFunctionsThatMustNotExist) { FunctionConstraintBuilder functionConstraint = null; functionConstraint.ApplyTo(actions); } // Load the existing C# file up var filename = entity.EntitySet.MappingSet.CodeParseResults.GetFilenameForParsedClass(entity.MappedClass); CurrentFileName = filename; string outputText; if (TemplateCache.ContainsKey(filename) || !System.IO.File.Exists(filename)) outputText = actions.RunActions((string)TemplateCache[filename], entity.MappedClass.Controller.Root, false); else outputText = actions.RunActions(System.IO.File.ReadAllText(filename), entity.MappedClass.Controller.Root, true); TemplateCache[filename] = outputText; return outputText; }
private static void ApplyInitialValueChanges(Field existingField, Field exampleField, Actions actions) { if (exampleField.InitialValue != null && existingField.InitialValue.Equals(exampleField.InitialValue) == false) { // Change initial value actions.AddAction(new ChangeInitialValueOfFieldAction(existingField, exampleField.InitialValue)); } }
public string Process(Entity entity, Dictionary<string, object> TemplateCache, bool globalUsePrivateSetters, string globalProjectNamespace, out string CurrentFileName) { Actions actions = new Actions(); if (entity.HasUserOption("Entity_MarkAsSerializable") && entity.GetUserOptionValue<bool>("Entity_MarkAsSerializable")) { entity.MappedClass.EnsureHas() .AttributeNamed("Serializable") .Finish() .ApplyTo(actions); } List<string> propertyNamesFromReferences = entity.DirectedReferences.Where(r => r.FromEndEnabled).Select(d => d.FromName).ToList(); List<string> propertyNamesFromComponents = entity.Components.Select(c => c.Name).ToList(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (var property in entity.Properties) sb.AppendLine(PropertyTemplate.ProcessPropertyTemplate(entity, property)); List<ArchAngel.Providers.CodeProvider.DotNet.Property> codeProps = ProcessTemplateProperties(sb.ToString()); Dictionary<ArchAngel.Providers.EntityModel.Model.EntityLayer.Property, ArchAngel.Providers.CodeProvider.DotNet.Property> propMap = new Dictionary<Property, ArchAngel.Providers.CodeProvider.DotNet.Property>(); for (int i = 0; i < entity.Properties.Count(); i++) propMap.Add(entity.Properties.ElementAt(i), codeProps[i]); foreach (var property in entity.Properties) { //sb.AppendLine(PropertyTemplate.ProcessPropertyTemplate(entity, property)); PropertyConstraintBuilder propertyConstraint = null; if (entity.ForeignKeyPropertiesToExclude.Contains(property)) { if (propertyNamesFromReferences.Contains(property.Name) || propertyNamesFromComponents.Contains(property.Name)) { // Don't mark this property for removal, because it's actually going to be added by a reference or component further down. continue; } propertyConstraint = entity.MappedClass.EnsureDoesNotHaveProperty(propMap[property].Name, property.OldNames); } else { // Add property's current name to its old name list if (property.OldNames.Count == 0 || !property.OldNames.Contains(propMap[property].Name)) property.OldNames.Add(propMap[property].Name); propertyConstraint = entity.MappedClass.EnsureHasProperty(propMap[property].Name, property.OldNames) .WithType(propMap[property].DataType.Name.Replace(string.Format("{0}.Model.", globalProjectNamespace), "")) .WithModifiers(propMap[property].Modifiers.ToArray()) .WithGetAccessorBody(propMap[property].GetAccessor.BodyText) .WithSetAccessorBody(propMap[property].SetAccessor.BodyText); //bool propertyUsePrivateSetter = property.HasUserOption("Property_UsePrivateSetter") && property.GetUserOptionValue<bool>("Property_UsePrivateSetter"); //if (propertyUsePrivateSetter || globalUsePrivateSetters) if (propMap[property].SetAccessor.Modifier == "private") propertyConstraint.WithSetAccessorAccessibility("private"); // Code insertions stuff for NHibernate Validator Attributes. Replaced with XML specs. //if(property.MaximumLength.HasValue && property.MaximumLength > 0 && property.MaximumLength < int.MaxValue) //{ // propertyConstraint.WithAttribute("Length") // .WithParameter(property.MaximumLength.Value.ToString()); //} } propertyConstraint.ApplyTo(actions); } #region DirectedReferences sb = new System.Text.StringBuilder(); foreach (var reference in entity.DirectedReferences) sb.AppendLine(PropertyTemplate.ProcessReferenceTemplate(entity, reference)); List<ArchAngel.Providers.CodeProvider.DotNet.Property> codeRefProps = ProcessTemplateProperties(sb.ToString()); Dictionary<ArchAngel.Providers.EntityModel.Model.EntityLayer.DirectedReference, ArchAngel.Providers.CodeProvider.DotNet.Property> refPropMap = new Dictionary<DirectedReference, ArchAngel.Providers.CodeProvider.DotNet.Property>(); for (int i = 0; i < entity.DirectedReferences.Count(); i++) refPropMap.Add(entity.DirectedReferences.ElementAt(i), codeRefProps[i]); foreach (var reference in entity.DirectedReferences.Where(r => r.FromEndEnabled).OrderBy(r => r.FromName)) { // Add reference's current name to its old name list if (reference.OldFromNames.Count == 0 || !reference.OldFromNames.Contains(refPropMap[reference].Name)) reference.OldFromNames.Add(refPropMap[reference].Name); var propertyConstraint = entity.MappedClass.EnsureHasProperty(refPropMap[reference].Name, reference.OldFromNames) .WithType(refPropMap[reference].DataType.Name.Replace(string.Format("{0}.Model.", globalProjectNamespace), "")) .WithModifiers(refPropMap[reference].Modifiers.ToArray()) .WithGetAccessorBody(refPropMap[reference].GetAccessor.BodyText) .WithSetAccessorBody(refPropMap[reference].SetAccessor.BodyText); if (refPropMap[reference].SetAccessor.Modifier == "private") propertyConstraint.WithSetAccessorAccessibility("private"); propertyConstraint.ApplyTo(actions); } #endregion #region Component Definitions sb = new System.Text.StringBuilder(); foreach (var component in entity.Components) sb.AppendLine(PropertyTemplate.ProcessComponentTemplate(entity, component)); List<ArchAngel.Providers.CodeProvider.DotNet.Property> codeComponentProps = ProcessTemplateProperties(sb.ToString()); Dictionary<ArchAngel.Providers.EntityModel.Model.EntityLayer.Component, ArchAngel.Providers.CodeProvider.DotNet.Property> componentPropMap = new Dictionary<Component, ArchAngel.Providers.CodeProvider.DotNet.Property>(); for (int i = 0; i < entity.Components.Count(); i++) componentPropMap.Add(entity.Components.ElementAt(i), codeComponentProps[i]); foreach (var component in entity.Components.OrderBy(c => c.Name)) { // Add component's current name to its old name list if (component.OldNames.Count == 0 || !component.OldNames.Contains(componentPropMap[component].Name)) component.OldNames.Add(componentPropMap[component].Name); var propertyConstraint = entity.MappedClass.EnsureHasProperty(componentPropMap[component].Name, component.OldNames) .WithType(componentPropMap[component].DataType.Name.Replace(string.Format("{0}.Model.", globalProjectNamespace), "")) .WithModifiers(componentPropMap[component].Modifiers.ToArray()) .WithGetAccessorBody(componentPropMap[component].GetAccessor.BodyText) .WithSetAccessorBody(componentPropMap[component].SetAccessor.BodyText); if (componentPropMap[component].SetAccessor.Modifier == "private") propertyConstraint.WithSetAccessorAccessibility("private"); propertyConstraint.ApplyTo(actions); } #endregion // Load the existing C# file up var filename = entity.EntitySet.MappingSet.CodeParseResults.GetFilenameForParsedClass(entity.MappedClass); CurrentFileName = filename; string outputText; if (TemplateCache.ContainsKey(filename) || !System.IO.File.Exists(filename)) outputText = actions.RunActions((string)TemplateCache[filename], entity.MappedClass.Controller.Root, false); else outputText = actions.RunActions(System.IO.File.ReadAllText(filename), entity.MappedClass.Controller.Root, true); TemplateCache[filename] = outputText; return outputText; }
private static void ApplyNameChanges(Field existingField, Field exampleField, Actions actions) { if (existingField.Name != exampleField.Name) { // Change name actions.AddAction(new ChangeNameOfFieldAction(existingField, exampleField.Name)); } }
private static void ApplyAccessorChanges(Property existingProperty, Property exampleProperty, Actions actions) { if (existingProperty.GetAccessor == null) { actions.AddAction(new AddAccessorToPropertyAction(existingProperty, exampleProperty.GetAccessor)); } else if (exampleProperty.GetAccessor != null && existingProperty.GetAccessor.Modifier != exampleProperty.GetAccessor.Modifier) { actions.AddAction(new ChangePropertyAccessorModifierAction(existingProperty.GetAccessor, exampleProperty.GetAccessor.Modifier)); } if (existingProperty.SetAccessor == null) { actions.AddAction(new AddAccessorToPropertyAction(existingProperty, exampleProperty.SetAccessor)); } else if (exampleProperty.SetAccessor != null && existingProperty.SetAccessor.Modifier != exampleProperty.SetAccessor.Modifier) { actions.AddAction(new ChangePropertyAccessorModifierAction(existingProperty.SetAccessor, exampleProperty.SetAccessor.Modifier)); } }
public void ApplyTo(Actions actions) { var field = Construct(_class); // Try find the field in the existing class using the old name var existingField = _class.Fields.FirstOrDefault(p => p.Name == _name); if (existingField == null) { for (int i = _oldNames.Count - 1; i >= 0; i--) { // No field found with the new name, so try the old name existingField = _class.Fields.FirstOrDefault(p => p.Name == _oldNames[i]); if (existingField != null) break; } } if (existingField == null) { // As a last ditch effort to find the field, ignore case when searching existingField = _class.Fields.FirstOrDefault(p => p.Name.ToLowerInvariant() == _name.ToLowerInvariant()); } if (existingField != null) { if (_mustRemove) ApplyFieldDeletion(actions, existingField); else { // Make sure the field matches the given property ApplyFieldChanges(actions, existingField, field); } } else if (_mustRemove) { // No existing field found, so we don't need to do anything, it's already removed. // Don't add any actions return; } else { // Add the property to the class. actions.AddAction(new AddFieldToClassAction(field, AdditionPoint.EndOfParent, _class)); } if (!_mustRemove) { var fieldToAddTo = existingField ?? field; foreach (var attrBuilder in attributes) { var attribute = attrBuilder.Build(fieldToAddTo.Controller); if (fieldToAddTo.Attributes.Any(a => a.Name == attribute.Name) || fieldToAddTo.AttributeSections.SelectMany(section => section.Attributes).Any(a => a.Name == attribute.Name)) { // if there are any attributes on the property currently with this name, then we should skip it. continue; } actions.AddAction(new AddAttributeToFieldAction(fieldToAddTo, attribute)); } } }
private static void ApplyModifierChanges(Property existingProperty, Property exampleProperty, Actions actions) { if (existingProperty.Modifiers.UnorderedEqual(exampleProperty.Modifiers) == false) { actions.AddAction(new RemoveModifiersFromPropertyAction(existingProperty)); foreach (var modifier in exampleProperty.Modifiers) { actions.AddAction(new AddModifierToPropertyAction(existingProperty, modifier, false)); } } }