コード例 #1
0
        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);
                }
            }));
        }
コード例 #2
0
 private List <int> CalculateProjectDependencies([NotNull] IPsiSourceFile file) => ProjectReferenceResolver
 .GetProjectDependencies(file.BuildT4Tree())
 .Select(it => Host.GetIdByProjectModelElement(it))
 .AsList();