private static CodeDomClientCodeGenerator CreateProxyGenerator(bool isCSharp)
        {
            MockCodeGenerationHost     host      = new MockCodeGenerationHost();
            CodeDomClientCodeGenerator generator = isCSharp
                                                        ? (CodeDomClientCodeGenerator) new CSharpCodeDomClientCodeGenerator()
                                                        : (CodeDomClientCodeGenerator) new VisualBasicCodeDomClientCodeGenerator();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = isCSharp ? "C#" : "VB",
            };

            generator.Initialize(host, new DomainServiceDescription[] { DomainServiceDescription.GetDescription(typeof(MockOrder_DomainService)) }, options);
            return(generator);
        }
        private string GenerateCode()
        {
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language            = this._isCSharp ? "C#" : "VisualBasic",
                ClientProjectPath   = "MockProject.proj",
                ClientRootNamespace = "TestRootNS",
                UseFullTypeNames    = this._useFullTypeNames
            };

            MockCodeGenerationHost     host      = TestHelper.CreateMockCodeGenerationHost(this.ConsoleLogger, this.MockSharedCodeService);
            CodeDomClientCodeGenerator generator = (this._isCSharp)
                                                        ? (CodeDomClientCodeGenerator) new CSharpCodeDomClientCodeGenerator()
                                                        : (CodeDomClientCodeGenerator) new VisualBasicCodeDomClientCodeGenerator();

            this._domainServiceCatalog = new DomainServiceCatalog(this._domainServiceTypes, this.ConsoleLogger);

            string generatedCode = generator.GenerateCode(host, this._domainServiceCatalog.DomainServiceDescriptions, options);

            return(generatedCode);
        }
Beispiel #3
0
        /// <summary>
        /// Validates code gen for a specific language by comparing it against a file containing the expected output.
        /// </summary>
        /// <param name="codeGenOptions">The options specifying the type of validation to perform</param>
        /// <returns>A command that updates the comparison file</returns>
        internal static string ValidateLanguageCodeGen(CodeGenValidationOptions codeGenOptions)
        {
            Assert.IsFalse(string.IsNullOrEmpty(codeGenOptions.Language));

            string outDataDir  = TestHelper.GetOutputTestDataDir(codeGenOptions.RelativeDeployDir);
            string extension   = TestHelper.ExtensionFromLanguage(codeGenOptions.Language);
            string diffMessage = string.Empty;

            // Compose the abs path to where the test file got deployed by MSTest
            string referenceFileName = TestHelper.GetTestFileName(codeGenOptions.RelativeDeployDir, codeGenOptions.BaseReferenceFileName + extension);

            Assert.IsTrue(File.Exists(referenceFileName), "Cannot find reference file " + referenceFileName);
            string generatedCode = string.Empty;

            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language            = codeGenOptions.Language,
                ClientRootNamespace = codeGenOptions.RootNamespace,
                ClientProjectPath   = "MockProject.proj",
                IsApplicationContextGenerationEnabled = codeGenOptions.GenerateApplicationContexts,
                UseFullTypeNames            = codeGenOptions.UseFullTypeNames,
                ClientProjectTargetPlatform = TargetPlatform.Silverlight
            };

            MockCodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(codeGenOptions.Logger, codeGenOptions.SharedCodeService);
            ILogger logger = host as ILogger;
            DomainServiceCatalog catalog = new DomainServiceCatalog(codeGenOptions.DomainServiceTypes, logger);
            IDomainServiceClientCodeGenerator generator;

            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                generator = dispatcher.FindCodeGenerator(host, options, /*compositionAssemblies*/ null, /*codeGeneratorName*/ null);
            }
            Assert.IsNotNull(generator, "Failed to find a code generator");
            generatedCode = generator.GenerateCode(host, catalog.DomainServiceDescriptions, options);

            ConsoleLogger consoleLogger = logger as ConsoleLogger;
            string        errors        = consoleLogger == null ? "" : consoleLogger.Errors;

            Assert.IsTrue(generatedCode.Length > 0, "No code was generated: " + errors);

            // Dump the generated code into a file for comparison
            bool   isCSharp          = options.Language.Equals("C#", StringComparison.InvariantCultureIgnoreCase);
            string generatedFileName = Path.Combine(outDataDir, Path.GetFileName(referenceFileName) + ".testgen");

            File.WriteAllText(generatedFileName, generatedCode);

            // TODO: (ron M3) Solve inability to get right MSBuild after checkin
            // First see if we compile
            List <string> referenceAssemblies = CompilerHelper.GetSilverlightClientAssemblies(codeGenOptions.RelativeDeployDir);
            List <string> files = new List <string>();

            files.Add(generatedFileName);

            // Unconditionally force generation of Xml doc comments to catch errors
            string documentationFile = Path.GetTempFileName();

            try
            {
                if (isCSharp)
                {
                    files.AddRange(codeGenOptions.SharedFiles.Where(sharedFile => Path.GetExtension(sharedFile).Equals(".cs")));
                    CompilerHelper.CompileCSharpSourceFromFiles(files, referenceAssemblies, documentationFile);
                }
                else
                {
                    files.AddRange(codeGenOptions.SharedFiles.Where(sharedFile => Path.GetExtension(sharedFile).Equals(".vb")));
                    CompilerHelper.CompileVisualBasicSourceFromFiles(files, referenceAssemblies, options.ClientRootNamespace, documentationFile);
                }
            }
            finally
            {
                File.Delete(documentationFile);
            }

            // Do the diff
            if (codeGenOptions.FailOnDiff)
            {
                TestHelper.ValidateFilesEqual(codeGenOptions.RelativeTestDir, codeGenOptions.RelativeDeployDir, generatedFileName, referenceFileName, codeGenOptions.Language);
            }
            else
            {
                TestHelper.FilesMatch(codeGenOptions.RelativeTestDir, codeGenOptions.RelativeDeployDir, generatedFileName, referenceFileName, codeGenOptions.Language, out diffMessage);
            }

            return(diffMessage);
        }
Beispiel #4
0
        // Creates a mock code generation host that uses the given logger and shared type service
        internal static MockCodeGenerationHost CreateMockCodeGenerationHost(ILoggingService logger, ISharedCodeService sharedCodeService)
        {
            MockCodeGenerationHost host = new MockCodeGenerationHost(logger, sharedCodeService);

            return(host);
        }
 // Creates a mock code generation host that uses the given logger and shared type service
 internal static MockCodeGenerationHost CreateMockCodeGenerationHost(ILoggingService logger, ISharedCodeService sharedCodeService)
 {
     MockCodeGenerationHost host = new MockCodeGenerationHost(logger, sharedCodeService);
     return host;
 }
 private static CodeDomClientCodeGenerator CreateProxyGenerator(bool isCSharp)
 {
     MockCodeGenerationHost host = new MockCodeGenerationHost();
     CodeDomClientCodeGenerator generator = isCSharp
                                                 ? (CodeDomClientCodeGenerator) new CSharpCodeDomClientCodeGenerator() 
                                                 : (CodeDomClientCodeGenerator) new VisualBasicCodeDomClientCodeGenerator();
     ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
     {
         Language = isCSharp ? "C#" : "VB",
     };
     generator.Initialize(host, new DomainServiceDescription[] { DomainServiceDescription.GetDescription(typeof(MockOrder_DomainService))}, options );
     return generator;
 }