protected override CommandResult ExecuteDetailed(GetProgramsCommandArgs args, ChangeObservable observable) { ExecutionContext.WriteWarning("This command is deprecated. Use 'get project-information' instead.", false); TemplateEntity project = TemplateEntity.Decorate(entityFactory.Create(Guid.NewGuid().ToByteString(), args).Root); IEnumerable <Entity> programs = project.EntityHierarchy.Where(e => e.Type.Equals("program", StringComparison.OrdinalIgnoreCase)) .ToArray(); if (!string.IsNullOrEmpty(args.Component)) { Entity component = project.EntityHierarchy.FirstOrDefault(e => e.HasName && e.Name == args.Component && e.Type.Equals("component", StringComparison.OrdinalIgnoreCase)); if (component != null) { programs = programs.Where(p => TemplateEntity.Decorate(p).RelatedEntites.Contains(component)); } else { ExecutionContext.WriteError($"A component with name {args.Component} does not exist in project {project.Name}."); return(new CommandResult(-1, ProgramsCommandResult.Empty)); } } if (!programs.Any()) { ExecutionContext.WriteInformation($"No programs found."); return(new CommandResult(0, ProgramsCommandResult.Empty)); } return(new CommandResult(0, new ProgramsCommandResult(programs.Select(p => { CodeEntity codeEntity = CodeEntity.Decorate(p); TemplateEntity templateEntity = TemplateEntity.Decorate(p); Entity componentEntity = templateEntity.RelatedEntites.FirstOrDefault(e => e.Type == "component"); if (componentEntity == null) { throw new InvalidOperationException($"Program {p.Name} has no related component. This should not be possible."); } CodeEntity componentCodeEntity = CodeEntity.Decorate(componentEntity); return new ProgramResult(p.Name, codeEntity.Namespace, componentEntity.Name, componentCodeEntity.Namespace); })))); }
protected override CommandResult ExecuteDetailed(GetComponentsCommandArgs args, ChangeObservable observable) { ExecutionContext.WriteWarning("This command is deprecated. Use 'get project-information' instead.", false); TemplateEntity project = TemplateEntity.Decorate(entityFactory.Create(Guid.NewGuid().ToByteString(), args).Root); IEnumerable <Entity> components = project.EntityHierarchy.Where(e => e.Type.Equals("component", StringComparison.OrdinalIgnoreCase)) .ToArray(); if (!components.Any()) { ExecutionContext.WriteInformation($"No components found for project {project.Name}"); return(new CommandResult(0, ComponentsCommandResult.Empty)); } return(new CommandResult(0, new ComponentsCommandResult(components.Select(c => { CodeEntity codeEntity = CodeEntity.Decorate(c); return new ComponentResult(c.Name, string.IsNullOrEmpty(codeEntity.Namespace) ? string.Empty : codeEntity.Namespace); })))); }
public void Initialize(Entity root) { ProjectEntity project = ProjectEntity.Decorate(root); TemplateEntity template = TemplateEntity.Decorate(root); Target[] availableTargets = sdkRepository.GetAllTargets().ToArray(); ICodeModel codeModel = root.Value <ICodeModel>(); SetProjectName(); SetProjectNamespace(); SetProjectType(); SetProjectTargets(); SetProjectEntities(); SetProjectIncludes(); void SetProjectName() { ProjectName = null; if (fileSystem.FileExists(System.IO.Path.Combine(root.Path, Constants.ProjectFileName))) { ProjectName = root.Name; } } void SetProjectNamespace() { ProjectNamespace = CodeEntity.Decorate(project).Namespace; } void SetProjectType() { ProjectType = project.Type; } void SetProjectTargets() { TargetsResult targetsResult = targetParser.Targets(project, false); ProjectTargets = targetsResult.ValidTargets .Select(t => new ProjectTarget(t, availableTargets.Any(at => t.Name == at.Name && at.LongVersion == t.LongVersion))); Exceptions = targetsResult.Errors; } void SetProjectEntities() { IEnumerable <CodeEntity> entities = template.EntityHierarchy.Select(e => { CodeEntity codeEntity = CodeEntity.Decorate(e); return(codeEntity); } ); ProjectCodeEntities = entities.Select(e => { TemplateEntity te = TemplateEntity.Decorate(e); return(e, te.RelatedEntites.Where(en => !en.Type.Contains("project"))); }) .Where(e => !e.Item1.Type.Contains("project")).ToDictionary(p => p.Item1, p => p.Item2); } void SetProjectIncludes() { IncludePaths = new List <IncludePath>(); IEnumerable <SdkInformation> relevantSdks = ProjectTargets.Select(t => availableTargets.FirstOrDefault(at => t.Target.Name == at.Name && at.LongVersion == t.Target.LongVersion)) .Where(t => t != null) .Select(sdkRepository.GetSdk) .Where(sdk => sdk != null) .Distinct(); var targetsWithIncludePaths = relevantSdks.Select(sdk => (sdk.Targets, sdk.IncludePaths.Concat(sdk.CompilerInformation.IncludePaths).Distinct())); foreach (var item in targetsWithIncludePaths) { foreach (Target target in item.Targets) { foreach (string includePath in item.Item2) { IncludePath existingIncludePath = IncludePaths.Where(i => i.PathValue.Equals(includePath, StringComparison.InvariantCulture)).FirstOrDefault(); if (existingIncludePath == null) { existingIncludePath = new IncludePath(includePath, true, new List <Target>()); IncludePaths.Add(existingIncludePath); } existingIncludePath.Targets = existingIncludePath.Targets.Concat(new[] { target }); } } } foreach (IncludePath codeModelIncludeDirectory in codeModel.IncludeDirectories) { IncludePath existingIncludePath = IncludePaths.Where(p => p.PathValue.Equals(codeModelIncludeDirectory.PathValue, StringComparison.InvariantCulture)).FirstOrDefault(); if (existingIncludePath == null) { IncludePaths.Add(codeModelIncludeDirectory); } else { foreach (Target target in codeModelIncludeDirectory.Targets) { if (!existingIncludePath.Targets.Contains(target)) { existingIncludePath.Targets = existingIncludePath.Targets.Concat(new[] { target }); } } } } } }
public void Initialize(Entity root) { ProjectEntity project = ProjectEntity.Decorate(root); TemplateEntity template = TemplateEntity.Decorate(root); Target[] availableTargets = sdkRepository.GetAllTargets().ToArray(); ICodeModel codeModel = root.Value <ICodeModel>(); SetProjectName(); SetProjectNamespace(); SetProjectType(); SetProjectTargets(); SetProjectEntities(); SetProjectIncludes(); void SetProjectName() { ProjectName = null; if (fileSystem.FileExists(System.IO.Path.Combine(root.Path, Constants.ProjectFileName))) { ProjectName = root.Name; } } void SetProjectNamespace() { ProjectNamespace = CodeEntity.Decorate(project).Namespace; } void SetProjectType() { ProjectType = project.Type; } void SetProjectTargets() { TargetsResult targetsResult = targetParser.Targets(project, false); ProjectTargets = targetsResult.ValidTargets .Select(t => new ProjectTarget(t, availableTargets.Any(at => t.Name == at.Name && at.LongVersion == t.LongVersion))); Exceptions = targetsResult.Errors; } void SetProjectEntities() { IEnumerable <CodeEntity> entities = template.EntityHierarchy.Select(e => { CodeEntity codeEntity = CodeEntity.Decorate(e); return(codeEntity); } ); ProjectCodeEntities = entities.Select(e => { TemplateEntity te = TemplateEntity.Decorate(e); return(e, te.RelatedEntites.Where(en => !en.Type.Contains("project"))); }) .Where(e => !e.Item1.Type.Contains("project")).ToDictionary(p => p.Item1, p => p.Item2); } void SetProjectIncludes() { IEnumerable <SdkInformation> relevantSdks = ProjectTargets.Select(t => availableTargets.FirstOrDefault(at => t.Target.Name == at.Name && at.LongVersion == t.Target.LongVersion)) .Where(t => t != null) .Select(sdkRepository.GetSdk) .Where(sdk => sdk != null) .Distinct(); IncludePaths = relevantSdks.SelectMany(sdk => sdk.IncludePaths) .Concat(relevantSdks.SelectMany(sdk => sdk.CompilerInformation.IncludePaths)) .Distinct() .ToDictionary(x => x, x => true); foreach (KeyValuePair <string, VirtualDirectory> codeModelIncludeDirectory in codeModel.IncludeDirectories) { if (!IncludePaths.ContainsKey(codeModelIncludeDirectory.Key)) { IncludePaths.Add(codeModelIncludeDirectory.Key, codeModelIncludeDirectory.Value != null); } } } }
//TODO Patch libmeta here public int Execute(Entity dataModel) { ProjectEntity project = ProjectEntity.Decorate(dataModel.Root); CodeEntity projectCodeEntity = CodeEntity.Decorate(project); string projectName = projectCodeEntity.Namespace; IEnumerable <Entity> targets = project.Targets.ToArray(); if (!targets.Any()) { throw new FormattableException("Please use --target to specify for which targets the library shall be generated."); } Dictionary <Entity, VirtualFile> projectLibraries = targets.ToDictionary(t => t, FindLibrary); List <string> externalLibraries = new List <string>(); List <VirtualFile> deletableFiles = new List <VirtualFile>(); foreach (Entity target in targets) { CopyExternalLibrariesToOutputDirectory(target, projectLibraries, out IEnumerable <string> libraries, out IEnumerable <VirtualFile> toBeDeleted); externalLibraries.AddRange(libraries); deletableFiles.AddRange(toBeDeleted); } CheckMetaFiles(targets.First()); string commandOptionsFile = GenerateCommandOptions(project, projectLibraries, projectName); int result = ExecuteLibraryBuilderWithCommandOptions(commandOptionsFile, project); foreach (VirtualFile deletable in deletableFiles) { deletable.Delete(); } return(result); VirtualFile FindLibrary(Entity target) { VirtualDirectory deployDirectory = DeployEntity.Decorate(target).DeployDirectory; string libFile = deployDirectory.Files("*.so", true) .OrderByDescending(f => f.Name.Equals($"lib{projectName}.so", StringComparison.OrdinalIgnoreCase)) .ThenByDescending(f => f.LastWriteTime) .FirstOrDefault() ?.FullName; if (string.IsNullOrEmpty(libFile)) { throw new LibraryNotFoundException(deployDirectory.FullName); } VirtualFile file = fileSystem.GetFile(libFile); return(file); } void CheckMetaFiles(Entity target) { TemplateEntity projectTemplateEntity = TemplateEntity.Decorate(project); VirtualDirectory deployDirectory = DeployEntity.Decorate(target).DeployDirectory; if (!fileSystem.FileExists(Path.Combine(deployDirectory.FullName, $"{projectName}.libmeta"))) { throw new MetaLibraryNotFoundException(deployDirectory.FullName); } PatchLibmeta(fileSystem.GetFile(Path.Combine(deployDirectory.FullName, $"{projectName}.libmeta"))); IEnumerable <VirtualFile> metaFiles = deployDirectory.Files(searchRecursive: true); IEnumerable <Entity> componentsWithoutMetaFile = projectTemplateEntity.EntityHierarchy .Where(e => e.Type.Equals("component", StringComparison.OrdinalIgnoreCase)) .Where(c => !metaFiles.Any(f => f.Name.Equals($"{c.Name}.{Constants.CompmetaExtension}", StringComparison.Ordinal))) .ToArray(); if (componentsWithoutMetaFile.Any()) { throw new MetaFileNotFoundException(deployDirectory.FullName, $"{componentsWithoutMetaFile.First().Name}.{Constants.CompmetaExtension}"); } IEnumerable <Entity> programsWithoutMetaFile = projectTemplateEntity.EntityHierarchy .Where(e => e.Type.Equals("program", StringComparison.OrdinalIgnoreCase)) .Where(p => !metaFiles.Any(f => f.Name.Equals($"{p.Name}.{Constants.ProgmetaExtension}", StringComparison.Ordinal))) .ToArray(); if (programsWithoutMetaFile.Any()) { throw new MetaFileNotFoundException(deployDirectory.FullName, $"{programsWithoutMetaFile.First().Name}.{Constants.ProgmetaExtension}"); } } void PatchLibmeta(VirtualFile libmetaFile) { if (!externalLibraries.Any()) { return; } XmlSerializer serializer = new XmlSerializer(typeof(MetaConfigurationDocument)); MetaConfigurationDocument document; try { using (Stream fileStream = libmetaFile.OpenRead()) using (XmlReader reader = XmlReader.Create(fileStream)) { document = (MetaConfigurationDocument)serializer.Deserialize(reader); } } catch (XmlException e) { executionContext.WriteWarning($"Cannot parse libmeta file. Cannot patch dependencies. {e.Message}"); return; } document.schemaVersion = "4.0"; LibraryDefinition definition = (LibraryDefinition)document.Item; IEnumerable <string> dependencies = externalLibraries.Select(Path.GetFileName) .Concat(definition.Dependencies?.Select(d => d.path) ?? Enumerable.Empty <string>()); definition.Dependencies = dependencies.Select(d => new DependencyDefinition { path = d }).ToArray(); using (Stream fileStream = libmetaFile.OpenWrite()) using (XmlWriter writer = XmlWriter.Create(fileStream, new XmlWriterSettings { Indent = true })) { serializer.Serialize(writer, document); } } }
public override Entity Resolve(Entity owner, string key, bool fallback = false) { if (key == EntityKeys.IncludeKey && owner.Type == EntityKeys.FormatKey) { return(ResolveInclude()); } throw new ContentProviderException(key, owner); Entity ResolveInclude() { IEntityBase templateOrigin = GetTemplateOrigin(out IEntityBase formatOrigin); CommandEntity commandEntity = CommandEntity.Decorate(owner.Origin); string basePath = string.Empty; if (templateOrigin.HasContent(EntityKeys.BaseDirectoryKey) && !string.IsNullOrEmpty(templateOrigin[EntityKeys.BaseDirectoryKey].Value <string>())) { string path = templateOrigin.Path; string relative = path.Split(new[] { templateOrigin[EntityKeys.BaseDirectoryKey].Value <string>() }, StringSplitOptions.None) .Last(); Match match = splitRegex.Match(relative); if (match.Success && match.Groups["split"].Success) { relative = match.Groups["split"].Value; } basePath = relative.Replace('\\', '/'); } else if (commandEntity.IsCommandArgumentSpecified(EntityKeys.OutputKey)) { string output = commandEntity.Output; string rootPath = templateOrigin.Root.Path; string outputDirectory = fileSystem.GetDirectory(output, rootPath, false).FullName; if (outputDirectory.StartsWith(rootPath, StringComparison.Ordinal) && outputDirectory != rootPath) { string partialPath = outputDirectory.Substring(rootPath.Length + 1).CleanPath(); Match match = relativeDirectoryRegex.Match(partialPath); if (match.Success && match.Groups["split"].Success) { basePath = match.Groups["split"].Value; } } } string result = resolver.Resolve(formatOrigin.Name, templateOrigin); if (!string.IsNullOrEmpty(basePath)) { if (!basePath.EndsWith("/", StringComparison.Ordinal)) { basePath += "/"; } result = basePath + result; } return(owner.Create(key, result)); IEntityBase GetTemplateOrigin(out IEntityBase realOrigin) { realOrigin = TemplateEntity.Decorate(owner).FormatOrigin; IEntityBase templateParent = realOrigin; while (templateParent != null && templateParent.Type != EntityKeys.TemplateKey) { templateParent = templateParent.Owner; } return(templateParent?.Owner ?? realOrigin); } } }
public override Entity Resolve(Entity owner, string key, bool fallback = false) { TemplateEntity ownerTemplateEntity = TemplateEntity.Decorate(owner); if (key == EntityKeys.FieldArpDataTypeKey && owner.Type == EntityKeys.FormatKey) { return(ResolveArpDataType()); } if (key == EntityKeys.TypeMetaDataFormatKey && owner.Type == EntityKeys.FormatKey) { return(ResolveTypeMetaDataFormat()); } if (key == EntityKeys.ExpandHiddenTypesFormatKey) { return(ExpandHiddenTypes()); } if (key == EntityKeys.FilterHiddenTypesFormatKey) { return(FilterHiddenTypes()); } if (key == EntityKeys.IsFieldKey) { return(owner.Create(key, CodeEntity.Decorate(owner).AsField != null)); } if (key == EntityKeys.IsTypeKey) { return(owner.Create(key, CodeEntity.Decorate(owner).AsType != null)); } if (key == EntityKeys.BigDataProjectKey) { return(GetBigDataProjectEntity()); } if (key == EntityKeys.NormalProjectKey) { return(GetNormalProjectEntity()); } ISymbol symbol = owner.Value <ISymbol>(); if (symbol != null) { Entity result = owner.PropertyValueEntity(key, symbol); if (result != null) { return(result); } throw new ContentProviderException(key, owner); } if (key == EntityKeys.BaseDirectoryKey && HasBaseDirectory(owner, out string baseDirectory)) { return(owner.Create(key, baseDirectory)); } IType type = owner.Value <IType>(); if (type != null) { return(ResolveType()); } IField field = owner.Value <IField>(); if (field != null) { return(ResolveField()); } IDataType dataType = owner.Value <IDataType>(); if (dataType != null) { return(ResolveDataType()); } ICodeModel codeModel = owner.Value <ICodeModel>(); if (codeModel != null) { return(ResolveCodeModel()); } throw new ContentProviderException(key, owner); IEnumerable <CodeEntity> GetPortEnums() { return(GetAllPorts().Concat(GetPortStructures().SelectMany(p => p.Fields)) .Select(f => f.ResolvedType) .Where(t => t.AsEnum != null) .Distinct()); } IEnumerable <CodeEntity> GetAllPorts() { bool IsPort(CodeEntity fieldEntity) { return(fieldEntity.AsField != null && fieldEntity.AsField.HasAttributeWithoutValue(EntityKeys.PortAttributeKey)); } return(TemplateEntity.Decorate(owner).EntityHierarchy .Select(CodeEntity.Decorate) .SelectMany(c => c.Fields) .Where(IsPort)); } IEnumerable <CodeEntity> GetPortStructures() { HashSet <CodeEntity> structures = new HashSet <CodeEntity>(GetAllPorts() .Select(f => f.ResolvedType) .Where(t => t.AsType != null && t.AsEnum == null), new FullNameCodeEntityComparer()); HashSet <CodeEntity> visited = new HashSet <CodeEntity>(new FullNameCodeEntityComparer()); while (structures.Except(visited).Any()) { foreach (CodeEntity structure in structures.Except(visited).ToArray()) { foreach (CodeEntity structureField in structure.Fields) { CodeEntity structureDataType = structureField.ResolvedType; if (structureDataType.AsType != null && structureDataType.AsEnum == null) { structures.Add(structureDataType); } } visited.Add(structure); } } return(structures); } Entity FilterHiddenTypes() { IEnumerable <IType> types = owner.Select(CodeEntity.Decorate) .Where(c => !c.IsHidden()) .Select(c => c.AsType) .ToArray(); string name = owner.FirstOrDefault()?.Name ?? string.Empty; return(owner.Create(key, types.Select(t => owner.Create(name, t.FullName, t)))); } Entity ExpandHiddenTypes() { IEnumerable <CodeEntity> dataSourceFields = owner.Select(CodeEntity.Decorate); dataSourceFields = ExpandHiddenStructureFields(dataSourceFields); string name = owner.FirstOrDefault()?.Name ?? string.Empty; return(owner.Create(key, dataSourceFields.Select(e => e.AsField) .Select(f => owner.Create(name, f.Name, f)))); IEnumerable <CodeEntity> ExpandHiddenStructureFields(IEnumerable <CodeEntity> fields) { foreach (CodeEntity hiddenField in fields) { CodeEntity hiddenType = hiddenField.ResolvedType; if (hiddenType == null || !hiddenType.IsHidden()) { yield return(hiddenField); continue; } foreach (CodeEntity hiddenTypeField in ExpandHiddenStructureFields(hiddenType.Fields)) { yield return(hiddenTypeField); } } } } T ChooseMetaDataTemplate <T>(IEnumerable <(T template, string name, string context)> templates) { return(templates.OrderByDescending(t => ContextComplexity(t.context ?? string.Empty)) .FirstOrDefault(Matches) .template); bool Matches((T template, string name, string context) template) { if (!key.Equals(template.name, StringComparison.OrdinalIgnoreCase)) { return(false); } if (string.IsNullOrEmpty(template.context)) { return(true); } string[] contextPath = template.context.Split(new [] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries) .Reverse().ToArray(); Entity current = owner; foreach (string part in contextPath) { current = current?.Owner; if (current?[$"is{part}"].Value <bool>() != true) { return(false); } } return(true); } int ContextComplexity(string context) { return(context.Count(c => c == '/' || c == '\\')); } } Entity ResolveType() { switch (key) { case EntityKeys.PathKey: { return(owner.Create(key, type.GetFile(owner).Parent.FullName)); } case EntityKeys.FieldsKey: { return(owner.Create(key, type.Fields.Select(f => owner.Create(key.Singular(), f.Name, f)))); } case EntityKeys.FileKey: { VirtualFile file = owner.Root.Value <ICodeModel>() .Types[type]; return(owner.Create(key, file.Name, file)); } case EntityKeys.BaseTypeKey: { IDataType baseType = type.BaseTypes.FirstOrDefault(); return(owner.Create(key, baseType)); } default: { if (TryResolveTypeTemplate(out Entity result)) { return(result); } if (TryResolveProperty(out result)) { return(result); } if (TryResolveFields(out result)) { return(result); } if (TryResolveRelationship(out result)) { return(result); } throw new ContentProviderException(key, owner); } } bool TryResolveTypeTemplate(out Entity result) { Templates.Type.metaDataTemplate typeMetaDataTemplate = ChooseMetaDataTemplate(templateRepository.TypeTemplates .Select(t => (t, t.name, t.context))); if (typeMetaDataTemplate != null) { result = GetTypeTemplateEntity(typeMetaDataTemplate); return(true); } result = null; return(false); Entity GetTypeTemplateEntity(Templates.Type.metaDataTemplate metaDataTemplate) { if (!metaDataTemplate.hasvalue) { bool hasAttribute = type.HasAttributeWithoutValue(metaDataTemplate.name); return(owner.Create(key, new Func <string>(hasAttribute.ToString), hasAttribute)); } (string[] values, CodePosition position) = GetTypeTemplateValue(); IEnumerable <string> formattedValues = VerifyValues().ToArray(); return(metaDataTemplate.multiplicity == Templates.Type.multiplicity.OneOrMore ? owner.Create(key, formattedValues.Select(v => owner.Create(key.Singular(), v))) : owner.Create(key, formattedValues.First())); IEnumerable <string> VerifyValues() { return(values.Select(VerifyValue)); string VerifyValue(string arg) { (bool success, string message, string newValue) = metaDataTemplate.ValueRestriction.Verify(arg); if (!success) { owner.AddCodeException(new FieldAttributeRestrictionException(metaDataTemplate.name, arg, message, position, type.GetFile(owner))); } return(newValue); } } (string[], CodePosition) GetTypeTemplateValue() { IAttribute attribute = type.Attributes.LastOrDefault(a => a.Name.Equals(metaDataTemplate.name, StringComparison.OrdinalIgnoreCase)); string value = attribute?.Values.FirstOrDefault() ?? resolver.Resolve(metaDataTemplate.defaultvalue, owner); return(metaDataTemplate.multiplicity == Templates.Type.multiplicity.OneOrMore ? value.Split(new[] { metaDataTemplate.split }, StringSplitOptions.RemoveEmptyEntries) : new[] { value }, attribute?.Position ?? new CodePosition(0, 0)); } } } bool TryResolveProperty(out Entity result) { result = owner.PropertyValueEntity(key, type); return(result != null); } bool TryResolveFields(out Entity result) { metaDataTemplate metaDataTemplate = templateRepository.FieldTemplates .FirstOrDefault(t => t.name.Plural().Equals(key, StringComparison .OrdinalIgnoreCase)); if (metaDataTemplate != null) { IEnumerable <IField> fields = type.Fields .Where(f => f.Attributes .Any(a => a.Name .Equals(metaDataTemplate.name, StringComparison.OrdinalIgnoreCase))); result = owner.Create(key, fields.Select(f => owner.Create(metaDataTemplate.name, f.Name, f))); return(true); } result = null; return(false); } bool TryResolveRelationship(out Entity result) { templateRelationship[] relationships = owner.Template().Relationship; templateRelationship relationship = relationships?.FirstOrDefault(r => r.name.Equals(key, StringComparison.OrdinalIgnoreCase)) ?? relationships?.FirstOrDefault(r => r.name.Equals(key.Singular(), StringComparison.OrdinalIgnoreCase)); TemplateDescription relationshipDescription = relationship != null?templateRepository.Template(relationship.type) : null; if (relationshipDescription != null) { IEnumerable <string> names = type.Attributes .Where(a => a.Name.Equals(relationship.name, StringComparison.OrdinalIgnoreCase)) .SelectMany(a => a.Values); result = relationship.GetRelationship(relationshipDescription, owner, names.ToArray()); return(true); } result = null; return(false); } } Entity ResolveField() { switch (key) { case EntityKeys.FieldNameKey: { return(owner.Create(key, field.Name)); } case EntityKeys.DataTypeKey: { IDataType fieldDataType = GetRealFieldDataType(); return(owner.Create(key, fieldDataType.Name, fieldDataType)); } case EntityKeys.ResolvedTypeKey: { ICodeModel model = owner.Root.Value <ICodeModel>(); IDataType fieldDataType = field.DataType; IType realType = fieldDataType.PotentialFullNames .Select(model.Type) .FirstOrDefault(t => t != null); return(owner.Create(key, realType?.Name, realType)); } case EntityKeys.MultiplicityKey: { return(owner.Create(key, field.Multiplicity.Select(m => owner.Create(key, new Func <string>(m.ToString), m)))); } default: { metaDataTemplate metaDataTemplate = templateRepository.FieldTemplates .FirstOrDefault(t => t.name.Equals(key, StringComparison.OrdinalIgnoreCase)); if (metaDataTemplate != null) { return(GetFieldTemplateEntity(metaDataTemplate)); } throw new ContentProviderException(key, owner); } } IDataType GetRealFieldDataType() { ICodeModel rootCodeModel = owner.Root.Value <ICodeModel>(); IEnum @enum = field.DataType.PotentialFullNames.Select(n => rootCodeModel.GetEnum(n)).FirstOrDefault(e => e != null); if (@enum != null) { (IDataType enumBaseType, _) = GetEnumBaseType(@enum); return(enumBaseType); } return(field.DataType); } Entity GetFieldTemplateEntity(metaDataTemplate metaDataTemplate) { if (!metaDataTemplate.hasvalue) { bool hasAttribute = field.HasAttributeWithoutValue(metaDataTemplate.name); return(owner.Create(key, new Func <string>(hasAttribute.ToString), hasAttribute)); } (string[] values, CodePosition position) = GetFieldTemplateValue(); IEnumerable <string> formattedValues = VerifyValues().ToArray(); return(metaDataTemplate.multiplicity == multiplicity.OneOrMore ? owner.Create(key, formattedValues.Select(v => owner.Create(key.Singular(), v))) : owner.Create(key, formattedValues.First())); IEnumerable <string> VerifyValues() { return(values.Select(VerifyValue)); string VerifyValue(string arg) { (bool success, string message, string newValue) = metaDataTemplate.ValueRestriction.Verify(arg); if (!success) { owner.AddCodeException(new FieldAttributeRestrictionException(metaDataTemplate.name, arg, message, position, field.GetFile(owner))); } return(newValue); } } (string[], CodePosition) GetFieldTemplateValue() { IAttribute attribute = field.Attributes.LastOrDefault(a => a.Name.Equals(metaDataTemplate.name, StringComparison.OrdinalIgnoreCase)); string value = string.IsNullOrEmpty(attribute?.Values.FirstOrDefault()) ? resolver.Resolve(metaDataTemplate.defaultvalue, owner) : attribute.Values.First(); return(metaDataTemplate.multiplicity == multiplicity.OneOrMore ? value.Split(new[] { metaDataTemplate.split }, StringSplitOptions.RemoveEmptyEntries) : new[] { value }, attribute?.Position ?? new CodePosition(0, 0)); } } } Entity ResolveCodeModel() { switch (key) { case EntityKeys.PortStructsKey: { return(owner.Create(key, GetPortStructures().Select(c => c.Base))); } case EntityKeys.PortEnumsKey: { return(owner.Create(key, GetPortEnums().Select(c => c.Base))); } case EntityKeys.NamespaceKey: { bool prior206Target = CheckProjectTargets(); IEnumerable <string> relevantTypes = TemplateEntity.Decorate(owner).EntityHierarchy .Select(CodeEntity.Decorate) .Where(c => !c.IsRoot()) .Select(c => c.FullName); string ns = prior206Target ? codeModel.RootNamespaceForOldTarget( relevantTypes.ToArray(), GetPortStructures().Concat(GetPortEnums()) .Select(c => c.FullName) .ToArray()) : codeModel.RootNamespace(relevantTypes.ToArray()); if (string.IsNullOrEmpty(ns)) { ns = owner.Name; } return(owner.Create(key, ns)); } default: throw new ContentProviderException(key, owner); } bool CheckProjectTargets() { IEnumerable <TargetEntity> targets = ProjectEntity.Decorate(owner).Targets.Select(t => TargetEntity.Decorate(t)); return(targets.Any(t => t.Version < new Version(20, 6))); } } Entity ResolveDataType() { switch (key) { case EntityKeys.NameKey: return(owner.Create(key, dataType.Name)); case EntityKeys.FullNameKey: string fullName = GetDataTypeFullName(); return(owner.Create(key, fullName)); default: throw new ContentProviderException(key, owner); } string GetDataTypeFullName() { ICodeModel rootCodeModel = owner.Root.Value <ICodeModel>(); return(dataType.PotentialFullNames.FirstOrDefault(fn => rootCodeModel.Type(fn) != null) ?? dataType.Name); } } Entity ResolveTypeMetaDataFormat() { IEntityBase dataSource = ownerTemplateEntity.FormatOrigin; IDataType dataSourceDataType = dataSource.HasValue <IDataType>() ? dataSource.Value <IDataType>() : dataSource.Value <IField>().DataType; ICodeModel rootCodeModel = dataSource.Root.Value <ICodeModel>(); string dataTypeName = string.Empty; if (dataSourceDataType != null) { dataTypeName = dataSourceDataType.PotentialFullNames .Select(n => rootCodeModel.Type(n)) .FirstOrDefault(t => t != null) ?.Name ?? datatypeConversion.Convert(dataSourceDataType); } return(owner.Create(key, dataTypeName)); } Entity ResolveArpDataType() { IEntityBase dataSource = ownerTemplateEntity.FormatOrigin; IField dataSourceField = dataSource.Value <IField>(); if (dataSourceField == null) { throw new FormatTargetMismatchException("arpDataType", "field|port", dataSource.Type); } ICodeModel rootCodeModel = dataSource.Root.Value <ICodeModel>(); bool isStruct = dataSourceField.DataType.PotentialFullNames.Any(n => rootCodeModel.GetClass(n) != null || rootCodeModel.GetStructure(n) != null); IEnum @enum = dataSourceField.DataType.PotentialFullNames.Select(n => rootCodeModel.GetEnum(n)).FirstOrDefault(e => e != null); bool isArray = dataSourceField.Multiplicity.Any(); string arpName = "DataType::" + GetArpDataType(dataSourceField.DataType.Name); return(owner.Create(key, arpName)); string GetArpDataType(string dataTypeName) { string postfix = isArray ? " | DataType::Array" : string.Empty; if (isStruct) { return("Struct" + postfix); } if (@enum != null) { (IDataType enumBaseType, string formattedBaseType) = GetEnumBaseType(@enum); return($"Enum | DataType::{formattedBaseType}" + postfix); } (bool success, string value) = FormatDataType(dataTypeName); if (!success) { throw new UnknownDataTypeException(value); } return(value + postfix); } } (bool success, string value) FormatDataType(string unformattedValue) { if (unformattedValue.Contains('<')) { unformattedValue = unformattedValue.Substring(0, unformattedValue.IndexOf('<')); } string result = owner.Create("temporaryCtnFormatContainer", unformattedValue).Format()["knownDataTypes"].Value <string>(); Match unkownMatch = UnkownDataTypeRegex.Match(result); if (unkownMatch.Success) { return(false, unkownMatch.Groups["dataType"].Value); } return(true, result); } (IDataType, string) GetEnumBaseType(IEnum @enum) { IDataType enumDataType = @enum.BaseTypes.FirstOrDefault(); string enumBaseType = enumDataType?.Name; if (string.IsNullOrEmpty(enumBaseType)) { throw new MissingEnumDataTypeException(@enum); } (bool formatted, string formattedBaseType) = FormatDataType(enumBaseType); if (!formatted) { throw new UnkownEnumDataTypeException(formattedBaseType, @enum); } return(enumDataType, formattedBaseType); } Entity GetBigDataProjectEntity() { if (HasMoreThan1000Fields()) { return(owner.Create(key, "true", true)); } return(owner.Create(key, "false", false)); } Entity GetNormalProjectEntity() { if (HasMoreThan1000Fields()) { return(owner.Create(key, "false", false)); } return(owner.Create(key, "true", true)); } bool HasMoreThan1000Fields() { ICodeModel model = owner.Value <ICodeModel>(); if (model == null || GetAllPorts().Concat(GetPortStructures().SelectMany(s => s.Fields)).Count() <= 1000) { return(false); } return(true); } }