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);
        }
        public static bool TryCreateFile(
            string filePath,
            ISolution solution,
            IProjectFile sourceFile,
            IProjectModelTransactionCookie transaction,
            out IProjectFile file)
        {
            file = null;

            string failReason;

            if (!SolutionFilePathHelpers.TryParseFilePath(filePath, out RelativePath relativeFilePath, out failReason))
            {
                MessageBox.ShowError(failReason);
                return(false);
            }

            if (!SolutionFilePathHelpers.TryGetProject(solution, relativeFilePath, out IProject project, out failReason))
            {
                MessageBox.ShowError(failReason);
                return(false);
            }

            if (!TryGetOrCreateProjectFolder(relativeFilePath, project, transaction, out IProjectFolder projectFolder))
            {
                return(false);
            }

            return(TryEnsureFileExists(relativeFilePath, projectFolder, sourceFile, transaction, out file));
        }
        private static bool TryGetOrCreateProjectFolder(
            RelativePath relativeFilePath,
            IProject project,
            IProjectModelTransactionCookie transaction,
            out IProjectFolder projectFolder)
        {
            RelativePath subFolderRelativePath =
                SolutionFilePathHelpers.GetProjectSubFolderRelativePath(relativeFilePath, project);

            if (!SolutionFilePathHelpers.TryGetProjectFolder(subFolderRelativePath, project, out projectFolder))
            {
                string message =
                    $"There is not folder '{subFolderRelativePath}' in project '{project.Name}'.{Environment.NewLine}" +
                    "Do you want to create it?";

                if (!MessageBox.ShowYesNo(message))
                {
                    return(false);
                }

                FileSystemPath fileSystemPath = subFolderRelativePath.MakeAbsoluteBasedOn(project.Location);
                projectFolder = project.GetOrCreateProjectFolder(fileSystemPath, transaction);
            }

            return(true);
        }
        private static bool TryCreateProjectFolders(
            List <FileGeneratorOutput> notFoundProjectFolderOutputs,
            IProjectModelTransactionCookie transaction)
        {
            if (notFoundProjectFolderOutputs.Count == 0)
            {
                return(true);
            }

            IEnumerable <string> projectFolderPaths = notFoundProjectFolderOutputs
                                                      .Select(x => (project: x.Project, folderPath: x.SubFolderRelativePath))
                                                      .Distinct()
                                                      .Select(x => RelativePath.Parse(x.project.Name).Combine(x.folderPath).ToString());

            string message =
                $"The following folders do not exist:{Environment.NewLine}" +
                string.Join(Environment.NewLine, projectFolderPaths) + Environment.NewLine + Environment.NewLine +
                "Do you want to create them?";


            if (!MessageBox.ShowYesNo(message))
            {
                return(false);
            }

            foreach (FileGeneratorOutput output in notFoundProjectFolderOutputs)
            {
                FileSystemPath fileSystemPath =
                    output.SubFolderRelativePath.MakeAbsoluteBasedOn(output.Project.Location);
                output.ProjectFolder = output.Project.GetOrCreateProjectFolder(fileSystemPath, transaction);
            }

            return(true);
        }
예제 #5
0
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            IProjectFile projectFile = (IProjectFile)this._highlight.OffendingProjectItem;

            using (IProjectModelTransactionCookie cookie =
                       solution.CreateTransactionCookie(DefaultAction.Rollback, this.GetType().Name, progress))
            {
                IProjectFolder newFolder =
                    (IProjectFolder)this._highlight.TargetProject.FindProjectItemByLocation(this._highlight.TargetFolder)
                    ?? this._highlight.TargetProject.GetOrCreateProjectFolder(this._highlight.TargetFolder, cookie);

                MoveToFolderWorkflow workflow     = new MoveToFolderWorkflow(solution, ManualMoveToFolderActionId);
                IProjectFolder       targetFolder = newFolder ?? this._highlight.TargetProject;

                MoveToFolderDataProvider dataProvider =
                    new MoveToFolderDataProvider(true, false, targetFolder, new List <string>(), new List <string>());
                workflow.SetDataProvider(dataProvider);

                Lifetime.Using(
                    lifetime => WorkflowExecuter.ExecuteWithCustomHost(
                        Shell.Instance.GetComponent <IActionManager>()
                        .DataContexts.CreateWithoutDataRules(lifetime
                                                             , DataRules.AddRule(ManualMoveToFolderActionId
                                                                                 , ProjectModelDataConstants.PROJECT_MODEL_ELEMENTS,
                                                                                 new IProjectModelElement[] { projectFile }).AddRule(ManualMoveToFolderActionId
                                                                                                                                     , ProjectModelDataConstants.SOLUTION, solution))
                        , workflow, new SimpleWorkflowHost()));

                cookie.Commit(progress);
            }

            return(null);
        }
        private static bool TryGenerateFiles(
            ISolution solution,
            List <FileGeneratorOutput> fileGeneratorOutputs,
            List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs)
        {
            if (!TryProcessFilePaths(solution, fileGeneratorOutputs, out List <FileGeneratorOutput> notFoundProjectFolderOutputs))
            {
                return(false);
            }

            using (var progressIndicator = NullProgressIndicator.Create())
            {
                IProjectModelTransactionCookie transaction =
                    solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating files", progressIndicator);
                using (transaction)
                {
                    if (!TryCreateProjectFolders(notFoundProjectFolderOutputs, transaction))
                    {
                        return(false);
                    }

                    if (!TryCreateFiles(fileGeneratorOutputs, transaction))
                    {
                        return(false);
                    }

                    MakeDocumentChanges(solution, fileGeneratorOutputs, inPlaceGeneratorOutputs);

                    transaction.Commit(progressIndicator);
                }
            }

            return(true);
        }
예제 #7
0
        protected override Action <ITextControl> ExecuteAfterPsiTransaction(
            ISolution solution,
            IProjectModelTransactionCookie cookie,
            IProgressIndicator progress)
        {
            var generator = GetGenerator().NotNull("Generator is not available");

            return(generator.GenerateAndUpdateDesignerDocument(cookie));
        }
예제 #8
0
        private IProjectFile UpdateProjectModel(
            [NotNull] IT4File file,
            [NotNull] FileSystemPath result,
            [NotNull] IProjectModelTransactionCookie cookie
            )
        {
            Locks.AssertReadAccessAllowed();
            Locks.AssertWriteAccessAllowed();
            var destination = GetOrCreateSameDestinationFile(cookie, file, result);

            return(destination);
        }
예제 #9
0
        private IProjectFile CreateSameDestinationFile(
            [NotNull] IProjectModelTransactionCookie cookie,
            [NotNull] IT4File file,
            [NotNull] string destinationName
            )
        {
            Locks.AssertWriteAccessAllowed();
            var projectFile    = file.PhysicalPsiSourceFile.ToProjectFile().NotNull();
            var folder         = projectFile.ParentFolder.NotNull();
            var targetLocation = folder.Location.Combine(destinationName);
            var parameters     = T4MSBuildProjectUtil.CreateTemplateMetadata(projectFile);

            return(cookie.AddFile(folder, targetLocation, parameters));
        }
예제 #10
0
        private IProjectFile GetOrCreateSameDestinationFile(
            [NotNull] IProjectModelTransactionCookie cookie,
            [NotNull] IT4File file,
            [NotNull] string destinationName
            )
        {
            Locks.AssertWriteAccessAllowed();
            var existingFile = GetSameDestinationFile(file, destinationName);

            if (existingFile != null)
            {
                return(existingFile);
            }
            return(CreateSameDestinationFile(cookie, file, destinationName));
        }
        public static IProjectFile CreateFile(
            string fileName,
            IProjectFolder projectFolder,
            IProjectFile sourceFile,
            IProjectModelTransactionCookie transaction)
        {
            FileSystemPath fileLocation = projectFolder.Location.Combine(fileName);

            IProjectFile file = transaction.AddFile(projectFolder, fileLocation);

            IDocument sourceDocument = sourceFile.GetDocument();
            IDocument document       = file.GetDocument();

            document.Encoding           = sourceDocument.Encoding;
            file.Properties.BuildAction = sourceFile.Properties.BuildAction;

            return(file);
        }
예제 #12
0
        private void RemoveLastGenOutputIfDifferent(
            [NotNull] IT4File file,
            [NotNull] IProjectModelTransactionCookie cookie,
            [NotNull] FileSystemPath destinationLocation
            )
        {
            var projectFile = file.PhysicalPsiSourceFile?.ToProjectFile();

            if (projectFile == null)
            {
                return;
            }
            foreach (var suspect in TemplateMetadataManager
                     .FindLastGenOutput(projectFile)
                     .Where(it => it.Location != destinationLocation)
                     )
            {
                cookie.Remove(suspect);
            }
        }
        protected override bool TryExecute(IInPlaceGenerator generator, GeneratorExecutionHost executionHost)
        {
            IDocument document = generator.DataProvider.Document;

            bool executed = ExecutionHelpers.TryExecuteGenerator(
                generator,
                out string generatedText,
                out TextRange rangeToDelete,
                out int positionToInsert,
                out IReadOnlyCollection <IUsingDirective> missingUsingDirectives
                );

            if (!executed)
            {
                return(false);
            }

            ISolution solution = generator.DataProvider.Solution;

            using (var progressIndicator = NullProgressIndicator.Create())
            {
                IProjectModelTransactionCookie transaction =
                    solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating file", progressIndicator);
                using (transaction)
                {
                    if (!rangeToDelete.IsEmpty)
                    {
                        document.DeleteText(rangeToDelete);
                    }

                    document.InsertText(positionToInsert, generatedText);

                    var treeTextRange = TreeTextRange.FromLength(new TreeOffset(positionToInsert), generatedText.Length);
                    ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, missingUsingDirectives);

                    transaction.Commit(progressIndicator);
                }
            }

            return(true);
        }
        private static bool TryEnsureFileExists(
            RelativePath relativeFilePath,
            IProjectFolder projectFolder,
            IProjectFile sourceFile,
            IProjectModelTransactionCookie transaction,
            out IProjectFile file)
        {
            file = projectFolder.GetSubFiles(relativeFilePath.Name).FirstOrDefault();
            if (file != null)
            {
                var message =
                    $"The file '{relativeFilePath.FullPath}' already exists.{Environment.NewLine}" +
                    "Do you want to overwrite it?";

                return(MessageBox.ShowYesNo(message));
            }

            file = ExecutionHelpers.CreateFile(relativeFilePath.Name, projectFolder, sourceFile, transaction);

            return(true);
        }
예제 #15
0
        // To fix offset issue had to implement ExecuteAfterPsiTransaction.
        // http://stackoverflow.com/questions/22545558/resharper-quickfix-highlighter-offset-issue
        protected override Action <ITextControl> ExecuteAfterPsiTransaction(ISolution solution,
                                                                            IProjectModelTransactionCookie cookie, IProgressIndicator progress)
        {
            DocumentRange docRange = _highlighter.Declaration.GetDocumentRange();
            IDocument     textDoc  = _highlighter.Declaration.GetSourceFile().Document;

            if (textDoc != null)
            {
                for (int i = 0; i < textDoc.GetTextLength(); i++)
                {
                    int    docRangeEndOffSet = docRange.TextRange.EndOffset + i;
                    int    docRangeStart     = docRange.TextRange.StartOffset;
                    string docRangeString    = docRange.Document.GetText(new TextRange(docRange.TextRange.StartOffset, docRangeEndOffSet));

                    int location;
                    if (docRangeString.EndsWith("\r\n"))
                    {
                        var lineEnd = docRangeString.LastIndexOf(";\r\n");
                        if (lineEnd >= 0)
                        {
                            location = docRangeStart + lineEnd + 1;
                            using (solution.CreateTransactionCookie(DefaultAction.Commit, "L10N insterted"))
                            {
                                textDoc.InsertText(location, " // Not L10N");
                            }
                            break;
                        }
                        location = docRangeEndOffSet - 2;
                        using (solution.CreateTransactionCookie(DefaultAction.Commit, "L10N insterted"))
                        {
                            textDoc.InsertText(location, " // Not L10N");
                        }
                        break;
                    }
                }
            }
            return(null);
        }
예제 #16
0
        // To fix offset issue had to implement ExecuteAfterPsiTransaction.
        // http://stackoverflow.com/questions/22545558/resharper-quickfix-highlighter-offset-issue
        protected override Action<ITextControl> ExecuteAfterPsiTransaction(ISolution solution,
            IProjectModelTransactionCookie cookie, IProgressIndicator progress)
        {
            DocumentRange docRange = _highlighter.Declaration.GetDocumentRange();
            IDocument textDoc = _highlighter.Declaration.GetSourceFile().Document;

            if (textDoc != null)
            {
                for (int i = 0; i < textDoc.GetTextLength(); i++)
                {
                    int docRangeEndOffSet = docRange.TextRange.EndOffset + i;
                    int docRangeStart = docRange.TextRange.StartOffset;
                    string docRangeString = docRange.Document.GetText(new TextRange(docRange.TextRange.StartOffset, docRangeEndOffSet));

                    int location;
                    if (docRangeString.EndsWith("\r\n"))
                    {
                        var lineEnd = docRangeString.LastIndexOf(";\r\n");
                        if (lineEnd >= 0)
                        {
                            location = docRangeStart + lineEnd + 1;
                            using (solution.CreateTransactionCookie(DefaultAction.Commit, "L10N insterted"))
                            {
                                textDoc.InsertText(location, " // Not L10N");
                            }
                            break;
                        }
                        location = docRangeEndOffSet - 2;
                        using (solution.CreateTransactionCookie(DefaultAction.Commit, "L10N insterted"))
                        {
                            textDoc.InsertText(location, " // Not L10N");
                        }
                        break;
                    }
                }
            }
            return null;
        }
        private static bool TryCreateFiles(
            List <FileGeneratorOutput> fileGeneratorOutputs,
            IProjectModelTransactionCookie transaction)
        {
            var alreadyExistedFilePaths = new List <string>();

            foreach (FileGeneratorOutput output in fileGeneratorOutputs)
            {
                IProjectFile file = output.ProjectFolder.GetSubFiles(output.FileName).FirstOrDefault();
                if (file == null)
                {
                    file = ExecutionHelpers.CreateFile(
                        output.FileName,
                        output.ProjectFolder,
                        output.SourceProjectFile,
                        transaction
                        );
                }
                else
                {
                    alreadyExistedFilePaths.Add(output.FilePath);
                }

                output.GeneratedProjectFile = file;
            }

            if (alreadyExistedFilePaths.Count > 0)
            {
                string message =
                    $"The following files already exist:{Environment.NewLine}" +
                    string.Join(Environment.NewLine, alreadyExistedFilePaths) + Environment.NewLine + Environment.NewLine +
                    "Do you want to overwrite them?";

                return(MessageBox.ShowYesNo(message));
            }

            return(true);
        }
예제 #18
0
 private IProjectFile GetOrCreateSameDestinationFile(
     [NotNull] IProjectModelTransactionCookie cookie,
     [NotNull] IT4File file,
     [NotNull] FileSystemPath temporary
     ) => GetOrCreateSameDestinationFile(cookie, file, temporary.Name);