Пример #1
0
        public void ClientCodeGenerationDispatcher_Finds_Derived_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "G#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                IClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, MockGSharpCodeGeneratorDerived.GeneratorName);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(MockGSharpCodeGeneratorDerived), "dispatcher found " + generator.GetType() + " but should have found MockGSharpCodeGeneratorDerived");

                string generatedCode = generator.GenerateCode(host, Enumerable.Empty <EntityDescription>(), options);

                Assert.AreEqual(MockGSharpCodeGeneratorDerived.DerivedGeneratedCode, generatedCode, "test code generator did not generate expected code.");
            }
        }
Пример #2
0
        public void ClientCodeGenerationDispatcher_Custom_Warns_Full()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location };

                IClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, MockCodeGenerator.GeneratorName);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(MockCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found MockCodeGenerator");

                // Setting this option makes our custom code generator emit the packet below to test LogWarning
                MockCodeGenerator.LogWarningsFull = true;
                string generatedCode = generator.GenerateCode(host, Enumerable.Empty <EntityDescription>(), options);

                Assert.AreEqual(MockCodeGenerator.FakeGeneratedCode, generatedCode, "test code generator did not generate expected code.");
                TestHelper.AssertContainsWarningPackets(logger, MockCodeGenerator.WarningPacket);
            }
        }
        /// <summary>
        /// Generates client proxy source code using the specified <paramref name="codeGeneratorName"/> in the context
        /// of the specified <paramref name="host"/>.
        /// </summary>
        /// <param name="host">The host for code generation.</param>
        /// <param name="options">The options to use for code generation.</param>
        /// <param name="catalog">The catalog containing the <see cref="Luma.Client.Entity"/> types.</param>
        /// <param name="compositionAssemblies">The optional set of assemblies to use to create the MEF composition container.</param>
        /// <param name="codeGeneratorName">Optional generator name.  A <c>null</c> or empty value will select the default generator.</param>
        /// <returns>The generated source code or <c>null</c> if none was generated.</returns>
        public string GenerateCode(ICodeGenerationHost host, ClientCodeGenerationOptions options, EntityCatalog catalog, IEnumerable <string> compositionAssemblies, string codeGeneratorName)
        {
            Debug.Assert(host != null, "host cannot be null");
            Debug.Assert(options != null, "options cannot be null");
            Debug.Assert(catalog != null, "catalog cannot be null");

            IEnumerable <EntityDescription> entityDescriptions = catalog.EntityDescriptions;
            IClientCodeGenerator            proxyGenerator     = FindCodeGenerator(host, options, compositionAssemblies, codeGeneratorName);
            string generatedCode = null;

            if (proxyGenerator != null)
            {
                try
                {
                    generatedCode = proxyGenerator.GenerateCode(host, entityDescriptions, options);
                }
                catch (Exception ex)
                {
                    // Fatal exceptions are never swallowed or processed
                    if (ex.IsFatal())
                    {
                        throw;
                    }

                    // Any exception from the code generator is caught and reported, otherwise it will
                    // hit the MSBuild backstop and report failure of the custom build task.
                    // It is acceptable to report this exception and "ignore" it because we
                    // are running in a separate AppDomain which will be torn down immediately
                    // after our return.
                    host.LogError(string.Format(CultureInfo.CurrentCulture,
                                                Resource.CodeGenerator_Threw_Exception,
                                                string.IsNullOrEmpty(codeGeneratorName) ? proxyGenerator.GetType().FullName : codeGeneratorName,
                                                options.ClientProjectPath,
                                                ex.Message));
                }
            }

            return(generatedCode);
        }