protected override string GenerateError(Microsoft.VisualStudio.Shell.Interop.IVsGeneratorProgress pGenerateProgress, Exception ex)
        {
            if (ex is SpecFlowParserException)
            {
                SpecFlowParserException sfpex = (SpecFlowParserException)ex;
                if (sfpex.ErrorDetails == null || sfpex.ErrorDetails.Count == 0)
                {
                    return(base.GenerateError(pGenerateProgress, ex));
                }

                foreach (var errorDetail in sfpex.ErrorDetails)
                {
                    pGenerateProgress.GeneratorError(0, 4, errorDetail.Message,
                                                     (uint)errorDetail.ForcedLine - 1,
                                                     (uint)errorDetail.ForcedColumn - 1);
                }

                return(GetMessage(ex));
            }

            return(base.GenerateError(pGenerateProgress, ex));
        }
Exemplo n.º 2
0
        // The docs say you can't rely on the 'inputFilePath' value, but there's *no other way* to know the base file name or the project!
        public int Generate(
            string inputFilePath,
            string inputFileContents,
            string defaultNamespace,
            IntPtr[] outputFileContents,
            out uint outputByteCount,
            IVsGeneratorProgress progress)
        {
            outputByteCount = 0;
            List <string>             generatedFiles = new List <string>();
            List <EnvDTE.ProjectItem> generatedItems = new List <EnvDTE.ProjectItem>();

            this.InputFilePath     = inputFilePath;
            this.InputFileContents = inputFileContents;
            this.DefaultNamespace  = defaultNamespace;

            this.GetProject();

            foreach (var iter in this.GetIterations())
            {
                try
                {
                    // Get the file for this iteration.
                    // TODO: correct for full/relative paths?
                    string file = this.GetFileName(iter);

                    // Keep track of generated files, we may need to add them to the project.
                    generatedFiles.Add(file);

                    // Create the file...
                    using (FileStream stream = File.Create(file))
                    {
                        byte[] content = this.GenerateContent(iter);

                        stream.Write(content, 0, content.Length);
                        ////stream.Close();
                    }

                    // Ensure the new item is a child of the input file...
                    EnvDTE.ProjectItem childItem = this.ProjectItem.ProjectItems.AddFromFile(file);
                    generatedItems.Add(childItem);
                    this.AdjustChildItem(iter, childItem);
                }
                catch (Exception ex)
                {
                    if (ErrorHandler.IsCriticalException(ex))
                    {
                        throw;
                    }
                }
            }

            // Delete any child items that aren't ours...
            foreach (EnvDTE.ProjectItem childItem in this.ProjectItem.ProjectItems)
            {
                if (!childItem.Name.EndsWith(this.GetDefaultExtension()) &&
                    !generatedItems.Contains(childItem))
                {
                    childItem.Delete();
                }
            }

            // Finally, generate the summary content...
            byte[] summaryContent = this.GenerateSummaryContent();

            if (summaryContent == null || summaryContent.Length == 0)
            {
                outputFileContents[0] = IntPtr.Zero;
                outputByteCount       = 0;
            }
            else
            {
                IntPtr mem = Marshal.AllocCoTaskMem(summaryContent.Length);
                Marshal.Copy(summaryContent, 0, mem, summaryContent.Length);

                outputFileContents[0] = mem;
                outputByteCount       = (uint)summaryContent.Length;
            }

            return(VSConstants.S_OK);
        }