Tool.cs :  » Installers-Generators » WiX » Microsoft » Tools » WindowsInstallerXml » Test » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Installers Generators » WiX 
WiX » Microsoft » Tools » WindowsInstallerXml » Test » Tool.cs
//-----------------------------------------------------------------------
// <copyright file="Tool.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
//    
//    The use and distribution terms for this software are covered by the
//    Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
//    which can be found in the file CPL.TXT at the root of this distribution.
//    By using this software in any fashion, you are agreeing to be bound by
//    the terms of this license.
//    
//    You must not remove this notice, or any other, from this software.
// </copyright>
// <summary>Wraps a command line tool</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Tools.WindowsInstallerXml.Test{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Text;

    /// <summary>
    /// This class is used to wrap a generic command line tool.
    /// </summary>
    public class Tool
    {
        /// <summary>
        /// The arguments to pass to the tool
        /// </summary>
        private string arguments;

        /// <summary>
        /// Print output from the tool execution to the console
        /// </summary>
        private bool printOutputToConsole = false;

        /// <summary>
        /// The full path to the tool
        /// </summary>
        private string toolFile;

        /// <summary>
        /// The previous run results
        /// </summary>
        private Stack<Result> results = new Stack<Result>();

        /// <summary>
        /// The working directory of the tool
        /// </summary>
        private string workingDirectory;

        /// <summary>
        /// Constructor for a tool
        /// </summary>
        public Tool()
            : this(null, null, null)
        {
        }

        /// <summary>
        /// Constructor for a tool
        /// </summary>
        /// <param name="toolFile">The full path to the tool. Eg. c:\bin\candle.exe</param>
        /// <param name="workingDirectory">The working directory of the tool. A null value assumes current directory.</param>
        public Tool(string toolFile, string workingDirectory)
            : this(toolFile, null, workingDirectory)
        {
        }

        /// <summary>
        /// Constructor for a tool
        /// </summary>
        /// <param name="toolFile">The full path to the tool. Eg. c:\bin\candle.exe</param>
        /// <param name="arguments">The command line arguments to use when running the tool</param>
        /// <param name="workingDirectory">The working directory of the tool. A null value assumes current directory.</param>
        public Tool(string toolFile, string arguments, string workingDirectory)
        {
            // Replace null with an empty string
            this.Arguments = (arguments ?? String.Empty);
            this.ToolFile = (toolFile ?? String.Empty);
            this.WorkingDirectory = (workingDirectory ?? String.Empty);
        }

        /// <summary>
        /// The arguments to pass to the tool
        /// </summary>
        public virtual string Arguments
        {
            get { return this.arguments; }
            set { this.arguments = value; }
        }

        /// <summary>
        /// The full command line that is executed
        /// </summary>
        public string CommandLine
        {
            get
            {
                return String.Format("{0} {1}", this.ToolFile, this.Arguments);
            }
        }

        /// <summary>
        /// Print output from the tool execution to the console
        /// </summary>
        public bool PrintOutputToConsole
        {
            get { return this.printOutputToConsole; }
            set { this.printOutputToConsole = value; }
        }

        /// <summary>
        /// The result of the last run
        /// </summary>
        public Result Result
        {
            get { return this.Results.Peek(); }
        }

        /// <summary>
        /// The previous run results
        /// </summary>
        public Stack<Result> Results
        {
            get { return this.results; }
            set { this.results = value; }
        }

        /// <summary>
        /// The full path to the tool
        /// </summary>
        public string ToolFile
        {
            get { return this.toolFile; }
            set { this.toolFile = value; }
        }

        /// <summary>
        /// The working directory of the tool
        /// </summary>
        public string WorkingDirectory
        {
            get { return this.workingDirectory; }
            set { this.workingDirectory = value; }
        }

        /// <summary>
        /// Print the last run result to the Console
        /// </summary>
        public virtual void PrintResult()
        {
            if (null != this.Result)
            {
                Console.WriteLine(this.Result.ToString());
            }
            else
            {
                Console.WriteLine("No results");
            }
        }

        /// <summary>
        /// Executes the tool with the currently set arguments
        /// </summary>
        /// <returns>A Result object containing data about the run</returns>
        public virtual Result Run()
        {
            return this.Run(this.Arguments);
        }

        /// <summary>
        /// Executes the tool with the specified arguments
        /// </summary>
        /// <param name="arguments">The arguments to pass on the command line. The value of arguments will be persisted in the Arguments property.</param>
        /// <returns>A Result object containing data about the run</returns>
        public virtual Result Run(string arguments)
        {
            if (String.Empty == this.ToolFile)
            {
                throw new Exception("The tool is not specified");
            }

            // Expand environment variables in the tool file
            string expandedToolFile = Environment.ExpandEnvironmentVariables(this.ToolFile);

            if (!File.Exists(expandedToolFile))
            {
                throw new FileNotFoundException(String.Format("The file {0} could not be found", expandedToolFile), expandedToolFile);
            }

            Process process = new Process();
            process.StartInfo.FileName = expandedToolFile;
            process.StartInfo.Arguments = Environment.ExpandEnvironmentVariables(arguments);
            process.StartInfo.WorkingDirectory = Environment.ExpandEnvironmentVariables(this.WorkingDirectory);
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardOutput = true;

            // Create the Result object
            Result result = new Result();
            result.Command = this.CommandLine;

            try
            {
                // Run the process
                process.Start();

                StringBuilder standardOutput = new StringBuilder();
                while (!process.StandardOutput.EndOfStream)
                {
                    standardOutput.AppendLine(process.StandardOutput.ReadLine());
                }

                result.StandardOutput = standardOutput.ToString();

                StringBuilder standardError = new StringBuilder();
                while (!process.StandardError.EndOfStream)
                {
                    standardError.AppendLine(process.StandardError.ReadLine());
                }

                result.StandardError = standardError.ToString();

                process.WaitForExit();
                result.ExitCode = process.ExitCode;
            }
            finally
            {
                if (null != process)
                {
                    process.Close();
                }
            }

            if (this.PrintOutputToConsole)
            {
                Console.WriteLine(result.ToString());
            }

            this.Results.Push(result);
            return result;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.