Example #1
0
        public bool BuildProject()
        {
            Log = "";
            bool buildResultOK = false;

            BuildResultDll    = BuildResultSymbols = "";
            BuildResultAssets = null;
            try
            {
                using (bLogger BuildLogger = new bLogger())
                {
                    // usage Microsoft.Build.BuildEngine.Engine is prevelegy of MONO runtime
                    Microsoft.Build.BuildEngine.Engine eng = Microsoft.Build.BuildEngine.Engine.GlobalEngine;
                    eng.RegisterLogger(BuildLogger);
                    Microsoft.Build.BuildEngine.Project prj = new Microsoft.Build.BuildEngine.Project(eng);

                    prj.Load(ProjectLocation);

                    logger.Debug("building project: {0}", ProjectLocation);
                    // todo: add configuration set option

                    if (!string.IsNullOrWhiteSpace(this.Configuration))
                    {
                        eng.GlobalProperties.SetProperty("Configuration", this.Configuration);
                    }
                    string outd = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "_tempArtifacts");
                    eng.GlobalProperties.SetProperty("OutputPath", outd);
                    eng.GlobalProperties.SetProperty("Platform", "AnyCPU");

                    string fileLib = System.IO.Path.Combine(outd,
                                                            prj.GetEvaluatedProperty("TargetFileName"));
                    string fileSymbols = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(fileLib), System.IO.Path.GetFileNameWithoutExtension(fileLib) + ".mdb");

                    buildResultOK = prj.Build();
                    eng.UnregisterAllLoggers();

                    Log = BuildLogger.Result();
                    if (buildResultOK)
                    {
                        BuildResultDll     = fileLib;
                        BuildResultSymbols = fileSymbols;
                        List <string> files = new List <string>();
                        foreach (string F in System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(fileLib)))
                        {// TODO: not recursive! not now...
                            if (F != BuildResultDll && F != BuildResultSymbols)
                            {
                                files.Add(F);
                            }
                        }
                        BuildResultAssets = files.ToArray();
                    }
                }
            }
            catch (Exception e)
            {
                Log += string.Format("major exception while build: {0}", e.Message);
            }
            return(buildResultOK);
        }
        public static Microsoft.Build.BuildEngine.Engine CreateMSEngine(NAnt.VSNet.Tasks.SolutionTask solutionTask)
        {
            if (_msbuild != null)
            {
                return(_msbuild);
            }

            // map current target framework to TargetDotNetFrameworkVersion
            TargetDotNetFrameworkVersion frameworkVersion = GetTargetDotNetFrameworkVersion(
                solutionTask.Project.TargetFramework);

            // use the framework directory as BinPath
            string frameworkDir = ToolLocationHelper.GetPathToDotNetFramework(
                frameworkVersion);

            _msbuild = new Microsoft.Build.BuildEngine.Engine(frameworkDir);
            _msbuild.UnregisterAllLoggers();

            _msbuild.RegisterLogger(
                new NAntLogger(solutionTask,
                               solutionTask.Verbose ? Microsoft.Build.Framework.LoggerVerbosity.Normal : Microsoft.Build.Framework.LoggerVerbosity.Minimal
                               )
                );

            /*
             * foreach (PropertyTask property in solutionTask.CustomProperties) {
             *  string val;
             *  // expand properties in context of current project for non-dynamic properties
             *  if (!property.Dynamic) {
             *      val = solutionTask.Project.ExpandProperties(property.Value, solutionTask.Location);
             *  }
             *  else
             *      val = property.Value;
             *  _msbuild.GlobalProperties.SetProperty(property.PropertyName, val);
             * }
             */

            return(_msbuild);
        }
Example #3
0
        public static Microsoft.Build.BuildEngine.Engine CreateMSEngine(NAnt.VSNet.Tasks.SolutionTask solutionTask)
        {
            if (_msbuild!=null) {
                return _msbuild;
            }

            // map current target framework to TargetDotNetFrameworkVersion
            TargetDotNetFrameworkVersion frameworkVersion = GetTargetDotNetFrameworkVersion(
                solutionTask.Project.TargetFramework);

            // use the framework directory as BinPath
            string frameworkDir = ToolLocationHelper.GetPathToDotNetFramework(
                frameworkVersion);

            _msbuild = new Microsoft.Build.BuildEngine.Engine(frameworkDir);
            _msbuild.UnregisterAllLoggers();

            _msbuild.RegisterLogger(
                new NAntLogger(solutionTask,
                solutionTask.Verbose ? Microsoft.Build.Framework.LoggerVerbosity.Normal : Microsoft.Build.Framework.LoggerVerbosity.Minimal
                )
                );

            /*
            foreach (PropertyTask property in solutionTask.CustomProperties) {
                string val;
                // expand properties in context of current project for non-dynamic properties
                if (!property.Dynamic) {
                    val = solutionTask.Project.ExpandProperties(property.Value, solutionTask.Location);
                }
                else
                    val = property.Value;
                _msbuild.GlobalProperties.SetProperty(property.PropertyName, val);
            }
            */

            return _msbuild;
        }
Example #4
0
        private static bool BuildProjectWithOldOM(string projectFile, string[] targets, string toolsVersion, Microsoft.Build.BuildEngine.BuildPropertyGroup propertyBag, ILogger[] loggers, LoggerVerbosity verbosity, DistributedLoggerRecord[] distributedLoggerRecords, bool needToValidateProject, string schemaFile, int cpuCount)
        {
            string msbuildLocation = Path.GetDirectoryName(Assembly.GetAssembly(typeof(MSBuildApp)).Location);
            string localNodeProviderParameters = "msbuildlocation=" + msbuildLocation; /*This assembly is the exe*/ ;

            localNodeProviderParameters += ";nodereuse=false";

            Microsoft.Build.BuildEngine.Engine engine = new Microsoft.Build.BuildEngine.Engine(propertyBag, Microsoft.Build.BuildEngine.ToolsetDefinitionLocations.ConfigurationFile | Microsoft.Build.BuildEngine.ToolsetDefinitionLocations.Registry, cpuCount, localNodeProviderParameters);
            bool success = false;

            try
            {
                foreach (ILogger logger in loggers)
                {
                    engine.RegisterLogger(logger);
                }

                // Targeted perf optimization for the case where we only have our own parallel console logger, and verbosity is quiet. In such a case
                // we know we won't emit any messages except for errors and warnings, so the engine should not bother even logging them.
                // If we're using the original serial console logger we can't do this, as it shows project started/finished context
                // around errors and warnings.
                // Telling the engine to not bother logging non-critical messages means that typically it can avoid loading any resources in the successful
                // build case.
                if (loggers.Length == 1 &&
                    verbosity == LoggerVerbosity.Quiet &&
                    loggers[0].Parameters.IndexOf("ENABLEMPLOGGING", StringComparison.OrdinalIgnoreCase) != -1 &&
                    loggers[0].Parameters.IndexOf("DISABLEMPLOGGING", StringComparison.OrdinalIgnoreCase) == -1 &&
                    loggers[0].Parameters.IndexOf("V=", StringComparison.OrdinalIgnoreCase) == -1 &&                // Console logger could have had a verbosity
                    loggers[0].Parameters.IndexOf("VERBOSITY=", StringComparison.OrdinalIgnoreCase) == -1)          // override with the /clp switch
                {
                    // Must be exactly the console logger, not a derived type like the file logger.
                    Type t1 = loggers[0].GetType();
                    Type t2 = typeof(ConsoleLogger);
                    if (t1 == t2)
                    {
                        engine.OnlyLogCriticalEvents = true;
                    }
                }

                Microsoft.Build.BuildEngine.Project project = null;

                try
                {
                    project = new Microsoft.Build.BuildEngine.Project(engine, toolsVersion);
                }
                catch (InvalidOperationException e)
                {
                    InitializationException.Throw("InvalidToolsVersionError", toolsVersion, e, false /*no stack*/);
                }

                project.IsValidated = needToValidateProject;
                project.SchemaFile = schemaFile;

                project.Load(projectFile);

                success = engine.BuildProject(project, targets);
            }
            // handle project file errors
            catch (InvalidProjectFileException)
            {
                // just eat the exception because it has already been logged
            }
            finally
            {
                // Unregister loggers and finish with engine
                engine.Shutdown();
            }
            return success;
        }