示例#1
0
        public override bool Execute()
        {
            string currentDirectory = Environment.CurrentDirectory;

            try
            {
                WsdlFileX wsdl = new WsdlFileX(m_wsdlFile);

                Environment.CurrentDirectory = Path.GetDirectoryName(wsdl.FileInfo.FullName);

                string service = Path.GetFileNameWithoutExtension(wsdl.FileInfo.Name);

                MFSvcUtil svcutil = new MFSvcUtil(m_buildTreeRoot);
                svcutil.WSDL = wsdl;
                if (!svcutil.GenerateCode())
                {
                    return(false);
                }

                string assemblyName = service + "TestFixture";

                ServiceProject sp = new ServiceProject(
                    svcutil.Interface,
                    svcutil.ClientProxy,
                    svcutil.HostedService,
                    wsdl)
                {
                    AssemblyName  = assemblyName,
                    RootNamespace = assemblyName
                };

                if (sp.IsSDKProject)
                {
                    sp.SetProperty("OutputPath", "bin\\DebugTemp");
                }

                sp.Save(assemblyName + ".csproj");

                MSBuildProcess msbuild = new MSBuildProcess();
                msbuild.Project = sp.FullPath;
                msbuild.Properties.Add("Configuration", m_buildFlavor);
                msbuild.Properties.Add("FLAVOR", m_buildFlavor);

                int result = msbuild.Build();

                if (result > 0)
                {
                    return(false);
                }

                IEnumerable <ProjectItem> references = sp.GetItems("Reference");

                ILocator locator = LocatorFactory.GetLocator(m_buildTreeRoot);

                List <Assembly> referenceAssemblies = new List <Assembly>();

                foreach (ProjectItem reference in references)
                {
                    string   mfAssemblyFile = locator.GetAssemblyFileInfo(reference.EvaluatedInclude).FullName;
                    Assembly asm            = Assembly.ReflectionOnlyLoadFrom(mfAssemblyFile);
                    referenceAssemblies.Add(asm);
                }

                // Load the assembly just built.
                string   assemblyFile = locator.TestAssemblies + "\\" + sp.AssemblyName + ".dll";
                Assembly assembly     = Assembly.ReflectionOnlyLoadFrom(assemblyFile);

                CodeCompileUnit ccu = new CodeCompileUnit();

                FixtureNamespace fns = new FixtureNamespace(
                    assembly,
                    referenceAssemblies.ToArray())
                {
                    TestIterations = m_testIterations
                };

                ccu.Namespaces.Add(fns.Namespace);

                CSharpCodeProvider provider = new CSharpCodeProvider();

                string codeFile = assemblyName + ".cs";

                IndentedTextWriter itw = new IndentedTextWriter(
                    new StreamWriter(codeFile), "    ");

                CodeGeneratorOptions options = new CodeGeneratorOptions();
                options.BracingStyle = "C";

                provider.GenerateCodeFromCompileUnit(
                    ccu,
                    itw,
                    options);

                itw.Flush();
                itw.Close();

                // Update the project file before the final build
                if (sp.IsSDKProject)
                {
                    sp.SetProperty("OutputPath", "bin\\Debug");
                }

                // Add the generated file to the project
                sp.AddItem("Compile", codeFile);

                // Add user supplied c# files if any
                if (m_csharpFiles != null && m_csharpFiles.Length > 0)
                {
                    foreach (string csharpfile in m_csharpFiles)
                    {
                        sp.AddItem("Compile", csharpfile);
                    }
                }

                sp.SetProperty("OutputType", "Exe");
                sp.Save(assemblyName + ".csproj");

                msbuild.Target = null;
                result         = msbuild.Build();

                if (result > 0)
                {
                    return(false);
                }

                if (sp.IsDevProject)
                {
                    new SlnProj(assemblyName + ".csproj", new Guid(m_componentGuid)).Save();
                }

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "\r\n\r\n" + e.StackTrace);
                return(false);
            }
            finally
            {
                Environment.CurrentDirectory = currentDirectory;
            }
        }