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 FileSystemPath GetDestinationLocation([NotNull] IT4File file, [NotNull] string temporaryName)
        {
            Locks.AssertReadAccessAllowed();
            var sourceFile = file.PhysicalPsiSourceFile.NotNull();

            return(sourceFile.ToProjectFile().NotNull().Location.Parent.Combine(temporaryName));
        }
Example #4
0
		private string GenerateCode([NotNull] IT4File file)
		{
			Locks.AssertReadAccessAllowed();
			var generator = new T4CSharpExecutableCodeGenerator(file, Solution);
			string code = generator.Generate().RawText;
			return code;
		}
        public T4BuildResult Compile(Lifetime lifetime, IPsiSourceFile sourceFile)
        {
            Logger.Verbose("Compiling a file");
            Locks.AssertReadAccessAllowed();
            // Since we need no context when compiling a file, we need to build the tree manually
            var file  = sourceFile.BuildT4Tree();
            var error = file.ThisAndDescendants <IErrorElement>().Collect();

            if (!error.IsEmpty())
            {
                return(Converter.SyntaxErrors(error));
            }

            return(lifetime.UsingNested(nested =>
            {
                try
                {
                    // Prepare the code
                    var references = ReferenceExtractionManager.ExtractPortableReferences(lifetime, file);
                    string code = GenerateCode(file);

                    // Prepare the paths
                    var executablePath = TargetManager.GetTemporaryExecutableLocation(file);
                    var compilation = CreateCompilation(code, references, executablePath);
                    var location = executablePath.Parent;
                    switch (location.Exists)
                    {
                    case FileSystemPath.Existence.File:
                        location.DeleteFile();
                        goto case FileSystemPath.Existence.Missing;

                    case FileSystemPath.Existence.Missing:
                        location.CreateDirectory();
                        break;
                    }
                    var pdbPath = location.Combine(executablePath.Name.WithOtherExtension("pdb"));

                    // Delegate to Roslyn
                    var emitOptions = new EmitOptions(
                        debugInformationFormat: DebugInformationFormat.PortablePdb,
                        pdbFilePath: pdbPath.FullPath
                        );
                    using var executableStream = executablePath.OpenFileForWriting();
                    using var pdbStream = pdbPath.OpenFileForWriting();
                    var emitResult = compilation.Emit(
                        peStream: executableStream,
                        pdbStream: pdbStream,
                        options: emitOptions,
                        cancellationToken: nested
                        );

                    return Converter.ToT4BuildResult(emitResult.Diagnostics.AsList(), file);
                }
                catch (T4OutputGenerationException e)
                {
                    return Converter.ToT4BuildResult(e);
                }
            }));
        }
        private string GetPreprocessingTargetFileName([NotNull] IT4File file)
        {
            Locks.AssertReadAccessAllowed();
            var    sourceFile = file.PhysicalPsiSourceFile.NotNull();
            string name       = sourceFile.Name;

            return(name.WithOtherExtension("cs"));
        }
        private string GetExpectedTargetFileName([NotNull] IT4File file)
        {
            Locks.AssertReadAccessAllowed();
            var    sourceFile      = file.PhysicalPsiSourceFile.NotNull();
            string name            = sourceFile.Name;
            string targetExtension = file.GetTargetExtension() ?? T4CSharpCodeGenerationUtils.DefaultTargetExtension;

            return(name.WithOtherExtension(targetExtension));
        }
Example #8
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));
        }
        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 string GenerateCode([NotNull] IT4File file)
 {
     Locks.AssertReadAccessAllowed();
     return(T4RiderCodeGeneration.GenerateExecutableCode(file).RawText);
 }