Пример #1
0
        public void FlowsProjectPropertiesIntoAssembly()
        {
            const string testName = "Test name";
            const string testTitle = "Test title";
            const string testDescription = "Test description";
            const string testCopyright = "Test copyright";
            const string testAssemblyFileVersion = "1.2.3.4";
            const string testVersion = "1.2.3-rc1";
            const string testFrameworkName = "DNX,Version=v4.5.1";

            // Arrange
            var compilationProjectContext = new CompilationProjectContext(
                new CompilationTarget(testName, new FrameworkName(testFrameworkName), string.Empty, string.Empty),
                string.Empty,
                string.Empty,
                testTitle,
                testDescription,
                testCopyright,
                testVersion,
                new Version(testAssemblyFileVersion),
                false,
                new CompilationFiles(
                    new List<string> { },
                    new List<string> { }),
                new Mock<ICompilerOptions>().Object);
            var compiler = new RoslynCompiler(
                new Mock<ICache>().Object,
                new Mock<ICacheContextAccessor>().Object,
                new Mock<INamedCacheDependencyProvider>().Object,
                new Mock<IAssemblyLoadContext>().Object,
                new Mock<IApplicationEnvironment>().Object,
                new Mock<IServiceProvider>().Object);
            var metadataReference = new Mock<IRoslynMetadataReference>();
            metadataReference
                .Setup(reference => reference.MetadataReference)
                .Returns(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));

            // Act
            var compilationContext = compiler.CompileProject(
                compilationProjectContext,
                new List<IMetadataReference> { metadataReference.Object },
                new List<ISourceReference> { },
                () => new List<ResourceDescriptor> { });

            // Assert
            var expectedAttributes = new Dictionary<string, string>
            {
                [typeof(AssemblyTitleAttribute).FullName] = testTitle,
                [typeof(AssemblyDescriptionAttribute).FullName] = testDescription,
                [typeof(AssemblyCopyrightAttribute).FullName] = testCopyright,
                [typeof(AssemblyFileVersionAttribute).FullName] = testAssemblyFileVersion,
                [typeof(AssemblyVersionAttribute).FullName] = testVersion.Substring(0, testVersion.IndexOf('-')),
                [typeof(AssemblyInformationalVersionAttribute).FullName] = testVersion,
            };
            var compilationAttributes = compilationContext.Compilation.Assembly.GetAttributes();

            Assert.All(compilationAttributes, compilationAttribute => expectedAttributes[compilationAttribute.AttributeClass.ToString()].Equals(
                compilationAttribute.ConstructorArguments.First().Value));
        }
        public IMetadataProjectReference CompileProject(
            CompilationProjectContext projectContext,
            Func <LibraryExport> referenceResolver,
            Func <IList <ResourceDescriptor> > resourcesResolver)
        {
            List <DiagnosticResult> diagnosticResults = new List <DiagnosticResult>();

            var module = new PostSharpCompilerModule(_services, _workingDirectory);

            var export = referenceResolver();

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

            var incomingReferences       = export.MetadataReferences;
            var incomingSourceReferences = export.SourceReferences;

            var processedIncomingReferences = new List <IMetadataReference>(incomingReferences.Count);

            foreach (var reference in incomingReferences)
            {
                var projectReference = reference as IMetadataProjectReference;
                if (projectReference != null)
                {
                    // If we have a PostSharpProjectReference, we have to compile it using EmitAssembly and replace the reference by a MetadataFileReference.
                    string referencePath = Path.Combine(_workingDirectory, projectReference.Name + ".dll");
                    if (!File.Exists(referencePath))
                    {
                        DiagnosticResult diagnostics = projectReference.EmitAssembly(_workingDirectory);
                        diagnosticResults.Add(diagnostics);
                    }

                    processedIncomingReferences.Add(new MetadataFileReference(projectReference.Name, referencePath));
                }
                else
                {
                    processedIncomingReferences.Add(reference);
                }
            }

            var compilationContext = _compiler.CompileProject(
                projectContext,
                processedIncomingReferences,
                incomingSourceReferences,
                resourcesResolver);

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

            compilationContext.Modules.Add(module);

            // Project reference
            return(new PostSharpProjectReference(new RoslynProjectReference(compilationContext), diagnosticResults, _workingDirectory));
        }
Пример #3
0
        private static CompilationContext Compile(FakeCompilerOptions compilerOptions, CompilationTarget target)
        {
            var cacheContextAccessor = new FakeCacheContextAccessor {
                Current = new CacheContext(null, (d) => { })
            };

            var compilationProjectContext = new CompilationProjectContext(
                target,
                Directory.GetCurrentDirectory(),
                "project.json",
                TestTitle,
                TestDescription,
                TestCopyright,
                TestVersion,
                Version.Parse(TestAssemblyFileVersion),
                false,
                new CompilationFiles(
                    new List <string> {
            },
                    new List <string> {
            }),
                compilerOptions);

            var compiler = new RoslynCompiler(null, cacheContextAccessor, new FakeNamedDependencyProvider(), null, null, null);

            var assembly          = typeof(object).GetTypeInfo().Assembly;
            var metadataReference = new FakeMetadataReference()
            {
                MetadataReference = MetadataReference.CreateFromFile((string)assembly.GetType().GetProperty("Location").GetValue(assembly))
            };

            var compilationContext = compiler.CompileProject(
                compilationProjectContext,
                new List <IMetadataReference> {
                metadataReference
            },
                new List <ISourceReference> {
            },
                () => new List <ResourceDescriptor>(),
                "Debug");

            return(compilationContext);
        }
Пример #4
0
        private static CompilationContext Compile(FakeCompilerOptions compilerOptions, CompilationTarget target)
        {
            var cacheContextAccessor = new FakeCacheContextAccessor {Current = new CacheContext(null, (d) => { })};

            var compilationProjectContext = new CompilationProjectContext(
               target,
                Directory.GetCurrentDirectory(),
                "project.json",
                TestTitle,
                TestDescription,
                TestCopyright,
                TestVersion,
                Version.Parse(TestAssemblyFileVersion),
                false,
                new CompilationFiles(
                    new List<string> {},
                    new List<string> {}),
                compilerOptions);

            var compiler = new RoslynCompiler(null, cacheContextAccessor, new FakeNamedDependencyProvider(), null, null, null);

            var assembly = typeof (object).GetTypeInfo().Assembly;
            var metadataReference = new FakeMetadataReference()
            {
                MetadataReference = MetadataReference.CreateFromFile((string)assembly.GetType().GetProperty("Location").GetValue(assembly))
            };

            var compilationContext = compiler.CompileProject(
                compilationProjectContext,
                new List<IMetadataReference> { metadataReference },
                new List<ISourceReference> {},
                () => new List<ResourceDescriptor>(),
                "Debug");
            return compilationContext;
        }
Пример #5
0
        public void FlowsProjectPropertiesIntoAssembly()
        {
            const string testName                = "Test name";
            const string testTitle               = "Test title";
            const string testDescription         = "Test description";
            const string testCopyright           = "Test copyright";
            const string testAssemblyFileVersion = "1.2.3.4";
            const string testVersion             = "1.2.3-rc1";
            const string testFrameworkName       = "DNX,Version=v4.5.1";

            // Arrange
            var compilationProjectContext = new CompilationProjectContext(
                new CompilationTarget(testName, new FrameworkName(testFrameworkName), string.Empty, string.Empty),
                string.Empty,
                string.Empty,
                testTitle,
                testDescription,
                testCopyright,
                testVersion,
                new Version(testAssemblyFileVersion),
                false,
                new CompilationFiles(
                    new List <string> {
            },
                    new List <string> {
            }),
                new Mock <ICompilerOptions>().Object);
            var compiler = new RoslynCompiler(
                new Mock <ICache>().Object,
                new Mock <ICacheContextAccessor>().Object,
                new Mock <INamedCacheDependencyProvider>().Object,
                new Mock <IAssemblyLoadContext>().Object,
                new Mock <IApplicationEnvironment>().Object,
                new Mock <IServiceProvider>().Object);
            var metadataReference = new Mock <IRoslynMetadataReference>();

            metadataReference
            .Setup(reference => reference.MetadataReference)
            .Returns(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));

            // Act
            var compilationContext = compiler.CompileProject(
                compilationProjectContext,
                new List <IMetadataReference> {
                metadataReference.Object
            },
                new List <ISourceReference> {
            },
                () => new List <ResourceDescriptor> {
            });

            // Assert
            var expectedAttributes = new Dictionary <string, string>
            {
                [typeof(AssemblyTitleAttribute).FullName]                = testTitle,
                [typeof(AssemblyDescriptionAttribute).FullName]          = testDescription,
                [typeof(AssemblyCopyrightAttribute).FullName]            = testCopyright,
                [typeof(AssemblyFileVersionAttribute).FullName]          = testAssemblyFileVersion,
                [typeof(AssemblyVersionAttribute).FullName]              = testVersion.Substring(0, testVersion.IndexOf('-')),
                [typeof(AssemblyInformationalVersionAttribute).FullName] = testVersion,
            };
            var compilationAttributes = compilationContext.Compilation.Assembly.GetAttributes();

            Assert.All(compilationAttributes, compilationAttribute => expectedAttributes[compilationAttribute.AttributeClass.ToString()].Equals(
                           compilationAttribute.ConstructorArguments.First().Value));
        }