CommandLine.cs :  » Testing » xUnit.net » Xunit » ConsoleClient » 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 » Testing » xUnit.net 
xUnit.net » Xunit » ConsoleClient » CommandLine.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;

namespace Xunit.ConsoleClient{
    public class CommandLine
    {
        public delegate bool FileExists(string fileName);

        Stack<string> arguments = new Stack<string>();
        string executablePath;

        protected CommandLine(string[] args)
        {
            for (int i = args.Length - 1; i >= 0; i--)
                arguments.Push(args[i]);

            executablePath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
            TeamCity = Environment.GetEnvironmentVariable("TEAMCITY_PROJECT_NAME") != null;
            Project = Parse();
        }

        public XunitProject Project { get; protected set; }

        public bool Silent { get; protected set; }

        public bool TeamCity { get; protected set; }

        public bool Wait { get; protected set; }

        protected virtual XunitProject GetMultiAssemblyProject(string filename)
        {
            return XunitProject.Load(filename);
        }

        static XunitProject GetSingleAssemblyProject(Dictionary<string, string> transforms, string assemblyFile, string configFile, bool noShadow)
        {
            XunitProjectAssembly assembly = new XunitProjectAssembly { AssemblyFilename = assemblyFile, ConfigFilename = configFile, ShadowCopy = !noShadow };
            foreach (var transform in transforms)
                assembly.Output.Add(transform.Key, transform.Value);

            XunitProject project = new XunitProject();
            project.AddAssembly(assembly);
            return project;
        }

        static void GuardNoOptionValue(KeyValuePair<string, string> option)
        {
            if (option.Value != null)
                throw new ArgumentException(String.Format("error: unknown command line option: {0}", option.Value));
        }

        static void GuardNoProjectFile(XunitProject project, KeyValuePair<string, string> option)
        {
            if (project != null)
                throw new ArgumentException(String.Format("the {0} command line option isn't valid for .xunit projects", option.Key));
        }

        public static bool IsProjectFilename(string filename)
        {
            return Path.GetExtension(filename).Equals(".xunit", StringComparison.OrdinalIgnoreCase);
        }

        public static CommandLine Parse(string[] args)
        {
            return new CommandLine(args);
        }

        protected virtual XunitProject Parse()
        {
            return Parse(fileName => File.Exists(fileName));
        }

        protected XunitProject Parse(FileExists fileExists)
        {
            Dictionary<string, string> transforms = new Dictionary<string, string>();
            string configFile = null;
            bool noShadow = false;
            XunitProject project = null;

            string filename = arguments.Pop();
            if (!fileExists(filename))
                throw new ArgumentException(String.Format("file not found: {0}", filename));

            if (IsProjectFilename(filename))
            {
                project = GetMultiAssemblyProject(filename);
            }
            else
            {
                if (arguments.Count > 0 && !arguments.Peek().StartsWith("/"))
                {
                    configFile = arguments.Pop();

                    if (!fileExists(configFile))
                        throw new ArgumentException(String.Format("config file not found: {0}", configFile));
                }
            }

            while (arguments.Count > 0)
            {
                KeyValuePair<string, string> option = PopOption(arguments);
                string optionName = option.Key.ToLowerInvariant();

                if (!optionName.StartsWith("/"))
                    throw new ArgumentException(String.Format("unknown command line option: {0}", option.Key));

                if (optionName == "/wait")
                {
                    GuardNoOptionValue(option);
                    Wait = true;
                }
                else if (optionName == "/silent")
                {
                    GuardNoOptionValue(option);
                    Silent = true;
                }
                else if (optionName == "/teamcity")
                {
                    GuardNoOptionValue(option);
                    TeamCity = true;
                }
                else if (optionName == "/noshadow")
                {
                    GuardNoProjectFile(project, option);
                    GuardNoOptionValue(option);
                    noShadow = true;
                }
                else
                {
                    GuardNoProjectFile(project, option);

                    if (option.Value == null)
                        throw new ArgumentException(String.Format("missing filename for {0}", option.Key));

                    transforms.Add(optionName.Substring(1), option.Value);
                }
            }

            if (project == null)
                project = GetSingleAssemblyProject(transforms, filename, configFile, noShadow);

            return project;
        }

        static KeyValuePair<string, string> PopOption(Stack<string> arguments)
        {
            string option = arguments.Pop();
            string value = null;

            if (arguments.Count > 0 && !arguments.Peek().StartsWith("/"))
                value = arguments.Pop();

            return new KeyValuePair<string, string>(option, value);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.