private static bool TryGenerateFile(
            ISolution solution,
            IProjectFile sourceFile,
            string filePath,
            string generatedText,
            out IProjectFile generatedFile)
        {
            using (var progressIndicator = NullProgressIndicator.Create())
            {
                IProjectModelTransactionCookie transaction =
                    solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating file", progressIndicator);
                using (transaction)
                {
                    if (!TryCreateFile(filePath, solution, sourceFile, transaction, out generatedFile))
                    {
                        return(false);
                    }

                    IDocument generatedDocument = generatedFile.GetDocument();
                    generatedDocument.ReplaceText(generatedDocument.DocumentRange, generatedText);

                    ExecutionHelpers.OrginizeUsingsAndFormatFile(solution, generatedDocument);

                    transaction.Commit(progressIndicator);
                }
            }

            return(true);
        }
        private static void MakeDocumentChanges(
            ISolution solution,
            List <FileGeneratorOutput> fileGeneratorOutputs,
            List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs)
        {
            foreach (FileGeneratorOutput output in fileGeneratorOutputs)
            {
                IDocument document = output.GeneratedProjectFile.GetDocument();
                document.ReplaceText(document.DocumentRange, output.GeneratedText);
                ExecutionHelpers.OrginizeUsingsAndFormatFile(solution, document);
            }

            foreach (InPlaceGeneratorOutput output in inPlaceGeneratorOutputs)
            {
                IDocument document = output.ProjectFile.GetDocument();
                if (!output.RangeToDelete.IsEmpty)
                {
                    document.DeleteText(output.RangeToDelete);
                }

                document.InsertText(output.PositionToInsert, output.GeneratedText);

                var treeTextRange = TreeTextRange.FromLength(new TreeOffset(output.PositionToInsert), output.GeneratedText.Length);
                ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, output.MissingUsingDirectives);
            }
        }