Exemplo n.º 1
0
 /// <summary>Создаёт экземпляр параметров кодогенерации</summary>
 /// <param name="Item">Объект кодогенерации</param>
 /// <param name="Template">Шаблон кодогенерации</param>
 /// <param name="TemplatesDictionary">Дополнительные шаблоны кодогенерации</param>
 /// <param name="ParentItem">Родительский элемент</param>
 public GenerationArguments(GenerationItem Item, string Template, IDictionary <string, string> TemplatesDictionary, GenerationItem ParentItem = null)
 {
     this.ParentItem          = ParentItem;
     this.Item                = Item;
     this.Template            = Template;
     this.TemplatesDictionary = TemplatesDictionary;
 }
Exemplo n.º 2
0
        private static void CreateUnsavedFile(IUnitTestGeneratorPackage package, GenerationItem generationItem)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var tempFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(generationItem.TargetFileName));

            try
            {
                File.WriteAllText(tempFile, Strings.DisconnectedFileHeader + generationItem.TargetContent);
                var dte = (DTE2)package.GetService(typeof(DTE));
                if (dte != null)
                {
                    var window = dte.ItemOperations.OpenFile(tempFile, Constants.vsViewKindCode);
                    window.Document.Saved = false;
                }
            }
            finally
            {
                try
                {
                    File.Delete(tempFile);
                }
                catch (IOException)
                {
                }
            }
        }
Exemplo n.º 3
0
        protected override void OnChildCreated(GenerationItem item)
        {
            if (!(item is Statement))
            {
                return;
            }

            Statement statement = (Statement)item;

            statements.Add(statement);
        }
Exemplo n.º 4
0
        public void Pop(GenerationItem item)
        {
            if (stackCount == 0)
            {
                throw new Exception("Trying to Pop when the stack is empty. This probably means Push and Pop are out of sync.");
            }
            if (currentStackItem != item)
            {
                throw new Exception("Trying to Pop with a invalid value. This probably means Push and Pop are out of sync.");
            }

            stack.RemoveAt(stackCount-1);
        }
Exemplo n.º 5
0
        private static async Task <GenerationResult> GenerateAsync(bool withRegeneration, IUnitTestGeneratorOptions options, Solution solution, GenerationItem generationItem, SemanticModel semanticModel)
        {
            GenerationResult result;

            if (File.Exists(generationItem.TargetFileName))
            {
                var documentIds = solution.GetDocumentIdsWithFilePath(generationItem.TargetFileName);
                if (documentIds.FirstOrDefault() is DocumentId targetDocumentId)
                {
                    var targetDocument = solution.GetDocument(targetDocumentId);

                    var targetSemanticModel = await targetDocument.GetSemanticModelAsync().ConfigureAwait(true);

                    result = await CoreGenerator.Generate(semanticModel, generationItem.SourceSymbol, targetSemanticModel, withRegeneration, options, generationItem.NamespaceTransform).ConfigureAwait(true);
                }
                else
                {
                    result = await CoreGenerator.Generate(semanticModel, generationItem.SourceSymbol, null, withRegeneration, options, generationItem.NamespaceTransform).ConfigureAwait(true);
                }
            }
            else
            {
                result = await CoreGenerator.Generate(semanticModel, generationItem.SourceSymbol, null, withRegeneration, options, generationItem.NamespaceTransform).ConfigureAwait(true);
            }

            return(result);
        }
Exemplo n.º 6
0
        private static async Task GenerateItemAsync(bool withRegeneration, IUnitTestGeneratorOptions options, Solution solution, GenerationItem generationItem)
        {
            var targetProject = solution.Projects.FirstOrDefault(x => string.Equals(x.Name, generationItem.Source.SourceProjectName, StringComparison.Ordinal));
            var documents     = solution.GetDocumentIdsWithFilePath(generationItem.Source.FilePath);
            var documentId    = documents.FirstOrDefault(x => x.ProjectId == targetProject?.Id) ?? documents.FirstOrDefault();

            if (documentId == null)
            {
                throw new InvalidOperationException("Could not find document in solution with file path '" + generationItem.Source.FilePath + "'");
            }

            var document = solution.GetDocument(documentId);

            var semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(true);

            var tree = await semanticModel.SyntaxTree.GetRootAsync().ConfigureAwait(true);

            if (!tree.DescendantNodes().OfType <ClassDeclarationSyntax>().Any() && !tree.DescendantNodes().OfType <StructDeclarationSyntax>().Any())
            {
                return;
            }

            var result = await GenerateAsync(withRegeneration, options, solution, generationItem, semanticModel).ConfigureAwait(true);

            generationItem.TargetContent = result.FileContent;

            foreach (var asset in result.RequiredAssets)
            {
                generationItem.RequiredAssets.Add(asset);
            }

            foreach (var reference in result.AssemblyReferences)
            {
                if (!generationItem.AssemblyReferences.Any(x => string.Equals(x.Name, reference.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    generationItem.AssemblyReferences.Add(reference);
                }
            }
        }
Exemplo n.º 7
0
        private static void AddTargetItem(IReadOnlyCollection <GenerationItem> generationItems, IUnitTestGeneratorOptions options, GenerationItem generationItem)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            if (TargetFinder.FindExistingTargetItem(generationItem.Source, options.GenerationOptions, out var targetItem) != FindTargetStatus.Found)
            {
                File.WriteAllText(generationItem.TargetFileName, generationItem.TargetContent);
                targetItem = generationItem.TargetProjectItems.AddFromFile(generationItem.TargetFileName);
            }
            else
            {
                if (targetItem.Document is null)
                {
                    File.WriteAllText(generationItem.TargetFileName, generationItem.TargetContent);
                }
                else
                {
                    var textSelection = (TextSelection)targetItem.Document.Selection;
                    textSelection.SelectAll();
                    textSelection.Insert(generationItem.TargetContent);
                    targetItem.Document.Save();
                }
            }

            if (generationItems.Count == 1)
            {
                VsProjectHelper.ActivateItem(targetItem);
            }
        }
Exemplo n.º 8
0
 public void Push(GenerationItem item)
 {
     stack.Add(item);
 }
Exemplo n.º 9
0
 private GenerationArguments GetSubarguments(GenerationArguments Arguments, GenerationItem SubItem, string TemplateName)
 {
     return(new GenerationArguments(SubItem, Arguments.TemplatesDictionary[TemplateName], Arguments.TemplatesDictionary, Arguments.Item));
 }