示例#1
0
        internal string CompileVisualBasicSource()
        {
            List <ITaskItem> sources = new List <ITaskItem>();

            sources.Add(new TaskItem(this.GeneratedCodeFile));

            // If client has added extra user code into the
            // compile request, add it in now
            string userCodeFile = this.UserCodeFile;

            if (!string.IsNullOrEmpty(userCodeFile))
            {
                sources.Add(new TaskItem(userCodeFile));
            }

            // Transform references into a list of ITaskItems.
            // Here, we skip over mscorlib explicitly because this is already included as a project reference.
            List <ITaskItem> references =
                this.ReferenceAssemblies
                .Where(reference => !reference.EndsWith("mscorlib.dll", StringComparison.Ordinal))
                .Select <string, ITaskItem>(reference => new TaskItem(reference) as ITaskItem)
                .ToList();

            Vbc             vbc         = new Vbc();
            MockBuildEngine buildEngine = this.MockBuildEngine;

            vbc.BuildEngine = buildEngine; // needed before task can log

            vbc.NoStandardLib    = true;   // don't include std lib stuff -- we're feeding it silverlight
            vbc.NoConfig         = true;   // don't load the vbc.rsp file to get references
            vbc.TargetType       = "library";
            vbc.Sources          = sources.ToArray();
            vbc.References       = references.ToArray();
            vbc.SdkPath          = CompilerHelper.GetSilverlightSdkReferenceAssembliesPath();
            vbc.RootNamespace    = "TestRootNS";
            vbc.DefineConstants += "SILVERLIGHT";

            vbc.OutputAssembly = new TaskItem(this.OutputAssemblyName);

            bool result = false;

            try
            {
                result = vbc.Execute();
            }
            catch (Exception ex)
            {
                Assert.Fail("Exception occurred invoking VBC task on " + sources[0].ItemSpec + ":\r\n" + ex);
            }

            Assert.IsTrue(result, "VBC failed to compile " + sources[0].ItemSpec + ":\r\n" + buildEngine.ConsoleLogger.Errors);
            return(vbc.OutputAssembly.ItemSpec);
        }
        private MemoryStream CompileSource()
        {
            string assemblyname = Path.GetFileNameWithoutExtension(this.OutputAssemblyName);
            var    contents     = new List <SourceText>(capacity: 2);

            contents.Add(SourceText.From(GeneratedCode));
            if (!string.IsNullOrEmpty(UserCode))
            {
                contents.Add(SourceText.From(UserCode));
            }

            if (this._isCSharp)
            {
                return(CompilerHelper.CompileCSharpSilverlightAssembly(assemblyname, contents, referenceAssemblies: ReferenceAssemblies));
            }
            else
            {
                return(CompilerHelper.CompileVBSilverlightAssembly(assemblyname, contents, ReferenceAssemblies, rootNamespace: "TestRootNS", documentationFile: null));
            }
        }
示例#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);
        }