public ComputedPropertyInfo(NamingConventions conventions, TypeInfo owner, string name, string type, bool cached, IEnumerable<string> dependsOn, string formula) : base(conventions, owner, name, type, false, false) { Cached = cached; DependsOn.AddRange(dependsOn); Formula = (formula == "" ? null : formula); Getter = new MethodInfo(GetGetterName(), TypeName); Setter = null; LazyInitializer = null; validator = null; if (Cached) { FieldName = FieldName + "Cache"; ValidFieldName = FieldName + "Valid"; Cacher = new MethodInfo(GetCacherName(), TypeName); Invalidate = new MethodInfo(GetInvalidateName()); } else { FieldName = null; ValidFieldName = null; Cacher = Getter; Invalidate = null; } }
public CollectionInfo(NamingConventions conventions, TypeInfo owner, string name, string contents, bool lazy, bool readOnly) : base(conventions, owner, name, "ObservableList<" + contents + ">", false, lazy) { Contract.Requires(!string.IsNullOrEmpty(contents)); ContentsType = new BaseFieldInfo(conventions, "Items", contents); Contents = contents; ReadOnly = false; // !lazy; <= Needed for serializationa ExposeAsReadOnly = readOnly; Setter = null; WithSetter = null; validator = null; Getter.TypeName = ExposedTypeName; }
private string CreateFile(TypeInfo type, string templateName, string className, bool markAsGenerated) { var relativeName = Path.Combine(GetPackageDir(type), className + (markAsGenerated ? ".generated" : "") + ".cs"); var fullname = Path.Combine(BaseOutputPath, relativeName); Directory.CreateDirectory(Path.Combine(BaseOutputPath, GetPackageDir(type))); TemplateWrapper template = new TemplateWrapper(templateName); template.StartSession(); template.SetAttribute("it", type); template.SetAttribute("class", className); File.WriteAllText(fullname, template.Render()); // FormatFileWithNArranger(fullname); FormatFileWithAStyle(fullname); log.Info("Created file " + relativeName); return relativeName; }
private string CreateFileIfNotExits(TypeInfo type, string templateName, string className, bool markAsGenerated) { var relativeName = Path.Combine(GetPackageDir(type), className + ".cs"); var fullname = Path.Combine(BaseOutputPath, relativeName); if (new FileInfo(fullname).Exists) { if (!overrideFiles && !IsMarkedToOverride(fullname)) { log.Info("Skipped (because already exists and was edited) file " + relativeName); return null; } log.Info("Overriding file " + relativeName); } return CreateFile(type, templateName, className, markAsGenerated); }
private string GetPackageDir(TypeInfo type) { var pkg = type.Package; if (string.IsNullOrWhiteSpace(pkg) || pkg == GlobalConfig.ProjectNamespace) return ""; if (!string.IsNullOrWhiteSpace(GlobalConfig.ProjectNamespace) && pkg.StartsWith(GlobalConfig.ProjectNamespace + '.')) pkg = pkg.Substring(GlobalConfig.ProjectNamespace.Length + 1); return pkg.Replace('.', '\\'); }
private string CreateDebugDisplay(TypeInfo type) { StringBuilder dd = new StringBuilder(); dd.Append("DebuggerDisplay(\"").Append(type.Name).Append("["); int count = 0; foreach (var prop in type.NonComputedProperties) { if (prop.IsComponent || prop.IsComputed) continue; if (count > 0) dd.Append(" "); dd.Append(prop.Name).Append("="); if (prop.IsCollection) dd.Append("{").Append(prop.Name).Append(".Count}items"); else dd.Append("{").Append(prop.Name).Append("}"); count++; } dd.Append("]\")"); return dd.ToString(); }
private bool HasParent(ModelInfo model, TypeInfo type, Func<TypeInfo, bool> filter = null) { // the max value is just to avoid an infinite loop for (int i = 0; i < 10000; i++) { if (type.Extends == null) return false; var baseType = model.GetType(type.Extends); if (baseType == null) return false; if (filter == null || filter(baseType)) return true; type = baseType; } throw new InvalidOperationException("Type hierarchy is too deep. Didn't you made a recursion?"); }
private void AddUsingsFromKnownTypes(TypeInfo type, string text) { if (text == null) return; if (text.StartsWith("List<") || text.StartsWith("HashSet<") || text.StartsWith("Dictionary<")) type.Using.Add("System.Collections.Generic"); else if (text.StartsWith("List") || text.StartsWith("HashSet") || text.StartsWith("Dictionary")) type.Using.Add("System.Collections"); }
private ModelInfo CreateModel(xml.model model) { NamingConventions conventions = new NamingConventions(); ModelInfo result = new ModelInfo(); if (!string.IsNullOrWhiteSpace(model.projectNamespace)) GlobalConfig.ProjectNamespace = model.projectNamespace; foreach (var modelItem in model.Items) { if (modelItem is config) { var config = (config) modelItem; if (config.serialization != null && !string.IsNullOrWhiteSpace(config.serialization.@namespace)) GlobalConfig.SerializationNamespace = [email protected](); } else if (modelItem is type) { var type = (type) modelItem; var ti = new TypeInfo(type.name, model.@namespace, type.immutable, type.cloneable, type.serializable, type.equals); if (type.deepCopySpecified) ti.DeepCopy = type.deepCopy; if (!string.IsNullOrWhiteSpace(type.doc)) ti.Documentation = type.doc; if (!string.IsNullOrWhiteSpace(type.implements)) { var impls = type.implements.Split(','); foreach (var impl in impls) { var tmp = impl.Trim(); if (!string.IsNullOrWhiteSpace(tmp)) ti.Implements.Add(tmp); } } if (type.extends != null && type.extends.Trim() != "") ti.Extends = type.extends.Trim(); if (type.baseClass != null) { var bc = type.baseClass; if (bc.hasChildPropertyChangedSpecified) ti.BaseClass.HasChildPropertyChanged = bc.hasChildPropertyChanged; if (bc.hasPropertyChangedSpecified) ti.BaseClass.HasPropertyChanged = bc.hasPropertyChanged; if (bc.hasChildPropertyChangingSpecified) ti.BaseClass.HasChildPropertyChanging = bc.hasChildPropertyChanging; if (bc.hasPropertyChangingSpecified) ti.BaseClass.HasPropertyChanging = bc.hasPropertyChanging; if (bc.hasCopyFromSpecified) ti.BaseClass.HasCopyFrom = bc.hasCopyFrom; if (bc.hasPropertiesSpecified) ti.BaseClass.HasProperties = bc.hasProperties; } foreach (var item in type.Items) { if (item is property) { var property = (property) item; var prop = new PropertyInfo(conventions, ti, property.name, property.type, property.required, false); if (property.deepCopySpecified) prop.DeepCopy = property.deepCopy; if (!string.IsNullOrWhiteSpace(property.doc)) prop.Documentation = property.doc; if (!string.IsNullOrWhiteSpace(property.@default)) prop.DefaultValue = property.@default; if (!string.IsNullOrWhiteSpace(property.getter) && ValidateVisibility(property.getter, "getter")) prop.GetterVisibility = property.getter; if (!string.IsNullOrWhiteSpace(property.setter) && ValidateVisibility(property.setter, "setter")) prop.SetterVisibility = property.setter; if (property.receiveInConstructorSpecified && property.receiveInConstructor) prop.ReceiveInConstructor = property.receiveInConstructor; if (property.precisionSpecified) prop.Precision = (double) property.precision; if (prop.Required && !prop.IsPrimitive) prop.Validations.Add(new ValidationInfo("value == null", property.requiredException ?? "new ArgumentNullException(property)", "#pragma warning disable 472\n// ReSharper disable ConditionIsAlwaysTrueOrFalse", "// ReSharper restore ConditionIsAlwaysTrueOrFalse\n#pragma warning restore 472")); prop.AddValidationAttrib(property.validationAttrib, property.validationException); prop.AddValidation(property.validation1, property.validationException); if (property.validation != null) { foreach (var validation in property.validation) { prop.AddValidationAttrib(validation.attrib, validation.exception); prop.AddValidation(validation.test, validation.exception); } } ti.Properties.Add(prop); } else if (item is component) { var component = (component) item; var comp = new ComponentInfo(conventions, ti, component.name, component.type, component.lazy); if (!string.IsNullOrWhiteSpace(component.doc)) comp.Documentation = component.doc; if (!string.IsNullOrWhiteSpace(component.@default)) comp.DefaultValue = component.@default; if (component.receiveInConstructorSpecified && component.receiveInConstructor) comp.ReceiveInConstructor = component.receiveInConstructor; comp.AddValidationAttrib(component.validationAttrib, component.validationException); comp.AddValidation(component.validation1, component.validationException); if (component.validation != null) { foreach (var validation in component.validation) { comp.AddValidationAttrib(validation.attrib, validation.exception); comp.AddValidation(validation.test, validation.exception); } } ti.Properties.Add(comp); } else if (item is collection) { var collection = (collection) item; var col = new CollectionInfo(conventions, ti, collection.name, collection.type, collection.lazy, collection.readOnly); if (collection.deepCopySpecified) col.DeepCopy = collection.deepCopy; if (!string.IsNullOrWhiteSpace(collection.doc)) col.Documentation = collection.doc; if (!string.IsNullOrWhiteSpace(collection.@default)) col.DefaultValue = collection.@default; ti.Properties.Add(col); } else if (item is computedproperty) { var computed = (computedproperty) item; var deps = (from d in computed.dependsOn.Split(',') where d.Trim() != "" select StringUtils.FirstUpper(d.Trim())); var prop = new ComputedPropertyInfo(conventions, ti, computed.name, computed.type, computed.cached, deps, computed.formula); if (!string.IsNullOrWhiteSpace(computed.getter) && ValidateVisibility(computed.getter, "getter")) prop.GetterVisibility = computed.getter; if (!string.IsNullOrWhiteSpace(computed.doc)) prop.Documentation = computed.doc; ti.Properties.Add(prop); } else if (item is @using) { var us = item as @using; ti.Using.Add(us.@namespace); } } result.AddType(ti); } else if (modelItem is @using) { var us = modelItem as @using; result.Using.Add(us.@namespace); } } return result; }
public PropertyInfo(NamingConventions conventions, TypeInfo owner, string name, string type, bool required, bool lazy) : base(conventions, name, type) { Owner = owner; Required = required; Lazy = lazy; string getter = GetGetterName(); if (getter != null) Getter = new MethodInfo(getter, TypeName); string setter = GetSetterName(); if (setter != null) Setter = new MethodInfo(setter, "bool", TypeName); string withSetter = GetWithSetterName(); if (withSetter != null) WithSetter = new MethodInfo(withSetter, owner.Name, TypeName); string lazyIntializer = GetLazyInitializerName(); if (lazyIntializer != null) LazyInitializer = new MethodInfo(lazyIntializer); string validatorName = GetValidatorName(); if (validatorName != null) validator = new MethodInfo(validatorName, "void", TypeName); }
public void AddType(TypeInfo type) { _types.Add(type); }