Ejemplo n.º 1
0
        private FileSystemPath TryFindTemporaryTargetFile([NotNull] IT4File file)
        {
            string name       = file.GetSourceFile().NotNull().Name.WithOtherExtension(".*");
            var    candidates = GetTemporaryTargetFileFolder(file).GetChildFiles(name);

            return(candidates.FirstOrDefault());
        }
Ejemplo n.º 2
0
        private FileSystemPath GetDestinationLocation([NotNull] IT4File file, [NotNull] string temporaryName)
        {
            Locks.AssertReadAccessAllowed();
            var sourceFile = file.GetSourceFile().NotNull();

            return(sourceFile.ToProjectFile().NotNull().Location.Parent.Combine(temporaryName));
        }
Ejemplo n.º 3
0
        public void SavePreprocessResults(IT4File file, string text)
        {
            Locks.AssertReadAccessAllowed();
            var sourceFile  = file.GetSourceFile().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);
        }
Ejemplo n.º 4
0
        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.GetSourceFile().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);
        }
Ejemplo n.º 5
0
        private string GetPreprocessingTargetFileName([NotNull] IT4File file)
        {
            Locks.AssertReadAccessAllowed();
            var    sourceFile = file.GetSourceFile().NotNull();
            string name       = sourceFile.Name;

            return(name.WithOtherExtension("cs"));
        }
Ejemplo n.º 6
0
        private string GetExpectedTargetFileName([NotNull] IT4File file)
        {
            Locks.AssertReadAccessAllowed();
            var    sourceFile      = file.GetSourceFile().NotNull();
            string name            = sourceFile.Name;
            string targetExtension = file.GetTargetExtension();

            return(name.WithOtherExtension(targetExtension));
        }
Ejemplo n.º 7
0
        public static string GetEncoding([NotNull] IT4File file)
        {
            var sourceFile = file.GetSourceFile();

            if (sourceFile == null)
            {
                return(Encoding.UTF8.CodePage.ToString());
            }
            return(sourceFile.Document.Encoding.CodePage.ToString());
        }
Ejemplo n.º 8
0
        private void Execute([NotNull] IT4File file, [NotNull] ISolution solution)
        {
            Statistics.TrackAction(T4StatisticIdBundle.RunSilently);
            var manager = solution.GetComponent <IT4TemplateExecutionManager>();

            if (manager.IsExecutionRunning(file.GetSourceFile().NotNull()))
            {
                return;
            }
            manager.ExecuteSilently(file);
        }
Ejemplo n.º 9
0
        private IProjectFile CreateSameDestinationFile(
            [NotNull] IProjectModelTransactionCookie cookie,
            [NotNull] IT4File file,
            [NotNull] string destinationName
            )
        {
            Locks.AssertWriteAccessAllowed();
            var projectFile    = file.GetSourceFile().ToProjectFile().NotNull();
            var folder         = projectFile.ParentFolder.NotNull();
            var targetLocation = folder.Location.Combine(destinationName);
            var parameters     = T4MSBuildProjectUtil.CreateTemplateMetadata(projectFile);

            return(cookie.AddFile(folder, targetLocation, parameters));
        }
Ejemplo n.º 10
0
        public FileSystemPath GetTemporaryExecutableLocation(IT4File file)
        {
            var projectFile = file.GetSourceFile().ToProjectFile();
            var project     = projectFile?.GetProject();

            if (project == null)
            {
                return(FileSystemPath.Empty);
            }
            var relativePath = projectFile.Location.MakeRelativeTo(project.Location.Parent);
            var ttLocation   = project.GetIntermediateDirectory(project.GetCurrentTargetFrameworkId())
                               .Combine("TextTemplating")
                               .Combine(relativePath);

            return(ttLocation.Parent.Combine(ttLocation.Name.WithoutExtension()).Combine("GeneratedTransformation.exe"));
        }
Ejemplo n.º 11
0
        private IProjectFile GetSameDestinationFile([NotNull] IT4File file, [NotNull] string temporaryName)
        {
            Locks.AssertWriteAccessAllowed();
            var sourceFile = file.GetSourceFile().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());
        }
Ejemplo n.º 12
0
        private string GetNamespace()
        {
            IPsiSourceFile sourceFile  = _file.GetSourceFile();
            IProjectFile   projectFile = sourceFile?.ToProjectFile();

            if (projectFile == null || !projectFile.IsPreprocessedT4Template())
            {
                return(null);
            }

            string ns = projectFile.GetCustomToolNamespace();

            if (!String.IsNullOrEmpty(ns))
            {
                return(ns);
            }

            return(sourceFile.Properties.GetDefaultNamespace());
        }
Ejemplo n.º 13
0
        private void RemoveLastGenOutputIfDifferent(
            [NotNull] IT4File file,
            [NotNull] IProjectModelTransactionCookie cookie,
            [NotNull] FileSystemPath destinationLocation
            )
        {
            var projectFile = file.GetSourceFile()?.ToProjectFile();

            if (projectFile == null)
            {
                return;
            }
            foreach (var suspect in TemplateMetadataManager
                     .FindLastGenOutput(projectFile)
                     .Where(it => it.Location != destinationLocation)
                     )
            {
                cookie.Remove(suspect);
            }
        }
Ejemplo n.º 14
0
        private void CreateOrUpdateData([NotNull] IT4File t4File)
        {
            IPsiSourceFile sourceFile = t4File.GetSourceFile();

            if (sourceFile == null || !sourceFile.LanguageType.Is <T4ProjectFileType>())
            {
                return;
            }

            var        newData = new T4FileData(t4File, _directiveInfoManager);
            T4FileData existingData;

            lock (_fileDataBySourceFile) {
                _fileDataBySourceFile.TryGetValue(sourceFile, out existingData);
                _fileDataBySourceFile[sourceFile] = newData;
            }

            T4FileDataDiff diff = newData.DiffWith(existingData);

            if (diff != null)
            {
                _fileDataChanged.Fire(Pair.Of(sourceFile, diff));
            }
        }
 protected string GetNamespace() => File.GetSourceFile()?.Properties.GetDefaultNamespace();