Example #1
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));
        }
        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());
        }
Example #3
0
        private FileSystemPath ResolveBaseReference(
            [NotNull] IT4File file,
            [NotNull] string assemblyName
            )
        {
            var resolved = AssemblyReferenceResolver.Resolve(assemblyName, file.LogicalPsiSourceFile);

            if (resolved != null)
            {
                return(resolved);
            }
            var    node    = FindSuitableNodeForErrorReporting(file);
            string message = $"Could not find standard assembly: {assemblyName}";

            throw new T4OutputGenerationException(T4FailureRawData.FromElement(node, message));
        }
        public FileSystemPath GetTemporaryExecutableLocation(IT4File file)
        {
            var projectFile = file.PhysicalPsiSourceFile.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"));
        }
        public void Debug(IT4File file)
        {
            Logger.Verbose("Trying to debug a file");
            lock (ExecutionLocker)
            {
                if (IsExecutionRunning(file))
                {
                    ShowNotification();
                    return;
                }

                RememberExecution(file, false);
            }

            Model.RequestDebug.Start(new T4ExecutionRequest(GetT4FileLocation(file), true));
        }
        public void ExecuteSilently(IT4File file)
        {
            Logger.Verbose("Trying to execute a file silently");
            lock (ExecutionLocker)
            {
                if (IsExecutionRunning(file))
                {
                    ShowNotification();
                    return;
                }

                RememberExecution(file, true);
            }

            Model.RequestExecution.Start(new T4ExecutionRequest(GetT4FileLocation(file), false));
        }
        public IEnumerable <IProject> GetProjectDependencies(IT4File file)
        {
            file.AssertContainsNoIncludeContext();
            var sourceFile     = file.LogicalPsiSourceFile.NotNull();
            var projectFile    = sourceFile.ToProjectFile().NotNull();
            var psiModule      = sourceFile.PsiModule;
            var resolveContext = projectFile.SelectResolveContext();

            using (CompilationContextCookie.GetOrCreate(resolveContext))
            {
                return(PsiModules
                       .GetModuleReferences(psiModule)
                       .Select(it => it.Module)
                       .OfType <IProjectPsiModule>()
                       .Select(it => it.Project)
                       .AsList());
            }
        }
 public T4CSharpCodeBehindIntermediateConverter(
     [NotNull] IT4File file,
     [NotNull] IT4GeneratedClassNameProvider classNameProvider,
     bool isRoot = true
     ) : base(file, classNameProvider)
 {
     IsRoot = isRoot;
     if (IsRoot)
     {
         TransformTextMethodName = DefaultTransformTextMethodName;
         TransformTextAttributes = "";
     }
     else
     {
         TransformTextMethodName = DefaultTransformTextMethodName + Guid.NewGuid().ToString("N");
         TransformTextAttributes = "[__ReSharperSynthetic]";
     }
 }
Example #9
0
        private static Pair <IT4File, IFile> GetFiles([NotNull] IPsiSourceFile sourceFile, DocumentRange documentRange)
        {
            IT4File primaryFile   = null;
            IFile   secondaryFile = null;

            foreach ((IFile file, _) in sourceFile.EnumerateIntersectingPsiFiles(documentRange))
            {
                if (file is IT4File t4File)
                {
                    primaryFile = t4File;
                }
                else
                {
                    secondaryFile = file;
                }
            }

            return(Pair.Of(primaryFile, secondaryFile));
        }
Example #10
0
        protected override T4IncludeData Build(IT4File file)
        {
            if (file.PhysicalPsiSourceFile?.IsBeingIndirectlyUpdated() == true)
            {
                // Since the contents of this file did not change,
                // the list of its direct includes did not change either,
                // so there's no point in doing anything here
                return(null);
            }

            return(new T4IncludeData(file
                                     .GetThisAndChildrenOfType <IT4IncludeDirective>()
                                     .Where(directive => directive.IsVisibleInDocument())
                                     .Select(directive => IncludeResolver.ResolvePath(directive.ResolvedPath))
                                     .Where(path => !path.IsEmpty)
                                     .Distinct()
                                     .ToList()
                                     ));
        }
        private static Pair <IT4File, IFile> GetFiles([NotNull] IPsiSourceFile sourceFile, DocumentRange documentRange)
        {
            IT4File primaryFile   = null;
            IFile   secondaryFile = null;

            foreach (Pair <IFile, TreeTextRange> pair in sourceFile.EnumerateIntersectingPsiFiles(documentRange))
            {
                var t4File = pair.First as IT4File;
                if (t4File != null)
                {
                    primaryFile = t4File;
                }
                else
                {
                    secondaryFile = pair.First;
                }
            }

            return(Pair.Of(primaryFile, secondaryFile));
        }
Example #12
0
        protected void DoInvalidateAssemblies([NotNull] IT4File t4File)
        {
            var sourceFile = t4File.PhysicalPsiSourceFile;

            if (sourceFile?.LanguageType.Is <T4ProjectFileType>() != true)
            {
                return;
            }
            var newData = new T4DeclaredAssembliesInfo(t4File);
            var existingDeclaredAssembliesInfo = sourceFile.GetDeclaredAssembliesInfo();

            sourceFile.SetDeclaredAssembliesInfo(newData);
            var diff = newData.DiffWith(existingDeclaredAssembliesInfo);

            if (diff == null)
            {
                return;
            }
            FileDataChanged.Fire(Pair.Of(sourceFile, diff));
        }
        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);
            }
        }
Example #14
0
        private static Pair <IT4File, IFile> GetFiles([NotNull] IPsiSourceFile sourceFile, DocumentRange documentRange)
        {
            IT4File primaryFile   = null;
            IFile   secondaryFile = null;

            foreach (IFile file in sourceFile.EnumeratePsiFiles(documentRange))
            {
                var t4File = file as IT4File;
                if (t4File != null)
                {
                    primaryFile = t4File;
                }
                else
                {
                    secondaryFile = file;
                }
            }

            return(Pair.Of(primaryFile, secondaryFile));
        }
Example #15
0
        private void AddDirective([NotNull] IT4File t4File, [NotNull] IT4Directive directive)
        {
            Pair <IT4Directive, BeforeOrAfter> anchor = directive.FindAnchor(t4File.GetDirectives().ToArray(), _directiveInfoManager);

            if (anchor.First != null)
            {
                if (anchor.Second == BeforeOrAfter.Before)
                {
                    t4File.AddDirectiveBefore(directive, anchor.First);
                }
                else
                {
                    t4File.AddDirectiveAfter(directive, anchor.First);
                }
            }
            else
            {
                t4File.AddDirective(directive);
            }
        }
        public T4CSharpCodeGenerationIntermediateResult Collect([NotNull] IT4File file)
        {
            var projectFile = file.PhysicalPsiSourceFile.ToProjectFile();

            if (projectFile == null)
            {
                return(new T4CSharpCodeGenerationIntermediateResult(file, Interrupter));
            }
            Results.Push(new T4CSharpCodeGenerationIntermediateResult(file, Interrupter));
            Guard.StartProcessing(file.LogicalPsiSourceFile.GetLocation());
            file.ProcessDescendants(this);
            string suffix = Result.State.ProduceBeforeEof();

            if (!string.IsNullOrEmpty(suffix))
            {
                AppendTransformation(suffix, Result.State.FirstNode);
            }
            Guard.EndProcessing();
            return(Results.Pop());
        }
        public ISelectedRange GetSelectedRange(IPsiSourceFile sourceFile, DocumentRange documentRange)
        {
            Pair <IT4File, IFile> pair = GetFiles(sourceFile, documentRange);
            IT4File t4File             = pair.First;
            IFile   codeBehindFile     = pair.Second;

            if (t4File == null)
            {
                return(null);
            }

            ITreeNode t4Node = t4File.FindNodeAt(documentRange);

            if (t4Node == null)
            {
                return(null);
            }

            // if the current selection is inside C# code, use the C# extend selection directly
            if (codeBehindFile != null)
            {
                ISelectEmbracingConstructProvider codeBehindProvider = PsiShared.GetComponent <PsiProjectFileTypeCoordinator>()
                                                                       .GetByPrimaryPsiLanguageType(codeBehindFile.Language)
                                                                       .SelectNotNull(fileType => Shell.Instance.GetComponent <IProjectFileTypeServices>().TryGetService <ISelectEmbracingConstructProvider>(fileType))
                                                                       .FirstOrDefault();

                if (codeBehindProvider != null)
                {
                    ISelectedRange codeBehindRange = codeBehindProvider.GetSelectedRange(sourceFile, documentRange);
                    if (codeBehindRange != null)
                    {
                        return(new T4CodeBehindWrappedSelection(t4File, codeBehindRange));
                    }
                }
            }

            return(new T4NodeSelection(t4File, t4Node));
        }
        public static string GetTargetExtension([NotNull] this IT4File file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            var    extension       = T4DirectiveInfoManager.Output.ExtensionAttribute;
            string targetExtension = file
                                     .GetThisAndChildrenOfType <IT4OutputDirective>()
                                     .FirstOrDefault()
                                     ?.GetFirstAttribute(extension)
                                     ?.Value
                                     .GetText();

            if (targetExtension == null)
            {
                return(DefaultTargetExtension);
            }

            return(targetExtension.StartsWith(".", StringComparison.Ordinal)
                                ? targetExtension.Substring(1)
                                : targetExtension);
        }
        public static PsiLanguageType GetLanguageType([CanBeNull] IT4File file)
        {
            string name = file
                          ?.Blocks
                          .OfType <IT4TemplateDirective>()
                          .FirstOrDefault()
                          ?.GetFirstAttribute(Template.LanguageAttribute)
                          ?.Value
                          ?.GetText();

            switch (name)
            {
            case null:
            case LanguageAttributeInfo.CSharpLanguageAttributeValue:
            case LanguageAttributeInfo.NewCSharpLanguageAttributeValue:
                return(CSharpLanguage.Instance);

            case LanguageAttributeInfo.VBLanguageAttributeValue:
                return(VBLanguage.Instance);

            default:
                return(UnknownLanguage.Instance);
            }
        }
Example #20
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));
            }
        }
Example #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T4DaemonStageProcess"/> class.
 /// </summary>
 /// <param name="file">The associated T4 file.</param>
 /// <param name="daemonProcess">The associated daemon process.</param>
 protected T4DaemonStageProcess([NotNull] IT4File file, [NotNull] IDaemonProcess daemonProcess)
 {
     File          = file;
     DaemonProcess = daemonProcess;
 }
 private FileSystemPath GetTemporaryTargetFileFolder([NotNull] IT4File file) =>
 GetTemporaryExecutableLocation(file).Parent;
 public FileSystemPath GetExpectedTemporaryTargetFileLocation(IT4File file) =>
 GetTemporaryExecutableLocation(file).Parent.Combine(GetExpectedTargetFileName(file));
Example #24
0
		protected override IDaemonStageProcess CreateProcess(IDaemonProcess process, IT4File file) {
			return new T4ErrorProcess(file, process, _directiveInfoManager);
		}
		public T4CodeStructureRootElement(IT4File file)
			: base(file) {
		}
		public T4CodeBehindWrappedSelection([NotNull] IT4File file, [NotNull] ISelectedRange codeBehindRange) {
			_file = file;
			_codeBehindRange = codeBehindRange;
		}
 private IProjectFile GetOrCreateSameDestinationFile(
     [NotNull] IProjectModelTransactionCookie cookie,
     [NotNull] IT4File file,
     [NotNull] FileSystemPath temporary
     ) => GetOrCreateSameDestinationFile(cookie, file, temporary.Name);
Example #28
0
 protected override IDaemonStageProcess CreateProcess(IDaemonProcess process, IT4File file)
 {
     return(new T4HighlightingProcess(file, process));
 }
Example #29
0
 protected override IDaemonStageProcess CreateProcess(IDaemonProcess process, IT4File file)
 {
     return new T4HighlightingProcess(file, process);
 }
 public T4CSharpIntermediateConverter(
     [NotNull] T4CSharpCodeGenerationIntermediateResult intermediateResult,
     [NotNull] IT4File file
     ) : base(intermediateResult, file)
 {
 }
Example #31
0
 protected override IDaemonStageProcess CreateProcess(
     IDaemonProcess process,
     IT4File file,
     IContextBoundSettingsStore settings
     ) => new T4IncludeAwareDaemonProcess(file, process);
Example #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T4DaemonStageProcess"/> class.
 /// </summary>
 /// <param name="file">The associated T4 file.</param>
 /// <param name="daemonProcess">The associated daemon process.</param>
 /// <param name="directiveInfoManager">An instance of <see cref="DirectiveInfoManager"/>.</param>
 public T4ErrorProcess([NotNull] IT4File file, [NotNull] IDaemonProcess daemonProcess, [NotNull] DirectiveInfoManager directiveInfoManager)
     : base(file, daemonProcess)
 {
     _directiveInfoManager = directiveInfoManager;
 }
 private void Preprocess([NotNull] IT4File file, [NotNull] ISolution solution)
 {
     Statistics.TrackAction(T4StatisticIdBundle.PreprocessSilently);
     solution.GetComponent <IT4TemplatePreprocessingManager>().TryPreprocess(file);
 }
Example #34
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T4CSharpCodeGenerator"/> class.
 /// </summary>
 /// <param name="file">The associated T4 file whose C# code behind will be generated.</param>
 /// <param name="directiveInfoManager">An instance of <see cref="DirectiveInfoManager"/>.</param>
 internal T4CSharpCodeGenerator([NotNull] IT4File file, [NotNull] DirectiveInfoManager directiveInfoManager)
 {
     _file = file;
     _directiveInfoManager = directiveInfoManager;
 }
 public T4CSharpCodeBehindGenerator(
     [NotNull] IT4File file,
     [NotNull] ISolution solution
     ) : base(file) => Solution = solution;
Example #36
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T4DaemonStageProcess"/> class.
 /// </summary>
 /// <param name="file">The associated T4 file.</param>
 /// <param name="daemonProcess">The associated daemon process.</param>
 protected T4DaemonStageProcess([NotNull] IT4File file, [NotNull] IDaemonProcess daemonProcess)
 {
     _file = file;
     _daemonProcess = daemonProcess;
 }