Beispiel #1
0
        static void Main(string[] args)
        {                      
            // We need to tell MSBuild where msbuild.exe is, so it can launch child nodes
            string parameters = @"MSBUILDLOCATION=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.System) + @"\..\Microsoft.NET\Framework\v3.5";
            
            // We need to tell MSBuild whether nodes should hang around for 60 seconds after the build is done in case they are needed again
            bool nodeReuse = true; // e.g.
            if (!nodeReuse)
            {
                parameters += ";NODEREUSE=false";
            }

            // We need to tell MSBuild the maximum number of nodes to use. It is usually fastest to pick about the same number as you have CPU cores
            int maxNodeCount = 3; // e.g.

            // Create the engine with this information
            Engine buildEngine = new Engine(null, ToolsetDefinitionLocations.Registry | ToolsetDefinitionLocations.ConfigurationFile, maxNodeCount, parameters);

            // Create a file logger with a matching forwarding logger, e.g.
            FileLogger fileLogger = new FileLogger();
            fileLogger.Verbosity = LoggerVerbosity.Detailed;
            Assembly engineAssembly = Assembly.GetAssembly(typeof(Engine));
            string loggerAssemblyName = engineAssembly.GetName().FullName;
            LoggerDescription fileLoggerForwardingLoggerDescription = new LoggerDescription("Microsoft.Build.BuildEngine.ConfigurableForwardingLogger", loggerAssemblyName, null, String.Empty, LoggerVerbosity.Detailed);

            // Create a regular console logger too, e.g.
            ConsoleLogger logger = new ConsoleLogger();
            logger.Verbosity = LoggerVerbosity.Normal;

            // Register all of these loggers
            buildEngine.RegisterDistributedLogger(fileLogger, fileLoggerForwardingLoggerDescription);
            buildEngine.RegisterLogger(logger);

            // Do a build
            buildEngine.BuildProjectFile("root.proj");

            // Finish cleanly
            buildEngine.Shutdown();
        }
Beispiel #2
0
        public void BuildProjectFilesInParallel()
        {

            //Gets the currently loaded assembly in which the specified class is defined
            Assembly engineAssembly = Assembly.GetAssembly(typeof(Engine));
            string loggerClassName = "Microsoft.Build.BuildEngine.ConfigurableForwardingLogger";
            string loggerAssemblyName = engineAssembly.GetName().FullName;
            LoggerDescription forwardingLoggerDescription = new LoggerDescription(loggerClassName, loggerAssemblyName, null, null, LoggerVerbosity.Normal);

            string[] fileNames = new string[10];
            string traversalProject = TraversalProjectFile("ABC");
            string[][] targetNamesPerProject = new string[fileNames.Length][];
            IDictionary[] targetOutPutsPerProject = new IDictionary[fileNames.Length];
            BuildPropertyGroup[] globalPropertiesPerProject = new BuildPropertyGroup[fileNames.Length];
            string[] tempfilesToDelete = new string[fileNames.Length];
            Engine engine = new Engine(null, ToolsetDefinitionLocations.ConfigurationFile | ToolsetDefinitionLocations.Registry, 4, "msbuildlocation="+AppDomain.CurrentDomain.BaseDirectory);
            engine.RegisterLogger(new ConsoleLogger(LoggerVerbosity.Normal));
            try
            {
                for (int i = 0; i < fileNames.Length; i++)
                {
                    string[] ProjectFiles1 = CreateGlobalPropertyProjectFileWithExtension("ABC");
                    fileNames[i] = ProjectFiles1[0];
                    tempfilesToDelete[i] = ProjectFiles1[1];
                    targetNamesPerProject[i] = new string[] { "Build" };
                }

                // Test building a traversal
              
                engine.BuildProjectFile(traversalProject);
                engine.Shutdown();
                
                // Test building the same set of files in parallel
                Console.Out.WriteLine("1:"+Process.GetCurrentProcess().MainModule.FileName);
                Console.Out.WriteLine("2:" + AppDomain.CurrentDomain.BaseDirectory);
                engine = new Engine(null, ToolsetDefinitionLocations.ConfigurationFile | ToolsetDefinitionLocations.Registry, 4, "msbuildlocation="+ AppDomain.CurrentDomain.BaseDirectory);
                engine.RegisterDistributedLogger(new ConsoleLogger(LoggerVerbosity.Normal), forwardingLoggerDescription);
                engine.BuildProjectFiles(fileNames, targetNamesPerProject, globalPropertiesPerProject, targetOutPutsPerProject, BuildSettings.None, new string[fileNames.Length]);
                engine.Shutdown();

                // Do the same using singleproc
                engine = new Engine(null, ToolsetDefinitionLocations.ConfigurationFile | ToolsetDefinitionLocations.Registry, 4, "msbuildlocation="+ AppDomain.CurrentDomain.BaseDirectory);
                engine.RegisterLogger(new ConsoleLogger(LoggerVerbosity.Normal));
                engine.BuildProjectFile(traversalProject);
                engine.Shutdown();

                engine = new Engine(null, ToolsetDefinitionLocations.ConfigurationFile | ToolsetDefinitionLocations.Registry, 1, "msbuildlocation="+ AppDomain.CurrentDomain.BaseDirectory);
                engine.RegisterLogger(new ConsoleLogger(LoggerVerbosity.Normal));
                engine.BuildProjectFiles(fileNames, targetNamesPerProject, globalPropertiesPerProject, targetOutPutsPerProject, BuildSettings.None, new string[fileNames.Length]);

            }
            finally
            {
                engine.Shutdown();
                for (int i = 0; i < fileNames.Length; i++)
                {
                    File.Delete(fileNames[i]);
                    File.Delete(tempfilesToDelete[i]);
                }
                File.Delete(traversalProject);
            }
        }