public MSBuild12Project(HarvesterCore harvesterCore, string configuration, string platform) : base(null, null, null, null) { this.buildParameters = new BuildParameters(); try { HarvestLogger logger = new HarvestLogger(); logger.HarvesterCore = harvesterCore; List <ILogger> loggers = new List <ILogger>(); loggers.Add(logger); this.buildParameters.Loggers = loggers; // MSBuild can't handle storing operating environments for nested builds. if (Util.RunningInMsBuild) { this.buildParameters.SaveOperatingEnvironment = false; } } catch (Exception e) { if (harvesterCore != null) { harvesterCore.OnMessage(VSWarnings.NoLogger(e.Message)); } } this.buildManager = new BuildManager(); 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 = new ProjectCollection(globalVariables); } else { this.projectCollection = new ProjectCollection(); } }
public MSBuild12Project(HarvesterCore harvesterCore, string configuration, string platform) : base(null, null, null, null) { this.buildParameters = new BuildParameters(); try { HarvestLogger logger = new HarvestLogger(); logger.HarvesterCore = harvesterCore; List<ILogger> loggers = new List<ILogger>(); loggers.Add(logger); this.buildParameters.Loggers = loggers; // MSBuild can't handle storing operating environments for nested builds. if (Util.RunningInMsBuild) { this.buildParameters.SaveOperatingEnvironment = false; } } catch (Exception e) { if (harvesterCore != null) { harvesterCore.OnMessage(VSWarnings.NoLogger(e.Message)); } } this.buildManager = new BuildManager(); 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 = new ProjectCollection(globalVariables); } else { this.projectCollection = new ProjectCollection(); } }
private static MSBuildProject ConstructMsbuild35Project(string projectFile, HarvesterCore harvesterCore, string configuration, string platform, string loadVersion) { const string MSBuildEngineAssemblyName = "Microsoft.Build.Engine, Version={0}, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"; Assembly msbuildAssembly = null; loadVersion = loadVersion ?? "3.5.0.0"; try { try { msbuildAssembly = Assembly.Load(String.Format(MSBuildEngineAssemblyName, loadVersion)); } catch (FileNotFoundException) { loadVersion = "2.0.0.0"; msbuildAssembly = Assembly.Load(String.Format(MSBuildEngineAssemblyName, loadVersion)); } } catch (Exception e) { throw new WixException(VSErrors.CannotLoadMSBuildAssembly(e.Message)); } Type engineType; Type projectType; Type buildItemType; object engine; object project; try { engineType = msbuildAssembly.GetType("Microsoft.Build.BuildEngine.Engine", true); if (msbuildAssembly.GetName().Version.Major >= 3) { // MSBuild v3.5 uses this constructor which automatically sets the tool path. engine = engineType.GetConstructor(new Type[] { }).Invoke(null); } else { //MSBuild v2.0 uses this constructor which requires specifying the MSBuild bin path. string msbuildBinPath = RuntimeEnvironment.GetRuntimeDirectory(); engine = engineType.GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { msbuildBinPath }); try { HarvestLogger logger = new HarvestLogger(); engineType.GetMethod("RegisterLogger").Invoke(engine, new object[] { logger }); } catch (TargetInvocationException tie) { if (harvesterCore != null) { harvesterCore.OnMessage(VSWarnings.NoLogger(tie.Message)); } } catch (Exception e) { if (harvesterCore != null) { harvesterCore.OnMessage(VSWarnings.NoLogger(e.Message)); } } } buildItemType = msbuildAssembly.GetType("Microsoft.Build.BuildEngine.BuildItem", true); projectType = msbuildAssembly.GetType("Microsoft.Build.BuildEngine.Project", true); project = projectType.GetConstructor(new Type[] { engineType }).Invoke(new object[] { engine }); } catch (TargetInvocationException tie) { // An assembly redirect (i.e. VS 2005) can cause a TypeLoadException at this point // Try again using MSBuild 2.0.0.0 at that point. if (String.Equals(tie.InnerException.GetType().Name, "TypeLoadException", StringComparison.Ordinal) && !String.Equals(loadVersion, "2.0.0.0", StringComparison.Ordinal)) { return ConstructMsbuild35Project(projectFile, harvesterCore, configuration, platform, "2.0.0.0"); } throw new WixException(VSErrors.CannotLoadMSBuildEngine(tie.InnerException.Message)); } catch (Exception e) { throw new WixException(VSErrors.CannotLoadMSBuildEngine(e.Message)); } if (configuration != null || platform != null) { try { object globalProperties = projectType.GetProperty("GlobalProperties").GetValue(project, null); MethodInfo setPropertyMethod = globalProperties.GetType().GetMethod("SetProperty", new Type[] { typeof(string), typeof(string) }); if (configuration != null) { setPropertyMethod.Invoke(globalProperties, new object[] { "Configuration", configuration }); } if (platform != null) { setPropertyMethod.Invoke(globalProperties, new object[] { "Platform", platform }); } } catch (TargetInvocationException tie) { harvesterCore.OnMessage(VSWarnings.NoProjectConfiguration(tie.InnerException.Message)); } catch (Exception e) { harvesterCore.OnMessage(VSWarnings.NoProjectConfiguration(e.Message)); } } return new MSBuild35Project(project, projectType, buildItemType, loadVersion); }
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); } }