Exemple #1
0
        public static SolutionBuildResult Build(string solutionFile, string buildConfiguration = null, string buildPlatform = null, string buildTarget = "Build", string logVerbosity = null)
        {
            string configString = String.Format("{0}|{1}", buildConfiguration ?? "<default>", buildPlatform ?? "<default>");

            if ((buildConfiguration ?? buildPlatform) != null)
                Console.Error.WriteLine("// Running target '{2}' of '{0}' ({1}) ...", Program.ShortenPath(solutionFile), configString, buildTarget);
            else
                Console.Error.WriteLine("// Running target '{1}' of '{0}' ...", Program.ShortenPath(solutionFile), buildTarget);

            var pc = new ProjectCollection();
            var parms = new BuildParameters(pc);
            var globalProperties = new Dictionary<string, string>();

            var hostServices = new HostServices();
            var eventRecorder = new BuildEventRecorder();
            LoggerVerbosity _logVerbosity;

            if ((logVerbosity == null) || !Enum.TryParse(logVerbosity, out _logVerbosity))
                _logVerbosity = LoggerVerbosity.Quiet;

            parms.Loggers = new ILogger[] {
                new ConsoleLogger(_logVerbosity), eventRecorder
            };

            var manager = BuildManager.DefaultBuildManager;

            Console.Error.Write("// Generating MSBuild projects for solution '{0}'...", Path.GetFileName(solutionFile));
            // Begin a fake build so the manager has a logger available.
            manager.BeginBuild(parms);

            var projects = ParseSolutionFile(
                solutionFile, buildConfiguration, buildPlatform,
                globalProperties, manager
            );

            manager.EndBuild();
            Console.Error.WriteLine(" {0} project(s) generated.", projects.Length);

            if (File.ReadAllText(solutionFile).Contains("ProjectSection(ProjectDependencies)")) {
                Console.Error.WriteLine("// WARNING: Your solution file contains project dependencies. MSBuild ignores these, so your build may fail. If it does, try building it in Visual Studio first to resolve the dependencies.");
            }

            var allItemsBuilt = new List<BuiltItem>();
            var resultFiles = new HashSet<string>();
            foreach (var project in projects) {

                // Save out the generated msbuild project for each solution, to aid debugging.
                try {
                    project.ToProjectRootElement().Save(project.FullPath, Encoding.UTF8);
                } catch (Exception exc) {
                    Console.Error.WriteLine("// Failed to save generated project '{0}': {1}", Path.GetFileName(project.FullPath), exc.Message);
                }

                Console.Error.WriteLine("// Building project '{0}'...", project.FullPath);

                var request = new BuildRequestData(
                    project, new string[] { buildTarget },
                    hostServices, BuildRequestDataFlags.None
                );

                BuildResult result = null;
                try {
                    result = manager.Build(parms, request);
                } catch (Exception exc) {
                    Console.Error.WriteLine("// Compilation failed: {0}", exc.Message);
                    continue;
                }

                allItemsBuilt.AddRange(ExtractChildProjectResults(manager));

                foreach (var kvp in result.ResultsByTarget) {
                    var targetResult = kvp.Value;

                    if ((targetResult.Exception != null) || (targetResult.ResultCode == TargetResultCode.Failure)) {
                        string errorMessage = "Unknown error";
                        if (targetResult.Exception != null)
                            errorMessage = targetResult.Exception.Message;
                        Console.Error.WriteLine("// Compilation failed for target '{0}': {1}", kvp.Key, errorMessage);
                    } else if (targetResult.Items.Length > 0) {
                        Console.Error.WriteLine("// Target '{0}' produced {1} output(s).", kvp.Key, targetResult.Items.Length);

                        foreach (var filename in targetResult.Items)
                            resultFiles.Add(filename.ItemSpec);
                    }
                }
            }

            return new SolutionBuildResult(
                resultFiles.ToArray(),
                eventRecorder.ProjectsById.Values.ToArray(),
                eventRecorder.TargetFiles.ToArray(),
                allItemsBuilt.ToArray()
            );
        }
Exemple #2
0
        public static SolutionBuildResult Build(string solutionFile, string buildConfiguration = null, string buildPlatform = null)
        {
            string configString = String.Format("{0}|{1}", buildConfiguration ?? "<default>", buildPlatform ?? "<default>");

            if ((buildConfiguration ?? buildPlatform) != null)
                Console.Error.Write("// Building '{0}' ({1}) ...", Program.ShortenPath(solutionFile), configString);
            else
                Console.Error.Write("// Building '{0}' ...", Program.ShortenPath(solutionFile));

            var pc = new ProjectCollection();
            var parms = new BuildParameters(pc);
            var globalProperties = new Dictionary<string, string>();

            if (buildConfiguration != null)
                globalProperties["Configuration"] = buildConfiguration;
            if (buildPlatform != null)
                globalProperties["Platform"] = buildPlatform;

            var request = new BuildRequestData(
                solutionFile, globalProperties,
                null, new string[] { "Build" }, null,
                BuildRequestDataFlags.ReplaceExistingProjectInstance
            );

            var eventRecorder = new BuildEventRecorder();

            parms.Loggers = new ILogger[] {
                new ConsoleLogger(LoggerVerbosity.Quiet), eventRecorder
            };

            var manager = BuildManager.DefaultBuildManager;
            var result = manager.Build(parms, request);
            var resultFiles = new HashSet<string>();

            Console.Error.WriteLine(" done.");

            foreach (var kvp in result.ResultsByTarget) {
                var targetResult = kvp.Value;

                if (targetResult.Exception != null)
                    Console.Error.WriteLine("// Compilation failed for target '{0}':\r\n{1}", kvp.Key, targetResult.Exception.Message);
                else {
                    foreach (var filename in targetResult.Items)
                        resultFiles.Add(filename.ItemSpec);
                }
            }

            return new SolutionBuildResult(
                resultFiles.ToArray(),
                eventRecorder.ProjectsById.Values.ToArray(),
                eventRecorder.TargetFiles.ToArray()
            );
        }
Exemple #3
0
        public static SolutionBuildResult Build(string solutionFile, string buildConfiguration = null, string buildPlatform = null, string buildTarget = "Build", string logVerbosity = null)
        {
            string configString = String.Format("{0}|{1}", buildConfiguration ?? "<default>", buildPlatform ?? "<default>");

            if ((buildConfiguration ?? buildPlatform) != null)
            {
                Console.Error.WriteLine("// Running target '{2}' of '{0}' ({1}) ...", Program.ShortenPath(solutionFile), configString, buildTarget);
            }
            else
            {
                Console.Error.WriteLine("// Running target '{1}' of '{0}' ...", Program.ShortenPath(solutionFile), buildTarget);
            }

            var pc               = new ProjectCollection();
            var parms            = new BuildParameters(pc);
            var globalProperties = new Dictionary <string, string>();

            var             hostServices  = new HostServices();
            var             eventRecorder = new BuildEventRecorder();
            LoggerVerbosity _logVerbosity;

            if ((logVerbosity == null) || !Enum.TryParse(logVerbosity, out _logVerbosity))
            {
                _logVerbosity = LoggerVerbosity.Quiet;
            }

            parms.Loggers = new ILogger[] {
                new ConsoleLogger(_logVerbosity), eventRecorder
            };

            var manager = BuildManager.DefaultBuildManager;

            Console.Error.Write("// Generating MSBuild projects for solution '{0}'...", Path.GetFileName(solutionFile));
            // Begin a fake build so the manager has a logger available.
            manager.BeginBuild(parms);

            var projects = ParseSolutionFile(
                solutionFile, buildConfiguration, buildPlatform,
                globalProperties, manager
                );

            manager.EndBuild();
            Console.Error.WriteLine(" {0} project(s) generated.", projects.Length);

            if (File.ReadAllText(solutionFile).Contains("ProjectSection(ProjectDependencies)"))
            {
                Console.Error.WriteLine("// WARNING: Your solution file contains project dependencies. MSBuild ignores these, so your build may fail. If it does, try building it in Visual Studio first to resolve the dependencies.");
            }

            var allItemsBuilt = new List <BuiltItem>();
            var resultFiles   = new HashSet <string>();

            foreach (var project in projects)
            {
                // Save out the generated msbuild project for each solution, to aid debugging.
                try {
                    project.ToProjectRootElement().Save(project.FullPath, Encoding.UTF8);
                } catch (Exception exc) {
                    Console.Error.WriteLine("// Failed to save generated project '{0}': {1}", Path.GetFileName(project.FullPath), exc.Message);
                }

                Console.Error.WriteLine("// Building project '{0}'...", project.FullPath);

                var request = new BuildRequestData(
                    project, new string[] { buildTarget },
                    hostServices, BuildRequestDataFlags.None
                    );

                BuildResult result = null;
                try {
                    result = manager.Build(parms, request);
                } catch (Exception exc) {
                    Console.Error.WriteLine("// Compilation failed: {0}", exc.Message);
                    continue;
                }

                allItemsBuilt.AddRange(ExtractChildProjectResults(manager));

                foreach (var kvp in result.ResultsByTarget)
                {
                    var targetResult = kvp.Value;

                    if ((targetResult.Exception != null) || (targetResult.ResultCode == TargetResultCode.Failure))
                    {
                        string errorMessage = "Unknown error";
                        if (targetResult.Exception != null)
                        {
                            errorMessage = targetResult.Exception.Message;
                        }
                        Console.Error.WriteLine("// Compilation failed for target '{0}': {1}", kvp.Key, errorMessage);
                    }
                    else if (targetResult.Items.Length > 0)
                    {
                        Console.Error.WriteLine("// Target '{0}' produced {1} output(s).", kvp.Key, targetResult.Items.Length);

                        foreach (var filename in targetResult.Items)
                        {
                            resultFiles.Add(filename.ItemSpec);
                        }
                    }
                }
            }

            return(new SolutionBuildResult(
                       resultFiles.ToArray(),
                       eventRecorder.ProjectsById.Values.ToArray(),
                       eventRecorder.TargetFiles.ToArray(),
                       allItemsBuilt.ToArray()
                       ));
        }