Mono.Util.Driver.Run C# (CSharp) Method

Run() public method

public Run ( string args ) : void
args string
return void
		public void Run (string[] args)
		{
			ArrayList unknownFiles = new ArrayList();
			bool generateClasses = false;
			bool readingFiles = true;
			bool schemasOptions = false;
			bool assemblyOptions = false;
			bool generateDataset = false;
			bool inference = false;

			foreach (string arg in args)
			{
				if (!arg.StartsWith ("--") && !arg.StartsWith ("/") ||
					(arg.StartsWith ("/") && arg.IndexOfAny (Path.InvalidPathChars) == -1)
					) 
				{
					if ((arg.EndsWith (".dll") || arg.EndsWith (".exe")) && !arg.Substring (1).StartsWith ("generator:") && !arg.Substring (1).StartsWith ("g:"))
					{
						if (!readingFiles) throw new ApplicationException (incorrectOrder);
						assemblies.Add (arg);
						assemblyOptions = true;
						continue;
					}
					else if (arg.EndsWith (".xsd"))
					{
						if (!readingFiles) Error (incorrectOrder);
						schemaNames.Add (arg);
						schemasOptions = true;
						continue;
					}
					else if (arg.EndsWith (".xml"))
					{
						if (generateClasses || generateDataset) Error (duplicatedParam);
						inferenceNames.Add (arg);
						inference = true;
						continue;
					}
					else if (!arg.StartsWith ("/"))
					{
						if (!readingFiles) Error (incorrectOrder);
						unknownFiles.Add (arg);
						continue;
					}
				}

				readingFiles = false;

				int i = arg.IndexOf (":");
				if (i == -1) i = arg.Length;
				string option = arg.Substring (1,i-1);
				string param = (i<arg.Length-1) ? arg.Substring (i+1) : "";

				if (option == "classes" || option == "c")
				{
					if (generateClasses || generateDataset || inference) Error (duplicatedParam, option);
					generateClasses = true;
					schemasOptions = true;
				}
				else if (option == "dataset" || option == "d")
				{
					if (generateClasses || generateDataset || inference) Error (duplicatedParam, option);
					generateDataset = true;
					schemasOptions = true;
				}
				else if (option == "element" || option == "e")
				{
					elements.Add (param);
					schemasOptions = true;
				}
				else if (option == "language" || option == "l")
				{
					if (provider != null) Error (duplicatedParam, option);
					if (language != null) Error (duplicatedParam, option);
					language = param;
					schemasOptions = true;
				}
				else if (option == "namespace" || option == "n")
				{
					if (namesp != null) Error (duplicatedParam, option);
					namesp = param;
					schemasOptions = true;
				}
				else if (option == "outputdir" || option == "o")
				{
					if (outputDir != null) Error (duplicatedParam, option);
					outputDir = param;
				}
				else if (option == "uri" || option == "u")
				{
					if (uri != null) Error (duplicatedParam, option);
					uri = param;
					schemasOptions = true;
				}
				else if (option == "type" || option == "t")
				{
					lookupTypes.Add (param);
					assemblyOptions = true;
				}
				else if (option == "generator" || option == "g")
				{
					providerOption = param;
				}
				else if (option == "help" || option == "h")
				{
					Console.WriteLine (helpString);
					return;
				}
				else if (option == "nologo")
				{
					// ignore, since we do not output a logo anyway
				}
				else
					Error (unknownOption, option);
			}

			if (!schemasOptions && !assemblyOptions && !inference)
				Error (invalidParams);

			if (schemasOptions && assemblyOptions)
				Error (incompatibleArgs);

			if (assemblies.Count > 1)
				Error (tooManyAssem);

			if (outputDir == null) outputDir = ".";

			string typename = null;
			Type generatorType = null;

			if (language != null) {
				switch (language) {
				case "CS":
					provider = new CSharpCodeProvider ();
					break;
				case "VB":
					provider = new VBCodeProvider ();
					break;
				default:
					typename = StripQuot (language);

					generatorType = Type.GetType (typename);
					if (generatorType == null)
						Error (generatorTypeNotFound, typename);
					break;
				}
			}

			if (providerOption != null) {
				string param = providerOption;
				int comma = param.IndexOf (',');
				if (comma < 0) {
					typename = StripQuot (param);
					generatorType = Type.GetType (param);
				} else {
					typename = param.Substring (0, comma);
					string asmName = param.Substring (comma + 1);
#if NET_1_1
					Assembly asm = Assembly.LoadFile (asmName);
#else
					Assembly asm = Assembly.LoadFrom (asmName);
#endif
					if (asm == null)
						Error (generatorAssemblyNotFound, asmName);
					generatorType = asm.GetType (typename);
				}
				if (generatorType == null)
					Error (generatorTypeNotFound, typename);
			}
			if (generatorType != null) {
				if (!generatorType.IsSubclassOf (typeof (CodeDomProvider)))
					Error (generatorTypeIsNotCodeGenerator, typename);
				try {
					provider = (CodeDomProvider) Activator.CreateInstance (generatorType, null);
				} catch (Exception ex) {
					Error (generatorThrewException, generatorType.AssemblyQualifiedName.ToString () + " --> " + ex.Message);
				}
				Console.WriteLine ("Loaded custom generator type " + generatorType + " .");
			}
			if (provider == null)
				provider = new CSharpCodeProvider ();

			if (schemasOptions)
			{
				if (!generateClasses && !generateDataset)
					Error (missingOutputForXsdInput);
				schemaNames.AddRange (unknownFiles);
				if (generateClasses)
					GenerateClasses ();
				else if (generateDataset)
					GenerateDataset ();
			}
			else if (inference)
			{
				foreach (string xmlfile in inferenceNames) {
					string genFile = Path.Combine (outputDir, Path.GetFileNameWithoutExtension (xmlfile) + ".xsd");
					DataSet ds = new DataSet ();
					ds.InferXmlSchema (xmlfile, null);
					ds.WriteXmlSchema (genFile);
					Console.WriteLine ("Written file " + genFile);
				}
			}
			else
			{
				assemblies.AddRange (unknownFiles);
				GenerateSchemas ();
			}
		}