public void SavePreprocessResults(IT4File file, string text)
        {
            Locks.AssertReadAccessAllowed();
            var sourceFile  = file.PhysicalPsiSourceFile.NotNull();
            var projectFile = sourceFile.ToProjectFile().NotNull();

            Locks.AssertWriteAccessAllowed();
            FileSystemPath destinationLocation = null;
            IProjectFile   destination         = null;

            Solution.InvokeUnderTransaction(cookie =>
            {
                string destinationName = GetPreprocessingTargetFileName(file);
                destination            = GetOrCreateSameDestinationFile(cookie, file, destinationName);
                destinationLocation    = destination.Location;
                RemoveLastGenOutputIfDifferent(file, cookie, destinationLocation);
                TemplateMetadataManager.UpdateTemplateMetadata(
                    cookie,
                    projectFile,
                    T4TemplateKind.Preprocessed,
                    destinationLocation
                    );
                TemplateMetadataManager.UpdateGeneratedFileMetadata(cookie, destination, projectFile);
            });
            destinationLocation.WriteAllText(text);
            OutputFileRefresher.Refresh(destination);
        }
        public void TryProcessExecutionResults(IT4File file)
        {
            Locks.AssertReadAccessAllowed();
            Locks.AssertWriteAccessAllowed();
            var temporary = TryFindTemporaryTargetFile(file);

            if (temporary == null)
            {
                return;
            }
            var destinationLocation = GetDestinationLocation(file, temporary.Name);

            destinationLocation.DeleteFile();
            File.Move(temporary.FullPath, destinationLocation.FullPath);
            var          sourceFile  = file.PhysicalPsiSourceFile.NotNull();
            var          projectFile = sourceFile.ToProjectFile().NotNull();
            IProjectFile destination = null;

            Solution.InvokeUnderTransaction(cookie =>
            {
                destination = UpdateProjectModel(file, destinationLocation, cookie);
                RemoveLastGenOutputIfDifferent(file, cookie, destinationLocation);
                TemplateMetadataManager.UpdateTemplateMetadata(
                    cookie,
                    projectFile,
                    T4TemplateKind.Executable,
                    destinationLocation
                    );
                TemplateMetadataManager.UpdateGeneratedFileMetadata(cookie, destination, projectFile);
            });
            OutputFileRefresher.Refresh(destination);
        }
        private IProjectFile UpdateProjectModel(
            [NotNull] IT4File file,
            [NotNull] FileSystemPath result,
            [NotNull] IProjectModelTransactionCookie cookie
            )
        {
            Locks.AssertReadAccessAllowed();
            Locks.AssertWriteAccessAllowed();
            var destination = GetOrCreateSameDestinationFile(cookie, file, result);

            return(destination);
        }
        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));
        }
        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));
        }
        private IProjectFile GetSameDestinationFile([NotNull] IT4File file, [NotNull] string temporaryName)
        {
            Locks.AssertWriteAccessAllowed();
            var sourceFile = file.PhysicalPsiSourceFile.NotNull();
            var candidates = sourceFile
                             .ToProjectFile()
                             ?.ParentFolder
                             ?.GetSubItems(temporaryName)
                             .ToList()
                             .OfType <IProjectFile>()
                             .AsList();

            Assertion.AssertNotNull(candidates, "candidates != null");
            Assertion.Assert(candidates.Count <= 1, "candidates.Value.Length <= 1");
            return(candidates.SingleOrDefault());
        }