Example #1
0
        private static MSBuildProject ConstructMsbuild40Project(string projectFile, HarvesterCore harvesterCore, string configuration, string platform, string loadVersion)
        {
            const string MSBuildEngineAssemblyName = "Microsoft.Build, Version={0}, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
            Assembly msbuildAssembly = null;

            loadVersion = loadVersion ?? "4.0.0.0";

            try
            {
                try
                {
                    msbuildAssembly = Assembly.Load(String.Format(MSBuildEngineAssemblyName, loadVersion));
                }
                catch (FileNotFoundException)
                {
                    loadVersion = "4.0.0.0";
                    msbuildAssembly = Assembly.Load(String.Format(MSBuildEngineAssemblyName, loadVersion));
                }
            }
            catch (Exception e)
            {
                throw new WixException(VSErrors.CannotLoadMSBuildAssembly(e.Message));
            }

            Type projectType;
            Type buildItemType;

            Type buildManagerType;
            Type buildParametersType;
            Type buildRequestDataFlagsType;
            Type buildRequestDataType;
            Type hostServicesType;
            Type projectCollectionType;
            Type projectInstanceType;

            try
            {
                buildItemType = msbuildAssembly.GetType("Microsoft.Build.Execution.ProjectItemInstance", true);
                projectType = msbuildAssembly.GetType("Microsoft.Build.Evaluation.Project", true);

                buildManagerType = msbuildAssembly.GetType("Microsoft.Build.Execution.BuildManager", true);
                buildParametersType = msbuildAssembly.GetType("Microsoft.Build.Execution.BuildParameters", true);
                buildRequestDataFlagsType = msbuildAssembly.GetType("Microsoft.Build.Execution.BuildRequestDataFlags", true);
                buildRequestDataType = msbuildAssembly.GetType("Microsoft.Build.Execution.BuildRequestData", true);
                hostServicesType = msbuildAssembly.GetType("Microsoft.Build.Execution.HostServices", true);
                projectCollectionType = msbuildAssembly.GetType("Microsoft.Build.Evaluation.ProjectCollection", true);
                projectInstanceType = msbuildAssembly.GetType("Microsoft.Build.Execution.ProjectInstance", true);
            }
            catch (TargetInvocationException tie)
            {
                throw new WixException(VSErrors.CannotLoadMSBuildEngine(tie.InnerException.Message));
            }
            catch (Exception e)
            {
                throw new WixException(VSErrors.CannotLoadMSBuildEngine(e.Message));
            }

            MSBuild40Types types = new MSBuild40Types();
            types.buildManagerType = buildManagerType;
            types.buildParametersType = buildParametersType;
            types.buildRequestDataFlagsType = buildRequestDataFlagsType;
            types.buildRequestDataType = buildRequestDataType;
            types.hostServicesType = hostServicesType;
            types.projectCollectionType = projectCollectionType;
            types.projectInstanceType = projectInstanceType;
            return new MSBuild40Project(null, projectType, buildItemType, loadVersion, types, harvesterCore, configuration, platform);
        }
Example #2
0
            public MSBuild40Project(object project, Type projectType, Type buildItemType, string loadVersion, MSBuild40Types types, HarvesterCore harvesterCore, string configuration, string platform)
                : base(project, projectType, buildItemType, loadVersion)
            {
                this.types = types;
                this.harvesterCore = harvesterCore;

                this.buildParameters = this.types.buildParametersType.GetConstructor(new Type[] { }).Invoke(null);

                try
                {
                    HarvestLogger logger = new HarvestLogger();
                    logger.HarvesterCore = harvesterCore;
                    List<ILogger> loggers = new List<ILogger>();
                    loggers.Add(logger);

                    // this.buildParameters.Loggers = loggers;
                    this.types.buildParametersType.GetProperty("Loggers").SetValue(this.buildParameters, loggers, null);

                    // MSBuild can't handle storing operating enviornments for nested builds.
                    if (Util.RunningInMsBuild)
                    {
                        this.types.buildParametersType.GetProperty("SaveOperatingEnvironment").SetValue(this.buildParameters, false, null);
                    }
                }
                catch (TargetInvocationException tie)
                {
                    if (this.harvesterCore != null)
                    {
                        this.harvesterCore.OnMessage(VSWarnings.NoLogger(tie.InnerException.Message));
                    }
                }
                catch (Exception e)
                {
                    if (this.harvesterCore != null)
                    {
                        this.harvesterCore.OnMessage(VSWarnings.NoLogger(e.Message));
                    }
                }

                this.buildManager = this.types.buildManagerType.GetConstructor(new Type[] { }).Invoke(null);

                if (configuration != null || platform != null)
                {
                    Dictionary<string, string> globalVariables = new Dictionary<string, string>();
                    if (configuration != null)
                    {
                        globalVariables.Add("Configuration", configuration);
                    }

                    if (platform != null)
                    {
                        globalVariables.Add("Platform", platform);
                    }

                    this.projectCollection = this.types.projectCollectionType.GetConstructor(new Type[] { typeof(IDictionary<string, string>) }).Invoke(new object[] { globalVariables });
                }
                else
                {
                    this.projectCollection = this.types.projectCollectionType.GetConstructor(new Type[] {}).Invoke(null);
                }
            }