예제 #1
0
        private async Task <(TaskReturnKind, CreateProjectGroup?)> SelectProjectTask(Solution solution)
        {
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Select project:");
            Project?project = await TaskExecutable.RunTask(CommonTaskFunctions.GetProjectTask, solution);

            if (project == null)
            {
                return(TaskReturnKind.Continue, null);
            }

            await foreach (TagProjectGroup? projectGroup in TaskExecutable.RunTaskAsyncEnumerable(this.SelectTemplateFileTask, project))
            {
                if (projectGroup == null)
                {
                    continue;
                }

                Console.WriteLine(Environment.NewLine);
                TagOption?tagOption = await TaskExecutable.RunTask(this.SelectTagOption);

                if (!tagOption.HasValue)
                {
                    return(TaskReturnKind.Exit, null);
                }

                IEnumerable <DocumentGroup> createDocuments = await this.GetDocumentsToCreate(tagOption.Value, projectGroup.Tags);

                List <Document> documents = new List <Document>();
                foreach (DocumentGroup createDocument in createDocuments)
                {
                    foreach (TagDocumentGroup documentGroup in projectGroup.Documents)
                    {
                        TagDocumentGroup newDocumentGroup = CommonTaskFunctions.ReplaceDocumentTags(documentGroup, createDocument.ReplacedTags);
                        Document?        newDocument      = CommonTaskFunctions.AppendDocumentToProject(project, newDocumentGroup);
                        if (newDocument != null)
                        {
                            project = newDocument.Project;
                            documents.Add(newDocument);
                        }
                    }
                }

                return(TaskReturnKind.Continue, new CreateProjectGroup(project, documents));
            }

            return(TaskReturnKind.Exit, null);
        }
        public async Task RunTask()
        {
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine(this.Name);
            Console.WriteLine(Environment.NewLine);

            TagTemplateGroup?template = await TaskExecutable.RunTask(this.LoadTemplate);

            if (template == null)
            {
                return;
            }

            Console.WriteLine("Input solution path.");
            Console.WriteLine(Environment.NewLine);
            string solutionPath = Console.ReadLine();

            if (!File.Exists(solutionPath))
            {
                Console.WriteLine("File does not exists!");
                return;
            }

            using MSBuildWorkspace workspace = await new MSBuildWorkspaceFactory().GetWorkspace();
            Solution solution = await workspace.OpenSolutionAsync(solutionPath);

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Select project:");
            Project?project = await TaskExecutable.RunTask(CommonTaskFunctions.GetProjectTask, solution);

            if (project == null)
            {
                return;
            }

            var selectedDocuments = new List <Document>();

            await foreach (Document? document in TaskExecutable.RunTaskAsyncEnumerable(CommonTaskFunctions.SelectDocuments, project.Documents.Where(whereDocument => !selectedDocuments.Contains(whereDocument))))
            {
                if (document != null)
                {
                    selectedDocuments.Add(document);
                }
            }

            TagProjectGroup projectGroup = await CommonTaskFunctions.ImplementsTags(selectedDocuments, template.TagElements);

            List <Document> documents = new List <Document>();

            foreach (DocumentGroup createDocument in template.Documents)
            {
                foreach (TagDocumentGroup documentGroup in projectGroup.Documents)
                {
                    TagDocumentGroup newDocumentGroup = CommonTaskFunctions.ReplaceDocumentTags(documentGroup, createDocument.ReplacedTags);
                    Document?        newDocument      = CommonTaskFunctions.AppendDocumentToProject(project, newDocumentGroup);
                    if (newDocument != null)
                    {
                        project = newDocument.Project;
                        documents.Add(newDocument);
                    }
                }
            }

            workspace.TryApplyChanges(project.Solution);
            workspace.CloseSolution();
        }