コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the Service class with default dependency resolution and loading.
        /// </summary>
        /// <param name="outputDirectory"></param>
        /// <param name="transformation"></param>
        /// <param name="assemblyTransformation"></param>
        /// <inheritdoc />
        public CompilationServiceManager(string outputDirectory, string registryFileName
                                         , DocumentTransformation transformation, AssemblyTransformation assemblyTransformation)
            : base(outputDirectory, registryFileName
                   , () => GeneratedSyntaxTreeRegistryJsonConverter.Converter)
        {
            Requires.NotNull(transformation, nameof(transformation));
            Requires.NotNull(transformation.ReferenceService, nameof(transformation.ReferenceService));
            Requires.NotNull(assemblyTransformation, nameof(assemblyTransformation));
            Requires.NotNull(assemblyTransformation.ReferenceService, nameof(assemblyTransformation.ReferenceService));
            Verify.Operation(ReferenceEquals(transformation.ReferenceService, assemblyTransformation.ReferenceService), "Reference Service must be the same instance.");

            ReferenceService       = transformation.ReferenceService;
            Transformation         = transformation;
            AssemblyTransformation = assemblyTransformation;
        }
        protected override void CompilationManager_OnEvaluateCompilation(object sender, CompilationDiagnosticEventArgs e)
        {
            base.CompilationManager_OnEvaluateCompilation(sender, e);

            //// TODO: TBD: may in fact make the base abstract...
            //base.CompilationManager_OnEvaluateCompilation(sender, e);

            // TODO: TBD: in and of itself Creating ReferenceService and DocumentTransformation is based on the Tool project...
            // TODO: TBD: perhaps which could be refactored to a better place...
            // TODO: TBD: fluent assertions notwithstanding...

            // TODO: TBD: do we need any ReferencePathList elements for this?
            var referencePath = GetRange <string>();
            // TODO: TBD: ditto GeneratorSearchPathList ... ?
            var generatorSearchPath = GetRange <string>();

            AssemblyReferenceServiceManager CreateReferenceService() => new AssemblyReferenceServiceManager(
                TransformationName, IntermediateAssemblyReferenceRegistryFileName
                , referencePath.ToArray(), generatorSearchPath.ToArray());

            var docs = e.Project.Documents;

            // TODO: TBD: this approach is loosely informed by the original project:
            // https://github.com/AArnott/CodeGeneration.Roslyn/blob/master/src/CodeGeneration.Roslyn.Tests/Helpers/CompilationTestsBase.cs#L75

            // ReSharper disable PossibleMultipleEnumeration
            docs.Any().AssertTrue();
            // TODO: TBD: "1" (or however many... may want to capture "sources" as a property which we can use to verify...)
            docs.Count().AssertEqual(1);

            // TODO: TBD: may better leverage the tree(s) in the actual e.Compilation instead...
            docs.First().TryGetSyntaxTree(out var tree).AssertTrue();
            // ReSharper restore PossibleMultipleEnumeration

            var transformation = new DocumentTransformation(CreateReferenceService().AssertNotNull())
            {
                ProjectDirectory = ProjectDirectory,
                InputDocument    = tree.AssertNotNull()
            };

            var compilation = e.Compilation as CSharpCompilation;

            // TODO: TBD: may want to convey task cancellation from other sources than `default´.
            var results = transformation.TransformAsync(compilation, Progress, default).Result;

            TransformedSources = results.Select(x => $"{x.GetText()}").ToArray();
        }