Exemplo n.º 1
0
        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);
            }))));
        }
Exemplo n.º 2
0
        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);
            }))));
        }
Exemplo n.º 3
0
        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);
                    }
                }
            }
        }