Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
        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();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Main running method for the application.
        /// </summary>
        /// <param name="args">Commandline arguments to the application.</param>
        /// <returns>Returns the application error code.</returns>
        private int Run(string[] args)
        {
            StringCollection extensionList = new StringCollection();

            heatCore = new HeatCore();

            HarvesterCore harvesterCore = new HarvesterCore();

            heatCore.Harvester.Core = harvesterCore;
            heatCore.Mutator.Core   = harvesterCore;

            try
            {
                // read the configuration file (heat.exe.config)
                AppCommon.ReadConfiguration(extensionList);

                // load any extensions
                foreach (string extensionType in extensionList)
                {
                    this.LoadExtension(extensionType);
                }

                // exit if there was an error loading an extension
                if (Messaging.Instance.EncounteredError)
                {
                    return(Messaging.Instance.LastErrorNumber);
                }

                // parse the command line
                this.ParseCommandLine(args);

                if (this.showHelp)
                {
                    return(this.DisplayHelp());
                }

                // exit if there was an error parsing the core command line
                if (Messaging.Instance.EncounteredError)
                {
                    return(Messaging.Instance.LastErrorNumber);
                }

                if (this.showLogo)
                {
                    AppCommon.DisplayToolHeader();
                }

                // set the extension argument for use by all extensions
                harvesterCore.ExtensionArgument = this.extensionArgument;

                // parse the extension's command line arguments
                string[] extensionOptionsArray = new string[this.extensionOptions.Count];
                this.extensionOptions.CopyTo(extensionOptionsArray, 0);
                foreach (HeatExtension heatExtension in this.extensions)
                {
                    heatExtension.ParseOptions(this.extensionType, extensionOptionsArray);
                }

                // exit if there was an error parsing the command line (otherwise the logo appears after error messages)
                if (Messaging.Instance.EncounteredError)
                {
                    return(Messaging.Instance.LastErrorNumber);
                }

                // harvest the output
                Wix.Wix wix = heatCore.Harvester.Harvest(this.extensionArgument);
                if (null == wix)
                {
                    return(Messaging.Instance.LastErrorNumber);
                }

                // mutate the output
                if (!heatCore.Mutator.Mutate(wix))
                {
                    return(Messaging.Instance.LastErrorNumber);
                }

                XmlWriterSettings xmlSettings = new XmlWriterSettings();
                xmlSettings.Indent             = true;
                xmlSettings.IndentChars        = new string(' ', this.indent);
                xmlSettings.OmitXmlDeclaration = true;

                string wixString;
                using (StringWriter stringWriter = new StringWriter())
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlSettings))
                    {
                        wix.OutputXml(xmlWriter);
                    }

                    wixString = stringWriter.ToString();
                }

                string mutatedWixString = heatCore.Mutator.Mutate(wixString);
                if (String.IsNullOrEmpty(mutatedWixString))
                {
                    return(Messaging.Instance.LastErrorNumber);
                }

                Directory.CreateDirectory(Path.GetDirectoryName(this.outputFile));

                using (StreamWriter streamWriter = new StreamWriter(this.outputFile, false, System.Text.Encoding.UTF8))
                {
                    xmlSettings.OmitXmlDeclaration = false;
                    xmlSettings.Encoding           = System.Text.Encoding.UTF8;
                    using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter, xmlSettings))
                    {
                        xmlWriter.WriteStartDocument();
                        xmlWriter.Flush();
                    }

                    streamWriter.WriteLine();
                    streamWriter.Write(mutatedWixString);
                }
            }
            catch (WixException we)
            {
                Messaging.Instance.OnMessage(we.Error);
            }
            catch (Exception e)
            {
                Messaging.Instance.OnMessage(WixErrors.UnexpectedException(e.Message, e.GetType().ToString(), e.StackTrace));
                if (e is NullReferenceException || e is SEHException)
                {
                    throw;
                }
            }

            return(Messaging.Instance.LastErrorNumber);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Main running method for the application.
        /// </summary>
        /// <param name="args">Commandline arguments to the application.</param>
        /// <returns>Returns the application error code.</returns>
        private int Run(string[] args)
        {
            StringCollection extensionList = new StringCollection();
            heatCore = new HeatCore(new MessageEventHandler(this.messageHandler.Display));

            HarvesterCore harvesterCore = new HarvesterCore(new MessageEventHandler(this.messageHandler.Display));
            heatCore.Harvester.Core = harvesterCore;
            heatCore.Mutator.Core = harvesterCore;

            try
            {
                // read the configuration file (heat.exe.config)
                AppCommon.ReadConfiguration(extensionList);

                // load any extensions
                foreach (string extensionType in extensionList)
                {
                    this.LoadExtension(extensionType);
                }

                // exit if there was an error loading an extension
                if (this.messageHandler.EncounteredError)
                {
                    return this.messageHandler.LastErrorNumber;
                }

                // parse the command line
                this.ParseCommandLine(args);

                if (this.showHelp)
                {
                    return this.DisplayHelp();
                }

                // exit if there was an error parsing the core command line
                if (this.messageHandler.EncounteredError)
                {
                    return this.messageHandler.LastErrorNumber;
                }

                if (this.showLogo)
                {
                    AppCommon.DisplayToolHeader();
                }

                // set the extension argument for use by all extensions
                harvesterCore.ExtensionArgument = this.extensionArgument;

                // parse the extension's command line arguments
                string[] extensionOptionsArray = new string[this.extensionOptions.Count];
                this.extensionOptions.CopyTo(extensionOptionsArray, 0);
                foreach (HeatExtension heatExtension in this.extensions)
                {
                    heatExtension.ParseOptions(this.extensionType, extensionOptionsArray);
                }

                // exit if there was an error parsing the command line (otherwise the logo appears after error messages)
                if (this.messageHandler.EncounteredError)
                {
                    return this.messageHandler.LastErrorNumber;
                }

                // harvest the output
                Wix.Wix wix = heatCore.Harvester.Harvest(this.extensionArgument);
                if (null == wix)
                {
                    return this.messageHandler.LastErrorNumber;
                }

                // mutate the output
                if (!heatCore.Mutator.Mutate(wix))
                {
                    return this.messageHandler.LastErrorNumber;
                }

                XmlWriterSettings xmlSettings = new XmlWriterSettings();
                xmlSettings.Indent = true;
                xmlSettings.IndentChars = new string(' ', this.indent);
                xmlSettings.OmitXmlDeclaration = true;

                string wixString;
                using (StringWriter stringWriter = new StringWriter())
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlSettings))
                    {
                        wix.OutputXml(xmlWriter);
                    }

                    wixString = stringWriter.ToString();
                }

                string mutatedWixString = heatCore.Mutator.Mutate(wixString);
                if (String.IsNullOrEmpty(mutatedWixString))
                {
                    return this.messageHandler.LastErrorNumber;
                }

                Directory.CreateDirectory(Path.GetDirectoryName(this.outputFile));

                using (StreamWriter streamWriter = new StreamWriter(this.outputFile, false, System.Text.Encoding.UTF8))
                {
                    xmlSettings.OmitXmlDeclaration = false;
                    xmlSettings.Encoding = System.Text.Encoding.UTF8;
                    using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter, xmlSettings))
                    {
                        xmlWriter.WriteStartDocument();
                        xmlWriter.Flush();
                    }

                    streamWriter.WriteLine();
                    streamWriter.Write(mutatedWixString);
                }
            }
            catch (WixException we)
            {
                this.messageHandler.Display(this, we.Error);
            }
            catch (Exception e)
            {
                this.messageHandler.Display(this, WixErrors.UnexpectedException(e.Message, e.GetType().ToString(), e.StackTrace));
                if (e is NullReferenceException || e is SEHException)
                {
                    throw;
                }
            }

            return this.messageHandler.LastErrorNumber;
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
 private static MSBuildProject ConstructMsbuild40Project(string projectFile, HarvesterCore harvesterCore, string configuration, string platform)
 {
     return ConstructMsbuild40Project(projectFile, harvesterCore, configuration, platform, null);
 }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
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);
                }
            }
Exemplo n.º 9
0
        private static MSBuildProject ConstructMsbuildWrapperProject(string projectFile, HarvesterCore harvesterCore, string configuration, string platform, string shortVersion)
        {
            // Until MSBuild 12.0, we were able to compile the HarvestLogger class which derives from ILogger and use that for all versions of MSBuild.
            // Starting in MSBuild 12.0, the ILogger that we compile against doesn't match the ILogger during runtime.  This DLL targets .NET 3.5,
            // so we can't reference the newer MSBuild assemblies.  This requires building new assemblies.  We reflect into these instead of MSBuild.

            const string MSBuildWrapperAssemblyName = "WixVSExtension.MSBuild{0}, Version={1}, Culture=neutral, PublicKeyToken={2}";
            Assembly msbuildWrapperAssembly = null;

            // Load the custom assembly for the requested version of MSBuild.
            try
            {
                Assembly thisAssembly = Assembly.GetExecutingAssembly();
                AssemblyName thisAssemblyName = thisAssembly.GetName();
                StringBuilder publicKeyToken = new StringBuilder();
                foreach (byte b in thisAssemblyName.GetPublicKeyToken())
                {
                    publicKeyToken.Append(b.ToString("x2"));
                }

                msbuildWrapperAssembly = Assembly.Load(String.Format(MSBuildWrapperAssemblyName, shortVersion, thisAssemblyName.Version, publicKeyToken));
            }
            catch (Exception e)
            {
                throw new WixException(VSErrors.CannotLoadMSBuildWrapperAssembly(e.Message));
            }

            const string MSBuildWrapperTypeName = "Microsoft.Tools.WindowsInstallerXml.Extensions.WixVSExtension.MSBuild{0}Project";
            Type projectWrapperType = null;

            // Get the type of the class that inherits from MSBuildProject.
            try
            {
                projectWrapperType = msbuildWrapperAssembly.GetType(String.Format(MSBuildWrapperTypeName, shortVersion), true);
            }
            catch (TargetInvocationException tie)
            {
                throw new WixException(VSErrors.CannotLoadMSBuildWrapperType(tie.InnerException.Message));
            }
            catch (Exception e)
            {
                throw new WixException(VSErrors.CannotLoadMSBuildWrapperType(e.Message));
            }

            try
            {
                // Get the constructor of the class so we can "new it up".
                ConstructorInfo wrapperCtor = projectWrapperType.GetConstructor(
                    new Type[]
                    {
                        typeof(HarvesterCore),
                        typeof(string),
                        typeof(string),
                    });
                return (MSBuildProject)wrapperCtor.Invoke(new object[] { harvesterCore, configuration, platform });
            }
            catch (TargetInvocationException tie)
            {
                throw new WixException(VSErrors.CannotLoadMSBuildWrapperObject(tie.InnerException.Message));
            }
            catch (Exception e)
            {
                throw new WixException(VSErrors.CannotLoadMSBuildWrapperObject(e.Message));
            }
        }